Reputation: 1212
I am trying to build an exponential moving average algo which produces the same output as the Pandas ewm()
function. In particular, I am trying to implement this approach:
The code below works correctly up until the point that the moving average window starts to move beyond the initial dataset, at which point I start to get different results versus the Pandas calculation.
I've been looking at this for hours now and am stumped. Can anyone point out to me how I have mis-implemented the above formula?
import numpy as np
import pandas as pd
class MovingAverages(object):
def __init__(self, **kwargs):
self.measures = []
self.lookback_period = 5
ema_multiplier = 2 / (self.lookback_period + 1)
self.lookback_alphas = []
for i in range(1,self.lookback_period+1):
self.lookback_alphas.append((1 - ema_multiplier ) ** i)
def insert_bar(self):
self.measures.insert(0, 0)
def on_calculate(self, c):
index = min(len(c), self.lookback_period+1)
y = c[0]
for i in range(1,index):
y += self.lookback_alphas[i-1] * c[i]
y /= 1 + sum(self.lookback_alphas[0:index-1])
self.measures[0] = y
if __name__ == "__main__":
data = [5.00,7.00,4.00,3.00,4.00,
5.00,6.00,7.00,9.00,13.00,
15.00,14.00,14.00,12.00,
11.00,10.00,9.00,8.00,
8.00,8.00,10.00,11.00,
13.00,16.00,18.00,20.00]
# Manually calculate exponential MA and write into list
ma_online = MovingAverages()
series = []
for d in data:
series.insert(0, d)
ma_online.insert_bar()
ma_online.on_calculate(series)
# Calculate a reference exponential MA using Pandas
df = pd.DataFrame({'close': data})
ma_pd = list(df.close.ewm(span=ma_online.lookback_period, adjust=True, ignore_na=True).mean())
# Compare the two lists
ma_online.measures.reverse()
for i in range(len(data)):
assert round(ma_pd[i], 2) == round(ma_online.measures[i], 2)
Upvotes: 3
Views: 4058
Reputation: 1457
I'm not familiar with Python or Panda, but from my understanding of the documentation, when you use span
in df.close.ewm(span=ma_online,...)
, you just define the decay (coefficient) in the formula, not the size of a sliding window. By default, there doesn't seem to be a fixed side window.
From another documentation of DataFrame.ewm :
span : float, optional
Specify decay in terms of span, α=2/(span+1), for span≥1
And:
See also:
rolling: Provides rolling window calculations
The rolling
method is probably what you need.
Upvotes: 1