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.
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
Three sources of error
ReadImagine training the same recipe on many datasets drawn from the same place, then predicting one new case.
- 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.
Which degree will win?
PredictPredict 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?
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.
Slide it
ExploreChange 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?
Commit your prediction above to see the result
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.
Every learner has this knob
Read| Learner | Less flexible | More flexible |
|---|---|---|
| Tree | Low Maximum depth | High depth, small leaves |
| k-nearest neighbors | Large k | Small k |
| Elastic net | Large Penalty (λ) | Small penalty |
| SVM | Small Cost (C) | Large cost, large gamma |
| Boosting | Fewer rounds, shallow trees | More rounds, deep trees |
Don't memorize this table. Ask which setting lets the model cut the data more finely.
Accuracy, interpretability, compute
ReadThe 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.
Check your understanding
CheckpointCheck yourself
In k-nearest neighbors, what happens as goes from 1 to 100?
Show the answer
Answer: a
copies the nearest row, noise and all. A large 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.
See it on your model
In the appOpen 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.
View data
| max depth | ROC AUC | set | lower | upper |
|---|---|---|---|---|
| 1 | 0.7677 | Training | 0.7637 | 0.7717 |
| 1 | 0.7469 | Validation | 0.7289 | 0.7649 |
| 2 | 0.8159 | Training | 0.8119 | 0.8199 |
| 2 | 0.7947 | Validation | 0.7767 | 0.8127 |
| 3 | 0.8513 | Training | 0.8473 | 0.8553 |
| 3 | 0.8184 | Validation | 0.8004 | 0.8364 |
| 4 | 0.8874 | Training | 0.8834 | 0.8914 |
| 4 | 0.8232 | Validation | 0.8052 | 0.8412 |
| 6 | 0.9203 | Training | 0.9163 | 0.9243 |
| 6 | 0.8405 | Validation | 0.8225 | 0.8585 |
| 8 | 0.9554 | Training | 0.9514 | 0.9594 |
| 8 | 0.8435 | Validation | 0.8255 | 0.8615 |
| 12 | 0.9723 | Training | 0.9683 | 0.9763 |
| 12 | 0.8146 | Validation | 0.7966 | 0.8326 |
| 16 | 0.9906 | Training | 0.9866 | 0.9946 |
| 16 | 0.7953 | Validation | 0.7773 | 0.8133 |
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)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")
}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