Pandas EWMA does not work as expected

I am trying to calculate EWMA using pandas, but the result is not the one I expected. I think the 4th element should be 13.179, but pandas gives 13.121. I converted the decay coefficient (a) to the center of mass according to the formula specified in the documentation . I do not understand something?

In[222]: y Out[222]: 0 NaN 1 NaN 2 13.192161 3 13.109292 4 12.623850 5 12.150520 Name: data, dtype: float64 In[223]: pd.ewma(y, com = 1.0 / a - 1) Out[223]: 0 NaN 1 NaN 2 13.192161 3 13.120667 4 12.701206 5 12.237839 dtype: float64 In[224]: a Out[224]: 0.8408964152537145 In[225]: a * 13.192161 + (1 - a) * 13.109292 Out[225]: 13.17897624503566 
+4
source share
1 answer

Because the docs say

 a = com/(1 + com) 

follows that

 com = a/(1.0-a) 

(for 0 <= a <1).


In addition, the values โ€‹โ€‹calculated in the initial periods "to take into account the imbalance in relative weights" are adjusted. To confirm the formula

enter image description here

disable this setting:

 z = pd.ewma(x, com=a/(1.0-a), adjust=False) print(z) 

then prints

 0 NaN 1 NaN 2 2.098920 3 3.850710 4 5.246548 5 6.344995 

and this result can be mimicked by calculating

 import pandas as pd import numpy as np import numpy.testing.utils as NTU nan = np.nan x = pd.Series([nan, nan, nan, 13.109292, 12.623850, 12.150520]) a = 0.8408964152537145 z = pd.ewma(x, com=a/(1.0-a), adjust=False) def nanzero(x): return 0 if np.isnan(x) else x x.ffill(inplace=True) y = [x[0]] for xt in x[1:]: yt1 = y[-1] if np.isnan(yt1) and np.isnan(xt): yt = nan else: yt1 = nanzero(yt1) xt = nanzero(xt) yt = a*yt1 + (1-a)*xt # yt = (1-a)*yt1 + a*xt y.append(yt) y = pd.Series(y) NTU.assert_allclose(y,z) 
+3
source

Source: https://habr.com/ru/post/1494354/


All Articles