Skip to content
BIASED

Estimators: bias and variance

Statistics and estimators, bias, variance and MSE, why the sample variance divides by n − 1, shrinkage that beats an unbiased estimator, consistency, and the bias–variance split of a model's error.

From data to guesses

The topic The central limit theorem closed the limit theorems. For i.i.d. X1,…,XnX_1, \dots, X_n with mean μ\mu and variance σ2\sigma^2, the sample mean is approximately normal:

Xˉn≈N ⁣(μ,σ2n).\bar X_n \approx \mathcal N\!\left(\mu, \frac{\sigma^2}{n}\right).

There we knew μ\mu and σ\sigma and asked how the average behaves. Statistics turns the question around. We see the data and do not know the parameter. A classifier made 40 errors on 400 test examples: what is its true error rate pp? A coin landed heads 7 times in 10: what is its bias?

A statistic is any function of the sample that can be computed from the data alone: Xˉn\bar X_n, max⁡iXi\max_i X_i, the number of errors. An estimator of a parameter θ\theta is a statistic

θ^=θ^(X1,…,Xn)\hat\theta = \hat\theta(X_1, \dots, X_n)

that we use as a guess for θ\theta. Its value on the data at hand, a number such as 0.10.1, is the estimate.

Here is the key shift. Before the data are drawn, θ^\hat\theta is a random variable. A fresh test set gives a different error count and a different p^\hat p. The distribution of θ^\hat\theta over repeated samples is its sampling distribution. Every statement about the quality of an estimator is a statement about that distribution, and the CLT above is one such statement about Xˉn\bar X_n.

One parameter can have many estimators. If Xi∼U(0,θ)X_i \sim U(0, \theta), then 2Xˉn2\bar X_n is a sensible guess for θ\theta, since E[Xi]=θ/2\mathbb E[X_i] = \theta/2, and so is the largest observation max⁡iXi\max_i X_i. We need a way to compare them.

Bias and variance

Two numbers describe how an estimator misses. The bias is the systematic error, how far off the estimator is on average:

bias⁡(θ^)=E[θ^]−θ.\operatorname{bias}(\hat\theta) = \mathbb E[\hat\theta] - \theta.

An estimator whose bias is zero for every value of θ\theta is unbiased. The variance Var⁡(θ^)=E[(θ^−E[θ^])2]\operatorname{Var}(\hat\theta) = \mathbb E\big[(\hat\theta - \mathbb E[\hat\theta])^2\big] is the random spread around its own mean. Its square root is the standard error se⁡(θ^)\operatorname{se}(\hat\theta), which the topic The central limit theorem already met for the sample mean. In both, the true θ\theta is held fixed and the expectation runs over the sampling distribution.

Think of darts. The bias is how far the centre of the cloud of throws sits from the bull's-eye. The variance is how wide the cloud is.

Back to the classifier. Let Xi=1X_i = 1 if it errs on the ii-th test example, so Xi∼Bern⁡(p)X_i \sim \operatorname{Bern}(p) independently, and estimate pp by the fraction of errors p^=Xˉn\hat p = \bar X_n. By the topics Expectation and variance and Joint distributions and independence,

E[p^]=p,Var⁡(p^)=p(1−p)n.\mathbb E[\hat p] = p, \qquad \operatorname{Var}(\hat p) = \frac{p(1-p)}{n}.

So p^\hat p is unbiased, and its standard error p(1−p)/n\sqrt{p(1-p)/n} shrinks like 1/n1/\sqrt n: four times more test data halve it. The same holds for any sample mean: Xˉn\bar X_n is unbiased for μ\mu with se⁡(Xˉn)=σ/n\operatorname{se}(\bar X_n) = \sigma/\sqrt n. In practice σ\sigma is unknown and gets replaced by an estimate; estimating it well is the subject of a block below.

Mean squared error

To compare estimators we want one number. The mean squared error is the average squared distance to the truth:

MSE⁡(θ^)=E[(θ^−θ)2].\operatorname{MSE}(\hat\theta) = \mathbb E\big[(\hat\theta - \theta)^2\big].

It splits into the two quantities we already have. Add and subtract E[θ^]\mathbb E[\hat\theta]:

θ^−θ=(θ^−E[θ^])+(E[θ^]−θ).\hat\theta - \theta = \big(\hat\theta - \mathbb E[\hat\theta]\big) + \big(\mathbb E[\hat\theta] - \theta\big).

Square and take the expectation. The first part gives the variance, the second gives the squared bias, and the cross term drops out (the check below asks why):

MSE⁡(θ^)=bias⁡(θ^)2+Var⁡(θ^).\operatorname{MSE}(\hat\theta) = \operatorname{bias}(\hat\theta)^2 + \operatorname{Var}(\hat\theta).

