Practice / Probability for ML

The bias–variance decomposition

Ten problems on bias and variance: the mean squared error of an estimator split into squared bias plus variance, the sample mean and a shrunken mean that beats it, the three-term decomposition of expected test error with the noise floor, the one-dimensional least-squares estimator and its ridge version with bias, variance and the λ that minimises their sum, how the two terms move with λ, why averaging models cuts variance but not bias and where correlation puts the floor, the variance estimator with the smallest error, and nearest-neighbour regression where k trades bias against variance, with worked solutions and the mistakes that prefer unbiased by reflex, apply the decomposition to training error, guess the ridge variance by analogy, forget the noise term, or divide the variance of correlated models by their number.

Before you start

An estimator can be wrong in two ways: on average, which is bias, and from one dataset to the next, which is variance. Mean squared error is the sum of the square of the first and the second, and almost every design decision in machine learning, regularisation strength, model size, how many neighbours, how many models to average, moves one term up and the other down. These ten problems prove the decomposition for a scalar estimator and for a prediction at a point, where a third term, the noise, appears; work it out for the sample mean and show that shrinking it towards zero lowers the error; do the same for the one-dimensional least-squares slope and its ridge version, finding the regularisation that minimises the total; show how the two terms move with the regulariser; compute what averaging models does to each term; find the variance estimator with the smallest error, which is neither the unbiased one nor the maximum-likelihood one; and end with nearest-neighbour regression, where the number of neighbours is the knob. The five mistakes at the end are the ones that sound like good statistics: unbiased taken to mean best, the decomposition applied to the training set, a ridge variance written by analogy, the noise term dropped, and the variance of an average divided by the number of models when the models are correlated.

  • An estimator θ^\hat\theta is a function of a random sample, so it is a random variable; θ\theta is the fixed quantity it estimates. Its bias is bias⁡(θ^)=E[θ^]−θ\operatorname{bias}(\hat\theta) = \mathbb{E}[\hat\theta] - \theta, its variance is Var⁡(θ^)=E[(θ^−E[θ^])2]\operatorname{Var}(\hat\theta) = \mathbb{E}\big[(\hat\theta - \mathbb{E}[\hat\theta])^2\big] and its mean squared error is MSE⁡(θ^)=E[(θ^−θ)2]\operatorname{MSE}(\hat\theta) = \mathbb{E}\big[(\hat\theta - \theta)^2\big] (the maximum-likelihood page). E\mathbb{E} and Var⁡\operatorname{Var} are over the sample; the variance page's rules E[aX+b]=aE[X]+b\mathbb{E}[aX + b] = a\mathbb{E}[X] + b, Var⁡(aX)=a2Var⁡(X)\operatorname{Var}(aX) = a^2\operatorname{Var}(X) and Var⁡(∑nXn)=∑nVar⁡(Xn)\operatorname{Var}\big(\sum_nX_n\big) = \sum_n\operatorname{Var}(X_n) for independent XnX_n are used throughout.
  • A sample x1,…,xNx_1, \dots, x_N is independent and identically distributed (i.i.d.) with mean μ\mu and variance σ2\sigma^2; xˉ=1N∑nxn\bar x = \tfrac1N\sum_nx_n is the sample mean and S=∑n(xn−xˉ)2S = \sum_n(x_n - \bar x)^2. For Gaussian data, S/σ2S/\sigma^2 has a chi-square distribution with N−1N - 1 degrees of freedom, whose mean is N−1N - 1 and variance 2(N−1)2(N - 1); this is taken as given.
  • Regression data are y=f(x)+εy = f(x) + \varepsilon with E[ε]=0\mathbb{E}[\varepsilon] = 0, Var⁡(ε)=σ2\operatorname{Var}(\varepsilon) = \sigma^2, and the noise on different points independent. A learner trained on a dataset DD produces the prediction f^D(x)\hat f_D(x); ED\mathbb{E}_D is the expectation over datasets and Var⁡D\operatorname{Var}_D the variance across them. A fresh test point has its own noise ε\varepsilon, independent of DD.
  • The one-dimensional linear model has fixed inputs x1,…,xNx_1, \dots, x_N, true slope ww, observations yn=wxn+εny_n = wx_n + \varepsilon_n, and s=∑nxn2s = \sum_nx_n^2. Least squares gives w^=∑nxnyn/s\hat w = \sum_nx_ny_n/s and ridge with penalty λ≥0\lambda \ge 0 gives w^λ=∑nxnyn/(s+λ)\hat w_\lambda = \sum_nx_ny_n/(s + \lambda), the minimiser of ∑n(yn−wxn)2+λw2\sum_n(y_n - wx_n)^2 + \lambda w^2 (the regression page).
  • 1\mathbf{1} is the all-ones vector, Σ\Sigma a covariance matrix and ρ\rho a correlation coefficient. kk-nearest-neighbour regression predicts f^(x)=1k∑j∈Nk(x)yj\hat f(x) = \tfrac1k\sum_{j \in N_k(x)}y_j, the mean of the kk training responses nearest to xx.

Builds on: Variance, covariance and correlation, Maximum likelihood estimation: derivations by hand

