Skip to content

Bias and variance of a model

Refit one model on many training sets from one curve: a too-simple model misses the same way each time, a too-flexible one changes each time.

Helps to know first
This topic

First, the picture

If you collected the data again, would you get the same curve? Below, one curve is measured 20 times over, each time with fresh noise, and a polynomial is fitted to each set of readings. Before the clip plays: which degree will stay closest to the true curve?

Refit on new data, and watch the curves

20 training sets of 12 noisy readings of a curve; polynomial fits up to degree 6.

Degree 1, a straight line, fitted to each of 20 training sets in turn: the same 12 positions, new noise each time.

bias², measured
–
bias², expected
0.2159
variance, measured
–
variance, expected
0.0150
degree
1
0.00 / 23.60 s
Describe this picture

One panel of yy against xx, with xx from −1 to 1 and yy from −2 to 2.5. The true curve is drawn thick: a cubic that rises to a hump, dips and rises again. There are 20 training sets, each with 12 noisy readings of the curve at the same 12 positions. The clip runs three blocks, for degree 1, 3 and 6. In each, the 20 fits appear one after another, each with its own 12 readings shown as dots while it is drawn, and stay as a bundle of thin lines; then their average is drawn dashed. The captions: at degree 1 “every fit is nearly the same line, and all miss the curve the same way”; at degree 3 “the fits follow the curve and stay close together”; at degree 6 “each fit chases its own dots: the bundle spreads, most near the ends”. Readouts, each averaged over the 12 positions: “bias², measured” beside “bias², expected”, “variance, measured” beside “variance, expected”, and the degree. After the clip a slider, “Polynomial degree”, runs from 0 to 6 and starts at 6; the training sets stay the same at every degree, so only the model changes. With reduced motion the bundles for degree 1, 3 and 6 are shown side by side.

One curve, many training sets

The true curve is ytrue(x)=2.5(x3−x)+x2y_\text{true}(x)=2.5(x^3-x)+x^2 on −1≤x≤1-1\le x\le1. I built it from a cubic on purpose, so that you can see a model match it exactly; real curves rarely oblige. It starts at 1 at x=−1x=-1, rises to 1.385 at x=−0.726x=-0.726, falls to −0.695 at x=0.459x=0.459 and returns to 1 at x=1x=1.

Each reading is the curve plus noise: yi=ytrue(xi)+εiy_i=y_\text{true}(x_i)+\varepsilon_i at the positions xix_i, i=1,…,12i=1,\dots,12. The noise εi\varepsilon_i has mean 0 and standard deviation σ=0.3\sigma=0.3, independent from reading to reading. Here σ\sigma is a noise’s standard deviation, as in statistics; in signal processing a bare σ\sigma means something else. The 12 readings, one training set, are m=12m=12 examples.

A polynomial of some degree is fitted to each set by least squares, as in Least squares:

y^(x)=∑r=0degreearxr,\hat y(x)=\sum_{r=0}^{\text{degree}}a_rx^r,

with p=degree+1p=\text{degree}+1 coefficients a0,…a_0,\dots, counted by rr. This is linear regression with the features x,x2,x3,…x, x^2, x^3,\dots; Features and basis functions builds that idea.

One fit tells me little. The point of the picture is the bundle: the same model fitted to 20 training sets that differ only in their noise. Now the answer to the question. At degree 1 the 20 lines are nearly the same, and all miss the curve in the same places. At degree 6 the fits are centred on the curve, but each bends towards its own dots, and the bundle spreads, most near the ends. Degree 3 is both tight and on the curve. Drag the degree from 0 to 6 and watch the bundle tighten onto the curve, then spread.

Bias and variance of a model

Imagine fitting not 20 training sets but every possible one, and averaging the fits at a position xx. That average is the expected fit, E{y^(x)}\mathbb{E}\{\hat y(x)\}, the average over training sets. Two things can go wrong.

The expected fit can miss the curve. That miss is the bias of the model at xx:

