I talked a bit about variance in the post on standard deviation, so this is a little bit a rehash from what I said there.

The variance is obtained by calculating the data set consisting of each data point in the original data set subtracting the arithmetic mean for the data set, and then squaring it.

We then take the arithmetic mean of the deviations, which gives us the variance for our data set.

		public static double Variance(double[] data, int items)
		{
			int i;
			double variance;
			double[] deviation = new double[items];

			mean = ArithmeticMean(data, items);

			for (i = 0; i < items; i++)
			{
				deviation[i] = Math.Pow((data[i] - mean), 2);
			}

			variance = ArithmeticMean(deviation, items);

			return variance;
		}
Share