Standard Deviation: Understanding Exponentially-weighted Mean

exponential-smoothingpythonstandard deviation

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?

Best Answer

You can use the following recurrent formula:

$\sigma_i^2 = S_i = (1 - \alpha) (S_{i-1} + \alpha (x_i - \mu_{i-1})^2)$

Here $x_i$ is your observation in the $i$-th step, $\mu_{i-1}$ is the estimated EWM, and $S_{i-1}$ is the previous estimate of the variance. See Section 9 here for the proof and pseudo-code.

Related Question