# 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

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?

• Are you after the standard error of the mean, or some estimate of the standard deviation of the process? – Glen_b Aug 14 '14 at 13:56
• @Glen_b I am trying to use this to see how much a stock price deviates from the exponentially-weighted mean by some multiple of the "standard deviation". Which one would you recommend? – Mariska Aug 14 '14 at 14:07
• From what I can see, there's a fundamental conflict (or inconsistency) underlying this question. People use the EWM when they do not care to analyze the data to characterize and quantify the serial correlation, but in order to answer this question the serial correlation must be estimated; but then why would you use the EWM in the first place? – whuber Aug 14 '14 at 14:37

## 1 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.

• using the above formula and the list [1,2,3,4,5], I got SD = 0.144, whereas the normal Sample SD is 1.58. There is a factor of 10x between the two different SD's. Is this normal? – Mariska Aug 15 '14 at 2:12
• Using $\alpha = 0.98$ you also get the mean = 4.98, which is equally useless. :) Using such coefficient, you put almost all weight on the last measurement. More realistic values of $\alpha$ are close to zero, in that case they account for long-range average. For your example, try $\alpha = 0.2$, but in practice you will probably need to average more measurements, so the values around $\alpha = 0.01$ are more realistic. – Roman Shapovalov Aug 15 '14 at 7:30
• The Section 9 link is broken, and the shape of the formula looks odd to me. I was expecting sth like (1-a)*sth + a*sth, instead of (1-a)*(...). – Albert Netymk Aug 19 '20 at 15:18