Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 3: Classical Machine LearningCore Concepts

The bias–variance trade-off

P3.A540 min●●○ IntermediateReviewed Dec 2, 2026

Why this matters for machine learning

Every model choice that sets how flexible a model is (the degree of a polynomial, the number of neighbours, the strength of a penalty) moves two kinds of error in opposite directions. Bias and variance name them, so you can tell why a model is failing and which fix to try.

Before this: Expectation and variance, Polynomial regression, underfitting and overfitting, Train, validation and test sets

After this lesson you can

  • Describe a fitted prediction at a fixed input as a random variable (it changes with the training set), and define bias, variance and irreducible noise from that picture
  • Derive, step by step, that the expected squared error at a point equals bias squared plus variance plus noise, and say which assumption (independent test noise) makes the cross term vanish
  • Explain why bias falls and variance rises as a model gets more flexible, and read the U-shaped total error and its lowest point from a table
  • Predict the effect of more data, a stronger penalty and averaging on bias and variance, and say what the decomposition does not give you (it cannot be computed from one data set, and it is not this clean for classification)

You fit a curve to 15 noisy points. A friend collects another 15 points from the same source and fits the same kind of curve. The two curves are not the same, because the two sets of points are not the same. Which curve do you trust? Neither exactly: each carries the accidents of its own 15 points. This lesson measures how much.

A very simple model gives nearly the same curve for both of you, but that shared curve can be wrong in the same way every time. A very flexible model follows each set of points closely, so your curve and your friend’s differ a lot, and each carries the noise of its own points. The first problem is called bias. The second is called variance. P3.A3 showed both failures on one data set (underfitting and overfitting). This lesson puts a number on each and shows how they add up.

A fitted prediction is a random variable

Fix one input x0x_0. Suppose the world works like this.

  • There is a true function ff that we do not know.
  • A target is the true value plus noise: y=f(x)+εy=f(x)+\varepsilon. The noise ε\varepsilon has mean 00 and variance σ2\sigma^2, and it is new every time a target is produced.
  • A training set D\mathcal{D} is a random draw of examples from this world. The model is fitted to it and gives a prediction f^D(x0)\hat f_{\mathcal{D}}(x_0).

In the simulations below only the noise is redrawn and the inputs stay fixed; the algebra is the same when the inputs are drawn too.

Draw another training set and the fitted prediction at x0x_0 changes. So f^(x0)\hat f(x_0) is a random variable (P1.D2), and its randomness comes from the choice of training set. Two numbers describe it, using the mean and variance of P1.D4.

  • The bias is where it is centred, compared with the truth: bias=E[f^(x0)]−f(x0)\mathrm{bias}=\mathbb{E}[\hat f(x_0)]-f(x_0). A model with zero bias is right on average over training sets.
  • The variance is how much it moves from one training set to the next: Var[f^(x0)]=E[(f^(x0)−E[f^(x0)])2]\mathrm{Var}[\hat f(x_0)]=\mathbb{E}\bigl[(\hat f(x_0)-\mathbb{E}[\hat f(x_0)])^2\bigr].

The third piece is not about the model at all: the target at x0x_0 has its own noise, with variance σ2\sigma^2.

All the expectations here are over the random training set (and over the noise of a fresh target). You cannot see them in a single experiment, which is why the next section draws many training sets. By the law of large numbers (P1.D8), averages over many training sets come close to these expectations.

See it: many fits from resampled data

The widget below runs the thought experiment. The true curve is made up: f(x)=sin⁡(2.4πx+0.5)f(x)=\sin(2.4\pi x+0.5). Each training set has nn points at fixed, equally spaced inputs, with fresh noise on the targets. The widget fits a polynomial of the chosen degree to each of NN training sets and draws all NN fitted curves on top of each other.

