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

stdev does not take ddof = 0 #354

Closed
dnabb opened this issue Jul 26, 2021 · 8 comments
Closed

stdev does not take ddof = 0 #354

dnabb opened this issue Jul 26, 2021 · 8 comments
Assignees
Labels
bug Something isn't working

Comments

@dnabb
Copy link

dnabb commented Jul 26, 2021

Which version are you running? The lastest version is on Github. Pip is for major releases.
0.3.2b0

Describe the bug
Calling stdev with ddof=0 (and all other derived indicator, like bbands) actually returns a standard deviation with ddof = 1.

To Reproduce

import pandas as pd
import pandas_ta as ta

data = {'close': ['1', '2', '2', '1']}
df = pd.DataFrame.from_dict(data)

df.ta.stdev(length=4, ddof=0)[3] # Shoukd be 0.5

df.ta.stdev(length=4, ddof=0).equals(df.ta.stdev(length=4, ddof=1)) # Should be False

Expected behavior
Should be the same as:

df.rolling(window=4).std(ddof=0)['close']

Additional context
The issue seems to be this validation which fails with ddof = 0.

@dnabb dnabb added the bug Something isn't working label Jul 26, 2021
@twopirllc
Copy link
Owner

twopirllc commented Jul 26, 2021

Hello @dnabb,

Doh! Nice catch!

In the mean time, edit your local copy of variance and stdev and replace that validation with the following until the code is updated.

ddof = int(ddof) if isinstance(ddof, int) and ddof >= 0 and ddof < length else 1

Hope this helps!

Kind Regards,
KJ

twopirllc added a commit that referenced this issue Jul 26, 2021
@twopirllc
Copy link
Owner

@dnabb,

Also I have added the changes to the development branch if you want to test it out.

$ pip install -U git+https://github.com/twopirllc/pandas-ta.git@development

Please let me know if it works as intended if you try the development branch.

Thanks,
KJ

@twopirllc
Copy link
Owner

Hello @dnabb,

New stable version v0.3.14b should help resolve this Issue. Please let me know otherwise.

$ pip install pandas_ta

Thanks,
KJ

@twopirllc
Copy link
Owner

Hello @dnabb,

I assume by no response that the solution provided was sufficient. Thus I will be closing this issue in a few days.

Kind Regards,
KJ

twopirllc added a commit that referenced this issue Aug 1, 2021
@twopirllc
Copy link
Owner

Hello @dnabb,

This should be fixed now. If you can test it out on the development branch and let me know, that would be great.

$ pip install -U git+https://github.com/twopirllc/pandas-ta.git@development

Thanks
KJ

@dnabb
Copy link
Author

dnabb commented Aug 5, 2021

Hello @twopirllc ,

I can confirm the issue seems to be solved in 0.3.18b0.

@krazykoder
Copy link

krazykoder commented Jun 14, 2022

Just reopening this post. I observed TALIB STDDEV and pandas_ta std_dev generates different output. A reference to this issue is noted here as well TA-Lib/ta-lib-python#69 (comment). Though not sure this is exactly the same or not.

But I just wanted to confirm with others that pandas_ta standard deviation should be safe to use right since it matches my final indicator calculations cross referencing tradingview. I will stop using talib for this function.

code to try below

length=100, fast=50, slow=50

import talib as talib
talib.STDDEV(closeSeries, timeperiod=fast)

2022-06-08 12:00:00    12.704822
2022-06-09 08:00:00    12.060018
2022-06-09 12:00:00    10.837907
2022-06-10 08:00:00    10.635564
2022-06-10 12:00:00    10.512141
Length: 2038, dtype: float64
import pandas_ta as ta

ta.stdev(closeSeries, fast)

2022-06-08 12:00:00    12.833808
2022-06-09 08:00:00    12.182457
2022-06-09 12:00:00    10.947939
2022-06-10 08:00:00    10.743542
2022-06-10 12:00:00    10.618866
Name: STDEV_50, Length: 2038, dtype: float64

@krazykoder
Copy link

Closing this issue, I found the answer in the same talib post (did not read fully sorry!)

While using TALIB need to add nbdev parameter which is np.sqrt(length / (length - 1)

talib.STDDEV(closeSeries * 2, timeperiod=fast, nbdev=np.sqrt(fast / (fast - 1))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants