Algorithm Search⌘K

Definitionintro

Sample variance

Also called Bessel's correction, unbiased sample variance.

The sum of squared deviations from the sample mean divided by n - 1. Dividing by n - 1 instead of n makes it unbiased for the population variance.

The sample variance of numbers x1,…,xnx_1, \ldots, x_n is the sum of their squared deviations from the sample mean, divided by n−1n - 1. As an estimator of the population variance σ2\sigma^2 it is unbiased whenever σ2\sigma^2 is finite, while the same sum divided by nn is biased downward by σ2/n\sigma^2/n. The factor n/(n−1)n/(n-1) that turns the second into the first is Bessel's correction. This page is the definition and its exact properties; Welford's algorithm and the Chan-Golub-LeVeque algorithm are ways to compute it.

Definition

Let X1,…,XnX_1, \ldots, X_n be the sample, n≥2n \ge 2, with sample mean Xˉ=1n∑i=1nXi\bar X = \frac1n \sum_{i=1}^n X_i. The sample variance is

S2=1n−1∑i=1n(Xi−Xˉ)2,S^2 = \frac{1}{n - 1} \sum_{i=1}^{n} (X_i - \bar X)^2,

and the sample standard deviation is S=S2S = \sqrt{S^2}. The version with divisor nn is written here as

σ^2=1n∑i=1n(Xi−Xˉ)2=n−1nS2.\hat\sigma^2 = \frac{1}{n} \sum_{i=1}^{n} (X_i - \bar X)^2 = \frac{n - 1}{n} S^2.

It is the maximum likelihood estimator of σ2\sigma^2 for normal data. Both are defined on the observed values xix_i by the same formulas. For n=1n = 1, S2S^2 is not defined, and σ^2=0\hat\sigma^2 = 0.

Equal forms. Write SS=∑i(xi−xˉ)2\mathrm{SS} = \sum_i (x_i - \bar x)^2. For all real x1,…,xnx_1, \ldots, x_n:

  1. SS=∑ixi2−nxˉ2\mathrm{SS} = \sum_i x_i^2 - n\bar x^2. Expand (xi−xˉ)2(x_i - \bar x)^2 and use ∑ixi=nxˉ\sum_i x_i = n\bar x.
  2. SS=∑i(xi−c)2−n(xˉ−c)2\mathrm{SS} = \sum_i (x_i - c)^2 - n(\bar x - c)^2 for every real cc. This is form 1 applied to xi−cx_i - c, whose mean is xˉ−c\bar x - c and whose SS\mathrm{SS} is the same. So S2S^2 does not change when every value is shifted by cc.
  3. SS=1n∑i<j(xi−xj)2\mathrm{SS} = \frac{1}{n} \sum_{i<j} (x_i - x_j)^2, so S2=1n(n−1)∑i<j(xi−xj)2S^2 = \frac{1}{n(n-1)} \sum_{i<j} (x_i - x_j)^2. Proof. ∑i=1n∑j=1n(xi−xj)2=2n∑ixi2−2(∑ixi)2=2n SS\sum_{i=1}^n \sum_{j=1}^n (x_i - x_j)^2 = 2n\sum_i x_i^2 - 2(\sum_i x_i)^2 = 2n\,\mathrm{SS} by form 1, and the double sum counts each pair i<ji < j twice. ■\blacksquare Form 3 shows that S2S^2 is the U-statistic with kernel (x−y)2/2(x - y)^2/2.

The forms are equal in exact arithmetic only. In floating point, form 1 subtracts two large, nearly equal numbers when ∣xˉ∣\lvert \bar x \rvert is large compared with the spread, and it can lose every digit (catastrophic cancellation). Form 2 with cc near xˉ\bar x is the cure used by the shifted and two-pass methods.

Unbiasedness. Let X1,…,XnX_1, \ldots, X_n be uncorrelated with common mean μ\mu and common variance σ2<∞\sigma^2 < \infty. Then E[S2]=σ2\mathbb{E}[S^2] = \sigma^2 and E[σ^2]=n−1nσ2\mathbb{E}[\hat\sigma^2] = \frac{n-1}{n}\sigma^2.

Proof. Form 2 with c=μc = \mu gives SS=∑i(Xi−μ)2−n(Xˉ−μ)2\mathrm{SS} = \sum_i (X_i - \mu)^2 - n(\bar X - \mu)^2. Take expectations: E[(Xi−μ)2]=σ2\mathbb{E}[(X_i - \mu)^2] = \sigma^2 for each ii, and E[(Xˉ−μ)2]=Var⁡(Xˉ)=σ2/n\mathbb{E}[(\bar X - \mu)^2] = \operatorname{Var}(\bar X) = \sigma^2/n for uncorrelated data (see standard error). So E[SS]=nσ2−σ2=(n−1)σ2\mathbb{E}[\mathrm{SS}] = n\sigma^2 - \sigma^2 = (n - 1)\sigma^2. Divide by n−1n - 1 and by nn. ■\blacksquare

The deviations Xi−XˉX_i - \bar X sum to zero, so only n−1n - 1 of them are free; the divisor n−1n - 1 is the number of these degrees of freedom. Measuring the spread around Xˉ\bar X, which is fitted to the same data, gives a smaller sum than measuring it around the true μ\mu, and the loss is exactly one σ2\sigma^2 on average.

