Course contents 15 / 22

15 / 22 · From Probability to Statistics

Estimators: bias and variance

Statistics turns probability around: from data back to the parameters that produced it. Estimators as random variables, bias, variance and the mean squared error that combines them, why a biased estimator can be better and why the sample variance divides by n − 1, consistency, standard errors, and the bias–variance trade-off of machine learning models.

On this page 12

Probability goes from a known distribution to the data it produces: given a fair coin, how many heads? Statistics goes the other way: given the heads, what is the coin? The unknown is a parameter θ\theta of a family of distributions, and a rule that turns data into a guess is an estimator θ^\hat\theta. Every number fitted in machine learning, from a mean to the weights of a network, is an estimator. This note introduces the vocabulary that judges them: bias, variance, mean squared error, consistency and the standard error, and ends with the bias–variance trade-off that governs the choice of a model’s complexity.

Notation. X1,…,XnX_1, \dots, X_n are i.i.d. from a distribution PθP_\theta with unknown parameter θ\theta. An estimator θ^=θ^(X1,…,Xn)\hat\theta = \hat\theta(X_1, \dots, X_n) is a function of the data. EθE_\theta and Var⁡θ\operatorname{Var}_\theta are computed under PθP_\theta.

Estimators#

The sample mean Xˉ\bar X estimates the mean, the share of successes estimates a probability, the sample variance estimates the variance, a fitted regression line estimates the regression function of the conditional expectation note. Because the data are random, an estimator is a random variable: a different sample gives a different value. Its distribution over repeated samples, the sampling distribution, is what makes it good or bad. The average of nn observations, for instance, has sampling distribution approximately N(μ,σ2/n)N(\mu, \sigma^2/n) by the central limit theorem.

The same parameter usually has many estimators. For a sample from U(0,θ)U(0, \theta), the end point θ\theta can be estimated by twice the sample mean, since E[X]=θ/2E[X] = \theta/2, or by the sample maximum, the largest value seen, or by the maximum scaled up slightly. They behave very differently.

Bias, variance and mean squared error#

The bias is the systematic error, the variance the random one:

bias⁡(θ^)=Eθ[θ^]−θ,Var⁡(θ^)=Eθ[(θ^−Eθ[θ^])2].\operatorname{bias}(\hat\theta) = E_\theta[\hat\theta] - \theta, \qquad \operatorname{Var}(\hat\theta) = E_\theta\big[(\hat\theta - E_\theta[\hat\theta])^2\big] .

The mean squared error combines them. Adding and subtracting Eθ[θ^]E_\theta[\hat\theta] inside the square, the cross term has mean zero:

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

For the uniform end point with n=10n = 10 and θ=1\theta = 1:

Figure

Three estimators of the end point of a uniform distribution

Histograms of three estimatorsA wide bell around 1, a narrow hump just below 1, a narrow hump around 1.2 × meanbias 0, MSE 0.03330.511.5maximumbias -0.091, MSE 0.01520.511.5maximum × 11/10bias 0, MSE 0.00830.511.5Histograms of three estimatorsA wide bell around 1, a narrow hump just below 1, a narrow hump around 1.2 × meanbias 0, MSE 0.03330.511.5maximumbias -0.091, MSE 0.01520.511.5maximum × 11/10bias 0, MSE 0.00830.511.5
A sample of 10 values from U(0, θ) with θ = 1 (dashed); histograms of 20,000 repetitions of each estimator. Twice the mean is unbiased but spread out (MSE 0.0333). The maximum is always below θ, with bias −1/11, but its spread is small (MSE 0.0152): a biased estimator beats an unbiased one. The maximum times 11/10 is unbiased and better than both (MSE 0.0083).
EstimatorBiasVarianceMSE
2Xˉ2\bar X0θ2/(3n)=0.0333\theta^2/(3n) = 0.03330.0333
max⁡iXi\max_i X_i−θ/(n+1)=−0.091-\theta/(n+1) = -0.0910.00690.00690.0152
n+1nmax⁡iXi\frac{n+1}{n} \max_i X_i0θ2/(n(n+2))=0.0083\theta^2/(n(n+2)) = 0.00830.0083

