0

I would like to calculate the exponential moving average of my data, as usual, there are a few different way to implement it in python. And before I use any of them, I would like to understand (verify) it, and the result is very surprising, none of them are the same!

Below I use the TA-Lib EMA, as well as the pandas ewm function. I have also included one from excel, using formula [data now-EMA (previous)] x multiplier + EMA (previous), with multiplier = 0.1818.

Can someone explain how they are calculated? why they all have different result? which one is correct?

df = pd.DataFrame({"Number": [x for x in range(1,7)]*5})
data = df["Number"]
df["TA_MA"] = MA(data, timeperiod = 5)
df["PD_MA"] = data.rolling(5).mean()
df["TA_EMA"] = EMA(data, timeperiod = 5)
df["PD_EMA_1"] = data.ewm(span=5, adjust=False).mean()
df["PD_EMA_2"] = data.ewm(span=5, adjust=True).mean()

    Number  TA_MA  PD_MA    TA_EMA  PD_EMA_1  PD_EMA_2  Excel_EMA
0        1    NaN    NaN       NaN  1.000000  1.000000  NaN
1        2    NaN    NaN       NaN  1.333333  1.600000  NaN
2        3    NaN    NaN       NaN  1.888889  2.263158  NaN
3        4    NaN    NaN       NaN  2.592593  2.984615  NaN
4        5    3.0    3.0  3.000000  3.395062  3.758294  3.00
5        6    4.0    4.0  4.000000  4.263374  4.577444  3.55
6        1    3.8    3.8  3.000000  3.175583  3.310831  3.08
7        2    3.6    3.6  2.666667  2.783722  2.856146  2.89
8        3    3.4    3.4  2.777778  2.855815  2.905378  2.91
9        4    3.2    3.2  3.185185  3.237210  3.276691  3.11
10       5    3.0    3.0  3.790123  3.824807  3.857846  3.45
11       6    4.0    4.0  4.526749  4.549871  4.577444  3.91
12       1    3.8    3.8  3.351166  3.366581  3.378804  3.38
13       2    3.6    3.6  2.900777  2.911054  2.917623  3.13
14       3    3.4    3.4  2.933852  2.940703  2.945145  3.11
15       4    3.2    3.2  3.289234  3.293802  3.297299  3.27
16       5    3.0    3.0  3.859490  3.862534  3.865443  3.58
17       6    4.0    4.0  4.572993  4.575023  4.577444  4.02
18       1    3.8    3.8  3.381995  3.383349  3.384424  3.47
19       2    3.6    3.6  2.921330  2.922232  2.922811  3.21
20       3    3.4    3.4  2.947553  2.948155  2.948546  3.17
21       4    3.2    3.2  3.298369  3.298770  3.299077  3.32
22       5    3.0    3.0  3.865579  3.865847  3.866102  3.63
23       6    4.0    4.0  4.577053  4.577231  4.577444  4.06
24       1    3.8    3.8  3.384702  3.384821  3.384915  3.50
25       2    3.6    3.6  2.923135  2.923214  2.923265  3.23
26       3    3.4    3.4  2.948756  2.948809  2.948844  3.19
27       4    3.2    3.2  3.299171  3.299206  3.299233  3.33
28       5    3.0    3.0  3.866114  3.866137  3.866160  3.64
29       6    4.0    4.0  4.577409  4.577425  4.577444  4.07
Victor
  • 419
  • 2
  • 5
  • 14
  • Please check ["Which site?"](https://meta.stackexchange.com/questions/129598/which-computer-science-programming-stack-exchange-do-i-post-in) for general issues. Stack Overflow is for specific programming problems, rather than wider tutorial work. – Prune Nov 01 '18 at 17:52
  • Possibly related: https://stackoverflow.com/questions/37924377/does-pandas-calculate-ewm-wrong – Josh Friedlander Nov 01 '18 at 18:21

0 Answers0