Further properties.

  • Normal data. For iid N(μ,σ2)N(\mu, \sigma^2) data, (n−1)S2/σ2(n - 1)S^2/\sigma^2 has the chi-square law with n−1n - 1 degrees of freedom and is independent of Xˉ\bar X (see sampling distribution). So Var⁡(S2)=2σ4/(n−1)\operatorname{Var}(S^2) = 2\sigma^4/(n - 1), the variance of that chi-square law times σ4/(n−1)2\sigma^4/(n-1)^2.
  • The divisor and the mean squared error. Unbiased does not mean best in squared error. For normal data the divisor n+1n + 1 gives the smallest mean squared error among the divisors; that page has the computation.
  • SS is biased for σ\sigma. E[S]<σ\mathbb{E}[S] < \sigma unless SS is constant; see bias of an estimator for the exact factor under normality.
  • Consistency. For iid data with σ2<∞\sigma^2 < \infty, S2→σ2S^2 \to \sigma^2 in probability; the proof is on consistent estimator. The same argument with the strong law of large numbers gives almost sure convergence.

Examples and non-examples

A sample of five. For x=(3,7,7,9,14)x = (3, 7, 7, 9, 14), xˉ=40/5=8\bar x = 40/5 = 8.

xix_i 3 7 7 9 14 sum
xi−xˉx_i - \bar x −5-5 −1-1 −1-1 1 6 0
(xi−xˉ)2(x_i - \bar x)^2 25 1 1 1 36 64

So S2=64/4=16S^2 = 64/4 = 16, S=4S = 4 and σ^2=64/5=12.8\hat\sigma^2 = 64/5 = 12.8. Form 1 gives ∑ixi2−nxˉ2=384−320=64\sum_i x_i^2 - n \bar x^2 = 384 - 320 = 64. The 10 pairwise squared differences sum to 320=5⋅64320 = 5 \cdot 64, as form 3 states.

A proportion. For 0 and 1 data with sample proportion p^\hat p, ∑ixi2=np^\sum_i x_i^2 = n\hat p, so σ^2=p^(1−p^)\hat\sigma^2 = \hat p(1 - \hat p) and S2=nn−1p^(1−p^)S^2 = \frac{n}{n-1}\hat p(1 - \hat p).

A finite population. For a whole finite population y1,…,yNy_1, \ldots, y_N there is nothing to estimate: its variance has divisor NN. Survey sampling texts still define SU2=1N−1∑i(yi−yˉU)2S_U^2 = \frac{1}{N-1}\sum_i (y_i - \bar y_U)^2, because with this divisor the sample variance S2S^2 of a simple random sample drawn without replacement is unbiased for SU2S_U^2 (Cochran, Chapter 2). Say which divisor a formula uses.

Frequency weights. If value xix_i occurs wiw_i times (integer wiw_i), the sample variance of the expanded data is 1W−1∑iwi(xi−xˉw)2\frac{1}{W - 1}\sum_i w_i (x_i - \bar x_w)^2 with W=∑iwiW = \sum_i w_i and xˉw=1W∑iwixi\bar x_w = \frac1W \sum_i w_i x_i. Weights that express precision instead of counts need a different correction.

Non-example: dividing by n−1n - 1 around the true mean. If μ\mu is known, 1n∑i(Xi−μ)2\frac1n \sum_i (X_i - \mu)^2 is unbiased. Dividing it by n−1n - 1 would bias it upward. The correction belongs to deviations from Xˉ\bar X only.

Non-example: the mean absolute deviation. 1n∑i∣xi−xˉ∣\frac1n\sum_i \lvert x_i - \bar x \rvert measures spread too, but it is not a variance, and it is not unbiased for σ\sigma or for σ2\sigma^2.

  • Variance: the population quantity σ2=E[(X−μ)2]\sigma^2 = \mathbb{E}[(X - \mu)^2] that S2S^2 estimates.
  • Standard error: S/nS/\sqrt n estimates the standard error of Xˉ\bar X.
  • Welford's algorithm: computes xˉ\bar x and SS\mathrm{SS} in one pass and in constant memory, without the cancellation of form 1.
  • Chan-Golub-LeVeque algorithm: combines the (n,xˉ,SS)(n, \bar x, \mathrm{SS}) of two parts into those of their union, for parallel and pairwise computation.
  • Covariance matrix: the sample covariance uses the same divisor n−1n - 1 and the same forms.
  • U-statistic: form 3 makes S2S^2 the simplest nontrivial example.

References

  1. George Casella and Roger L. Berger, Statistical Inference, second edition, Duxbury, 2002, Section 5.2 (the sample mean and the sample variance, and E S^2 = sigma^2) and Section 5.3.1 (the chi-square law of (n - 1) S^2 / sigma^2 for normal data)
  2. William G. Cochran, Sampling Techniques, third edition, Wiley, 1977, Chapter 2 (simple random sampling, the finite population variance with divisor N - 1)
  3. Nicholas J. Higham, Accuracy and Stability of Numerical Algorithms, second edition, SIAM, 2002, Section 1.9 (computing the sample variance, the one-pass and two-pass formulas)

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/DWN5JT/text.yaml