The numbers for the maximum come from its distribution function: the maximum is at most tt exactly when all nn observations are, so

P(max⁡iXi≤t)=(t/θ)n,0≤t≤θ,E[max⁡iXi]=nn+1 θ,Var⁡(max⁡iXi)=E[(max⁡iXi)2]−(nθn+1)2=nθ2n+2−n2θ2(n+1)2=nθ2(n+1)2(n+2),\begin{aligned} P\big(\max_i X_i \le t\big) &= (t/\theta)^n, \quad 0 \le t \le \theta, \qquad E\big[\max_i X_i\big] = \frac{n}{n+1}\,\theta, \\ \operatorname{Var}\big(\max_i X_i\big) &= E\big[(\max_i X_i)^2\big] - \Big(\frac{n\theta}{n+1}\Big)^2 \\ &= \frac{n\theta^2}{n+2} - \frac{n^2\theta^2}{(n+1)^2} = \frac{n\theta^2}{(n+1)^2(n+2)} , \end{aligned}

with the moments computed from the density ntn−1/θnn t^{n-1}/\theta^n; for n=10n = 10 the variance is 10/(121⋅12)=0.006910/(121 \cdot 12) = 0.0069.

Unbiasedness is not the goal. The maximum is always too small, yet its mean squared error is less than half that of the unbiased 2Xˉ2\bar X, because the maximum uses the one observation that carries the most information about the end point. Correcting its bias keeps that advantage and gives the best of the three; its variance even falls like 1/n21/n^2, faster than the 1/n1/n of any average.

The sample variance and n − 1#

The natural estimator of σ2\sigma^2 averages the squared deviations from the sample mean. It is biased:

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

because the deviations are measured from Xˉ\bar X, which is fitted to the same data and so sits closer to them than the true mean μ\mu does. Dividing by n−1n - 1 instead (Bessel’s correction) removes the bias; this s2s^2 is what most statistical software reports. But for normal data the mean squared error is smallest with yet another divisor, n+1n + 1: for n=10n = 10 the divisors 99, 1010 and 1111 give mean squared errors 0.222σ40.222\sigma^4, 0.190σ40.190\sigma^4 and 0.182σ40.182\sigma^4. Again the unbiased choice is not the most accurate one, though for moderate nn the differences are small.

Consistency#

An estimator is consistent if it converges in probability to the true value as nn grows: θ^n→θ\hat\theta_n \to \theta. A sufficient condition is that both the bias and the variance go to zero, since then the mean squared error does. The sample mean is consistent by the law of large numbers. The maximum of a uniform sample is biased for every nn and still consistent. The first observation alone, θ^=X1\hat\theta = X_1, is unbiased for the mean and not consistent: it ignores all the other data. Consistency is the minimum requirement; bias, variance and their rates are what distinguish consistent estimators.

Standard errors#

The standard deviation of an estimator is its standard error, the typical size of the random part of its error; for an unbiased estimator, the typical deviation from the truth. For the sample mean it is σ/n\sigma/\sqrt n, estimated by s/ns/\sqrt n. Quoting an estimate without its standard error says nothing about its precision; with it, the central limit theorem gives an approximate interval, the subject of the confidence intervals note. For estimators without a simple formula, the standard error can be estimated by resampling the data, the bootstrap of the same note.

The bias–variance trade-off#

A model f^\hat f trained with squared error is an estimator of the regression function m(x)=E[Y∣X=x]m(x) = E[Y \mid X = x] (other losses estimate other summaries of the conditional distribution, as the conditional expectation note shows), and its sampling distribution comes from the randomness of the training set. At a point xx, with a new YY independent of the training data, the expected squared error splits in three:

