Machine learningLoading progress…

Bias and variance

Compare model flexibility, regularization and validation procedures without using the test set for tuning.

Ask what would happen with a different sample

Your training set is one random sample. Had you collected a different 15 points from the same source, you would have trained a slightly different model. Two things can then go wrong, and they are different:

  • Bias is systematic error: the average of all those different models is still off target. A straight line fitted to a wave is wrong in the same way on every sample.
  • Variance is sensitivity to the sample: the models disagree with each other. A very flexible curve wiggles to wherever this particular sample's noise pushed it.

Together with the noise in the data itself (which no model can predict), they add up: expected squared error = bias² + variance + noise. In the demo we can measure all three, because we know the true curve and can draw as many training sets as we like. In real projects you only have one sample, so you can only reason about this split. Here you can watch it.

Refit the model many times

-1-0.500.51-1.5-1-0.500.511.5red: average of the fitsdashed: the true curveblue: individual fits
squared bias0.0176
variance0.0144
noise (cannot be removed)0.0625
bias + variance + noise0.0945

Squared bias is how far the red average sits from the dashed truth. Variance is how far the blue curves wander from the red one. Both are averages over x between -1 and 1 and over the 12 training sets.

degree12345679
squared bias0.260.1390.0180.0050.0020.0040.0050.086
variance0.0080.010.0140.0220.0350.0540.120.384
total0.3310.2110.0950.090.0990.120.1880.533
Each faint blue curve is a fit to a fresh training set. The red curve is their average, the dashed curve is the truth. Raise the number of training sets and change the degree.

Read the picture in two steps. First look at the red average: how far is it from the dashed truth? That distance, squared and averaged, is the bias². Then look at how far the blue curves stray from the red one: that spread is the variance. At degree 1 the blue curves are tightly bunched but all in the wrong place. At degree 9 their average is close to the truth but individual fits fly off in all directions.

degree   squared bias   variance   bias + variance + noise
1        0.2616        0.0091     0.3332
3        0.0168        0.0172     0.0966
4        0.0042        0.0244     0.0911
9        0.0147        0.7677     0.8449

Predict first. Going from degree 1 to degree 9, what happens to the squared bias and to the variance?

Predict first. You keep a degree-5 model but give each training set 60 points instead of 15. What happens to its variance?

Common mistakes

Also, bias and variance are properties of the whole learning procedure on a given kind of data, not of a model in isolation, and the "tradeoff" is not a law. More data cuts variance without raising bias. A better representation can cut both. Do not read "bias" as unfairness or "variance" as just the spread of the data: here they are two precise averages over repeated training sets.

Chapter context

A model should capture reusable structure without chasing accidental patterns in the training sample. Model selection compares this tradeoff using validation data that resembles future use.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

A possible objective is

J(w)=MSE⁡(w)+λ∑jwj2.J(w)=\operatorname{MSE}(w)+\lambda\sum_jw_j^2.

For L1 replace the squared-weight sum by ∑j∣wj∣\sum_j|w_j|. Here lambda is nonnegative and the bias convention must be specified. Different libraries normalize objectives differently.

Worked example

If MSE=0.1, w=[2,−1] and lambda=0.2, the stated L2 objective is 0.1+0.2×5=1.1. The corresponding L1 objective is 0.1+0.2×3=0.7. These values come from different objectives and should not be compared as if they were the same validation metric.

Reference code

The following example uses the Python standard library and does not call a remote service.

from math import isclose
w, mse, penalty = [2,-1], .1, .2
assert isclose(mse + penalty*sum(x*x for x in w), 1.1)
assert isclose(mse + penalty*sum(abs(x) for x in w), .7)
assert isclose(sum([.7,.8,.9])/3, .8)

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

Cross-validation does not repair leakage introduced before folding. More regularization can increase bias. A chosen validation result is not an unbiased final test result.