Now compare three estimators of θ\theta for Xi∼U(0,θ)X_i \sim U(0, \theta).

  • 2Xˉn2\bar X_n is unbiased, with variance 4⋅θ212n=θ23n4 \cdot \frac{\theta^2}{12n} = \frac{\theta^2}{3n}.
  • The maximum M=max⁡iXiM = \max_i X_i never exceeds θ\theta, so it is biased down. From its CDF P(M≤x)=(x/θ)nP(M \le x) = (x/\theta)^n one gets E[M]=nn+1θ\mathbb E[M] = \frac{n}{n+1}\theta, a bias of −θn+1-\frac{\theta}{n+1}, and variance nθ2(n+1)2(n+2)\frac{n\theta^2}{(n+1)^2(n+2)}.
  • The rescaled maximum n+1nM\frac{n+1}{n} M removes that bias. Its variance is θ2n(n+2)\frac{\theta^2}{n(n+2)}.

For n=10n = 10 and θ=1\theta = 1:

Estimator Bias Variance MSE
2Xˉn2\bar X_n 00 0.03330.0333 0.03330.0333
MM −0.0909-0.0909 0.00690.0069 0.01520.0152
n+1nM\frac{n+1}{n} M 00 0.00830.0083 0.00830.0083

The biased MM beats the unbiased 2Xˉn2\bar X_n by more than a factor of two. Its bias costs 0.00830.0083 in squared error, but its variance is five times smaller. Unbiasedness is a property, not a goal; what we pay for is the total error.

Why n − 1

Now estimate the variance σ2\sigma^2 itself. The natural guess is the average squared deviation, with the unknown μ\mu replaced by Xˉn\bar X_n:

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

It is biased. The sample mean sits in the middle of the data: it is the number cc that minimizes ∑i(Xi−c)2\sum_i (X_i - c)^2. So deviations from Xˉn\bar X_n are, on average, smaller than deviations from the true μ\mu.

By how much? Write each deviation as Xi−Xˉn=(Xi−μ)−(Xˉn−μ)X_i - \bar X_n = (X_i - \mu) - (\bar X_n - \mu). Expanding the squares and summing over ii gives the identity

∑i=1n(Xi−Xˉn)2=∑i=1n(Xi−μ)2−n(Xˉn−μ)2.\sum_{i=1}^n (X_i - \bar X_n)^2 = \sum_{i=1}^n (X_i - \mu)^2 - n(\bar X_n - \mu)^2.

Each (Xi−μ)2(X_i - \mu)^2 has mean σ2\sigma^2, and E[(Xˉn−μ)2]=Var⁡(Xˉn)=σ2/n\mathbb E[(\bar X_n - \mu)^2] = \operatorname{Var}(\bar X_n) = \sigma^2/n. Hence

E[∑i=1n(Xi−Xˉn)2]=nσ2−σ2=(n−1)σ2,E[σ^2]=n−1n σ2.\mathbb E\Big[\sum_{i=1}^n (X_i - \bar X_n)^2\Big] = n\sigma^2 - \sigma^2 = (n-1)\sigma^2, \qquad \mathbb E[\hat\sigma^2] = \frac{n-1}{n}\,\sigma^2.

Dividing by n−1n - 1 instead of nn removes the bias. This is Bessel's correction:

S2=1n−1∑i=1n(Xi−Xˉn)2,E[S2]=σ2.S^2 = \frac{1}{n-1} \sum_{i=1}^n (X_i - \bar X_n)^2, \qquad \mathbb E[S^2] = \sigma^2.

One way to remember it: the nn deviations Xi−XˉnX_i - \bar X_n always sum to zero, so only n−1n - 1 of them are free. With a single observation σ^2=0\hat\sigma^2 = 0 whatever the data. Libraries disagree on the default: numpy.var divides by nn, pandas.Series.var by n−1n - 1. For large nn the difference hardly matters; for n=5n = 5 it is 20%20\%.

Shrinkage: bias that pays

The uniform example showed a biased estimator winning by accident. It can also be done on purpose. Take an unbiased estimator and pull it toward a fixed point. This is shrinkage: the bias grows, the variance falls, and when the variance falls faster, the MSE drops.

Take a coin with unknown bias pp and kk heads in nn flips. The unbiased estimate k/nk/n has MSE p(1−p)/np(1-p)/n. Now add one imaginary head and one imaginary tail:

p~=k+1n+2.\tilde p = \frac{k + 1}{n + 2}.

This pulls the estimate toward 1/21/2. Its bias is 1−2pn+2\frac{1 - 2p}{n+2} and its variance np(1−p)(n+2)2\frac{np(1-p)}{(n+2)^2}. For n=10n = 10 and a fair coin the bias is zero, and the MSE is 10⋅0.25/144≈0.017410 \cdot 0.25/144 \approx 0.0174 against 0.0250.025 for k/nk/n: about 30%30\% less. Near p=0p = 0 or p=1p = 1, though, the bias catches up. Whether this shrinkage helps depends on the unknown pp. That is the general catch: bias and MSE are functions of θ\theta, so "better" has to say for which θ\theta.

