This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET 3.5+

# LINQ Style Variance and Standard Deviation Operators

In statistics, the variance and standard deviation for a set of data indicate how spread out the individual values are. Small values indicate that the elements of a set are close to the average value, whereas larger values suggest a greater spread.

## Testing the Methods

We can now test the calculations with some code in the Main method of the console application. The following calls each of the eight public extension methods for the same set of data:

```double[] stuff = new double[] { 98, 100, 105 };

double pVariance = stuff.VarianceOfPopulation();        // 8.6666666666666661
double pStdDev = stuff.StdDevOfPopulation();            // 2.9439202887759488
double sVariance = stuff.VarianceOfSample();            // 13.0
double sStdDev = stuff.StdDevOfSample();                // 3.6055512754639891

double pVariance1P = stuff.VarianceOfPopulation1P();    // 8.6666666666666661
double pStdDev1P = stuff.StdDevOfPopulation1P();        // 2.9439202887759488
double sVariance1P = stuff.VarianceOfSample1P();        // 13.0
double sStdDev1P = stuff.StdDevOfSample1P();            // 3.6055512754639891```

In the above example the variances and standard deviations are identical for the multiple pass and on-line algorithms. This is not always the case though, as the on-line variations are more susceptible to rounding errors. Try running the code below. Note that the single-pass methods have slightly different results due to the additional multiplication and division. In many cases this will not present a problem but if the results are used further, particularly in large multiplications, the errors introduced could be quite large.

```double[] stuff = new double[] {700, 500, 250, 350, 900, 500 };

double pVariance = stuff.VarianceOfPopulation();        // 46388.8888888889
double pStdDev = stuff.StdDevOfPopulation();            // 215.38079972200146
double sVariance = stuff.VarianceOfSample();            // 55666.666666666672
double sStdDev = stuff.StdDevOfSample();                // 235.9378449224852

double pVariance1P = stuff.VarianceOfPopulation1P();    // 46388.888888888883
double pStdDev1P = stuff.StdDevOfPopulation1P();        // 215.3807997220014
double sVariance1P = stuff.VarianceOfSample1P();        // 55666.666666666664
double sStdDev1P = stuff.StdDevOfSample1P();            // 235.93784492248517```
2 April 2013