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

Calculation of first item in the exponential weighted mean ( EWM) #25147

Open
borisRa opened this issue Feb 4, 2019 · 2 comments
Open

Calculation of first item in the exponential weighted mean ( EWM) #25147

borisRa opened this issue Feb 4, 2019 · 2 comments
Labels
Enhancement Window rolling, ewma, expanding

Comments

@borisRa
Copy link

borisRa commented Feb 4, 2019

pandas/pandas/core/generic.py

Lines 10159 to 10166 in 1700680

@Appender(rwindow.ewm.__doc__)
def ewm(self, com=None, span=None, halflife=None, alpha=None,
min_periods=0, adjust=True, ignore_na=False,
axis=0):
axis = self._get_axis_number(axis)
return rwindow.ewm(self, com=com, span=span, halflife=halflife,
alpha=alpha, min_periods=min_periods,
adjust=adjust, ignore_na=ignore_na, axis=axis)

Running this code :

pd.DataFrame({'B': [1,20,400,1,1,1]}).ewm(span=2,min_periods=2,adjust=False).mean().values.flatten()
I am getting
array([ nan, 13.66666667, 271.22222222, 91.07407407, 31.02469136, 11.00823045])

As I understand the first item should be the simple moving average with window equals to span ( here it is 2 ). See for example see this link : https://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:moving_averages

There are three steps to calculating an exponential moving average (EMA). First, calculate the simple moving average for the initial EMA value. An exponential moving average (EMA) has to start somewhere, so a simple moving average is used as the previous period's EMA in the first calculation.

so instead of 13.66666 it should be (20+1)/2 =>10.5

Afterwards the formula is :
image

The formula also can be found here : http://pandas.pydata.org/pandas-docs/stable/user_guide/computation.html when adjust is set to False .

What do you think ?

Thanks,
Boris

@chris-b1
Copy link
Contributor

chris-b1 commented Feb 5, 2019

I think this is a reasonable common adjustment to an EWM in practice, but not sure it should be a default, as it's not really the "standard" definition, e.g. https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average

So I could see expanding the api here, or this is not that hard to do yourself.

seed_ewm = df['B'].rolling(2).mean().iloc[1]

df.iloc[:2] = seed_ewm

df.ewm(span=2,min_periods=2,adjust=False).mean()
Out[129]: 
            B
0         NaN
1   10.500000
2  270.166667
3   90.722222
4   30.907407
5   10.969136

10.5 * (1 - 2/3) + 400 * (2/3)
Out[130]: 270.16666666666663

@chris-b1 chris-b1 added API Design Window rolling, ewma, expanding labels Feb 5, 2019
@borisRa
Copy link
Author

borisRa commented Feb 6, 2019

Got it thanks ! there are two formulas :
image

image

Pandas applies the second formula and instead of S2 = moving average of Y1 & Y2 it starts from S1 = Y1 .

So instead of :
df.iloc[:2] = seed_ewm
It should be :
df.iloc[:1] = seed_ewm

and the result is :

0 NaN
1 16.833333
2 272.277778
3 91.425926
4 31.141975
5 11.047325

Thank you !

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

No branches or pull requests

3 participants