Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between EMA and EWMA?

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.

like image 241
Parishi Gupta Avatar asked Jan 18 '26 07:01

Parishi Gupta


1 Answers

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 enter image description here


In default, adjust is activated, it divide by decaying adjustment factor enter image description here

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.

like image 73
Baron Legendre Avatar answered Jan 20 '26 19:01

Baron Legendre