00.20.40.60.81−2−1012xy
true curve f one fit per training set mean of the fits f(x₀) mean fit at x₀ each fit at x₀
Points per set n:
At x₀ = 0.5: true f(x₀) = −0.904; mean of the 30 fits = −0.542.
from the 30 setsexact
At the chosen x₀
bias²0.1310.137
variance0.0130.014
noise σ²0.090.09
sum0.2330.241
Averaged over x (101 values from 0 to 1)
bias²0.0980.097
variance0.0220.025
noise σ²0.090.09
sum = expected test MSE0.210.212

Averaged over x, exact values for each degree (the limit of very many training sets)

00.20.40.60123456789degree
bias² variance bias² + variance + noise noise σ²

Lowest total here: degree 4, at 0.129.

The true curve is made up: f(x) = sin(2.4πx + 0.5). The 15 training inputs are fixed and equally spaced; each training set has fresh random noise. The first column is measured from the 30 sets drawn (bias² of their mean fit, variance of their fits); the second is the exact value that very many sets would give.

The true curve (black), one faint blue fit per training set, and the mean of the fits (dashed pink). At the marker x₀: the black ring is f(x₀), the pink square is the mean fit, the blue dots are the individual fits, and the pink bar is the bias. The table gives bias², variance and noise at x₀ and averaged over x, from the N sets and exact. The chart shows the exact values against the degree. Controls: degree, training sets, noise, x₀ (drag, arrow keys or slider), points per set, new draw.

Things to try.

  • Degree 0 to 1. All the faint curves lie almost on top of each other, so the variance is tiny. But their mean is far from the true curve, so the bias is large: the model cannot bend. More training sets do not help.
  • Degree 9. The faint curves now fan out, especially near the ends, so the variance is large. Their mean is close to the true curve, so the exact bias is almost zero (the measured value shrinks towards it as NN grows). Each single fit chases its own noise.
  • Move x0x_0. The bias and the variance both depend on where you look. At degree 3 the exact bias² is about 0.140.14 at x0=0.5x_0=0.5 and 0.170.17 at x0=0.8x_0=0.8, but only about 0.0010.001 at x0=0.35x_0=0.35, where the mean fit happens to cross the true curve. The variance is also largest near the two ends: about 0.100.10 at x0=0x_0=0 and x0=1x_0=1, against 0.0140.014 at x0=0.5x_0=0.5.
  • Slide the number of training sets. With N=1N=1 the “variance” is zero, because there is only one fit. The values in the first column of the table get steadier as NN grows and approach the exact column. With few sets the measured bias² is too big by about variance/NN, because the mean of a few fits is itself noisy, and the measured variance is a little too small. At N=1N=1 the whole error of the one fit shows up as “bias²”. (The exact column is a formula for what very many sets would give; it does not depend on the sets drawn.)
  • Raise the noise σ. The variance grows with σ2\sigma^2. Compare σ=0.1\sigma=0.1 (best degree 6) with σ=0.3\sigma=0.3 (best degree 4) at n=15n=15: more noise favours simpler models. At n=60n=60 the best degree is 6 for σ\sigma up to 0.30.3, 5 at σ=0.4\sigma=0.4 and 4 from σ=0.5\sigma=0.5.
  • Points per set. Going from n=15n=15 to n=60n=60 at degree 5 shrinks the fan of curves. The bias does not change.

The decomposition

For the squared error at the fixed input x0x_0, with a fresh target y=f(x0)+εy=f(x_0)+\varepsilon (in the formulas, ff and f^\hat f stand for f(x0)f(x_0) and f^(x0)\hat f(x_0)):

E[(y−f^)2]=bias2+variance+σ2,bias=E[f^]−f,variance=Var[f^].\begin{aligned} \mathbb{E}\bigl[(y-\hat f)^2\bigr]&=\text{bias}^2\\ &\quad+\text{variance}+\sigma^2,\\ \text{bias}&=\mathbb{E}[\hat f]-f,\\ \text{variance}&=\mathrm{Var}[\hat f] . \end{aligned}

The expectation is over the training set and over the noise in the fresh target. Here is why it holds, one step at a time.

Step 1: split off the noise. Since y=f+εy=f+\varepsilon, we have y−f^=ε+(f−f^)y-\hat f=\varepsilon+(f-\hat f). Squaring gives

(y−f^)2=ε2+2ε (f−f^)+(f−f^)2.\begin{aligned} (y-\hat f)^2&=\varepsilon^2+2\varepsilon\,(f-\hat f)\\ &\quad+(f-\hat f)^2 . \end{aligned}

Step 2: take the expectation of each term. The first term gives E[ε2]=σ2\mathbb{E}[\varepsilon^2]=\sigma^2, because ε\varepsilon has mean 00. For the middle term we assume that the noise ε\varepsilon of the fresh target is independent of the training set, and hence of f^\hat f. Then E[ε(f−f^)]=E[ε]  E[f−f^]=0\mathbb{E}[\varepsilon(f-\hat f)]=\mathbb{E}[\varepsilon]\;\mathbb{E}[f-\hat f]=0. So

E[(y−f^)2]=σ2+E[(f^−f)2].\mathbb{E}[(y-\hat f)^2]=\sigma^2+\mathbb{E}[(\hat f-f)^2] .

Step 3: split the model’s own error. The remaining term is the expected squared distance of a random variable f^\hat f from the constant ff. P1.D4 showed that for any random variable XX and constant cc, E[(X−c)2]=Var(X)+(E[X]−c)2\mathbb{E}[(X-c)^2]=\mathrm{Var}(X)+(\mathbb{E}[X]-c)^2. With X=f^X=\hat f and c=fc=f:

E[(f^−f)2]=Var[f^]+(E[f^]−f)2.\mathbb{E}[(\hat f-f)^2]=\mathrm{Var}[\hat f]+\bigl(\mathbb{E}[\hat f]-f\bigr)^2 .

Together, steps 2 and 3 give bias² + variance + σ². ■\blacksquare

The independence in Step 2 is a real assumption. It holds when the fresh target is a new example that was not in the training set. If you measure the error on the training examples themselves, the noise in yy also went into f^\hat f, the cross term is not zero, and the formula no longer holds. That is another reason to measure error on held-out data (P3.A4).

From one input to the whole test set. A test example has an input xx as well as noise. If the test inputs are drawn from some distribution, take the average of the formula over xx, and call the left side MSEtest\mathrm{MSE}_{\text{test}}, the expected test MSE:

MSEtest=Ex[bias2(x)]+Ex[variance(x)]+σ2,\begin{aligned} \mathrm{MSE}_{\text{test}}&=\mathbb{E}_x\bigl[\text{bias}^2(x)\bigr]\\ &\quad+\mathbb{E}_x\bigl[\text{variance}(x)\bigr]+\sigma^2 , \end{aligned}

with a constant noise level σ2\sigma^2 (if the noise level depends on xx, use its average). The “averaged over x” rows of the widget take the plain average over 101 equally spaced values of xx between 00 and 11, which is the case where the test inputs are uniform on [0,1][0,1].

A small example you can do by hand

At one input, suppose the true value is f(x0)=2f(x_0)=2. Over the possible training sets, the fitted value is 1.01.0 with probability 0.250.25, 1.41.4 with probability 0.50.5 and 1.81.8 with probability 0.250.25. The noise is ±0.5\pm0.5 with equal probability, so σ2=0.25\sigma^2=0.25. (These numbers are made up to keep the arithmetic short.)

  • The mean fit is 0.25⋅1.0+0.5⋅1.4+0.25⋅1.8=1.40.25\cdot1.0+0.5\cdot1.4+0.25\cdot1.8=1.4, so the bias is 1.4−2=−0.61.4-2=-0.6 and bias2=0.36\text{bias}^2=0.36.
  • The variance is 0.25⋅(0.4)2+0.5⋅0+0.25⋅(0.4)2=0.080.25\cdot(0.4)^2+0.5\cdot0+0.25\cdot(0.4)^2=0.08.
  • The total is 0.36+0.08+0.25=0.690.36+0.08+0.25=0.69.

