Algorithms for calculating variance play a major 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.
Contents
Naïve algorithm
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:
def naive_variance(data):
n = 0
Sum = 0
Sum_sqr = 0
for x in data:
n = n + 1
Sum = Sum + x
Sum_sqr = Sum_sqr + x*x
mean = Sum/n
variance = (Sum_sqr  Sum*mean)/(n  1)
return variance
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 floatingpoint arithmetic used to perform the computation. This is particularly bad if the standard deviation is small relative to the mean.
[edit] Twopass algorithm
An alternate approach, using a different formula for the variance, first computes the sample mean:
and then computes the sum of the squares of the differences from the mean:
is given by the following pseudocode:
def two_pass_variance(data):
n = 0
sum1 = 0
sum2 = 0
for x in data:
n = n + 1
sum1 = sum1 + x
mean = sum1/n
for x in data:
sum2 = sum2 + (x  mean)*(x  mean)
variance = sum2/(n  1)
return variance
This algorithm is often more numerically reliable than the naïve algorithm 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 error to a degree.
[edit] Compensated variant
The compensatedsummation version of the algorithm above reads^{[citation needed]}:
def compensated_variance(data):
n = 0
sum1 = 0
for x in data:
n = n + 1
sum1 = sum1 + x
mean = sum1/n
sum2 = 0
sumc = 0
for x in data:
sum2 = sum2 + (x  mean)**2
sumc = sumc + (x  mean)
variance = (sum2  sumc**2/n)/(n  1)
return variance
[edit] Online algorithm
It is often useful to be able to compute the variance in a single pass, inspecting each value x_{i} only once; for example, when the data are being collected without enough storage to keep all the values, or when costs of memory access dominate those of computation. For such an online algorithm, a recurrence relation is required between quantities from which the required statistics can be calculated in a numerically stable fashion.
The following formulas can be used to update the mean and (estimated) variance of the sequence, for an additional element x_{new}. Here, x_{n} denotes the sample mean of the first n samples (x_{1}, ..., x_{n}), s^{2}_{n} their sample variance, and σ^{2}_{n} their population variance.
It turns out that a more suitable quantity for updating is the sum of squares of differences from the (current) mean, , here denoted M_{2,n}:
Full article ▸
