The Bias–Variance Trade-off
Full algebraic decomposition of expected squared error into bias, variance and noise, with a simulation.
Assumes you know
The Bias–Variance Trade-off
Intuition first
Imagine repeating your whole project many times: each time you collect a fresh training set from the same source, fit the same kind of model, and predict at one particular input . You get a spread of predictions.
Two things can be wrong with that spread.
Bias — the centre of the spread sits away from the truth. Every version of the model makes the same systematic mistake, so averaging them would not help. A straight line fitted to a curve is biased at almost every point.
Variance — the spread is wide. Individual models are wildly different depending on which sample they happened to see. Any single one of them is unreliable even if the average is perfect.
And a third thing that is not your fault at all: the target itself may be noisy. Even a perfect model cannot predict the unpredictable part.
The trade-off is that the knobs which reduce one usually increase the other.
| Symbol | Meaning | Read aloud |
|---|---|---|
| f(x) | The true underlying function | f of x |
| ε | Noise in the observed target, mean 0 and variance σ² | epsilon |
| f̂(x) | Prediction of the model fitted to one training sample | f hat of x |
| E[f̂(x)] | Average prediction over all possible training samples | expected f hat |
| σ² | Irreducible noise variance — the error floor | sigma squared |
Setup
Assume the data is generated as
with independent of . We fit on a random training sample. The randomness we take expectations over is which training sample we happened to get, plus the noise in the test point.
The decomposition
Full derivationAdvanced
Write , the average prediction across training samples, and suppress the argument . Start from the definition and insert :
Expand the square of the three-term sum:
Take the six terms one at a time.
. Both and are fixed constants (not random), so
. This is the definition of the variance of the prediction:
. Since , .
. is constant, so it factors out, and :
. constant and :
. The noise on the test point is independent of the training sample that produced , so and are independent:
All three cross terms vanish, leaving
Note which assumption did the work: required the test-point noise to be independent of the training data. If the same noisy observation appears in both training and test — the definition of leakage — the cross term is non-zero and the decomposition, along with your error estimate, is invalid.
Reading the trade-off
| Change | Bias | Variance |
|---|---|---|
| More flexible model (deeper tree, higher degree) | ↓ | ↑ |
| Stronger regularisation | ↑ | ↓ |
| More training data | – | ↓ |
| More features | ↓ | ↑ |
| Bagging / averaging many models | – | ↓↓ |
| Boosting (sequential fitting) | ↓↓ | ↑ |
| Early stopping | ↑ | ↓ |
Two rows are worth dwelling on.
More data reduces variance without touching bias. That is why data is the highest-leverage intervention when you can get it — every other row involves a trade.
Bagging reduces variance without touching bias, which is the entire reason random forests work: take a high-variance low-bias model (a deep tree) and average away the variance.
Solved problem 1 · Computing bias and variance from repeated fits
The true function is and we predict at , so . Noise has . Two models are each fitted on five independent training samples, giving these predictions at :
- Model A (a constant predictor):
- Model B (a flexible fit):
Compute bias², variance and total expected error for each.
Step 1 — Model A: average prediction
Step 2 — Model A: bias²
Step 3 — Model A: variance
Deviations from : .
Step 4 — Model A: total
Step 5 — Model B: average prediction
Step 6 — Model B: bias²
Model B is unbiased — on average it is exactly right.
Step 7 — Model B: variance
Deviations from : .
Step 8 — Model B: total, and the comparison
Model B wins, against — but not because it is unbiased. Being unbiased is worth nothing on its own; what matters is the sum.
Answer
Model A: bias² , variance , total . Model B: bias² , variance , total .
Model B is better overall, though it is far less stable. Note that if Model A's bias were only instead of , its total would be and it would win decisively — a small systematic error can beat a large random one.
Measuring it yourself
The decomposition is defined over repeated samples, so simulate that directly:
import numpy as np
rng = np.random.default_rng(0)
def true_f(x):
return np.sin(1.5 * x)
SIGMA = 0.3
X_TEST = np.linspace(0, 4, 60)
TRUTH = true_f(X_TEST)
def fit_predict(degree, n=25):
"""Fit a polynomial of the given degree to one fresh noisy sample."""
x = rng.uniform(0, 4, n)
y = true_f(x) + rng.normal(0, SIGMA, n)
coeffs = np.polyfit(x, y, degree)
return np.polyval(coeffs, X_TEST)
for degree in (1, 3, 9, 15):
preds = np.array([fit_predict(degree) for _ in range(200)]) # 200 repeats
mean_pred = preds.mean(axis=0)
bias2 = np.mean((TRUTH - mean_pred) ** 2)
variance = np.mean(preds.var(axis=0))
total = bias2 + variance + SIGMA ** 2
print(f"degree {degree:2d} bias²={bias2:7.4f} var={variance:8.4f} total={total:8.4f}")Running this shows bias falling and variance rising with degree, with the total minimised at a middle value — the U-curve, measured rather than asserted.
Exercise 1
A random forest and a single deep decision tree are trained on the same data. The tree gets 0.01 training error and 0.24 validation error; the forest gets 0.03 and 0.14. Explain in bias–variance terms.
Show solutionHide solution
A single deep tree has very low bias — it can carve the input space finely enough to fit almost anything — and very high variance, since a slightly different sample produces an entirely different tree. Its 0.23 gap is variance.
A random forest averages many such trees, each fitted to a bootstrap sample with a random feature subset. Averaging roughly independent predictors cuts variance by approximately a factor of while leaving bias essentially unchanged, because each tree is individually low-bias.
The numbers match: training error rose slightly, 0.01 to 0.03, which is the small bias cost of averaging and of restricting features per split. Validation error fell from 0.24 to 0.14, which is the large variance gain. Trading a little bias for a lot of variance is exactly the intended bargain.
Exercise 2
The irreducible error at some input is . A model achieves expected squared error of 0.45. How much room for improvement remains, and what does this imply about further tuning?
Show solutionHide solution
Only 0.05 of the error is attributable to the model; the remaining 0.40 is noise in the target and no model can remove it.
Even a perfect model would score 0.40, so the best possible improvement is about 11% of the current error. Effort is better spent elsewhere — reducing label noise, finding features that genuinely reduce by explaining part of what currently looks like noise, or accepting the model and shipping.
The broader lesson: without an estimate of the noise floor you cannot tell an excellent model from a mediocre one. Estimating it — via repeated measurements, annotator agreement, or a known physical limit — should precede a tuning campaign.
Next: Train, Validation and Test Splits, where we set up the machinery that lets these quantities be estimated honestly.