Check it the long way: list all six combinations of fit and noise, square y−f^y-\hat f for each, and weigh by probability. Running the code confirms that the average is 0.690.69. The two pieces you can control (bias² and variance) add up to 0.440.44 of the 0.690.69.

Bias falls, variance rises: the U

Now fix the setting of the widget (n=15n=15 points per set, σ=0.3\sigma=0.3 so the noise is σ2=0.09\sigma^2=0.09) and change only the degree. Here are the exact averages over xx.

degreebias²variancetotal
00.5190.0060.615
10.5040.0120.606
20.1580.0190.267
30.0970.0250.212
40.0060.0330.129
50.0020.0420.134
6under 0.0010.0540.144
7under 0.0010.0740.164
8under 0.0010.1180.208
9under 0.0010.2500.340

(“Total” is bias² + variance + σ².) Read the two columns separately. Bias² falls from 0.5190.519 to almost zero, because a more flexible family can come closer to the true curve. Variance rises from 0.0060.006 to 0.2500.250, because the extra freedom is used to follow the noise of each training set. The total is the sum plus the constant 0.090.09, so it falls while bias is dropping faster than variance rises, and then climbs once variance takes over. Its lowest point here is degree 44, with 0.1290.129.

The exact numbers belong to this made-up setting. The shape does not: bias falling and variance rising with flexibility is the usual pattern, and it is why test error against flexibility is often U-shaped (P3.A3 showed the U on one data set). It is a tendency, not a law. The degree where the total is lowest depends on the true curve, the noise and the number of points.

Two details are worth seeing in the table. Degrees 0 and 1 have almost the same bias here: the true curve goes up, down and up again, so a tilted line does hardly better than a flat one. And the variance climbs steeply at the end. With only n=10n=10 points and degree 99, the polynomial has 10 free numbers, so it passes exactly through every training point (it interpolates them), and in the widget (click n=10n=10 and set the degree to 9; the table above is for n=15n=15) the exact average variance is 21.521.5, about 240 times the noise level σ2=0.09\sigma^2=0.09.

The three pieces, one at a time

Noise, σ2\sigma^2, is a floor. Take a model with zero bias and zero variance: it predicts f(x)f(x) exactly. Its expected squared error on a new target is still σ2\sigma^2, because the target itself is f(x)f(x) plus noise that nobody could have predicted. No model, however good and however much data, gets below it for squared error. This is called irreducible error. In the widget’s chart it is the dashed line that the total never crosses. It also tells you when to stop: if your validation error is already close to a plausible noise level, a bigger model cannot help.

More data lowers variance and leaves bias alone. Variance comes from the fit following the noise of a particular training set, and with more points each point matters less. Bias comes from the family of curves not being able to match the truth, and more points do not change what the family can do. A simulation at degree 5 with 2000 training sets per row (the code below) gives:

nnbias²variance
100.00350.0736
150.00240.0415
300.00210.0194
600.00210.0095

Doubling nn roughly halves the variance. For a fit by least squares with pp parameters, that is a known result for fixed inputs and independent noise: the variance, averaged over the training inputs, is exactly σ2p/n\sigma^2p/n (see the Go-deeper box below). This is why “get more data” is the standard cure for overfitting, and why it does little for underfitting.