E[(Y−f^(x))2]=Var⁡(Y∣X=x)⏟noise+(E[f^(x)]−m(x))2⏟bias2+Var⁡(f^(x))⏟variance.E\big[(Y - \hat f(x))^2\big] = \underbrace{\operatorname{Var}(Y \mid X = x)}_{\text{noise}} + \underbrace{\big(E[\hat f(x)] - m(x)\big)^2}_{\text{bias}^2} + \underbrace{\operatorname{Var}\big(\hat f(x)\big)}_{\text{variance}} .

The noise is the irreducible error of the conditional expectation note. A model class too simple for mm has high bias: every training set gives about the same wrong answer. A class flexible enough to follow mm also follows the noise of each training set and has high variance:

Figure

Bias and variance of polynomial regression

Bundles of fits of three degreesLeft, a bundle of nearly identical lines; centre, curves close to the sine; right, widely scattered curves.degree 1: high biasbias² 0.218, variance 0.014degree 3: balancedbias² 0.006, variance 0.026degree 9: high variancebias² 0.000, variance 0.116Bundles of fits of three degreesLeft, a bundle of nearly identical lines; centre, curves close to the sine; right, widely scattered curves.degree 1: high biasbias² 0.218, variance 0.014degree 3: balancedbias² 0.006, variance 0.026degree 9: high variancebias² 0.000, variance 0.116
Each grey curve is a polynomial fitted by least squares to its own set of 12 points y = sin 2πx + noise (σ = 0.3); red is the true function. The straight line (degree 1) is nearly the same for every dataset and equally wrong: high bias, low variance. Degree 9 matches the true function on average, but every fit chases the noise of its own dataset: low bias, high variance. Over 2,000 datasets the squared bias and the variance, averaged over x, are 0.218 and 0.014, 0.006 and 0.026, 0.000 and 0.116; the noise adds 0.09 to each.

With 12 training points, degree 1 has total expected error 0.218+0.014+0.09=0.320.218 + 0.014 + 0.09 = 0.32, degree 3 has 0.006+0.026+0.09=0.120.006 + 0.026 + 0.09 = 0.12, degree 9 has 0.000+0.116+0.09=0.210.000 + 0.116 + 0.09 = 0.21. The best complexity balances the two, and it depends on the amount of data: more data lowers the variance of every class and moves the balance toward more flexible models. This U shape is a common regime, not a law. Bias and variance need not move monotonically with every measure of flexibility, and heavily overparameterized models, such as large neural networks, can see the test error fall again beyond the point where they fit the training data exactly, the double descent of Belkin et al. (2019). Regularization (ridge, weight decay, early stopping, dropout) is a deliberate trade of a little bias for a large cut in variance, the same trade that made the uniform maximum beat 2Xˉ2\bar X. In many dimensions it is unavoidable: the James–Stein estimator shows that, for three or more normal means, shrinking all sample means by the right amount toward a fixed point chosen in advance lowers the total mean squared error for every value of the true means.

What changed#

ProbabilityStatistics
Knownthe distribution, θ\thetathe data
Unknownthe dataθ\theta
Object of studya random variablean estimator, a random variable through the data
Qualitybias, variance, MSE⁡=bias⁡2+variance⁡\operatorname{MSE} = \operatorname{bias}^2 + \operatorname{variance}, consistency, standard error
For a trained modelnoise + bias² + variance; complexity and regularization set the balance

Limitations#

  • Bias and MSE depend on the unknown θ\theta. An estimator can be better for some values of the parameter and worse for others; comparing them needs a criterion such as the worst case or an average over a prior.
  • Mean squared error is one loss among many. Absolute error, coverage of intervals or the cost of a wrong decision can rank estimators differently.
  • The decomposition for models is pointwise and over training sets. In practice there is one training set; bias and variance are estimated by resampling or by validation curves, not observed directly.

