-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rolling.Std ValueError: math domain error #20
Comments
Thanks for the report - this is definitely a bug |
It seems to me that the bug is in import pandas as pd
import numpy as np
from rolling import Var
class VarFixed(Var):
def _update_window(self, new):
self._remove_old()
self._add_new(new)
window = 3
values = [138, 136, 137, 137, 135, 136, 135, 135, 135]
def test(VarianceClass):
std = VarianceClass(values, window_size=window)
for _ in values:
try:
print(next(std))
except StopIteration:
break
print("existing")
test(Var)
print("fixed")
test(VarFixed)
print("pandas")
print(pd.Series(values, dtype=np.float64).rolling(window).var())
On a separate note, I wonder if it's worth considering adding in Kahan summation to ensure numerical stability; I noticed this while looking at the pandas source: https://github.com/pandas-dev/pandas/blob/e8dbdb07782437ad592736f6d212d0af5e7324be/pandas/_libs/window/aggregations.pyx#L317 EDIT:
|
@ajcr there have been several releases since the report-- how about a fix? using this expression in sqrt(max(0, self._sslm / (self._obs - self.ddof))) |
An immediate fix for this issue (variance dropping below zero) has been merged. I've opened a separate issue to solve the broader precision issue with the float arithmetic (#25). |
Environment: OSX 10.15.7, python 3.7.9, rolling=0.2.0
When I run the following code,
I got an error.
This is because
self._sslm
is a negative value.The value of sslm changed as follows.
The text was updated successfully, but these errors were encountered: