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. with mean and variance , the sample mean is approximately normal:
There we knew and 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 ? 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: , , the number of errors. An estimator of a parameter is a statistic
that we use as a guess for . Its value on the data at hand, a number such as , is the estimate.
Here is the key shift. Before the data are drawn, is a random variable. A fresh test set gives a different error count and a different . The distribution of 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 .
One parameter can have many estimators. If , then is a sensible guess for , since , and so is the largest observation . 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:
An estimator whose bias is zero for every value of is unbiased. The variance is the random spread around its own mean. Its square root is the standard error , which the topic The central limit theorem already met for the sample mean. In both, the true 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 if it errs on the -th test example, so independently, and estimate by the fraction of errors . By the topics Expectation and variance and Joint distributions and independence,
So is unbiased, and its standard error shrinks like : four times more test data halve it. The same holds for any sample mean: is unbiased for with . In practice 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:
It splits into the two quantities we already have. Add and subtract :
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):
Now compare three estimators of for .
- is unbiased, with variance .
- The maximum never exceeds , so it is biased down. From its CDF one gets , a bias of , and variance .
- The rescaled maximum removes that bias. Its variance is .
For and :
| Estimator | Bias | Variance | MSE |
|---|---|---|---|
The biased beats the unbiased by more than a factor of two. Its bias costs 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 itself. The natural guess is the average squared deviation, with the unknown replaced by :
It is biased. The sample mean sits in the middle of the data: it is the number that minimizes . So deviations from are, on average, smaller than deviations from the true .
By how much? Write each deviation as . Expanding the squares and summing over gives the identity
Each has mean , and . Hence
Dividing by instead of removes the bias. This is Bessel's correction:
One way to remember it: the deviations always sum to zero, so only of them are free. With a single observation whatever the data. Libraries disagree on the default: numpy.var divides by , pandas.Series.var by . For large the difference hardly matters; for it is .
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 and heads in flips. The unbiased estimate has MSE . Now add one imaginary head and one imaginary tail:
This pulls the estimate toward . Its bias is and its variance . For and a fair coin the bias is zero, and the MSE is against for : about less. Near or , though, the bias catches up. Whether this shrinkage helps depends on the unknown . That is the general catch: bias and MSE are functions of , so "better" has to say for which .
Sometimes the win holds for every .
- Sample variance. For normal data, divide by instead of . This shrinks the estimate toward zero. For the MSE is with , with and with , at every .
- James–Stein. Estimate 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 . Consistency asks what happens as the data grow. An estimator is consistent if it converges in probability to : for every ,
A handy sufficient condition is , that is, both the bias and the variance vanish. Markov's inequality from the topic Inequalities and concentration, applied to , gives
Unbiasedness and consistency are different properties.
- is both: unbiased, and consistent by the law of large numbers.
- The maximum of a uniform sample is biased at every , yet consistent: its bias and its variance both go to zero.
- alone is unbiased for but not consistent. It throws away the other 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 trained on a random dataset is an estimator of the regression function from the topic Conditional expectation. Take a new point whose label is independent of the training set. Averaging over both the training set and , the expected squared error splits into three terms:
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 , labels plus normal noise with , polynomials of three degrees fitted by least squares, repeated over 2,000 datasets and averaged over .
| Degree | Bias² | Variance | Noise | Total |
|---|---|---|---|---|
| 1 | ||||
| 3 | ||||
| 9 |
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: , the maximum, the "+1" counts, the divisor . We need a general recipe that builds an estimator for any model, one that is consistent and hard to beat once is large. That recipe is the topic Maximum likelihood. It will give back and , and for the variance of normal data it will divide by , not .
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.