Sometimes the win holds for every θ\theta.

  • Sample variance. For normal data, divide ∑i(Xi−Xˉn)2\sum_i (X_i - \bar X_n)^2 by n+1n + 1 instead of n−1n - 1. This shrinks the estimate toward zero. For n=10n = 10 the MSE is 0.222 σ40.222\,\sigma^4 with n−1n - 1, 0.190 σ40.190\,\sigma^4 with nn and 0.182 σ40.182\,\sigma^4 with n+1n + 1, at every σ\sigma.
  • James–Stein. Estimate d≥3d \ge 3 normal means at once. Shrinking the vector of sample means toward a common point lowers the total MSE for every true vector of means (Stein, 1956; James and Stein, 1961). The obvious unbiased estimator can be beaten everywhere.

The "+1" trick is Laplace smoothing of counts in naive Bayes and n-gram language models. In the topic Bayesian inference it returns as a prior.

Consistency

MSE judges an estimator at a fixed nn. Consistency asks what happens as the data grow. An estimator θ^n\hat\theta_n is consistent if it converges in probability to θ\theta: for every ε>0\varepsilon > 0,

P(∣θ^n−θ∣>ε)→0as n→∞.P\big(|\hat\theta_n - \theta| > \varepsilon\big) \to 0 \quad\text{as } n \to \infty.

A handy sufficient condition is MSE⁡(θ^n)→0\operatorname{MSE}(\hat\theta_n) \to 0, that is, both the bias and the variance vanish. Markov's inequality from the topic Inequalities and concentration, applied to (θ^n−θ)2(\hat\theta_n - \theta)^2, gives

P(∣θ^n−θ∣>ε)≤MSE⁡(θ^n)ε2.P\big(|\hat\theta_n - \theta| > \varepsilon\big) \le \frac{\operatorname{MSE}(\hat\theta_n)}{\varepsilon^2}.

Unbiasedness and consistency are different properties.

  • Xˉn\bar X_n is both: unbiased, and consistent by the law of large numbers.
  • The maximum MM of a uniform sample is biased at every nn, yet consistent: its bias −θ/(n+1)-\theta/(n+1) and its variance both go to zero.
  • X1X_1 alone is unbiased for μ\mu but not consistent. It throws away the other n−1n - 1 observations, so its spread never shrinks.

Consistency is the least we ask of an estimator: with enough data, it should find the truth.

Bias and variance of a model

The same split runs through machine learning. A model f^\hat f trained on a random dataset is an estimator of the regression function m(x)=E[Y∣X=x]m(x) = \mathbb E[Y \mid X = x] from the topic Conditional expectation. Take a new point xx whose label YY is independent of the training set. Averaging over both the training set and YY, the expected squared error splits into three terms:

E[(Y−f^(x))2]=Var⁡(Y∣X=x)+(E[f^(x)]−m(x))2+Var⁡(f^(x)).\mathbb E\big[(Y - \hat f(x))^2\big] = \operatorname{Var}(Y \mid X = x) + \big(\mathbb E[\hat f(x)] - m(x)\big)^2 + \operatorname{Var}\big(\hat f(x)\big).

The first term is the noise in the labels; no model removes it. The second is the squared bias of the model, the third its variance.

An experiment: 12 equally spaced points on [0,1][0, 1], labels sin⁡(2πx)\sin(2\pi x) plus normal noise with σ=0.3\sigma = 0.3, polynomials of three degrees fitted by least squares, repeated over 2,000 datasets and averaged over xx.

Degree Bias² Variance Noise Total
1 0.220.22 0.0140.014 0.090.09 0.320.32
3 0.0060.006 0.0250.025 0.090.09 0.120.12
9 0.0000.000 0.120.12 0.090.09 0.210.21

The line cannot bend, so it is biased. The degree-9 polynomial chases the noise of each dataset, so it varies. Degree 3 balances the two. Regularization (weight decay, ridge regression, early stopping) is shrinkage of the weights toward zero: it accepts a little bias for much less variance. Very large networks can break this U shape (double descent), but the decomposition itself always holds.

Every estimator in this topic was invented by hand: 2Xˉn2\bar X_n, the maximum, the "+1" counts, the divisor n−1n - 1. We need a general recipe that builds an estimator for any model, one that is consistent and hard to beat once nn is large. That recipe is the topic Maximum likelihood. It will give back k/nk/n and Xˉn\bar X_n, and for the variance of normal data it will divide by nn, not n−1n - 1.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.