Sub topic 6
The bias-variance trade-off
This chapter assumes you know what underfitting and overfitting look like in training and validation error, and that you can fit a scikit-learn pipeline. Here we ask why those two failures happen and how they relate. The answer is the bias-variance trade-off, which splits a model's expected error into parts you can reason about separately.
Two sources of error
Suppose the data comes from y = f(x) + noise, where f is the true relationship and the noise has variance σ². You never see f. You see one random training set, fit a model, and get predictions ĝ(x). A different random training set would give a slightly different model.
That second fact is the key. A fitted model is a random object, because it depends on which sample you happened to draw. For squared error at a fixed input x, the expected test error over training sets and noise splits exactly into three terms:
expected error = bias² + variance + σ²
- Bias is the gap between the average prediction (averaged over many training sets) and the true value
f(x). High bias means the model family cannot represent the pattern, however much data you give it. A straight line fitted to a sine wave is biased. - Variance is how much predictions at
xmove from one training set to another. High variance means the fit is sensitive to which points were sampled. A degree-15 polynomial on 28 points chases individual noisy points, so it has high variance. - Irreducible noise (σ²) is randomness in
ythat no model can predict. It sets a floor on achievable error.
Underfitting is the high-bias case. Overfitting is the high-variance case. The terms are not the same thing, but the mapping is useful for choosing remedies.
How capacity moves you along the trade-off
Increasing model capacity (higher polynomial degree, deeper trees, more parameters) generally lowers bias, because the model can represent more shapes. It generally raises variance, because more flexibility means more ways to fit the noise in one particular sample. Total error is the sum, so it is typically U-shaped.
The first line is bias², the second is variance, and the third is their sum. The values are made up to show the shape. Real curves are noisier, and the minimum of the sum is the capacity you want.
Measuring bias and variance directly
You can only compute the decomposition when you know f, so it works on synthetic data. The script below draws 200 independent training sets from the same sine-plus-noise process used earlier in this tutorial. It fits the same model on each and measures, on a fixed grid, how far the average prediction is from the truth (bias²) and how much predictions scatter (variance).
import numpy as np
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.linear_model import LinearRegression
rng = np.random.default_rng(0)
noise_sd = 0.2
x_grid = np.linspace(-3, 3, 200).reshape(-1, 1)
f_true = np.sin(x_grid).ravel()
for degree in (1, 4, 15):
preds = []
for _ in range(200):
X = rng.uniform(-3, 3, size=(40, 1))
y = np.sin(X).ravel() + rng.normal(0, noise_sd, size=40)
model = make_pipeline(PolynomialFeatures(degree), StandardScaler(), LinearRegression())
model.fit(X, y)
preds.append(model.predict(x_grid))
preds = np.array(preds)
bias_sq = np.mean((preds.mean(axis=0) - f_true) ** 2)
variance = np.mean(preds.var(axis=0))
total = bias_sq + variance + noise_sd ** 2
print(degree, round(bias_sq, 3), round(variance, 3), round(total, 3))Expect the pattern, not specific numbers. Degree 1 should show large bias² and small variance. Degree 15 should show small bias² and a much larger variance, especially near the edges of the range where training points are sparse. Degree 4 should have the lowest total. The noise term (0.04 here) appears in every row and cannot be removed.
What changes the balance
Capacity is not the only lever.
- Training set size. More data shrinks variance, because each fitted model averages over more points. It does little for bias. A model that cannot represent the pattern stays biased with a million rows.
- Regularisation. A penalty on coefficients trades a small increase in bias for a larger decrease in variance. Later chapters on ridge and lasso build on this idea.
- Feature set. Adding informative features lowers bias. Adding uninformative ones raises variance.
- Ensembling. Averaging many high-variance models (as in bagging) reduces variance while keeping bias roughly unchanged.
Diagnosing from error values
You cannot measure bias and variance on real data, but training and validation error give a practical proxy.
| Training error | Validation error | Likely diagnosis | Typical remedies |
|---|---|---|---|
| High | High, close to training | High bias | More capacity, better features, weaker regularisation |
| Low | Much higher than training | High variance | More data, stronger regularisation, simpler model, early stopping, dropout |
| Low | Low, close to training | Good fit | Stop, then confirm once on the test set |
| High | Much higher than training | Both problems, or a split mismatch | Fix bias first, then check the split |
"High" and "low" are relative to a reference, such as the noise level, a simple baseline, or human performance. A training error of 0.2 is high if the noise floor is 0.04 and low if the floor is 0.19.
Common mistakes
- Believing the trade-off is unavoidable at every point. The U-shape describes one model family on a fixed dataset. More data or better features can lower the whole curve.
- Reading a single validation score as variance. The scatter between training sets is variance. One validation split gives one draw, so a gap between training and validation error is a symptom of variance and not a measurement of it. Cross-validation, covered in the next chapter, gives a more reliable view.
- Adding capacity to fix a high-variance model. If validation error is far above training error, a bigger model usually widens the gap.
- Assuming interpolation always fails. Very large neural networks that fit the training data exactly can still generalise well, which departs from the classical U-shape. For the models in this tutorial, treat the classical picture as the working model.
Exercise
A model has training MSE 0.03 and validation MSE 0.31. The noise floor is about 0.04. You can either double the training set or add polynomial features. Which do you try first, and why?
Show answer
Training error is near the noise floor, but validation error is about ten times higher, so the model has high variance. Doubling the training set reduces variance, so try that first. Adding polynomial features raises capacity, which typically increases variance further and widens the gap.
Exercise
In the simulation script, you change the training set size from 40 to 400 points per draw and rerun it. Which of bias² and variance do you expect to change most for degree 15, and which for degree 1?
Show answer
For degree 15, variance should fall sharply, because each fit averages over ten times more points, while bias² is already small. For degree 1, bias² stays large because a straight line still cannot follow a sine wave, and its variance, which was already small, falls only a little.