Problems

  1. ·

    Show that for any estimator θ^\hat\theta of a fixed θ\theta, MSE⁡(θ^)=bias⁡(θ^)2+Var⁡(θ^)\operatorname{MSE}(\hat\theta) = \operatorname{bias}(\hat\theta)^2 + \operatorname{Var}(\hat\theta).

  2. ··

    For an i.i.d. sample with mean μ\mu and variance σ2\sigma^2, compute the bias, variance and MSE of the sample mean xˉ\bar x. Then do the same for the shrunken estimator cxˉc\bar x with 0≤c≤10 \le c \le 1, find the c∗c^* that minimises its MSE, and evaluate for μ=1\mu = 1, σ2=1\sigma^2 = 1, N=4N = 4.

  3. ··

    For a fixed test input xx with y=f(x)+εy = f(x) + \varepsilon and a learner f^D\hat f_D, show that ED,ε[(y−f^D(x))2]=σ2+(ED[f^D(x)]−f(x))2+Var⁡D(f^D(x))\mathbb{E}_{D,\varepsilon}\big[(y - \hat f_D(x))^2\big] = \sigma^2 + \big(\mathbb{E}_D[\hat f_D(x)] - f(x)\big)^2 + \operatorname{Var}_D\big(\hat f_D(x)\big). Which term can a better learner not reduce?

  4. ·

    For the one-dimensional model yn=wxn+εny_n = wx_n + \varepsilon_n with fixed inputs, show that the least-squares estimator w^=∑nxnyn/s\hat w = \sum_nx_ny_n/s satisfies w^=w+∑nxnεn/s\hat w = w + \sum_nx_n\varepsilon_n/s, that it is unbiased, and that Var⁡(w^)=σ2/s\operatorname{Var}(\hat w) = \sigma^2/s.

  5. ··

    For ridge, w^λ=∑nxnyn/(s+λ)\hat w_\lambda = \sum_nx_ny_n/(s + \lambda), show that w^λ=ss+λw^\hat w_\lambda = \dfrac{s}{s + \lambda}\hat w, and compute its bias, variance and MSE as functions of λ\lambda.

  6. ···

    Find the λ∗\lambda^* that minimises MSE⁡(w^λ)\operatorname{MSE}(\hat w_\lambda) and the minimum value. Show that λ∗>0\lambda^* > 0 always, so some ridge penalty beats least squares, and evaluate for w=1w = 1, σ2=1\sigma^2 = 1, s=4s = 4.

  7. ··

    Show that bias⁡(w^λ)2\operatorname{bias}(\hat w_\lambda)^2 is increasing in λ\lambda and Var⁡(w^λ)\operatorname{Var}(\hat w_\lambda) is decreasing, by computing their derivatives, and find their limits as λ→∞\lambda \to \infty. At what λ\lambda are the two terms equal?

  8. ··

    BB predictors f^1(x),…,f^B(x)\hat f_1(x), \dots, \hat f_B(x) each have mean mm, variance vv and pairwise correlation ρ\rho. Show that their average fˉ(x)=1B∑bf^b(x)\bar f(x) = \tfrac1B\sum_b\hat f_b(x) has mean mm, so the bias is unchanged, and variance ρv+(1−ρ)vB\rho v + \dfrac{(1 - \rho)v}B. What are the two limits ρ=0\rho = 0 and B→∞B \to \infty?

  9. ···

    For Gaussian data consider the estimators cScS of σ2\sigma^2, where S=∑n(xn−xˉ)2S = \sum_n(x_n - \bar x)^2. Compute bias, variance and MSE as functions of cc, show that the MSE is minimised at c∗=1/(N+1)c^* = 1/(N + 1), and compare the MSEs of S/(N−1)S/(N - 1) (unbiased), S/NS/N (maximum likelihood) and S/(N+1)S/(N + 1) for N=5N = 5.

  10. ···

    Training inputs lie on a grid with spacing hh, f(x)=ax2f(x) = ax^2, and kk-nearest-neighbour regression with k=2m+1k = 2m + 1 uses the mm grid points on each side of a query xx that is itself a grid point. Show that the prediction has variance σ2/k\sigma^2/k and bias ah2k2−112ah^2\dfrac{k^2 - 1}{12}, write the MSE above the noise floor, and find the best kk among {1,3,5,7,9}\{1, 3, 5, 7, 9\} for a=1a = 1, σ2=1\sigma^2 = 1, h=14h = \tfrac14.

Worked solutions

Problem 1

Show that for any estimator θ^\hat\theta of a fixed θ\theta, MSE⁡(θ^)=bias⁡(θ^)2+Var⁡(θ^)\operatorname{MSE}(\hat\theta) = \operatorname{bias}(\hat\theta)^2 + \operatorname{Var}(\hat\theta).

  1. Let m=E[θ^]m = \mathbb{E}[\hat\theta]. Then θ^−θ=(θ^−m)+(m−θ)\hat\theta - \theta = (\hat\theta - m) + (m - \theta).Add and subtract the mean of the estimator; the second bracket is the bias, a constant.
  2. (θ^−θ)2=(θ^−m)2+2(θ^−m)(m−θ)+(m−θ)2(\hat\theta - \theta)^2 = (\hat\theta - m)^2 + 2(\hat\theta - m)(m - \theta) + (m - \theta)^2.Expand the square.
  3. E[(θ^−m)(m−θ)]=(m−θ) E[θ^−m]=(m−θ)⋅0\mathbb{E}[(\hat\theta - m)(m - \theta)] = (m - \theta)\,\mathbb{E}[\hat\theta - m] = (m - \theta)\cdot 0.m−θm - \theta is a constant and comes out; E[θ^]−m=0\mathbb{E}[\hat\theta] - m = 0 by the definition of mm.
  4. E[(θ^−θ)2]=E[(θ^−m)2]+(m−θ)2=Var⁡(θ^)+bias⁡(θ^)2\mathbb{E}[(\hat\theta - \theta)^2] = \mathbb{E}[(\hat\theta - m)^2] + (m - \theta)^2 = \operatorname{Var}(\hat\theta) + \operatorname{bias}(\hat\theta)^2.Take expectations of step 2; the cross term vanishes by step 3; the remaining terms are the definitions.
  5. MSE⁡(θ^)=bias⁡(θ^)2+Var⁡(θ^)\operatorname{MSE}(\hat\theta) = \operatorname{bias}(\hat\theta)^2 + \operatorname{Var}(\hat\theta)The error splits into a systematic part, how far the average estimate sits from the truth, and a random part, how much the estimate moves from sample to sample, and the two add because the cross term averages out. Nothing about the distribution was used, only that θ\theta is fixed and θ^\hat\theta has a mean and a variance. The decomposition is an identity, not an inequality, so lowering the MSE means lowering the sum, and Problems 2, 6 and 9 all do it by accepting some bias for a larger drop in variance; Mistake 1 refuses the trade.

Problem 2