What you should remember#

  • An estimator is a function of the data and so a random variable; its sampling distribution decides its quality.
  • MSE⁡=bias⁡2+variance⁡\operatorname{MSE} = \operatorname{bias}^2 + \operatorname{variance}; a biased estimator can have a smaller MSE than an unbiased one.
  • The sample variance with 1/n1/n underestimates σ2\sigma^2 on average by the factor (n−1)/n(n-1)/n; 1/(n−1)1/(n-1) removes the bias.
  • Consistent: θ^n→θ\hat\theta_n \to \theta in probability; vanishing bias and variance suffice.
  • The standard error is the standard deviation of the estimator; for a mean, σ/n\sigma/\sqrt n.
  • Expected prediction error = noise + bias² + variance; in the common regime of the polynomial example flexibility lowers bias and raises variance, though not always (double descent); regularization trades bias for variance.

Code#

The uniform estimators, the divisors of the sample variance and the polynomial fits, by simulation:

import numpy as np

rng = np.random.default_rng(0)

# three estimators of theta = 1 from n = 10 draws of U(0, theta): bias, variance, MSE over 200,000 samples
n = 10
X = rng.random((200_000, n))
for name, est in [('2 x mean', 2 * X.mean(axis=1)), ('max', X.max(axis=1)), ('max x 11/10', X.max(axis=1) * (n + 1) / n)]:
    print(name, round(float(est.mean() - 1), 3), round(float(est.var()), 4), round(float(((est - 1) ** 2).mean()), 4))
# 2 x mean 0.0 0.0333 0.0333 | max -0.091 0.0069 0.0151 | max x 11/10 0.0 0.0083 0.0083

# the sample variance of 10 standard normals with divisors n - 1, n, n + 1: bias and MSE
Z = rng.normal(size=(200_000, n))
ss = ((Z - Z.mean(axis=1, keepdims=True)) ** 2).sum(axis=1)
for d in (n - 1, n, n + 1):
    print(d, round(float((ss / d).mean() - 1), 3), round(float(((ss / d - 1) ** 2).mean()), 3))
# 9 -0.001 0.221 | 10 -0.101 0.189 | 11 -0.182 0.181

# squared bias and variance of polynomial fits: 12 points of sin(2 pi x) + N(0, 0.3^2), 2,000 datasets
f = lambda x: np.sin(2 * np.pi * x)
x, grid = np.linspace(0, 1, 12), np.linspace(0, 1, 101)
for deg in (1, 3, 9):
    P = np.array([np.polynomial.polynomial.polyval(2 * grid - 1, np.polynomial.polynomial.polyfit(2 * x - 1, f(x) + 0.3 * rng.normal(size=12), deg)) for _ in range(2000)])
    print(deg, round(float(((P.mean(axis=0) - f(grid)) ** 2).mean()), 3), round(float(P.var(axis=0).mean()), 3))
# 1 0.218 0.014 | 3 0.006 0.026 | 9 0.0 0.116

Next#

Bias and variance judge an estimator once it is given; they do not say where to find a good one. The next note introduces the most widely used recipe, maximum likelihood: choose the parameter under which the observed data are most probable. It derives squared error and cross-entropy as negative log-likelihoods, and shows why maximum likelihood estimators are, for large samples, about as good as any estimator can be.

References#

  • L. Wasserman. All of Statistics. Springer, 2004. Chapter 6 (point estimation, bias, variance, MSE, consistency, standard errors).
  • G. Casella, R. L. Berger. Statistical Inference, 2nd ed. Duxbury, 2002. Chapter 7 (methods of finding and evaluating estimators).
  • T. Hastie, R. Tibshirani, J. Friedman. The Elements of Statistical Learning, 2nd ed. Springer, 2009. Sections 2.9 and 7.3 (model complexity, the bias–variance decomposition).
  • S. Geman, E. Bienenstock, R. Doursat. Neural Networks and the Bias/Variance Dilemma. Neural Computation, 4(1), 1992.
  • M. Belkin, D. Hsu, S. Ma, S. Mandal. Reconciling modern machine-learning practice and the classical bias–variance trade-off. PNAS, 116(32), 2019.
  • C. Stein. Inadmissibility of the Usual Estimator for the Mean of a Multivariate Normal Distribution. Proceedings of the Third Berkeley Symposium on Mathematical Statistics and Probability, 1956.