Calculating standard deviation of a moving window

1.1k Views Asked by At

I am receiving a stream of numbers. I need to calculate the standard deviation of the last 100 numbers at each step. I don't have enough time for calculating it from scratch.

Is there a cheaper way of this calculation?

1

There are 1 best solutions below

1
On BEST ANSWER

Assuming you are using SD with Bessel's correction, call $\mu_n$ and $SD_n$ the mean and standard deviation from $n$ to $n+99$. Then, calculate $\mu_1$ and $SD_{1}$ afterwards, you can use the recursive relation $\mu_{n+1}=\mu_n-\frac 1{99}x_n+ \frac 1{99}x_{n+100}$ and $SD_{n+1}=\sqrt{SD_n^2-\frac1{99}(x_n-\mu_n)^2+\frac1{99}(x_{n+100}-\mu_{n+1})^2}$.