# standard deviation computation

If I currently have a sum of N elements (I don't have the elements them self), their mean and the corresponding standard deviation. Later on I receive M (known) other elements. I want to update the mean and standard deviation that I already have according to this new elements. Is it possible to do so ?

-
Yes you can. Add to the sum of N elements the sum of the new M elements. Then dividing it by N+M you get the new mean. Now try with the variance; remember that the variance is the mean of the squares minus the square of the mean. – Kolmo Mar 26 '12 at 10:47
So I need to save the sum of the N elements (to be able to compute the mean later) as well as the sum of squared N elements (to be able to compute the variance later) ? Then the variance when I have M new elements will be: the last sum of squared N elements plus the sum of squared M elements, minus the square of the new mean (which can be computed easily by deviding the sum of all elements by N+M) ? – shn Mar 26 '12 at 11:25
Yes. So suppose you have the old mean and variance (call them m and v) and the number N. Then you recive M new elements. So (Nm+sum of new) / (N+M) is the new mean. This is because Nm produce the sum of the old, then you add the sum of the new, and finally you divide by the total number of elements. For the variance: s = (v+m^2)*N is the sum of the old values (suqred). Then you add to s the sum of the new values (suqred, this I mean you have to do the square and then sum) and then divide it by (N+M). So you have the (new) mean of the squared (called second moment). Subtract the new mean... – Kolmo Mar 26 '12 at 11:41
...squared to get the new variance. – Kolmo Mar 26 '12 at 11:42

We need to save the sum of the $N$ elements (to be able to compute the mean later) as well as the sum of squared $N$ elements (to be able to compute the variance later). Then the variance when we have $M$ new elements will be: the last sum of squared N elements plus the sum of squared $M$ elements, minus the square of the new mean (which can be computed easily by dividing the sum of all elements by $N+M$).