I recently came across the term EMA (Exponential Moving Average) and EWMA (Exponential Weighted Moving Average) in Time Series Data. I am not able to figure out the difference between these two? Also I want to know how to implement the EWMA in pandas.
EMA is EWMA, the key is to understand why do we need something Exponentially, compared with the simplest average SMA, you might know it's all related to time:
Recently, stocks' prices, and COVID-19 concern us more than 10-20 years ago's Lehman Brothers and the dot-com bubble, talking to how they influenced the price and economics, we might want to put weight/ratio on them to calculate the consequence we assume, something decay within time, you can feel it.
Let's prepare the data to plot the EMA, let's say we wanna see an apple:
from pandas_datareader import data
df = data.DataReader(['AAPL'], 'yahoo', start='2022/02/15', end='2022/06/21')
df = df['Adj Close'].round(2)
print(df.tail())
Output
Symbols AAPL
Date
2022-06-14 132.76
2022-06-15 135.43
2022-06-16 130.06
2022-06-17 131.56
2022-06-21 135.87
And here's EMA
# we take 5 for every EW moving average
df['AAPL-ema'] = df.iloc[:,0].ewm(span=5).mean()
print(df)
Output
Symbols AAPL AAPL-ema
Date
2022-02-14 168.63 168.630000
2022-02-15 172.54 170.976000
2022-02-16 172.30 171.603158
2022-02-17 168.63 170.368154
2022-02-18 167.05 169.094360
... ... ...
2022-06-14 132.76 137.133554
2022-06-15 135.43 136.565703
2022-06-16 130.06 134.397135
2022-06-17 131.56 133.451423
2022-06-21 135.87 134.257616
and the plot:
df[['AAPL','AAPL-ema']].plot(figsize=(15,6))
Output

In default, adjust is activated, it divide by decaying adjustment factor

it leads to different weights on price and varied over time.
Refer to pandas.DataFrame.ewm
there are still tons of arguments we need to concern, adjust, alpha, com, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With