For an i.i.d. sample with mean μ\mu and variance σ2\sigma^2, compute the bias, variance and MSE of the sample mean xˉ\bar x. Then do the same for the shrunken estimator cxˉc\bar x with 0≤c≤10 \le c \le 1, find the c∗c^* that minimises its MSE, and evaluate for μ=1\mu = 1, σ2=1\sigma^2 = 1, N=4N = 4.

  1. E[xˉ]=1N∑nE[xn]=μ\mathbb{E}[\bar x] = \tfrac1N\sum_n\mathbb{E}[x_n] = \mu, so bias⁡(xˉ)=0\operatorname{bias}(\bar x) = 0.Linearity of expectation; every xnx_n has mean μ\mu.
  2. Var⁡(xˉ)=1N2∑nVar⁡(xn)=σ2N\operatorname{Var}(\bar x) = \tfrac1{N^2}\sum_n\operatorname{Var}(x_n) = \dfrac{\sigma^2}N, so MSE⁡(xˉ)=σ2N\operatorname{MSE}(\bar x) = \dfrac{\sigma^2}N.Independence makes the variance of the sum the sum of the variances; Var⁡(aX)=a2Var⁡(X)\operatorname{Var}(aX) = a^2\operatorname{Var}(X) with a=1/Na = 1/N; Problem 1 with zero bias.
  3. E[cxˉ]=cμ\mathbb{E}[c\bar x] = c\mu, so bias⁡(cxˉ)=(c−1)μ\operatorname{bias}(c\bar x) = (c - 1)\mu; Var⁡(cxˉ)=c2σ2/N\operatorname{Var}(c\bar x) = c^2\sigma^2/N.Scaling by the constant cc scales the mean by cc and the variance by c2c^2.
  4. MSE⁡(cxˉ)=(1−c)2μ2+c2σ2N\operatorname{MSE}(c\bar x) = (1 - c)^2\mu^2 + c^2\dfrac{\sigma^2}N.Problem 1.
  5. ddcMSE⁡=−2(1−c)μ2+2cσ2N=0  ⟺  c∗=μ2μ2+σ2/N\dfrac{d}{dc}\operatorname{MSE} = -2(1 - c)\mu^2 + 2c\dfrac{\sigma^2}N = 0 \iff c^* = \dfrac{\mu^2}{\mu^2 + \sigma^2/N}, a minimum since the second derivative 2μ2+2σ2/N>02\mu^2 + 2\sigma^2/N > 0.Differentiate the quadratic in cc and solve; it is convex, so the stationary point is the minimum.
  6. μ=1\mu = 1, σ2=1\sigma^2 = 1, N=4N = 4: c∗=11+1/4=0.8c^* = \dfrac{1}{1 + 1/4} = 0.8, MSE⁡(0.8xˉ)=0.04⋅1+0.64⋅0.25=0.2<0.25=MSE⁡(xˉ)\operatorname{MSE}(0.8\bar x) = 0.04\cdot 1 + 0.64\cdot 0.25 = 0.2 < 0.25 = \operatorname{MSE}(\bar x).Step 5, then step 4 with c=0.8c = 0.8: bias −0.2-0.2, variance 0.160.16.
  7. xˉ\bar x: bias 00, variance σ2/N\sigma^2/N, MSE σ2/N\sigma^2/N; cxˉc\bar x: bias (c−1)μ(c - 1)\mu, variance c2σ2/Nc^2\sigma^2/N, MSE (1−c)2μ2+c2σ2/N(1 - c)^2\mu^2 + c^2\sigma^2/N, minimised at c∗=μ2μ2+σ2/Nc^* = \dfrac{\mu^2}{\mu^2 + \sigma^2/N}; for μ=1\mu = 1, σ2=1\sigma^2 = 1, N=4N = 4: c∗=0.8c^* = 0.8 and MSE 0.20.2 against 0.250.25Shrinking towards zero trades a bias of 0.20.2 for a variance cut from 0.250.25 to 0.160.16, and wins. The catch is that c∗c^* depends on μ\mu, the thing being estimated, so it is not an estimator one can use directly; it says the unbiased choice is not the floor, and that any prior knowledge of the size of μ\mu (here, that μ2\mu^2 is comparable to σ2/N\sigma^2/N) can be spent. Ridge regression in Problem 6 is the same calculation with the same answer, and the Bayesian posterior mean under a Gaussian prior lands on exactly c∗xˉc^*\bar x with μ2\mu^2 replaced by the prior variance.

Problem 3

For a fixed test input xx with y=f(x)+εy = f(x) + \varepsilon and a learner f^D\hat f_D, show that ED,ε[(y−f^D(x))2]=σ2+(ED[f^D(x)]−f(x))2+Var⁡D(f^D(x))\mathbb{E}_{D,\varepsilon}\big[(y - \hat f_D(x))^2\big] = \sigma^2 + \big(\mathbb{E}_D[\hat f_D(x)] - f(x)\big)^2 + \operatorname{Var}_D\big(\hat f_D(x)\big). Which term can a better learner not reduce?

  1. y−f^D(x)=ε+(f(x)−f^D(x))y - \hat f_D(x) = \varepsilon + \big(f(x) - \hat f_D(x)\big).Substitute y=f(x)+εy = f(x) + \varepsilon and regroup.
  2. (y−f^D(x))2=ε2+2ε(f(x)−f^D(x))+(f(x)−f^D(x))2(y - \hat f_D(x))^2 = \varepsilon^2 + 2\varepsilon\big(f(x) - \hat f_D(x)\big) + \big(f(x) - \hat f_D(x)\big)^2.Expand the square.
  3. E[ε2]=σ2\mathbb{E}[\varepsilon^2] = \sigma^2 and E[ε (f(x)−f^D(x))]=E[ε] ED[f(x)−f^D(x)]=0\mathbb{E}\big[\varepsilon\,(f(x) - \hat f_D(x))\big] = \mathbb{E}[\varepsilon]\,\mathbb{E}_D[f(x) - \hat f_D(x)] = 0.ε\varepsilon has mean zero and variance σ2\sigma^2; the test noise is independent of the training set DD, so the expectation of the product is the product of the expectations (the variance page).
  4. ED[(f(x)−f^D(x))2]=(ED[f^D(x)]−f(x))2+Var⁡D(f^D(x))\mathbb{E}_D\big[(f(x) - \hat f_D(x))^2\big] = \big(\mathbb{E}_D[\hat f_D(x)] - f(x)\big)^2 + \operatorname{Var}_D\big(\hat f_D(x)\big).Problem 1 with θ^=f^D(x)\hat\theta = \hat f_D(x), an estimator of the fixed number θ=f(x)\theta = f(x).
  5. E[(y−f^D(x))2]=σ2+bias⁡2+variance⁡\mathbb{E}\big[(y - \hat f_D(x))^2\big] = \sigma^2 + \operatorname{bias}^2 + \operatorname{variance}, with bias⁡=ED[f^D(x)]−f(x)\operatorname{bias} = \mathbb{E}_D[\hat f_D(x)] - f(x) and variance⁡=Var⁡D(f^D(x))\operatorname{variance} = \operatorname{Var}_D(\hat f_D(x)); the σ2\sigma^2 cannot be reducedThe expected test error at a point has a floor, the noise variance, that no learner reaches: even f^D=f\hat f_D = f exactly scores σ2\sigma^2 (Mistake 4). Above the floor sit the two terms of Problem 1 for the learner regarded as an estimator of f(x)f(x); averaging over xx gives the usual statement about test error. Step 3 used that the test noise is independent of DD, and that is what fails on the training set, where the same εn\varepsilon_n sits in yny_n and in f^D\hat f_D (Mistake 2). The check evaluates the left side exactly for the linear model of Problems 4 and 5, where f^D(x)=w^ x\hat f_D(x) = \hat w\,x is Gaussian, and compares it with the three terms.

Problem 4

