ENCYCLOPEDIA 4U .com



Encyclopedia Home Page

Google
  Web Encyclopedia4u.com

 

Algorithms for calculating variance

The formula for calculating variance is:

Variance = [n{x12 + x22 + ... + xn2} - {x1 + x2 + ... + xn}2] / n2-n

The method of calculation may be more easily understood from the table below where the mean is 8.

 
i xi xi-mean (xi-mean)2
(index) (datum) (deviation) (squared deviation)
1 5 -3 9
2 7 -1 1
3 8 0 0
4 10 2 4
5 10 2 4
n=5 sum=40 0 18

     
  • mean = 40/5 = 8
  • variance = (5*338 - 402)/(25-5) = 4.5
  • standard deviation = SQUARE ROOT [ VARIANCE ] = 2.12

Note: Details of the variance calculation:

338 = [52 + 72 + 82 + 102 + 102]
40 = [5 + 7 + 8 + 10 + 10]

Algorithm

Therefore a simple algorithm to calculate variance can be:

double sum;
double sum_sqr;
double variance;
long n = data.length; // the number of elements in the data array (the actual syntax is language-specific)

for i = 0 to n sum += data[i]; sum_sqr += ( data[i] * data[i] ); end for

variance = ((n * sum_sqr) - (sum * sum))/(n*(n-1));

Corrected by: Salman Quazi [salman at ucla dot edu]
http://www.setcomputing.com/





Content on this web site is provided for informational purposes only. We accept no responsibility for any loss, injury or inconvenience sustained by any person resulting from information published on this site. We encourage you to verify any critical information with the relevant authorities.



Copyright © 2005 Par Web Solutions All Rights reserved.
| Privacy

This article is licensed under the GNU Free Documentation License. It uses material from the Wikipedia article "Algorithms for calculating variance".