Sub topic 6
The bias-variance trade-off
This chapter assumes you know what underfitting and overfitting are, and that you can fit a scikit-learn pipeline. It explains why those two failures pull in opposite directions.
Where prediction error comes from
Suppose you could draw many training sets from the same source and fit the same model to each. The predictions at a given point would differ from set to set. For squared error, the expected error at that point splits into three parts:
expected error = bias² + variance + irreducible noise
- Bias is the gap between the average prediction across training sets and the true value. It measures how wrong the model is on average, because its assumptions are too rigid.
- Variance is how much the predictions change from one training set to the next. It measures sensitivity to the particular sample you happened to get.
- Irreducible noise is randomness in the labels. No model can remove it.
Underfitting corresponds to high bias. Overfitting corresponds to high variance.
How capacity moves the balance
A low-capacity model, such as a straight line fitted to sine data, makes the same systematic mistake on every sample. Its bias is high and its variance is low. A high-capacity model can bend to fit any sample, noise included. Its bias is low and its variance is high.
Total error is lowest between the two extremes.
Measuring both terms
You can estimate bias and variance by simulation, because here you know the true function. The code below refits each model on 200 fresh training sets and compares the predictions with the truth.
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)
x_test = np.linspace(-3, 3, 50).reshape(-1, 1)
truth = np.sin(x_test).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, 0.2, size=40)
model = make_pipeline(PolynomialFeatures(degree), StandardScaler(), LinearRegression())
model.fit(X, y)
preds.append(model.predict(x_test))
preds = np.array(preds)
bias_sq = np.mean((preds.mean(axis=0) - truth) ** 2)
variance = np.mean(preds.var(axis=0))
print(degree, round(bias_sq, 3), round(variance, 3))Expect degree 1 to show large bias² and small variance. Expect degree 15 to show the reverse. Degree 4 should keep both small. The exact numbers depend on the random seed.
Practical consequences
In real projects you cannot compute bias and variance directly, because you do not know the true function. You infer them from error patterns:
| Training error | Validation error | Likely problem | Typical remedy |
|---|---|---|---|
| High | High, close to training | High bias | More capacity, better features, weaker regularisation |
| Low | Much higher | High variance | More data, stronger regularisation, less capacity |
Common mistakes
- Treating the trade-off as a law for every model. Adding data lowers variance without raising bias, so it can improve both.
- Adding capacity when training error is already low. This only increases variance.
- Judging the model on training error alone. Training error always favours the more flexible model.
Exercise
A model has a training MSE of 0.05 and a validation MSE of 0.80. Is the main problem bias or variance? Name one change that should help.
Show answer
Variance. The large gap between training and validation error shows the model fits its training sample too closely. Collecting more data, increasing the regularisation strength, or reducing model capacity should help. Adding capacity would make it worse.