bias(x)=E{y^(x)}−ytrue(x).\text{bias}(x)=\mathbb{E}\{\hat y(x)\}-y_\text{true}(x).

The fits can also scatter round their own average. That scatter is the variance of the model at xx:

variance(x)=E{(y^(x)−E{y^(x)})2}.\text{variance}(x)=\mathbb{E}\big\{\big(\hat y(x)-\mathbb{E}\{\hat y(x)\}\big)^2\big\}.

Both describe the bundle, not one fit. Variance here is the spread of the fitted curves, not the noise in the readings. And this bias is a property of a model’s average fit; it is not the bias term bb of a linear model.

In probability and statistics · bias and variance of an estimator

Estimators: bias and variance in the statistics course splits the error of one estimated number into the same two parts. Here the estimate is a whole curve, and each position xx has its own bias and variance.

With only 20 fits, the instrument averages what it has. I write y^ˉS(x)\bar{\hat y}_S(x) for the average of the S=20S=20 fits at xx; the bar marks an average. The readout “bias², measured” is the squared miss of that average, and “variance, measured” the spread of the 20 fits about it, each averaged over the 12 positions. The average of only 20 fits still carries a twentieth of the variance in its squared miss, so the measured bias² sits a little above the true one, and the “expected” readout beside it adds that twentieth: bias2+variance/20\text{bias}^2+\text{variance}/20. At degree 3 the true bias is 0, yet the expected readout shows 0.0015, which is 0.03/200.03/20.

Three parts of the error

What matters is the error on a new reading, y=ytrue(x)+εy=y_\text{true}(x)+\varepsilon, with fresh noise ε\varepsilon of mean 0 and variance σ2\sigma^2, independent of the training sets. Its squared error, averaged over training sets and over the new noise, splits into three parts:

E{(y−y^(x))2}=bias(x)2+variance(x)+σ2.\mathbb{E}\big\{\big(y-\hat y(x)\big)^2\big\}=\text{bias}(x)^2+\text{variance}(x)+\sigma^2.

Here is why. Write y−y^=ε+(ytrue−E{y^})+(E{y^}−y^)y-\hat y=\varepsilon+\big(y_\text{true}-\mathbb{E}\{\hat y\}\big)+\big(\mathbb{E}\{\hat y\}-\hat y\big). Square it and average. The new noise is independent of the fit and has mean 0, so every cross term with ε\varepsilon averages to 0. The last bracket also has mean 0, so its cross term with the fixed middle bracket vanishes too. What is left are the three squares: σ2\sigma^2, the bias squared, and the variance.

The noise σ2\sigma^2 is the floor: no model can predict fresh noise. Bias and variance are the two parts a choice of model can change.

Averaged over the positions

Average each part over the 12 training positions; a bar over a quantity marks that average. Least squares then gives three exact results, when the positions stay fixed and only the noise is redrawn:

average variance=σ2 pm,expected training error=bias2‾+σ2(1−pm),expected error on new readings at the same positions=bias2‾+σ2(1+pm).\begin{aligned} \text{average variance}&=\sigma^2\,\frac{p}{m},\\ \text{expected training error}&=\overline{\text{bias}^2}+\sigma^2\Big(1-\frac{p}{m}\Big),\\ \text{expected error on new readings at the same positions}&=\overline{\text{bias}^2}+\sigma^2\Big(1+\frac{p}{m}\Big). \end{aligned}

Once the bias has gone, each extra coefficient adds σ2/m\sigma^2/m to the variance, the training error falls by the same amount and the error on new readings rises by it. These are the formulas of Overfitting and model choice, now with a bias term for models that cannot reach the curve.

error, averaged over the positions00.20.40.60123456polynomial degreelowest
  • error on new readings
  • training error
  • bias²
  • variance
  • noise σ²
Drawn from the exact formulas for 12 positions and σ = 0.3. The training error falls all the way to degree 6 (0.0375); the error on new readings is lowest at degree 3 (0.12) and rises by 0.0075 with each degree after it. The U holds for these fits: very flexible models can behave differently.
Describe this picture

