Calculating the Rolling Variance of a set of numbers I would like to subscribe to a WebSocket stream which will supply me with many numbers per second. From this data, I would like to calculate the variance of say the last 1000 numbers.
How can I do this in a rolling fashion? That is, I would like some computation comparable to this one for the mean of the last 1000 numbers:
$$\rm{mean}_{i+1} = \rm{mean}_{i} + \frac{1}{1000}\left(x_{i+1}-x_{i-999}\right)$$
Thanks in advance for any help.
Ben

$$\rm{variance}_{i+1} = \frac{1}{1000}\sum_{j=i-999}^{i+1}\left(x_j-\rm{mean}_{i+1}\right)^2\\
=\frac{1}{1000}(x_{i+1}^2-x_{i-999}^2)+\rm{variance}_{i}-\rm{mean}_{i+1}^2+\rm{mean}_{i}^2,$$
where you already computed $\rm{mean}_{i+1}$ according to your equation.
If you wonder why the equality holds, see here.