A penalty or an average lowers variance. Two ways of reducing variance without simplifying the family are previewed here and taught later.

  • Regularization (P3.A6) adds a penalty on large weights. It pulls the fit towards smoother curves, so it moves less from set to set, at the price of some bias. The code below does this for the degree-9 fit with a ridge penalty of strength λ\lambda (here the penalty is λ\lambda times the sum of squared coefficients of the same Chebyshev basis, intercept included, added to the sum of squared errors, so these λ\lambda values only make sense in this setup; P3.A6 sets this up properly; the numbers are simulated from 2000 sets, so the last digit can differ from the exact values): the variance drops from 0.2460.246 to 0.0390.039 at λ=1\lambda=1 while bias² rises from 0.00010.0001 to 0.0170.017, so their sum falls from 0.2460.246 to 0.0560.056. A penalty that is too strong (λ=10\lambda=10) adds so much bias that the sum rises again to 0.1710.171.
  • Averaging (bagging, P3.B4). The widget’s pink curve, the mean of many fits, has far less spread than any one fit. If you could average BB models whose errors were independent, each with variance vv, the average would have variance v/Bv/B and the same bias as one of them. You usually have only one training set, so bagging builds the different models from resampled copies of it. Those models are correlated, so the saving is smaller than v/Bv/B. (If each model has variance vv and any two have correlation ρ\rho, the average has variance ρv+(1−ρ)v/B\rho v+(1-\rho)v/B.)
Go deeper Why the variance of a least-squares fit is σ²p/n on average

For a linear least-squares fit with fixed inputs, the fitted values at the training inputs are f^=Hy\hat{\mathbf{f}}=\mathbf{H}\mathbf{y}, where H\mathbf{H} is a fixed n×nn\times n matrix (the “hat” matrix, which projects y\mathbf{y} onto the span of the pp columns of the design matrix). Only y\mathbf{y} is random, with independent noise of variance σ2\sigma^2, so Var[f^(xi)]=σ2∑jHij2=σ2Hii\mathrm{Var}[\hat f(x_i)]=\sigma^2\sum_j H_{ij}^2=\sigma^2H_{ii} (using H2=H\mathbf{H}^2=\mathbf{H} and H⊤=H\mathbf{H}^\top=\mathbf{H}). The diagonal entries add up to the trace, which equals the rank pp. So the variance averaged over the nn training inputs is σ2p/n\sigma^2p/n. This needs fixed inputs and independent noise of equal variance σ2\sigma^2. It holds even if the model is wrong; a wrong model only adds bias. A polynomial of degree dd has p=d+1p=d+1. For n=15n=15 and σ2=0.09\sigma^2=0.09, a degree-3 fit has 0.09⋅4/15=0.0240.09\cdot4/15=0.024, and a degree-9 fit has 0.09⋅10/15=0.060.09\cdot10/15=0.06. Running the code confirms these.

This is the average over the training inputs only. At other inputs, especially near the ends of the range, the variance can be much larger, which is why the widget’s average over all of [0,1][0,1] gives 0.0250.025 and 0.2500.250 for these two degrees, not 0.0240.024 and 0.060.06. The formula also says what happens near p=np=n: the model has as many knobs as data points, and the variance averaged over the training inputs reaches σ2\sigma^2.

You cannot compute this from one data set

To get bias and variance you need the true function ff (to subtract it) and the ability to draw many training sets (to average over them). In real problems you have neither: one data set, and an unknown truth. The widget and the code in this lesson work because we made up ff and can simulate as many data sets as we like. In practice you do not measure bias and variance. You measure the total, on data that the model has not seen (P3.A4), and you use the picture to reason about what to try: a large gap between training and validation error suggests variance (more data, a simpler model or a penalty); a high error on both suggests bias (a more flexible model or better features). These are reasonable rules of thumb, not proofs.

Try it: simulate many training sets and check the decomposition

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

The column “sum” is bias² + variance + σ2\sigma^2, computed from the 40004000 fits. The column “test” is the squared error measured directly against fresh noisy targets. The two agree to within about 0.010.01 at each degree, as the decomposition says they should (they would match more closely with more training sets). In this run the average over xx confirms the table: bias² is 0.5040.504 at degree 1 and variance is 0.2500.250 at degree 9, as in the exact values.

Try it: more data, a penalty, and p/n

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

Where the clean picture stops