For the one-dimensional model yn=wxn+εny_n = wx_n + \varepsilon_n with fixed inputs, show that the least-squares estimator w^=∑nxnyn/s\hat w = \sum_nx_ny_n/s satisfies w^=w+∑nxnεn/s\hat w = w + \sum_nx_n\varepsilon_n/s, that it is unbiased, and that Var⁡(w^)=σ2/s\operatorname{Var}(\hat w) = \sigma^2/s.

  1. ∑nxnyn=∑nxn(wxn+εn)=ws+∑nxnεn\sum_nx_ny_n = \sum_nx_n(wx_n + \varepsilon_n) = ws + \sum_nx_n\varepsilon_n.Substitute the model and use ∑nxn2=s\sum_nx_n^2 = s.
  2. w^=ws+∑nxnεns=w+∑nxnεns\hat w = \dfrac{ws + \sum_nx_n\varepsilon_n}{s} = w + \dfrac{\sum_nx_n\varepsilon_n}{s}.Divide by ss.
  3. E[w^]=w+∑nxnE[εn]s=w\mathbb{E}[\hat w] = w + \dfrac{\sum_nx_n\mathbb{E}[\varepsilon_n]}{s} = w.The xnx_n are fixed and every εn\varepsilon_n has mean zero.
  4. Var⁡(w^)=1s2∑nxn2Var⁡(εn)=σ2ss2=σ2s\operatorname{Var}(\hat w) = \dfrac{1}{s^2}\sum_nx_n^2\operatorname{Var}(\varepsilon_n) = \dfrac{\sigma^2s}{s^2} = \dfrac{\sigma^2}s.Independent noise: the variance of ∑nxnεn\sum_nx_n\varepsilon_n is ∑nxn2σ2\sum_nx_n^2\sigma^2; then Var⁡(aX)=a2Var⁡(X)\operatorname{Var}(aX) = a^2\operatorname{Var}(X) with a=1/sa = 1/s.
  5. w^=w+∑nxnεn/s\hat w = w + \sum_nx_n\varepsilon_n/s; bias⁡=0\operatorname{bias} = 0; Var⁡(w^)=σ2/s\operatorname{Var}(\hat w) = \sigma^2/s, so MSE⁡(w^)=σ2/s\operatorname{MSE}(\hat w) = \sigma^2/sThe estimator is the truth plus a weighted average of the noise, and its variance falls as s=∑nxn2s = \sum_nx_n^2 grows: more points, or points farther from the origin, pin the slope down. This is the scalar case of the least-squares page's Cov⁡(w^)=σ2(X⊤X)−1\operatorname{Cov}(\hat w) = \sigma^2(X^\top X)^{-1}, with X⊤X=sX^\top X = s. The regression page shows that least squares is also the maximum-likelihood estimator under Gaussian noise, and the MLE page's variance estimator is biased for the same structural reason that Problem 9 revisits; the slope estimator itself is not.

Problem 5

For ridge, w^λ=∑nxnyn/(s+λ)\hat w_\lambda = \sum_nx_ny_n/(s + \lambda), show that w^λ=ss+λw^\hat w_\lambda = \dfrac{s}{s + \lambda}\hat w, and compute its bias, variance and MSE as functions of λ\lambda.

  1. w^λ=∑nxnyns+λ=ss+λ⋅∑nxnyns=ss+λw^\hat w_\lambda = \dfrac{\sum_nx_ny_n}{s + \lambda} = \dfrac{s}{s + \lambda}\cdot\dfrac{\sum_nx_ny_n}{s} = \dfrac{s}{s + \lambda}\hat w.Multiply and divide by ss.
  2. E[w^λ]=ss+λw\mathbb{E}[\hat w_\lambda] = \dfrac{s}{s + \lambda}w, so bias⁡(w^λ)=ss+λw−w=−λs+λw\operatorname{bias}(\hat w_\lambda) = \dfrac{s}{s + \lambda}w - w = -\dfrac{\lambda}{s + \lambda}w.Problem 4's E[w^]=w\mathbb{E}[\hat w] = w scaled by the constant; ss+λ−1=−λs+λ\tfrac{s}{s + \lambda} - 1 = -\tfrac{\lambda}{s + \lambda}.
  3. Var⁡(w^λ)=(ss+λ)2σ2s=σ2s(s+λ)2\operatorname{Var}(\hat w_\lambda) = \Big(\dfrac{s}{s + \lambda}\Big)^2\dfrac{\sigma^2}s = \dfrac{\sigma^2s}{(s + \lambda)^2}.Var⁡(aX)=a2Var⁡(X)\operatorname{Var}(aX) = a^2\operatorname{Var}(X) with Problem 4's variance.
  4. MSE⁡(w^λ)=λ2w2(s+λ)2+σ2s(s+λ)2=λ2w2+σ2s(s+λ)2\operatorname{MSE}(\hat w_\lambda) = \dfrac{\lambda^2w^2}{(s + \lambda)^2} + \dfrac{\sigma^2s}{(s + \lambda)^2} = \dfrac{\lambda^2w^2 + \sigma^2s}{(s + \lambda)^2}.Problem 1; common denominator.
  5. w^λ=ss+λw^\hat w_\lambda = \dfrac{s}{s + \lambda}\hat w; bias⁡=−λws+λ\operatorname{bias} = -\dfrac{\lambda w}{s + \lambda}; Var⁡=σ2s(s+λ)2\operatorname{Var} = \dfrac{\sigma^2s}{(s + \lambda)^2}; MSE⁡=λ2w2+σ2s(s+λ)2\operatorname{MSE} = \dfrac{\lambda^2w^2 + \sigma^2s}{(s + \lambda)^2}Ridge is Problem 2's shrinkage with c=s/(s+λ)c = s/(s + \lambda): the estimate is pulled towards zero by a factor that the data size ss and the penalty λ\lambda fight over, the bias is the part of ww lost to the pull, and the variance is cut by c2c^2. At λ=0\lambda = 0 everything reduces to Problem 4; as λ→∞\lambda \to \infty the estimate goes to 00, with bias −w-w and no variance. The variance is σ2s/(s+λ)2\sigma^2s/(s + \lambda)^2 and not σ2/(s+λ)\sigma^2/(s + \lambda) (Mistake 3), a distinction that matters for everything in Problems 6 and 7.

Problem 6

