Skip to content
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

BUG: Pandas rolling std precision error #46049

Closed
3 tasks done
juanmpga opened this issue Feb 18, 2022 · 2 comments
Closed
3 tasks done

BUG: Pandas rolling std precision error #46049

juanmpga opened this issue Feb 18, 2022 · 2 comments
Labels
Usage Question Window rolling, ewma, expanding

Comments

@juanmpga
Copy link

juanmpga commented Feb 18, 2022

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

data = pd.Series([-3.0, -3.0, -4.0, -5.0, -4.0, -3.0, -4.0, -4.0, -3.0, -4.0, -3.0, -4.0, -3.0, -2.0, -2.0, -3.0, -2.0, -2.0, -3.0, -3.0, -4.0, -4.0, -4.0, -4.0, -4.0, -5.0, -5.0, -5.0, -5.0, -5.0])

print(data.rolling(5).std().iloc[-1]) # <-- yields 1.2904784139758924e-08 instead of 0
print(data.tail(25).rolling(5).std().iloc[-1]) # <-- correctly yields 0

Issue Description

There seems to be a precision problem with rolling.std(). Including more data in the pd.Series affects noticeably the result of calculations that are quite far apart (several times the rolling window). I think it may be an error introduced by the online algorithm that is used.

Related:
#37055
#37051
#39872

Expected Behavior

print(data.rolling(5).std().iloc[-1])
The above should yield 0 as the 5 last data points are equal.

Installed Versions

INSTALLED VERSIONS

commit : 06d2301
python : 3.8.12.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.18363
machine : AMD64
processor : Intel64 Family 6 Model 85 Stepping 4, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : es_ES.cp1252

pandas : 1.4.1
numpy : 1.21.5
pytz : 2021.3
dateutil : 2.8.2
pip : 20.3.3
setuptools : 58.0.4
Cython : None
pytest : 7.0.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.2
IPython : 7.31.1
pandas_datareader: None
bs4 : None
bottleneck : 1.3.2
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.1
numba : 0.55.1
numexpr : 2.8.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.0
sqlalchemy : 1.3.24
tables : None
tabulate : 0.8.9
xarray : None
xlrd : None
xlwt : None
zstandard : None

@juanmpga juanmpga added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 18, 2022
@phofl
Copy link
Member

phofl commented Feb 18, 2022

The issues you have linked contain discussion about this topic. This was announced in 1.3.

See https://pandas.pydata.org/docs/whatsnew/v1.3.0.html#removed-artificial-truncation-in-rolling-variance-and-standard-deviation

@phofl phofl closed this as completed Feb 18, 2022
@phofl phofl added Usage Question Window rolling, ewma, expanding and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 18, 2022
@juanmpga
Copy link
Author

The issues you have linked contain discussion about this topic. This was announced in 1.3.

See https://pandas.pydata.org/docs/whatsnew/v1.3.0.html#removed-artificial-truncation-in-rolling-variance-and-standard-deviation

Thanks! I did see the ~1e-15 error for variance but thought it wasn't exactly for the same reason given the difference in the order of magnitude (i.e., I didn't realise ~1e-15 in variance is equivalent to ~1e-8 in std).

Thanks again for the quick response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Usage Question Window rolling, ewma, expanding
Projects
None yet
Development

No branches or pull requests

2 participants