Algorithms For Calculating Variance Shopping
Variance
Website Links For
Algorithms
 

Information About

Algorithms For Calculating Variance





ALGORITHM I

A Formula for calculating the variance of a Population of size ''n'' is:

:\sigma^2 = rac {\sum_{i=1}^{n} x_i^2 - (\sum_{i=1}^{n} x_i)^2/n}{n}. \!

A formula for calculating an Unbiased estimate of the population variance from a finite Sample of ''n'' observations is:

:s^2 = rac {\sum_{i=1}^{n} x_i^2 - (\sum_{i=1}^{n} x_i)^2/n}{n-1}. \!

Therefore a naive algorithm to calculate the estimated variance is given by the following Pseudocode :

long n = 0
double sum = 0
double sum_sqr = 0

foreach x in data:
n += 1
sum += x
  • x

  • end for


double mean = sum / n
  • mean) / (n - 1)


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.

  • mean are 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

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, ''s''2 the estimate of the population variance, and ''n'' the number of elements in the sequence before the addition.

:m_{\mathrm{new}} = rac{n \; m_{\mathrm{old}} + x_{\mathrm{new}}}{n+1} = m_{\mathrm{old}} + rac{x_{\mathrm{new}} - m_{\mathrm{old}}}{n+1} \!

:s^2_{\mathrm{new}} = rac{(n-1) \; s^2_{\mathrm{old}} + (x_{\mathrm{new}} - m_{\mathrm{new}}) \, (x_{\mathrm{new}} - m_{\mathrm{old}})}{n} \!

A numerically stable algorithm is given below. It also computes the mean.
This algorithm is due to Knuth Donald E. Knuth (1998). '' The Art Of Computer Programming '', volume 2: ''Seminumerical Algorithms'', 3rd edn., p. 232. Boston: Addison-Wesley.,
who cites WelfordB. P. Welford (1962). "Note on a method for calculating corrected sums of squares and products". ''Technometrics'' 4(3):419–420..

long n = 0
double mean = 0
double S = 0

foreach x in data:
n += 1
double delta = x - mean
mean += delta / n
  • (x - mean) // This expression uses the new value of mean

  • end for


double variance = S / (n - 1)

As above, the population variance can be computed instead of the sample variance by changing the last division.

This algorithm is much less prone to loss of precision due to massive cancellation. 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.


REFERENCES




EXTERNAL LINKS