Find the λ∗\lambda^* that minimises MSE⁡(w^λ)\operatorname{MSE}(\hat w_\lambda) and the minimum value. Show that λ∗>0\lambda^* > 0 always, so some ridge penalty beats least squares, and evaluate for w=1w = 1, σ2=1\sigma^2 = 1, s=4s = 4.

  1. MSE⁡(λ)=u(λ)v(λ)\operatorname{MSE}(\lambda) = \dfrac{u(\lambda)}{v(\lambda)} with u=λ2w2+σ2su = \lambda^2w^2 + \sigma^2s and v=(s+λ)2v = (s + \lambda)^2; u′=2λw2u' = 2\lambda w^2 and v′=2(s+λ)v' = 2(s + \lambda).Name the pieces for the quotient rule.
  2. ddλMSE⁡=u′v−uv′v2=2(s+λ)[λw2(s+λ)−λ2w2−σ2s](s+λ)4=2s (λw2−σ2)(s+λ)3\dfrac{d}{d\lambda}\operatorname{MSE} = \dfrac{u'v - uv'}{v^2} = \dfrac{2(s + \lambda)\big[\lambda w^2(s + \lambda) - \lambda^2w^2 - \sigma^2s\big]}{(s + \lambda)^4} = \dfrac{2s\,(\lambda w^2 - \sigma^2)}{(s + \lambda)^3}.Quotient rule; factor 2(s+λ)2(s + \lambda) from the numerator; inside the bracket λw2s+λ2w2−λ2w2−σ2s=s(λw2−σ2)\lambda w^2s + \lambda^2w^2 - \lambda^2w^2 - \sigma^2s = s(\lambda w^2 - \sigma^2).
  3. The derivative is negative for λ<σ2/w2\lambda < \sigma^2/w^2 and positive for λ>σ2/w2\lambda > \sigma^2/w^2, so λ∗=σ2w2\lambda^* = \dfrac{\sigma^2}{w^2} is the minimum.The sign of step 2 is the sign of λw2−σ2\lambda w^2 - \sigma^2; the function decreases then increases.
  4. MSE⁡(λ∗)=σ4/w2+σ2s(s+σ2/w2)2=σ2(s+σ2/w2)(s+σ2/w2)2=σ2s+σ2/w2\operatorname{MSE}(\lambda^*) = \dfrac{\sigma^4/w^2 + \sigma^2s}{(s + \sigma^2/w^2)^2} = \dfrac{\sigma^2(s + \sigma^2/w^2)}{(s + \sigma^2/w^2)^2} = \dfrac{\sigma^2}{s + \sigma^2/w^2}.Substitute λ∗\lambda^*; the numerator is σ2\sigma^2 times the base of the denominator.
  5. λ∗=σ2/w2>0\lambda^* = \sigma^2/w^2 > 0 whenever σ2>0\sigma^2 > 0, and σ2s+σ2/w2<σ2s=MSE⁡(w^)\dfrac{\sigma^2}{s + \sigma^2/w^2} < \dfrac{\sigma^2}s = \operatorname{MSE}(\hat w).A positive number divided by w2w^2; a larger denominator.
  6. w=1w = 1, σ2=1\sigma^2 = 1, s=4s = 4: λ∗=1\lambda^* = 1, MSE⁡(λ∗)=14+1=0.2\operatorname{MSE}(\lambda^*) = \dfrac1{4 + 1} = 0.2 against MSE⁡(w^)=0.25\operatorname{MSE}(\hat w) = 0.25; at λ∗=1\lambda^* = 1 the bias is −0.2-0.2 and the variance 4/25=0.164/25 = 0.16.Steps 3 and 4; Problem 5's bias and variance at λ=1\lambda = 1.
  7. λ∗=σ2/w2\lambda^* = \sigma^2/w^2 with MSE⁡(λ∗)=σ2s+σ2/w2<σ2s\operatorname{MSE}(\lambda^*) = \dfrac{\sigma^2}{s + \sigma^2/w^2} < \dfrac{\sigma^2}s; for w=1w = 1, σ2=1\sigma^2 = 1, s=4s = 4: λ∗=1\lambda^* = 1, MSE 0.20.2 against 0.250.25The best penalty is the noise-to-signal ratio σ2/w2\sigma^2/w^2, and it does not depend on ss: more data does not change the right λ\lambda, it makes the shrinkage factor s/(s+λ)s/(s + \lambda) closer to 11 so that the penalty matters less. The numbers are Problem 2's exactly, because w^λ=cw^\hat w_\lambda = c\hat w with c=s/(s+λ)c = s/(s + \lambda) and λ∗\lambda^* corresponds to c∗=w2/(w2+σ2/s)c^* = w^2/(w^2 + \sigma^2/s). In the Bayesian reading, λ=σ2/τ2\lambda = \sigma^2/\tau^2 is the ridge penalty that makes the posterior mean under the prior w∼N(0,τ2)w \sim \mathcal{N}(0, \tau^2), and λ∗\lambda^* is the prior whose variance matches w2w^2. As in Problem 2, λ∗\lambda^* needs ww, so in practice it is chosen by cross-validation, which estimates the MSE curve of Problem 7 from the data.

Problem 7

Show that bias⁡(w^λ)2\operatorname{bias}(\hat w_\lambda)^2 is increasing in λ\lambda and Var⁡(w^λ)\operatorname{Var}(\hat w_\lambda) is decreasing, by computing their derivatives, and find their limits as λ→∞\lambda \to \infty. At what λ\lambda are the two terms equal?

  1. bias⁡2=λ2w2(s+λ)2\operatorname{bias}^2 = \dfrac{\lambda^2w^2}{(s + \lambda)^2} and ddλbias⁡2=w2⋅2λ(s+λ)2−λ2⋅2(s+λ)(s+λ)4=2λsw2(s+λ)3>0\dfrac{d}{d\lambda}\operatorname{bias}^2 = w^2\cdot\dfrac{2\lambda(s + \lambda)^2 - \lambda^2\cdot 2(s + \lambda)}{(s + \lambda)^4} = \dfrac{2\lambda sw^2}{(s + \lambda)^3} > 0 for λ>0\lambda > 0.Quotient rule; factor 2λ(s+λ)2\lambda(s + \lambda) from the numerator, leaving (s+λ)−λ=s(s + \lambda) - \lambda = s.
  2. Var⁡=σ2s(s+λ)2\operatorname{Var} = \dfrac{\sigma^2s}{(s + \lambda)^2} and ddλVar⁡=−2σ2s(s+λ)3<0\dfrac{d}{d\lambda}\operatorname{Var} = -\dfrac{2\sigma^2s}{(s + \lambda)^3} < 0.Power rule on (s+λ)−2(s + \lambda)^{-2}.
  3. As λ→∞\lambda \to \infty: bias⁡2→w2\operatorname{bias}^2 \to w^2 and Var⁡→0\operatorname{Var} \to 0.λ2/(s+λ)2→1\lambda^2/(s + \lambda)^2 \to 1 and s/(s+λ)2→0s/(s + \lambda)^2 \to 0.
  4. bias⁡2=Var⁡  ⟺  λ2w2=σ2s  ⟺  λ=σsw\operatorname{bias}^2 = \operatorname{Var} \iff \lambda^2w^2 = \sigma^2s \iff \lambda = \dfrac{\sigma\sqrt s}{w}.Equate the numerators, which share the denominator; take the positive root.
  5. ddλbias⁡2=2λsw2(s+λ)3>0\dfrac{d}{d\lambda}\operatorname{bias}^2 = \dfrac{2\lambda sw^2}{(s + \lambda)^3} > 0, ddλVar⁡=−2σ2s(s+λ)3<0\dfrac{d}{d\lambda}\operatorname{Var} = -\dfrac{2\sigma^2s}{(s + \lambda)^3} < 0; limits w2w^2 and 00; the terms are equal at λ=σs/w\lambda = \sigma\sqrt s/wThis is the picture behind every regularisation plot: a rising bias curve, a falling variance curve and a U-shaped sum. Adding the two derivatives gives 2s(λw2−σ2)(s+λ)3\dfrac{2s(\lambda w^2 - \sigma^2)}{(s + \lambda)^3}, Problem 6's derivative, and the sum is minimised where the two slopes cancel, at λ∗=σ2/w2\lambda^* = \sigma^2/w^2, which is not where the curves cross: for Problem 6's numbers they cross at λ=2\lambda = 2 while the minimum is at λ=1\lambda = 1, where the variance term (0.160.16) is still four times the squared bias (0.040.04). The same shape appears with model size, training time and the number of neighbours (Problem 10) as the knob.

Problem 8

