Section 11.14

Variance and standard deviation

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 NN values the average divides by NN. The usual estimate from a sample of nn values measures distances from the sample mean §11.12 and divides by n−1n - 1.

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 −2-2, 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 nn underestimates the population variance. Dividing by n−1n - 1 corrects this exactly; the change is called Bessel’s correction.

How it works

Let the values be x1,x2,…,xnx_1, x_2, \dots, x_n, where xix_i is the ii-th value and nn is how many values there are.

  1. Compute the mean x‾\bar x (read x-bar): add the values and divide by nn.

  2. Compute each deviation xi−x‾x_i - \bar x. The deviations always sum to 0.

  3. Square each deviation.

  4. Add the squares. The total is called the sum of squares.

  5. 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 σ2\sigma^2 (read sigma squared). If the values are a sample used to estimate the variance of a larger population or process, divide by n−1n - 1; the result is the sample variance, written s2s^2.

  6. Take the square root to get the standard deviation: σ\sigma for the population, ss for the sample.

The number n−1n - 1 is called the degrees of freedom of the sum of squares. The nn deviations from x‾\bar x must sum to 0, so any n−1n - 1 of them fix the last one: only n−1n - 1 of them are free to vary. That count explains the name. The proof that n−1n - 1 is the right divisor is the calculation under “Stated precisely”.

Worked example

Take timer B’s values 2, 4 and 6 milliseconds, so n=3n = 3. The mean is (2+4+6)/3=12/3=4(2 + 4 + 6) / 3 = 12 / 3 = 4 milliseconds. The deviations are 2−4=−22 - 4 = -2, 4−4=04 - 4 = 0 and 6−4=26 - 4 = 2 milliseconds, and they sum to 0. Their squares are 4, 0 and 4 square milliseconds, so the sum of squares is 4+0+4=84 + 0 + 4 = 8 square milliseconds.

For timer A every deviation is 0, so both versions give 0.

A small exact case shows why n−1n - 1 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 σ2=((1−2)2+(3−2)2)/2=(1+1)/2=1\sigma^2 = ((1 - 2)^2 + (3 - 2)^2) / 2 = (1 + 1) / 2 = 1 square millisecond. Take samples of n=2n = 2 independent values. The four possible samples (1, 1), (1, 3), (3, 1) and (3, 3) are equally likely, each with probability 1/2×1/2=1/41/2 \times 1/2 = 1/4. Their sums of squares are 0, 2, 2 and 0; for (1, 3) the mean is 2, the deviations are −1-1 and 1, and 1+1=21 + 1 = 2.

The square roots of the divide-by-n−1n - 1 results are 0, 2≈1.414\sqrt 2 \approx 1.414, 1.414 and 0 milliseconds, whose average is 2×1.414/4≈0.7072 \times 1.414 / 4 \approx 0.707 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 ∑i=1Nai\sum_{i=1}^{N} a_i for the sum a1+a2+…+aNa_1 + a_2 + \dots + a_N. For a finite population of NN values x1,…,xNx_1, \dots, x_N with mean μ=1N∑i=1Nxi\mu = \frac{1}{N} \sum_{i=1}^{N} x_i, the population variance and population standard deviation are σ2=1N∑i=1N(xi−μ)2,σ=σ2.\sigma^2 = \frac{1}{N} \sum_{i=1}^{N} (x_i - \mu)^2, \qquad \sigma = \sqrt{\sigma^2}. For a random variable XX, a quantity whose value is decided by chance, write E[⋅]\mathrm{E}[\,\cdot\,] for the expected value: the average of a quantity over all outcomes, each weighted by its probability. With μ=E[X]\mu = \mathrm{E}[X], the variance of XX is Var(X)=E[(X−μ)2]=E[X2]−μ2\operatorname{Var}(X) = \mathrm{E}[(X - \mu)^2] = \mathrm{E}[X^2] - \mu^2.

For a sample of n≥2n \ge 2 values x1,…,xnx_1, \dots, x_n with sample mean x‾=1n∑i=1nxi\bar x = \frac{1}{n} \sum_{i=1}^{n} x_i, the sample variance and sample standard deviation are s2=1n−1∑i=1n(xi−x‾)2,s=s2.s^2 = \frac{1}{n - 1} \sum_{i=1}^{n} (x_i - \bar x)^2, \qquad s = \sqrt{s^2}.

Guarantee. Let the observations be random variables X1,…,XnX_1, \dots, X_n with sample mean X‾\bar X, and let s2s^2 be computed from them by the formula above. Assume:

  1. every XiX_i has the same mean μ\mu and the same finite variance σ2\sigma^2;

  2. the observations are pairwise uncorrelated, meaning that the covariance §15.44 E[(Xi−μ)(Xj−μ)]\mathrm{E}[(X_i - \mu)(X_j - \mu)] of every pair i≠ji \ne j is 0, so that no two of them tend to move together. Independent observations satisfy this.

Then E[s2]=σ2\mathrm{E}[s^2] = \sigma^2. In words, s2s^2 is an unbiased estimator §11.2 of σ2\sigma^2: its average over all possible samples equals the true value.

Proof. Expanding the square gives an identity that holds for any numbers: ∑i=1n(Xi−X‾)2=∑i=1n(Xi−μ)2−n(X‾−μ)2.\sum_{i=1}^{n} (X_i - \bar X)^2 = \sum_{i=1}^{n} (X_i - \mu)^2 - n (\bar X - \mu)^2. By assumption 1, the first sum on the right has expected value nσ2n \sigma^2. The second term has expected value nVar(X‾)=n×σ2/n=σ2n \operatorname{Var}(\bar X) = n \times \sigma^2 / n = \sigma^2, because by assumption 2 the variance of the mean of nn uncorrelated values is σ2/n\sigma^2 / n. The expected sum of squares is therefore nσ2−σ2=(n−1)σ2n \sigma^2 - \sigma^2 = (n - 1) \sigma^2, and dividing it by n−1n - 1 gives σ2\sigma^2.

The guarantee does not carry over to ss. The square root is a concave function, one whose graph bends downward, so Jensen’s inequality [468] gives E[s]≤E[s2]=σ\mathrm{E}[s] \le \sqrt{\mathrm{E}[s^2]} = \sigma, with equality only when ss 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 nn independent observations is estimated by s/ns / \sqrt n. 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 nn 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 X‾\bar X exceeds σ2/n\sigma^2 / n, so the term n(X‾−μ)2n (\bar X - \mu)^2 subtracted in the proof is larger on average, and even s2s^2 underestimates σ2\sigma^2 on average.

Unbiased does not mean most accurate. For data from the bell-shaped normal distribution, dividing by nn gives a smaller mean squared error, the average squared distance from the true value, at the cost of a small systematic underestimate. For large nn the choice hardly matters: for n=100n = 100 the two results differ by the factor 100/99100 / 99, about 1%.

Software differs in its default divisor. In Python §12.23, statistics.variance divides by n−1n - 1 and statistics.pvariance by nn; the NumPy §12.29 library’s numpy.var divides by nn 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 10910^{9} 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 NN. The usual sample estimate, the sample variance s2s^2, uses only the nn sampled values, measures their distances from the sample mean x‾\bar x because the true mean is unknown, and divides the sum of squared distances by n−1n - 1 instead of nn.

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 n−1n - 1 (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 s2s^2 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 8/3≈2.6678 / 3 \approx 2.667; as a sample the estimate is 8/2=48 / 2 = 4. The square root ss 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].