Classification. The decomposition above is an identity for squared error. For a classifier scored by the 0–1 loss (right or wrong), there is no single standard way to split the error into bias, variance and noise that keeps the clean “add up” form. Several different decompositions have been proposed, and they are not equivalent, so you will see different definitions in different books. The intuition carries over: a stiff classifier is stable but can be systematically wrong, a very flexible one changes with the training set. But do not expect the formula bias2+variance+σ2\text{bias}^2+\text{variance}+\sigma^2 to hold for classification accuracy. When a classifier outputs probabilities, scored by squared error on the probability (the Brier score), the squared-error argument above applies to those numbers (with a noise term that depends on the input).

Very large models. The U-shaped picture comes from models whose flexibility grows slowly and is tuned to the data. In modern machine learning, networks with far more parameters than training examples have been observed to fit the training data exactly and still do well on new data, and test error plotted against model size has been seen to rise and then fall again, a pattern called double descent. This has been reported in several kinds of models. It does not contradict the decomposition (the identity above still holds for squared error); it shows that variance does not have to keep rising with the number of parameters. The details are still an active area of research, so this lesson only names it; P4.24 shows it with a small experiment. P3.A3 and P3.A4 give the practical advice that still holds: judge a model on data it has not seen.

Common misunderstandings

Intuition

Think of many archers. Each learns from their own random set of practice shots, then shoots once. Bias is how far the centre of all the final arrows is from the bullseye. Variance is how spread out the arrows are. Noise is a bullseye that jitters by a random amount each round, so even an archer who aims at its average position misses by that jitter. The analogy breaks because an archer’s “shot” here is a whole fitted curve.

Math

At a fixed x0x_0, with y=f(x0)+εy=f(x_0)+\varepsilon, Eε=0\mathbb{E}\varepsilon=0, Var ε=σ2\mathrm{Var}\,\varepsilon=\sigma^2, and ε\varepsilon independent of the training set:

E[(y−f^)2]=(Ef^−f)2+Var f^+σ2.\begin{aligned} \mathbb{E}\bigl[(y-\hat f)^2\bigr]={}&\bigl(\mathbb{E}\hat f-f\bigr)^2\\ &+\mathrm{Var}\,\hat f+\sigma^2 . \end{aligned}

Average over xx for the expected test MSE. For least squares with pp parameters and fixed inputs, the variance averaged over the training inputs is σ2p/n\sigma^2p/n. For BB models with variance vv and pairwise correlation ρ\rho, the average has variance ρv+(1−ρ)v/B\rho v+(1-\rho)v/B.

Code

import numpy as np
# the whole experiment: draw many training sets, fit each, look at the fits at x0
fits = np.array([fit(draw_training_set())(x0) for _ in range(S)])  # pseudo-code
bias2 = (fits.mean() - f(x0)) ** 2        # needs the true f: only possible in a simulation
variance = fits.var()                      # spread of the fits
expected_mse = bias2 + variance + sigma**2

The cells above are the runnable version.

Check your understanding

  1. At one input, a model has bias² equal to 0.040.04 and variance equal to 0.090.09, and the noise variance is σ2=0.25\sigma^2=0.25. What is the expected squared error on a new target there?

  2. A degree-5 polynomial fits well on average but changes a lot from one training set to the next. Which change lowers its variance without raising its bias?

  3. Over thousands of training sets, a model's prediction at x0x_0 averages to exactly the true value f(x0)f(x_0), but individual predictions are often far above or below it. How would you describe the model at x0x_0?

  4. A model has zero bias and zero variance at every input, so its prediction is exactly f(x)f(x). For targets y=f(x)+εy=f(x)+\varepsilon with noise variance σ2\sigma^2, what is its expected squared error on new targets?

  5. Which statement about the decomposition is correct?

