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
 A: $$\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.
A: This is basically the same idea as Nameless' answer, but I think it's cleaner if you just keep and update $SX$, the sum of the terms, and $SX^2$, the sum of their squares, update those via
$$\begin{align}
SX_{i+1} &= SX_i + x_{i+1}-x_{i-999}\\
SX^2_{i+1} &= SX^2_i + x_{i+1}^2-x_{i-999}^2\\
\end{align}
$$
and then compute
$$\begin{align}
mean_{i+1} &= SX_{i+1}/1000\\
var_{i+1} &= \frac{1}{1000}SX^2_{i+1} - (\frac{1}{1000}SX_{i+1})^2\\
 &= \frac{1}{1000}SX^2_{i+1} - mean_{i+1}^2\\
\end{align}
$$
(This is the way that old calculators would let you accumulate an arbitrary number of data values and then let you compute mean and variance at the end.)
There is a possible issue in that you have to be able to store a number of magnitude roughly $1000*(\text{typical }x)^2$. Hopefully your values aren't too large.
If you're really worried about numerical instability, @awkward's link to Wellford's online algorithm looks to be the state-of-the-art; I'd check that out if your output values start to go wrong after you've been running for a while.
