Standard Deviation of an Exponentially-weighted Mean

I wrote a simple function in Python to calculate the exponentially weighted mean:

def test():
  x = [1,2,3,4,5]
  alpha = 0.98
  s_old = x[0]

  for i in range(1, len(x)):
    s = alpha * x[i] + (1- alpha) * s_old
    s_old = s

  return s

However, how can I calculate the corresponding SD?

Answer

You can use the following recurrent formula:

σ2i=Si=(1α)(Si1+α(xiμi1)2)

Here xi is your observation in the i-th step, μi1 is the estimated EWM, and Si1 is the previous estimate of the variance. See Section 9 here for the proof and pseudo-code.

Attribution
Source : Link , Question Author : Mariska , Answer Author : Roman Shapovalov

Leave a Comment