BB predictors f^1(x),…,f^B(x)\hat f_1(x), \dots, \hat f_B(x) each have mean mm, variance vv and pairwise correlation ρ\rho. Show that their average fˉ(x)=1B∑bf^b(x)\bar f(x) = \tfrac1B\sum_b\hat f_b(x) has mean mm, so the bias is unchanged, and variance ρv+(1−ρ)vB\rho v + \dfrac{(1 - \rho)v}B. What are the two limits ρ=0\rho = 0 and B→∞B \to \infty?

  1. E[fˉ]=1B∑bE[f^b]=1B⋅Bm=m\mathbb{E}[\bar f] = \tfrac1B\sum_b\mathbb{E}[\hat f_b] = \tfrac1B\cdot Bm = m.Linearity; every predictor has the same mean. The bias m−f(x)m - f(x) is therefore the same as each member's.
  2. Let z=(f^1,…,f^B)⊤z = (\hat f_1, \dots, \hat f_B)^\top with covariance Σ\Sigma, Σbb=v\Sigma_{bb} = v and Σbb′=ρv\Sigma_{bb'} = \rho v for b≠b′b \neq b'. Then fˉ=1B1⊤z\bar f = \tfrac1B\mathbf{1}^\top z and Var⁡(fˉ)=1B21⊤Σ1\operatorname{Var}(\bar f) = \tfrac1{B^2}\mathbf{1}^\top\Sigma\mathbf{1}.The variance page's Cov⁡(Az)=AΣA⊤\operatorname{Cov}(Az) = A\Sigma A^\top with the 1×B1\times B matrix A=1B1⊤A = \tfrac1B\mathbf{1}^\top.
  3. 1⊤Σ1=∑b,b′Σbb′=Bv+B(B−1)ρv\mathbf{1}^\top\Sigma\mathbf{1} = \sum_{b,b'}\Sigma_{bb'} = Bv + B(B - 1)\rho v.BB diagonal entries equal to vv and B(B−1)B(B - 1) off-diagonal entries equal to ρv\rho v.
  4. Var⁡(fˉ)=Bv+B(B−1)ρvB2=v+(B−1)ρvB=ρv+(1−ρ)vB\operatorname{Var}(\bar f) = \dfrac{Bv + B(B - 1)\rho v}{B^2} = \dfrac{v + (B - 1)\rho v}B = \rho v + \dfrac{(1 - \rho)v}B.Divide by B2B^2; split (B−1)ρv=Bρv−ρv(B - 1)\rho v = B\rho v - \rho v.
  5. E[fˉ]=m\mathbb{E}[\bar f] = m (bias unchanged); Var⁡(fˉ)=ρv+(1−ρ)vB\operatorname{Var}(\bar f) = \rho v + \dfrac{(1 - \rho)v}B; for ρ=0\rho = 0 it is v/Bv/B, and as B→∞B \to \infty it tends to ρv\rho vAveraging models attacks only the variance term of Problem 3, and only the part of it that is not shared: with independent members the variance falls like 1/B1/B (the variance page's sample mean again), and with correlated members it stops at the floor ρv\rho v however many are averaged (Mistake 5). Bagging trains members on bootstrap resamples to make them differ, and random forests decorrelate them further by restricting each split to a random subset of features, lowering ρ\rho at the cost of a little bias; the Jensen page's ambiguity decomposition is the same conclusion reached for one dataset rather than in expectation. Boosting is the opposite strategy: it attacks the bias term by fitting each new model to the residual of the current average.

Problem 9

For Gaussian data consider the estimators cScS of σ2\sigma^2, where S=∑n(xn−xˉ)2S = \sum_n(x_n - \bar x)^2. Compute bias, variance and MSE as functions of cc, show that the MSE is minimised at c∗=1/(N+1)c^* = 1/(N + 1), and compare the MSEs of S/(N−1)S/(N - 1) (unbiased), S/NS/N (maximum likelihood) and S/(N+1)S/(N + 1) for N=5N = 5.

  1. E[S]=(N−1)σ2\mathbb{E}[S] = (N - 1)\sigma^2 and Var⁡(S)=2(N−1)σ4\operatorname{Var}(S) = 2(N - 1)\sigma^4.S/σ2S/\sigma^2 is chi-square with N−1N - 1 degrees of freedom, with mean N−1N - 1 and variance 2(N−1)2(N - 1); scaling by σ2\sigma^2 scales the mean by σ2\sigma^2 and the variance by σ4\sigma^4.
  2. bias⁡(cS)=c(N−1)σ2−σ2=(c(N−1)−1)σ2\operatorname{bias}(cS) = c(N - 1)\sigma^2 - \sigma^2 = \big(c(N - 1) - 1\big)\sigma^2 and Var⁡(cS)=2c2(N−1)σ4\operatorname{Var}(cS) = 2c^2(N - 1)\sigma^4.Scale step 1 by cc; subtract the target σ2\sigma^2.
  3. MSE⁡(cS)=σ4[(c(N−1)−1)2+2c2(N−1)]\operatorname{MSE}(cS) = \sigma^4\Big[\big(c(N - 1) - 1\big)^2 + 2c^2(N - 1)\Big].Problem 1.
  4. ddcMSE⁡=σ4[2(N−1)(c(N−1)−1)+4c(N−1)]=2(N−1)σ4[c(N−1)−1+2c]=2(N−1)σ4[c(N+1)−1]\dfrac{d}{dc}\operatorname{MSE} = \sigma^4\Big[2(N - 1)\big(c(N - 1) - 1\big) + 4c(N - 1)\Big] = 2(N - 1)\sigma^4\big[c(N - 1) - 1 + 2c\big] = 2(N - 1)\sigma^4\big[c(N + 1) - 1\big].Differentiate the quadratic in cc; collect the terms in cc.
  5. c∗=1N+1c^* = \dfrac1{N + 1}, a minimum since the MSE is a convex quadratic in cc; there bias⁡=−2σ2N+1\operatorname{bias} = -\dfrac{2\sigma^2}{N + 1}, Var⁡=2(N−1)σ4(N+1)2\operatorname{Var} = \dfrac{2(N - 1)\sigma^4}{(N + 1)^2} and MSE⁡=σ4(4+2(N−1))(N+1)2=2σ4N+1\operatorname{MSE} = \dfrac{\sigma^4\big(4 + 2(N - 1)\big)}{(N + 1)^2} = \dfrac{2\sigma^4}{N + 1}.Set step 4 to zero; substitute c∗c^* into step 2 and add.
  6. c=1N−1c = \tfrac1{N - 1}: bias 00, MSE⁡=2σ4N−1\operatorname{MSE} = \dfrac{2\sigma^4}{N - 1}. c=1Nc = \tfrac1N: bias −σ2N-\dfrac{\sigma^2}N, MSE⁡=σ4[1N2+2(N−1)N2]=(2N−1)σ4N2\operatorname{MSE} = \sigma^4\Big[\dfrac1{N^2} + \dfrac{2(N - 1)}{N^2}\Big] = \dfrac{(2N - 1)\sigma^4}{N^2}.Step 3 at the two values; (N−1N−1)2=1N2(\tfrac{N - 1}N - 1)^2 = \tfrac1{N^2}.
  7. N=5N = 5, in units of σ4\sigma^4: S/4S/4 gives 24=0.5\tfrac24 = 0.5; S/5S/5 gives 925=0.36\tfrac9{25} = 0.36; S/6S/6 gives 26≈0.333\tfrac26 \approx 0.333.Steps 5 and 6 with N=5N = 5.
  8. MSE⁡(cS)=σ4[(c(N−1)−1)2+2c2(N−1)]\operatorname{MSE}(cS) = \sigma^4\big[(c(N - 1) - 1)^2 + 2c^2(N - 1)\big], minimised at c∗=1N+1c^* = \dfrac1{N + 1} with MSE⁡=2σ4N+1\operatorname{MSE} = \dfrac{2\sigma^4}{N + 1}; for N=5N = 5: S/(N−1)S/(N - 1) scores 0.5σ40.5\sigma^4, S/NS/N scores 0.36σ40.36\sigma^4, S/(N+1)S/(N + 1) scores 0.333σ40.333\sigma^4The unbiased estimator has the largest error of the three: its lack of bias is bought with variance, and the maximum-likelihood 1/N1/N already does better; the best divisor is N+1N + 1, which nobody uses because the gain over 1/N1/N is small and the argument requires Gaussian data. The MLE page's Problem 5 explains the bias of S/NS/N; this problem shows that bias was not the thing to minimise. The check evaluates E[S]\mathbb{E}[S] and E[S2]\mathbb{E}[S^2] exactly by quadrature for small NN, so the chi-square facts are verified rather than assumed.

Problem 10

Training inputs lie on a grid with spacing hh, f(x)=ax2f(x) = ax^2, and kk-nearest-neighbour regression with k=2m+1k = 2m + 1 uses the mm grid points on each side of a query xx that is itself a grid point. Show that the prediction has variance σ2/k\sigma^2/k and bias ah2k2−112ah^2\dfrac{k^2 - 1}{12}, write the MSE above the noise floor, and find the best kk among {1,3,5,7,9}\{1, 3, 5, 7, 9\} for a=1a = 1, σ2=1\sigma^2 = 1, h=14h = \tfrac14.

  1. f^(x)=1k∑j=−mm(f(x+jh)+εj)\hat f(x) = \dfrac1k\sum_{j=-m}^m\big(f(x + jh) + \varepsilon_j\big), so Var⁡(f^(x))=1k2⋅kσ2=σ2k\operatorname{Var}(\hat f(x)) = \dfrac1{k^2}\cdot k\sigma^2 = \dfrac{\sigma^2}k.The kk neighbours are the grid points x+jhx + jh; their noises are independent with variance σ2\sigma^2, and the average of kk of them has variance σ2/k\sigma^2/k.
  2. E[f^(x)]=1k∑j=−mma(x+jh)2=ax2+2axhk∑jj+ah2k∑jj2=ax2+ah2k∑j=−mmj2\mathbb{E}[\hat f(x)] = \dfrac1k\sum_{j=-m}^ma(x + jh)^2 = ax^2 + \dfrac{2axh}k\sum_jj + \dfrac{ah^2}k\sum_jj^2 = ax^2 + \dfrac{ah^2}k\sum_{j=-m}^mj^2.Expand the square; ∑j=−mmj=0\sum_{j=-m}^mj = 0 by symmetry.
  3. ∑j=−mmj2=2⋅m(m+1)(2m+1)6=m(m+1)k3\sum_{j=-m}^mj^2 = 2\cdot\dfrac{m(m + 1)(2m + 1)}6 = \dfrac{m(m + 1)k}3, so bias⁡=E[f^(x)]−ax2=ah2m(m+1)3\operatorname{bias} = \mathbb{E}[\hat f(x)] - ax^2 = \dfrac{ah^2m(m + 1)}3.The sum of the first mm squares, doubled for the negative side; 2m+1=k2m + 1 = k cancels the 1/k1/k.
  4. m=k−12m = \dfrac{k - 1}2 gives m(m+1)=(k−1)(k+1)4=k2−14m(m + 1) = \dfrac{(k - 1)(k + 1)}4 = \dfrac{k^2 - 1}4, so bias⁡=ah2k2−112\operatorname{bias} = ah^2\dfrac{k^2 - 1}{12}.Substitute and simplify.
  5. MSE⁡−σ2=σ2k+a2h4(k2−1)2144\operatorname{MSE} - \sigma^2 = \dfrac{\sigma^2}k + a^2h^4\dfrac{(k^2 - 1)^2}{144}.Problem 3's bias squared plus variance.
  6. With a=1a = 1, σ2=1\sigma^2 = 1, h=14h = \tfrac14 (h4=1256h^4 = \tfrac1{256}): k=1k = 1: 11; k=3k = 3: 0.3333+6436864=0.33510.3333 + \tfrac{64}{36864} = 0.3351; k=5k = 5: 0.2+57636864=0.21560.2 + \tfrac{576}{36864} = 0.2156; k=7k = 7: 0.1429+230436864=0.20540.1429 + \tfrac{2304}{36864} = 0.2054; k=9k = 9: 0.1111+640036864=0.28470.1111 + \tfrac{6400}{36864} = 0.2847.Step 5 term by term; 144⋅256=36864144\cdot 256 = 36864.
  7. Var⁡=σ2/k\operatorname{Var} = \sigma^2/k, bias⁡=ah2(k2−1)/12\operatorname{bias} = ah^2(k^2 - 1)/12, MSE⁡−σ2=σ2k+a2h4(k2−1)2144\operatorname{MSE} - \sigma^2 = \dfrac{\sigma^2}k + a^2h^4\dfrac{(k^2 - 1)^2}{144}; for a=1a = 1, σ2=1\sigma^2 = 1, h=14h = \tfrac14 the best of {1,3,5,7,9}\{1, 3, 5, 7, 9\} is k=7k = 7, with 0.20540.2054k=1k = 1 has no bias and all the variance; growing kk averages the noise down as 1/k1/k but reaches farther from xx, where the curvature aa of ff makes the neighbours' average drift above f(x)f(x), and that bias grows like k2k^2. The best kk depends on everything: more curvature or coarser data (larger ah2ah^2) wants a smaller kk, and more noise wants a larger one. With h=1h = 1 the same numbers give k=3k = 3, and with h=116h = \tfrac1{16} they give k=19k = 19: denser data allows more smoothing, which is the general rule that the optimal amount of regularisation falls, but does not vanish, as the dataset grows. The bias here is pure curvature, which is why kk-NN is unbiased for linear ff on a symmetric neighbourhood and why the symmetry assumption fails at the edge of the data.

Where this goes wrong

1. Preferring the unbiased estimator by reflex

Unbiased sounds like correct, and an estimator that is biased on purpose sounds like a trick.

  1. MSE⁡(θ^)=bias⁡2+Var⁡\operatorname{MSE}(\hat\theta) = \operatorname{bias}^2 + \operatorname{Var}, and xˉ\bar x has bias 00Right so far: Problems 1 and 2.
  2. “Zero bias means the error is as small as it can be.”The habit that causes the mistake: zero bias sets one term of the sum to zero and says nothing about the other.
  3. MSE⁡(cxˉ)≥MSE⁡(xˉ)\operatorname{MSE}(c\bar x) \ge \operatorname{MSE}(\bar x) for every c≠1c \ne 1For μ=1\mu = 1, σ2=1\sigma^2 = 1, N=4N = 4, c=0.8c = 0.8 gives 0.2<0.250.2 < 0.25 (Problem 2); ridge at λ∗=σ2/w2\lambda^* = \sigma^2/w^2 beats least squares for every ww and ss (Problem 6); and S/(N+1)S/(N + 1) beats the unbiased S/(N−1)S/(N - 1) for every NN (Problem 9). What is true is that among unbiased estimators the one with the smallest variance is best, and that is a different and smaller competition. The reflex has a real basis, though: shrinkage needs to know which way to shrink and how far, and when nothing is known about θ\theta the unbiased estimator is the one that cannot be fooled. Regularisation is exactly the act of putting that knowledge, usually “the true parameters are not huge”, into the estimator.

2. Applying the decomposition to the training error

The three-term formula is about expected squared error, and the error that is to hand is the one on the training set.

  1. E[(y−f^D(x))2]=σ2+bias⁡2+Var⁡\mathbb{E}\big[(y - \hat f_D(x))^2\big] = \sigma^2 + \operatorname{bias}^2 + \operatorname{Var} for a test point (x,y)(x, y)Right so far: Problem 3.
  2. “The training points are points too, so the same formula gives the expected training error.”The habit that causes the mistake: Problem 3's step 3 dropped the cross term because the test noise ε\varepsilon is independent of DD; a training point's noise εn\varepsilon_n is part of DD.
  3. E[(yn−f^D(xn))2]=σ2+bias⁡(xn)2+Var⁡(xn)\mathbb{E}\big[(y_n - \hat f_D(x_n))^2\big] = \sigma^2 + \operatorname{bias}(x_n)^2 + \operatorname{Var}(x_n) for a training point xnx_nThe cross term is −2 E[εn(f^D(xn)−f(xn))]-2\,\mathbb{E}\big[\varepsilon_n(\hat f_D(x_n) - f(x_n))\big], and for Problem 4's least squares, f^D(xn)−f(xn)=xn∑mxmεm/s\hat f_D(x_n) - f(x_n) = x_n\sum_mx_m\varepsilon_m/s, so it equals −2xn2σ2/s<0-2x_n^2\sigma^2/s < 0: the fit leans towards its own noise. Averaged over the NN training points the expected training error is σ2−σ2/N\sigma^2 - \sigma^2/N, below the noise floor that no test error can beat, and the check confirms σ2(N−1)/N\sigma^2(N - 1)/N exactly. The general version is σ2(N−p)/N\sigma^2(N - p)/N for a pp-parameter linear model, the reason the MLE page's σ^2\hat\sigma^2 is biased low, and the reason training error underestimates test error by an amount that grows with the number of parameters.

3. Guessing the ridge variance by analogy

Least squares has variance σ2/s\sigma^2/s, ridge replaces ss by s+λs + \lambda everywhere else, and the variance is written the same way.

  1. w^=∑nxnyn/s\hat w = \sum_nx_ny_n/s has Var⁡(w^)=σ2/s\operatorname{Var}(\hat w) = \sigma^2/sRight so far: Problem 4.
  2. “Ridge just replaces ss by s+λs + \lambda.”The shortcut that causes the mistake: it does in the estimator, and the variance is not linear in the estimator's denominator.
  3. Var⁡(w^λ)=σ2s+λ\operatorname{Var}(\hat w_\lambda) = \dfrac{\sigma^2}{s + \lambda}w^λ=∑nxnyn/(s+λ)\hat w_\lambda = \sum_nx_ny_n/(s + \lambda) is ∑nxnεn/(s+λ)\sum_nx_n\varepsilon_n/(s + \lambda) plus a constant, with variance σ2s/(s+λ)2\sigma^2s/(s + \lambda)^2 (Problem 5): the numerator keeps the ss because the noise enters through ∑nxnεn\sum_nx_n\varepsilon_n, whose variance is σ2s\sigma^2s regardless of λ\lambda. The two agree only at λ=0\lambda = 0; at Problem 6's λ∗=1\lambda^* = 1 the wrong formula gives 0.20.2 where the variance is 0.160.16, and it reports the total MSE at λ∗\lambda^* as 0.240.24 rather than 0.20.2, nearly erasing the gain over least squares. The matrix version has the same shape: Cov⁡(w^λ)=σ2(X⊤X+λI)−1X⊤X(X⊤X+λI)−1\operatorname{Cov}(\hat w_\lambda) = \sigma^2(X^\top X + \lambda I)^{-1}X^\top X(X^\top X + \lambda I)^{-1}, not σ2(X⊤X+λI)−1\sigma^2(X^\top X + \lambda I)^{-1}, the latter being the Bayesian posterior covariance, a different object that answers a different question.

4. Dropping the noise term

Bias and variance are the two things a model can change, and the decomposition is remembered as those two.

  1. ED[(f(x)−f^D(x))2]=bias⁡2+Var⁡\mathbb{E}_D\big[(f(x) - \hat f_D(x))^2\big] = \operatorname{bias}^2 + \operatorname{Var}Right so far: Problem 3, step 4, for the error against the noiseless f(x)f(x).
  2. “Test error is bias squared plus variance.”The shortcut that causes the mistake: that is the error against f(x)f(x); the test error is measured against y=f(x)+εy = f(x) + \varepsilon, and the noise does not go away because it is not the model's fault.
  3. A learner with zero bias and zero variance has expected test error 00It has expected test error σ2\sigma^2, the floor in Problem 3, and so does f^D=f\hat f_D = f itself. The practical damage is a target that cannot be hit: a model whose test error has stopped at σ2\sigma^2 is being pushed to fit noise, and the bias–variance accounting then charges every extra bit of fit to variance. A related reading error is to take Problem 3 as saying the test error is at least σ2\sigma^2 at every point; it is at least σ2\sigma^2 in expectation, and Mistake 2 shows the training error sitting below it because its noise is not independent of the fit.

5. Dividing the variance of correlated models by their number

Averaging BB independent measurements divides the variance by BB, and the BB models in an ensemble are counted as if they were independent.

  1. Var⁡(fˉ)=1B21⊤Σ1\operatorname{Var}(\bar f) = \tfrac1{B^2}\mathbf{1}^\top\Sigma\mathbf{1}Right so far: Problem 8, step 2.
  2. “Each model is its own fit, so the models are independent and Σ=vI\Sigma = vI.”The habit that causes the mistake: models trained on resamples of the same data, with the same features and the same algorithm, make many of the same errors, and Σ\Sigma has off-diagonal entries ρv\rho v with ρ\rho typically well above zero.
  3. Var⁡(fˉ)=vB\operatorname{Var}(\bar f) = \dfrac vB, so a large enough ensemble has negligible varianceIt is ρv+(1−ρ)v/B\rho v + (1 - \rho)v/B (Problem 8), which never falls below ρv\rho v: with ρ=0.5\rho = 0.5, averaging 100100 models leaves 0.505v0.505v, almost the same as averaging 1010 (0.55v0.55v), and the 1/B1/B line predicts 0.01v0.01v. The error compounds if the leftover variance is then blamed on bias and the members made more flexible, which raises vv and often ρ\rho too. The lever that works is ρ\rho: random forests subsample features at every split for exactly this reason, and the variance page's sample mean under common correlation is the same formula with the same floor.

Print this set: bias-variance-decomposition.pdf (problems, answers, and worked solutions on separate pages).