One panel against the degree, from 0 to 6, drawn from the exact formulas for the 12 positions and σ=0.3\sigma=0.3. Five lines join the points: bias², variance, the noise level 0.09, the error on new readings and the training error. Bias² falls from 0.5624 to 0 at degree 3 and stays there; variance climbs by 0.0075 per degree, from 0.0075 to 0.0525; the training error falls all the way to degree 6, to 0.0375. The error on new readings falls, then rises, in a U, and a ring marks its lowest point, 0.1200 at degree 3. The caption says the U holds for these fits: very flexible models can behave differently. There is no control.

The U-shaped curve of Overfitting and model choice is the sum of these parts. On the left, bias dominates: the model is too simple. On the right, variance dominates: the model is flexible enough to follow the noise. In between, at degree 3, bias has reached 0 while variance is still small.

Watch out

The model with the lowest training error is not the best. Here degree 6 has the lowest training error and a higher error on new readings than degree 3.

More data

More readings shrink one part and not the other. With 120 positions instead of 12, σ2p/m\sigma^2p/m is ten times smaller at every degree. Bias does not change in that way: a straight line cannot follow a cubic however many readings it gets.

Ten times the readings: the degree-6 bundle tightens (variance 0.0605 at 12 readings, 0.0060 at 120), while the degree-1 bundle, tighter still, misses the curve just as much (bias² 0.2156 and 0.2316). More data shrinks variance, not bias.
Describe this picture

Four small panels of yy against xx from −1 to 1, each with the true curve drawn thick, 20 thin fits and their dashed average. The top row is degree 1, the bottom row degree 6; in each row the left panel has 12 readings per training set (the sets of the clip) and the right panel 120. On a narrow screen the panels stack one per row. Above each panel are its measured bias² and variance. At degree 6 the bundle spreads widely at 12 readings, most near the ends, and lies close together on the curve at 120: its variance falls about tenfold, to 0.0060. At degree 1 both bundles are nearly the same straight line, tighter still, and both miss the curve the same way: bias² 0.2156 at 12 readings and 0.2316 at 120. There is no control.

So “enough data” cures variance, never bias. And whether a model “overfits” is not a yes-or-no question: it depends on how much data it has, and by how much its error on new readings exceeds the best.

Worked example

Take the 12 positions xi=(2i−13)/12x_i=(2i-13)/12: ±112,±312,…,±1112\pm\tfrac1{12},\pm\tfrac3{12},\dots,\pm\tfrac{11}{12}, with σ=0.3\sigma=0.3, so σ2=0.09\sigma^2=0.09.

The sums. Over the 12 positions, ∑xi2=3.972222\sum x_i^2=3.972222, ∑xi4=2.344715\sum x_i^4=2.344715 and ∑xi6=1.632297\sum x_i^6=1.632297.

Odd and even parts. The curve splits into an odd part, 2.5(x3−x)2.5(x^3-x), and an even part, x2x^2. On positions symmetric about 0, least squares fits the two separately: odd powers fit the odd part and even powers the even part.

  • The odd part left unfitted (degree 0) leaves ∑(xi3−xi)2=∑xi6−2∑xi4+∑xi2=0.915089\sum(x_i^3-x_i)^2=\sum x_i^6-2\sum x_i^4+\sum x_i^2=0.915089. After the best slope (degrees 1 and 2) it leaves ∑xi6−(∑xi4)2/∑xi2=0.248263\sum x_i^6-(\sum x_i^4)^2/\sum x_i^2=0.248263. From degree 3 it leaves 0.
  • The even part, after the best constant (degrees 0 and 1), leaves ∑xi4−(∑xi2)2/12=1.029836\sum x_i^4-(\sum x_i^2)^2/12=1.029836. From degree 2 it leaves 0.

