3

Generally, we calculate exponential moving averages as the following:

y_t = (1 - alpha) * y_tminus1 + alpha * x_t

where alpha is the alpha specified for the exponential moving average, y_t is the resulting moving average, and x_t is the new inputted data.

This seems to be confirmed in the methodology behind Pandas' implementation of the exponentially weighted moving average as well.

So I wrote an online algorithm for calculating the exponentially weighted moving average of a dataset:

def update_stats_exp(new, mean): mean = (1 - ALPHA) * mean + ALPHA* new return mean

However, this returns a different moving average compared to that of Pandas' implementation, called by the following two lines of code:

exponential_window = df['price'].ewm(alpha=ALPHA, min_periods=LOOKBACK, adjust=False, ignore_na=True) df['exp_ma'] = exponential_window.mean()

In both of the above pieces of code, I kept ALPHA the same, yet they resulted in different moving averages, even though the documentation that Pandas provided on exponentially weighted windows seems to match the methodology I had in mind.

Can someone elucidate the differences between the online function I've provided for calculating moving average and Pandas' implementation for the same thing? Also, is there an easy way of formulating Pandas' implementation into an online algorithm?

Thanks so much!

sos
  • 331
  • 2
  • 8

0 Answers0