notes
Variance

Variance

Definition

Definition

The variance of a population or a sample of a population is a measure of spread. The definition is slightly different depending on whether the data is a population or a sample.

For population data x ix_i, it is defined as:

σ x 2=1n(x ix¯) 2 \sigma^2_x = \frac1n \sum (x_i - \bar{x})^2

where x¯\bar{x} is the mean of the population.

For sample data x ix_i, it is defined as:

s x 2=1n1(x ix¯) 2 s^2_x = \frac1{n-1} \sum (x_i - \bar{x})^2

The rationale behind the definition is as follows. We want to measure how spread out the data is, so we imagine an extreme situation where all the data takes the same value. That value would then be the mean, x¯\bar{x}. To measure the spread of our data, we measure the distance between our actual data and this extreme case. Now one standard way to measure the distance between two lists of numbers is to use a generalisation of Pythagoras' theorem?, namely: calculate the differences, square them, and then add them up.

There are two extras here. Firstly, the formula in Pythagoras' theorem? then takes the square root. This would result in the standard deviation, but often the variance is easier to work with. Secondly, we often want to compare the variance of data of different lengths. Dividing the answer by the number of data values, nn, means that it makes sense to compare the variance of different sizes of data.

Alternative Formula

The formula for the population variance can be rearranged to:

σ x 2=1nx i 2x¯ 2 \sigma^2_x = \frac1n \sum x_i^2 - \bar{x}^2

This is often more straightforward to calculate in practice than the formula in the definition.

The sample variance can be similarly rearranged to:

s x 2=1n1x i 2nn1x¯ 2 s^2_x = \frac1{n-1} \sum x_i^2 - \frac{n}{n-1} \bar{x}^2

These formulae are derived as follows. It uses the fact that x¯=1nx i\bar{x} = \frac1n \sum x_i and that the sum of nn copies of a number is nn times that number.

σ x 2 =1n(x ix¯) 2 =1n(x i 22x ix¯+x¯ 2) =1nx i 22x¯1nx i+1nx¯ 2 =1nx i 22x¯x¯+1nnx¯ 2 =1nx i 22x¯ 2+x¯ 2 =1nx i 2x¯ 2 s x 2 =1n1(x ix¯) 2 =1n1(x i 22x ix¯+x¯ 2) =1n1x i 22x¯1n1x i+1n1x¯ 2 =1n1x i 22x¯nn1x¯+1n1nx¯ 2 =1n1x i 22nn1x¯ 2+nn1x¯ 2 =1n1x i 2nn1x¯ 2 \begin{aligned} \sigma^2_x &= \frac1n \sum (x_i - \bar{x})^2 \\ &= \frac1n \sum (x_i^2 - 2 x_i \bar{x} + \bar{x}^2) \\ &= \frac1n \sum x_i^2 - 2 \bar{x} \frac1n \sum x_i + \frac1n \sum \bar{x}^2 \\ &= \frac1n \sum x_i^2 - 2 \bar{x} \bar{x} + \frac1n n \bar{x}^2 \\ &= \frac1n \sum x_i^2 - 2 \bar{x}^2 + \bar{x}^2 \\ &= \frac1n \sum x_i^2 - \bar{x}^2 \\ s^2_x &= \frac1{n-1} \sum (x_i - \bar{x})^2 \\ &= \frac1{n-1} \sum (x_i^2 - 2 x_i \bar{x} + \bar{x}^2) \\ &= \frac1{n-1} \sum x_i^2 - 2 \bar{x} \frac1{n-1} \sum x_i + \frac1{n-1} \sum \bar{x}^2 \\ &= \frac1{n-1} \sum x_i^2 - 2 \bar{x} \frac{n}{n-1} \bar{x} + \frac1{n-1} n \bar{x}^2 \\ &= \frac1{n-1} \sum x_i^2 - 2 \frac{n}{n-1} \bar{x}^2 + \frac{n}{n-1}\bar{x}^2 \\ &= \frac1{n-1} \sum x_i^2 - \frac{n}{n-1}\bar{x}^2 \end{aligned}

From a Frequency Table

If the data is given in a frequency table, where data point x ix_i occurs with frequency f if_i, the formulae are:

σ x 2 =1n(x ix¯) 2f i =1nx i 2f ix¯ 2 s 2 =1n1(x ix¯) 2f i =1n1x i 2f inn1x¯ 2 \begin{aligned} \sigma^2_x &= \frac1n \sum (x_i - \bar{x})^2 f_i \\ &= \frac1n \sum x_i^2 f_i - \bar{x}^2 \\ s^2 &= \frac1{n-1} \sum (x_i - \bar{x})^2 f_i \\ &= \frac1{n-1} \sum x_i^2 f_i - \frac{n}{n-1} \bar{x}^2 \end{aligned}

From a Grouped Frequency Table

If the data has been put into a grouped frequency table?, an estimate for the variance can be calculated using the midpoints of the classes.

Spreadsheet

In a spreadsheet, the formula to calculate the variance of a range is:

=varp(<range>)
=var(<range>)

The p suffix is for the population version.