Algorithms for calculating variance

# Overview
Algorithms for calculating variance play a minor role in statistical computing. A key problem in the design of good algorithms for this problem is that formulas for the variance may involve sums of squares, which can lead to numerical instability as well as to arithmetic overflow when dealing with large values.

# Algorithm I
The formula for calculating the variance of an entire population of size n is:
The formula for calculating an unbiased estimate of the population variance from a finite sample of n observations is:
Therefore a naive algorithm to calculate the estimated variance is given by the following pseudocode:
This algorithm can easily be adapted to compute the variance of a finite population: simply divide by n instead of n  1 on the last line.
Because sum_sqr and sum - mean can be very similar numbers, the precision of the result can be much less than the inherent precision of the floating-point arithmetic used to perform the computation.  This is particularly bad if the variance is small relative to the sum of the numbers.

# Algorithm II
An alternate approach, using a different formula for the variance, is given by the following pseudocode:
This algorithm is often more numerically reliable than Algorithm I for large sets of data, although it can be worse if much of the data is very close to but not precisely equal to the mean and some are quite far away from it.
The results of both of these simple algorithms (I and II) can depend inordinately on the ordering of the data and can give poor results for very large data sets due to repeated roundoff error in the accumulation of the sums; techniques such as compensated summation can be used to combat this to a degree.

# Algorithm II (compensated)
The compensated-summation version of the algorithm above reads:

# Algorithm III
The following formulas can be used to update the mean and (estimated) variance of the sequence, for an additional element x_{\mathrm{new}}. Here, m denotes the estimate of the population mean, s2n-1 the estimate of the population variance, s2n the estimate of the sample variance, and n the number of elements in the sequence before the addition.
A numerically stable algorithm is given below.  It also computes the mean.
This algorithm is due to Knuth,
who cites Welford.
This algorithm is much less prone to loss of precision due to massive cancellation, but might not be as efficient because of the division operation inside the loop.  For a particularly robust two-pass algorithm for computing the variance, first compute and subtract an estimate of the mean, and then use this algorithm on the residuals.

# Example
Assume that all floating point operations use the standard IEEE 754 double-precision arithmetic. Consider the sample (4, 7, 13, 16) from an infinite population. Based on this sample, the estimated population mean is 10, and the estimated population variance is 30.  Both algorithms compute these values correctly.  Next consider the sample (10^8+4, 10^8+7, 10^8+13, 10^8+16), which gives rise to the same estimated variance as the first sample.  Algorithm II computes this variance estimate correctly, but Algorithm I returns 29.333333333333332 instead of 30.  While this loss of precision may be tolerable and viewed as a minor flaw of Algorithm I, it is easy to find data that reveal a major flaw in the naive algorithm: Take the sample to be (10^9+4, 10^9+7, 10^9+13, 10^9+16).  Again the estimated population variance of 30 is computed correctly by Algorithm II, but the naive algorithm now computes it as -170.66666666666666.  This is a serious problem with Algorithm I, since the variance can, by definition, never be negative.