Skip to content

BUG: Erratic behavior of rolling().std() #53289

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

Closed
2 of 3 tasks
DevinPrescott opened this issue May 18, 2023 · 4 comments
Closed
2 of 3 tasks

BUG: Erratic behavior of rolling().std() #53289

DevinPrescott opened this issue May 18, 2023 · 4 comments
Labels
Bug Duplicate Report Duplicate issue or pull request Reduction Operations sum, mean, min, max, etc. Window rolling, ewma, expanding

Comments

@DevinPrescott
Copy link

DevinPrescott commented May 18, 2023

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

W = 50 # Window Size
R = 14 # Array Resolution
C = 9 # Size of array range

x = np.linspace(0,C*np.pi,int(R*W))
y = np.sin(x)*np.exp(-x)
y = (y-y.min())/(y-y.min()).max()
df = pd.DataFrame({'y':y}).set_index(x)

fig,ax = plt.subplots()
df.rolling(window=W).std().plot(lw=3,ax=ax)
df.rolling(window=W).apply(np.std).plot(ax=ax,logy=True,lw=3)
ax.grid(True,axis='both',which='both')
ax.legend(['Pandas','Numpy']);

Issue Description

Using .std() on a rolling window on a data set with a "large" range of magnitudes (ie 1e1 to 1e-10) seems to produce erratic results. Comparing to numpy.std shows the expected behavior. The problem seems to be convolved with the range of the data set and the size (1xn) of the data set. Changing just the size of the array in the example above give very different behavior, all incorrect and unexpected:

Array Size R*W, R = 30, W=50
w50r30c9

Array Size R*W, R = 17, W=50
w50r17c9

Array Size R*W, R = 14, W=50
w50r14c9

Expected Behavior

df.rolling(window=W).std() == df.rolling(window=W).apply(np.std)

Installed Versions

INSTALLED VERSIONS

commit : 37ea63d
python : 3.10.10.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 13, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 2.0.1
numpy : 1.24.3
pytz : 2023.3
dateutil : 2.8.2
setuptools : 65.5.0
pip : 23.1.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.2
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.11.0
pandas_datareader: None
bs4 : 4.11.2
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.7.1
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@DevinPrescott DevinPrescott added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 18, 2023
@topper-123
Copy link
Contributor

Possibly duplicate of #52407.

@topper-123 topper-123 added Duplicate Report Duplicate issue or pull request Numeric Operations Arithmetic, Comparison, and Logical operations and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 20, 2023
@jbrockmendel jbrockmendel added Window rolling, ewma, expanding Reduction Operations sum, mean, min, max, etc. and removed Numeric Operations Arithmetic, Comparison, and Logical operations labels Jun 7, 2023
@mroeschke
Copy link
Member

Closing as a duplicate

@kaixiongg
Copy link

Hello, I'm encountering the same issue. Any updates on this? It seems like roll_var already uses the Welford method combined with Kahan summation for more stable precision. Is there any algorithm that offers even greater stability than that?

@kaixiongg
Copy link

Closing as a duplicate

Could you reopen this issue since #52407 has been taken for one year without any useful update...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Duplicate Report Duplicate issue or pull request Reduction Operations sum, mean, min, max, etc. Window rolling, ewma, expanding
Projects
None yet
Development

No branches or pull requests

5 participants