Definition
Variance and standard deviation are measures of spread: they say how far values typically lie from their mean. Variance is the average of the squared distances of the values from their mean, so its unit is the square of the data’s unit. Standard deviation is the square root of the variance, which returns the measure to the data’s unit. For a whole population of values the average divides by . The usual estimate from a sample of values measures distances from the sample mean §11.12 and divides by .
The idea
Two timers each measure the same delay three times. Timer A reports 4, 4 and 4 milliseconds; timer B reports 2, 4 and 6 milliseconds. Both have mean 4 milliseconds, yet B is clearly less consistent. A number for that inconsistency should start from the deviations, meaning each value minus the mean. Their plain average is useless: deviations above and below the mean always cancel. The deviations of B, which are , 0 and 2 milliseconds, sum to 0, exactly as those of A do.
Squaring each deviation makes it nonnegative, and averaging the squares gives the variance. Squaring, rather than taking absolute values, has two merits. The variance of a sum of independent quantities, quantities that do not influence one another, is the sum of their variances, which makes the uncertainty of averages easy to calculate. And the mean is exactly the value that makes the sum of squared deviations smallest. The price is a squared unit, square milliseconds here, which nobody can picture; the square root repairs that and gives the standard deviation.
A second question arises when the values are a sample from a larger population, or from a process that keeps producing values, and the goal is the variance of that population. The true mean is unknown, so the deviations are measured from the sample mean, which is computed from these same values and makes their sum of squared deviations smallest. The squared deviations from it are therefore smaller, on average, than those from the true mean, and dividing their total by underestimates the population variance. Dividing by corrects this exactly; the change is called Bessel’s correction.
How it works
Let the values be , where is the -th value and is how many values there are.
Compute the mean (read x-bar): add the values and divide by .
Compute each deviation . The deviations always sum to 0.
Square each deviation.
Add the squares. The total is called the sum of squares.
Divide the sum of squares. If the values are the entire population that the statement is about, divide by the number of values; the result is the population variance, written (read sigma squared). If the values are a sample used to estimate the variance of a larger population or process, divide by ; the result is the sample variance, written .
Take the square root to get the standard deviation: for the population, for the sample.
The number is called the degrees of freedom of the sum of squares. The deviations from must sum to 0, so any of them fix the last one: only of them are free to vary. That count explains the name. The proof that is the right divisor is the calculation under “Stated precisely”.
Worked example
Take timer B’s values 2, 4 and 6 milliseconds, so . The mean is milliseconds. The deviations are , and milliseconds, and they sum to 0. Their squares are 4, 0 and 4 square milliseconds, so the sum of squares is square milliseconds.
If these three values are the whole population of interest, for example the only three runs that will ever be made, the population variance is square milliseconds and the population standard deviation is milliseconds.
If they are a sample from a timer that will keep producing values, the sample variance is square milliseconds and the sample standard deviation is milliseconds.
For timer A every deviation is 0, so both versions give 0.
A small exact case shows why is the right divisor. A process produces 1 or 3 milliseconds, each with probability 1/2. Its true mean is 2 milliseconds and its true variance is square millisecond. Take samples of independent values. The four possible samples (1, 1), (1, 3), (3, 1) and (3, 3) are equally likely, each with probability . Their sums of squares are 0, 2, 2 and 0; for (1, 3) the mean is 2, the deviations are and 1, and .
Dividing by gives 0, 1, 1 and 0, whose average over the four samples is : half the true variance.
Dividing by gives 0, 2, 2 and 0, whose average is : exactly the true variance.
The square roots of the divide-by- results are 0, , 1.414 and 0 milliseconds, whose average is milliseconds, below the true standard deviation of 1 millisecond. Bessel’s correction makes the variance right on average, not the standard deviation.
Stated precisely
Write for the sum . For a finite population of values with mean , the population variance and population standard deviation are For a random variable , a quantity whose value is decided by chance, write for the expected value: the average of a quantity over all outcomes, each weighted by its probability. With , the variance of is .
For a sample of values with sample mean , the sample variance and sample standard deviation are
Guarantee. Let the observations be random variables with sample mean , and let be computed from them by the formula above. Assume:
every has the same mean and the same finite variance ;
the observations are pairwise uncorrelated, meaning that the covariance §15.44 of every pair is 0, so that no two of them tend to move together. Independent observations satisfy this.
Then . In words, is an unbiased estimator §11.2 of : its average over all possible samples equals the true value.
Proof. Expanding the square gives an identity that holds for any numbers: By assumption 1, the first sum on the right has expected value . The second term has expected value , because by assumption 2 the variance of the mean of uncorrelated values is . The expected sum of squares is therefore , and dividing it by gives .
The guarantee does not carry over to . The square root is a concave function, one whose graph bends downward, so Jensen’s inequality [468] gives , with equality only when is the same for every possible sample. The worked example shows the gap: 0.707 against 1.
Limits and common mistakes
The standard deviation describes how much single observations vary. It does not describe how precisely their mean is known; that is the standard error §11.15, which for independent observations is estimated by . An error bar §18.1.11 must say which of the two it shows.
Squaring gives large deviations great weight: one value far from the rest can dominate the variance. Some heavy-tailed distributions, in which extreme values are relatively common, have no finite variance at all, and then the sample variance never settles as grows. The median §11.13 absolute deviation, the median distance of the values from their median, is a measure of spread that resists extreme values.
Dependent observations break the guarantee. If the observations are positively correlated, for example consecutive measurements that share a slowly changing background load, the variance of exceeds , so the term subtracted in the proof is larger on average, and even underestimates on average.
Unbiased does not mean most accurate. For data from the bell-shaped normal distribution, dividing by gives a smaller mean squared error, the average squared distance from the true value, at the cost of a small systematic underestimate. For large the choice hardly matters: for the two results differ by the factor , about 1%.
Software differs in its default divisor. In Python §12.23, statistics.variance divides by and statistics.pvariance by ; the NumPy §12.29 library’s numpy.var divides by unless called with ddof=1.
Computing the variance as the mean of the squares minus the square of the mean is numerically fragile. For values that are large compared with their spread, such as timestamps near that differ by a few units, floating-point rounding can make that result wrong or even negative. Subtract the mean first, as in the steps above, or use Welford’s one-pass algorithm §14.1.
Questions answered
What is the difference between population variance and its usual sample estimate?
Population variance is the average squared distance of the values in a whole population from the true population mean: the sum of the squared distances divided by the population size . The usual sample estimate, the sample variance , uses only the sampled values, measures their distances from the sample mean because the true mean is unknown, and divides the sum of squared distances by instead of .
The divisor differs because the sample mean is computed from the same values and lies closer to them than the true mean does, so the squared distances from it are too small on average. Dividing by (Bessel’s correction) compensates exactly when the sampled values share one mean and one finite variance and no two of them are correlated, as for independent and identically distributed §11.5 (IID) observations: the average of over all possible samples then equals the population variance. For the values 2, 4 and 6, the sum of squared distances from their mean 4 is 8. As a whole population their variance is ; as a sample the estimate is . The square root is still slightly too small on average.
Related entries: Independent and identically distributed samples (§11.5), Standard error (§11.15), Measurement uncertainty (§11.33), Sample mean (§11.12). Further reading: [468].