Skip to content

BUG: Series rolling standard deviation gives zero for small numbers #40448

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
wuyuanyi135 opened this issue Mar 15, 2021 · 2 comments
Closed
2 of 3 tasks
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@wuyuanyi135
Copy link

wuyuanyi135 commented Mar 15, 2021

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

import pandas as pd
s = pd.Series([5, 5, 6, 7, 5, 2, 5])
print("## Working sample:")
print(s.rolling(3).std())

s = pd.Series([5, 5, 6, 7, 5, 2, 5]) * 1e-8
print("## Doesn't work for small data:")
std = s.rolling(3).std()
print(std)

assert std.iloc[-2] == 0

data = list(s.rolling(3))[-2]
std_manual = data.std()
print("## Manually calculated std:", std_manual)

assert std.iloc[-2] == std_manual, "Rolling std and manually calculated rolling std do not match"

Problem description

I found that when the series consists of small floating numbers (about the order of 1e-8) the rolling standard deviation will be zero. If I use the same window and compute the standard deviation directly the output is non-zero. Is there an internal precision setting for the rolling std function?

Expected Output

s = pd.Series([5, 5, 6, 7, 5, 2, 5]) * 1e-8
std = s.rolling(3).std()

std should be nonzero for the last few elements.

Output of pd.show_versions()

INSTALLED VERSIONS

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

pandas : 1.2.3
numpy : 1.20.1
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 49.6.0.post20210108
Cython : None
pytest : None
hypothesis : None
sphinx : 3.2.1
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.21.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.4
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : None
tables : None
tabulate : 0.8.7
xarray : None
xlrd : None
xlwt : None
numba : 0.52.0

@wuyuanyi135 wuyuanyi135 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 15, 2021
@jeet-parekh
Copy link
Contributor

I think the values are being set to zero by this function.

cdef inline float64_t calc_var(int64_t minp, int ddof, float64_t nobs,
float64_t ssqdm_x) nogil:
cdef:
float64_t result
# Variance is unchanged if no observation is added or removed
if (nobs >= minp) and (nobs > ddof):
# pathological case
if nobs == 1:
result = 0
else:
result = ssqdm_x / (nobs - <float64_t>ddof)
# Fix for numerical imprecision.
# Can be result < 0 once Kahan Summation is implemented
if result < 1e-14:
result = 0
else:
result = NaN
return result

@mroeschke
Copy link
Member

Thanks for the report. We're discussing how to proceed with this issue in #37051. Going to close in favor of that issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

3 participants