Exercises

  1. At one input the true value is 1.51.5. The fitted value is 00 or 22, each with probability 12\tfrac12 over training sets, and the noise variance is σ2=0.16\sigma^2=0.16. Find the bias, the variance and the expected squared error.
    Show answerThe mean fit is 11, so the bias is 1−1.5=−0.51-1.5=-0.5 and bias² is 0.250.25. The variance is 12(0−1)2+12(2−1)2=1\tfrac12(0-1)^2+\tfrac12(2-1)^2=1. The expected squared error is 0.25+1+0.16=1.410.25+1+0.16=1.41.
  2. Using the table of this lesson (n = 15, σ = 0.3), which degree has the lowest total? Why is the degree with the lowest bias² (degrees 7 to 9) not the best?
    Show answerDegree 4, with total 0.1290.129. From degree 5 on, bias² is already below 0.0030.003, so there is almost nothing left to gain, while the variance keeps rising (0.0330.033 at degree 4, 0.0420.042 at degree 5, 0.2500.250 at degree 9). The total grows with it.
  3. Four models each have variance 0.160.16. What is the variance of their average if their errors are independent? What if each pair has correlation 0.50.5?
    Show answerIndependent: 0.16/4=0.040.16/4=0.04. With ρ=0.5\rho=0.5: 0.5⋅0.16+0.5⋅0.16/4=0.08+0.02=0.100.5\cdot0.16+0.5\cdot0.16/4=0.08+0.02=0.10. Correlated models share their errors, so averaging helps much less.
  4. In the widget, set σ=0.1\sigma=0.1 and compare with σ=0.3\sigma=0.3. Does the degree with the lowest total move up or down? Explain.
    Show answerIt moves up: the exact totals at σ=0.1\sigma=0.1 are lowest at degree 6 (0.0160.016, against 0.0200.020 at degree 4), while at σ=0.3\sigma=0.3 it is degree 4. Variance is proportional to σ2\sigma^2, so with less noise a flexible model pays a smaller variance price for each extra degree, and the bias savings are worth more. The best degree rises as the noise falls.
  5. A student says: “My model has training error 0.010.01 and test error 0.300.30, so its bias is large.” Which of bias or variance does that pattern suggest more, and what would you try?
    Show answerA big gap between a tiny training error and a much larger test error suggests variance (overfitting). Reasonable things to try are more data, a simpler model or a penalty (P3.A6). This is a rule of thumb: it does not prove the diagnosis.
  6. Code: in the second cell, change the degree from 55 to 11 in the “more data” loop. Does the bias² change with nn?
    Show answerIt stays near 0.5040.504 for every nn, because a straight line cannot follow this curve however many points it sees. The variance falls roughly in proportion to 1/n1/n.

Next: P3.A6 takes the regularization idea previewed here and shows how an L2 or L1 penalty shapes the fit, and P3.B4 builds bagging and random forests on the averaging idea.

Key takeaways

  • A fitted model is a random thing: a new training set gives a new fit. At a fixed input x₀ the prediction f̂(x₀) is a random variable. Its mean minus the true value is the bias, its spread is the variance, and the target also carries noise ε with variance σ² that no model can predict.
  • For squared error, E[(y − f̂(x₀))²] = bias² + variance + σ², where bias = E[f̂(x₀)] − f(x₀). The cross terms vanish because the test noise is independent of the training set and has mean 0. Averaged over inputs, the left side is the expected test MSE.
  • More flexible models have lower bias and higher variance, so the total is U-shaped. In the made-up example of this lesson (n = 15, σ = 0.3) the total is lowest at degree 4: 0.129, against 0.615 for a constant and 0.340 for degree 9.
  • σ² is a floor: even the true curve cannot beat it. More data lowers variance (roughly in proportion to 1/n for a fixed model) but leaves bias alone. A penalty or averaging many models lowers variance and costs a little bias, or none.
  • The decomposition describes an average over training sets that you do not have. In practice you estimate the total test error with held-out data (P3.A4), not the pieces. For classification with 0–1 loss the split is not this clean, and very large models have been observed to have test error that falls again after fitting the training data exactly.
Found a mistake? Tell us