Skip to content

Bias and variance

Error on new data splits into systematic error, wobble between training sets, and noise. Flexibility trades the first for the second, and rows shift the balance.

Applied ~12 min8 steps

You will be able to

  • Decompose expected error into squared bias, variance and noise
  • Explain why more flexibility lowers bias and raises variance
  • Name the knob that moves each learner along the trade-off

Read first

01

Three sources of error

Read

Imagine training the same recipe on many datasets drawn from the same place, then predicting one new case.

E[(y−f^(x))2]=(f(x)−Ef^(x))2⏟bias2+Var f^(x)⏟variance+σ2⏟noise\mathbb{E}\big[(y - \hat{f}(x))^2\big] = \underbrace{\big(f(x) - \mathbb{E}\hat{f}(x)\big)^2}_{\text{bias}^2} + \underbrace{\text{Var}\,\hat{f}(x)}_{\text{variance}} + \underbrace{\sigma^2}_{\text{noise}}
  • Bias: the average fit misses the truth. The family is too rigid.
  • Variance: the fit wobbles from one training set to the next.
  • Noise: nothing can predict it.
02

Which degree will win?

Predict

Predict first

You fit polynomials to noisy data and find the degree that predicts new data best. Now you collect eight times as many rows. What happens to the best degree?

The best degree stays the sameThe best degree goes upThe best degree goes down
Reveal

It goes up. More rows shrink variance, so the model can afford more flexibility before the wobble outweighs the bias it removes. This is why the same learner can be right on a big table and wrong on a small one.

03

Slide it

Explore

Change Polynomial degree and read Bias² and Variance. Then raise Training points n and watch Best degree. Finally, raise Noise SD.

DExplorable · The bias–variance trade-off

Why does a more flexible model eventually predict worse?

Which polynomial degree will predict new data best?

5
How sure?
3
25
0.30
Simulating…

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

Notice that noise changes where the best degree is, even though no model can predict it: noisier data has more accidents to chase.

04

Every learner has this knob

Read
LearnerLess flexibleMore flexible
TreeLow Maximum depthHigh depth, small leaves
k-nearest neighborsLarge kSmall k
Elastic netLarge Penalty (λ)Small penalty
SVMSmall Cost (C)Large cost, large gamma
BoostingFewer rounds, shallow treesMore rounds, deep trees

Don't memorize this table. Ask which setting lets the model cut the data more finely.

05

Accuracy, interpretability, compute

Read

The trade-off doesn't stop at error. Flexible models cost more to fit, more to explain and more to keep running, and they need more rows.

So the guide's advice for prediction is modest: prefer the simplest model within noise of the best. A model you can read and retrain beats a slightly better one you can't, unless that small gap has real value in your decision.

06

Check your understanding

Checkpoint

Check yourself

In k-nearest neighbors, what happens as kk goes from 1 to 100?

Show the answer

Answer: a

k=1k = 1 copies the nearest row, noise and all. A large kk gives a steady but blurry average.

Check yourself

Which part of the error can no model remove, however it's tuned?

Show the answer

Answer: c

Bias and variance belong to the recipe. Noise belongs to the outcome given these features. New features can turn some of it into signal.

Check yourself

A validation curve for tree depth has training AUC rising all the way, while validation AUC peaks at depth 4 and then falls. What does the right-hand side show?

Show the answer

Answer: b

Past the peak the lines part. On the left of the peak, both are poor and close together, which is bias.

07

See it on your model

In the app

Open a run and go to Diagnostics. Under Validation curve, choose a hyperparameter and press Compute. It re-fits across the range and plots training and validation scores, and the banner names the best setting by cross-validation.

Validation curveHow does one hyperparameter trade underfitting for overfitting?
View data
max depthROC AUCsetlowerupper
10.7677Training0.76370.7717
10.7469Validation0.72890.7649
20.8159Training0.81190.8199
20.7947Validation0.77670.8127
30.8513Training0.84730.8553
30.8184Validation0.80040.8364
40.8874Training0.88340.8914
40.8232Validation0.80520.8412
60.9203Training0.91630.9243
60.8405Validation0.82250.8585
80.9554Training0.95140.9594
80.8435Validation0.82550.8615
120.9723Training0.96830.9763
120.8146Validation0.79660.8326
160.9906Training0.98660.9946
160.7953Validation0.77730.8133
Python
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures

rng = np.random.default_rng(8)
for n in [25, 200]:
    x = rng.uniform(-1, 1, n).reshape(-1, 1)
    y = np.sin(np.pi * x[:, 0]) + rng.normal(0, 0.3, n)
    mse = [-cross_val_score(make_pipeline(PolynomialFeatures(d), LinearRegression()), x, y,
                            cv=5, scoring="neg_mean_squared_error").mean() for d in range(1, 11)]
    print(n, "best degree", int(np.argmin(mse)) + 1)
R
set.seed(8)
for (n in c(25, 200)) {
  x <- runif(n, -1, 1); y <- sin(pi * x) + rnorm(n, 0, 0.3)
  fold <- sample(rep(1:5, length.out = n))
  mse <- sapply(1:10, function(d) mean(sapply(1:5, function(k) {
    fit <- lm(y ~ poly(x, d, raw = TRUE), subset = fold != k)
    mean((y[fold == k] - predict(fit, data.frame(x = x[fold == k])))^2)
  })))
  cat(n, "best degree", which.min(mse), "\n")
}
08

Recap

Recap
  • Expected error is squared bias, plus variance, plus noise.
  • More flexibility lowers bias and raises variance. More rows let you afford it.
  • Prefer the simplest model within noise of the best.

Concepts in this lesson