Bias². Average over the 12 positions, with 2.52=6.252.5^2=6.25 on the odd part:

  • degree 0: (6.25×0.915089+1.029836)/12=0.5624(6.25\times0.915089+1.029836)/12=0.5624;
  • degree 1: (6.25×0.248263+1.029836)/12=0.2151(6.25\times0.248263+1.029836)/12=0.2151;
  • degree 2: 6.25×0.248263/12=0.12936.25\times0.248263/12=0.1293;
  • degrees 3 to 6: 0.

The table. Variance is 0.09p/120.09p/12, training error bias2‾+0.09(1−p/12)\overline{\text{bias}^2}+0.09(1-p/12) and error on new readings bias2‾+0.09(1+p/12)\overline{\text{bias}^2}+0.09(1+p/12):

DegreeCoefficientsBias²VarianceTraining errorError on new readings
010.56240.00750.64490.6599
120.21510.01500.29010.3201
230.12930.02250.19680.2418
3400.03000.06000.1200
4500.03750.05250.1275
5600.04500.04500.1350
6700.05250.03750.1425

The training error falls at every degree. The error on new readings is lowest at degree 3, 0.12, and each degree after it adds 0.09/12=0.00750.09/12=0.0075, one more coefficient’s share of the noise. Degree 6 is 0.0225 worse than degree 3. Is it overfitting? It is worse by a measurable amount, which is a better answer than yes or no.

At 120 positions, xi=(2i−121)/120x_i=(2i-121)/120, the variance falls tenfold: degree 6’s is 0.09×7/120=0.005250.09\times7/120=0.00525. Bias² does not fall: degree 1’s is 0.2316 and degree 2’s 0.1427, a little above their values at 12 positions and close to their values over the whole interval, 1/7+4/45=0.23171/7+4/45=0.2317 and 1/7=0.14291/7=0.1429, which more positions approach. More data cannot remove bias.

The floor. No model does better on new readings than σ2=0.09\sigma^2=0.09, the noise itself.

Where you’ll meet this

Calibrating a sensor. A thermocouple’s voltage is turned into a temperature by a polynomial. Too low a degree misses the true curve everywhere; too high a degree follows the noise in the calibration readings.

Nearest neighbours. In Nearest neighbours, k=1k=1 is the high-variance end: each stray training dot moves the answer. A large kk averages many dots and moves towards bias.

Choosing a setting. On real data the true curve is unknown, so bias cannot be measured. Validation and cross-validation estimates the error on new readings directly, Learning curves shows which part more data would help, and Ridge regression trades a little bias for less variance.

In signal processing · choosing a model order

Parametric models and linear prediction in the signal processing course chooses how many past samples to predict from. Too few coefficients miss the signal’s shape; too many fit the noise of one recording. It is the same trade.

Reference card

QuantityFormulaNotes
Biasbias(x)=E{y^(x)}−ytrue(x)\text{bias}(x)=\mathbb{E}\{\hat y(x)\}-y_\text{true}(x)expectation over training sets
VarianceE{(y^(x)−E{y^(x)})2}\mathbb{E}\{(\hat y(x)-\mathbb{E}\{\hat y(x)\})^2\}the spread of the fits, not of the noise
Error on a new readingbias(x)2+variance(x)+σ2\text{bias}(x)^2+\text{variance}(x)+\sigma^2at the same positions; noise independent of the training sets
Average varianceσ2p/m\sigma^2p/mpp coefficients, mm fixed positions
Training errorbias2‾+σ2(1−p/m)\overline{\text{bias}^2}+\sigma^2(1-p/m)falls as the degree rises
New readingsbias2‾+σ2(1+p/m)\overline{\text{bias}^2}+\sigma^2(1+p/m)at the same positions; lowest where bias has gone and pp is small
More datavariance falls, bias stays
Floorσ2\sigma^2no model predicts fresh noise

Keep reading with a free account.

By creating an account you accept the terms of use.

Sign in

Already have an account? Use the service you signed up with.

Or keep reading: get new lessons by email

Are you 13 or over?

Accounts and email updates are for people aged 13 or over. We keep only your answer, never your date of birth.

End of the lesson

Where to go next.

Phasorium

Account

Sign inKeep your place on any device

Subjects

Site

Look