Bias–variance decomposition
The expected error of a learning method over random training sets is noise plus squared bias plus variance; what this says about the number of neighbours, the penalty λ, the depth of a tree and averaging, how to estimate the terms by simulation, and the double-descent caveat.
Two trees from one distribution
The topic Decision trees ended with two observations. A fully grown tree fits its training set exactly and still loses to a tree of depth 3 on new data. And two trees grown on samples from the same distribution disagree almost everywhere. "Flexible models overfit" is true but vague. This topic makes it precise.
Both observations are about a learning method, the procedure that turns a training set into a model, not about one fitted model. The training set is random, so the fitted model is random too. Throughout the topic the task is regression with squared loss.
The notation follows the topic Conditional expectation of Probability Theory. Pairs come from a fixed distribution. The regression function is the best predictor under squared loss, and is the noise at . A training set holds independent draws, and the method maps it to a function . The symbols and average over training sets of size .
One new object does most of the work. The average model is the prediction at averaged over all training sets the method could have seen:
Nobody can fit it: it needs infinitely many training sets. But it separates two ways to fail. A method can be wrong on average, with far from . Or it can be right on average while every single fit wanders off in its own direction.
Noise, bias and variance
Fix a test point and draw a new target at it, independent of the training set. The error is random through both and . Its expectation splits in two steps, and each step is a result we already have.
Step 1: separate the noise. Add and subtract inside the square. The cross term vanishes (the check asks why), and
This is the best-predictor decomposition from the topic Conditional expectation, with a random in place of a fixed function .
Step 2: split the estimation error. At a fixed the number is an estimator of the number , random through . The topic Estimators: bias and variance showed that its mean squared error is squared bias plus variance. Here we add and subtract :
Put the steps together and average over the test point :
The topic Estimators: bias and variance closed with this split at a single point. Here it is derived, and the average over test points makes it a property of the whole method. The noise is the floor that no method goes below. The squared bias is the error of the average model. The variance is how much the model moves from one training set to the next.
The topic Conditional expectation split the same excess over the floor into approximation and estimation error. Take its example: straight lines fitted to plus noise. At , and the squared bias of the line stays at about , the approximation error, while the variance falls from to to . Bias and variance belong to a method and a sample size together.
Bundles of trees
The end of the topic Estimators: bias and variance fitted polynomials of degree 1, 3 and 9 to 12 noisy points of a sine. The line had large bias, degree 9 had large variance, and degree 3 had the smallest total. Trees do the same with depth.
Take the problem from the topic Decision trees: uniform on , with , 40 points per training set. Grow a tree of each depth on 200 independent training sets and compare the fits at 201 test points. The noise is , and the last column is the sum of the three terms:
| Depth | Bias² | Variance | Expected test error |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 6 | |||
| full |
Picture twenty of the fits drawn on top of each other. The stumps form a tight bundle: they agree with one another and all flatten the peaks of the sine the same way. The fully grown trees form a wide bundle around the sine. Their mean is nearly the sine itself, but each one copies the noise of its own sample. The squared bias falls with depth, the variance roughly doubles (not at every single step), and their sum is smallest at depth 3.
The variance of the full tree, , is close to for a simple reason. A leaf with one training point predicts that point's target, and the target carries the full noise. With 40 points on an interval, the point alone in the leaf of lies close to , so barely differs between the two.
This also names the two failures from the topic Validation and overfitting. Overfitting is the variance term taking over. Underfitting is the bias term taking over.
Complexity knobs: k and λ
Every model of the course has a knob that moves it along the same curve. Depth was one. Two more come with exact formulas.
Nearest neighbours. Hold the training inputs fixed and let only the targets be random. The -NN prediction at is the mean of targets with independent noise, so
For the variance is , and at , and . The bias grows as the neighbours reach farther from . Small is the flexible end and large the rigid one, as in the topic Nearest neighbours. With more data a fixed finds closer neighbours, and the bias falls too.
Ridge regression. A toy from the topic Regularization with exact numbers. One feature, no intercept, fixed inputs with , and targets with and noise variance . The ridge slope is
We predict at , so the prediction error is the error of the slope:
| Bias² | Variance | Sum (without noise) | |
|---|---|---|---|
Least squares () is unbiased. With a little bias buys a bigger cut in variance, and the total falls below that of the unbiased fit. At the bias takes over. Minimizing the sum over gives the best penalty , the ratio of noise to signal. It is the penalty of a Gaussian prior with variance in the prior reading of ridge from the topic Regularization.
Regularization in every form — a ridge penalty, a minimum leaf size, early stopping — is a deliberate purchase of bias with variance.
Estimating by simulation
When the data distribution is known, the three terms are easy to estimate. Draw many training sets, fit the method on each, and at each test point take the mean and the variance of the predictions across sets. This code produced the tree table:
import numpy as np
from sklearn.tree import DecisionTreeRegressor
rng = np.random.default_rng(0)
m = lambda x: np.sin(2 * np.pi * x)
grid = np.linspace(0, 1, 201)[:, None] # test points
sets = [rng.random((40, 1)) for _ in range(200)] # 200 training sets of 40 points
sets = [(x, m(x[:, 0]) + 0.3 * rng.normal(size=40)) for x in sets]
for depth in (1, 2, 3, 4, 6, None):
P = np.array([DecisionTreeRegressor(max_depth=depth).fit(x, y).predict(grid)
for x, y in sets]) # row = training set, column = test point
bias2 = ((P.mean(0) - m(grid[:, 0])) ** 2).mean()
var = P.var(0).mean()
print(depth, round(bias2, 3), round(var, 3), round(0.09 + bias2 + var, 3))1 0.071 0.047 0.208
2 0.023 0.068 0.182
3 0.003 0.059 0.152
4 0.001 0.066 0.157
6 0.001 0.086 0.177
None 0.001 0.094 0.185The same recipe on a harder problem: Friedman's function of 10 inputs, 5 of them irrelevant (make_friedman1 in scikit-learn). Each training set has points with freshly drawn inputs, the noise is , and there are 50 training sets. Ridge is fitted on all 286 monomials of degree at most 3:
| Method | Bias² | Variance | Expected test error |
|---|---|---|---|
| kNN, / / | / / | / / | / / |
| ridge, / / | / / | / / | / / |
| tree, depth / / full | / / | / / | / / |
Each knob traces the same U: bias falls, variance rises, and the best setting lies in between.
Two limits. On real data there is one training set and no known , so the three terms are never observed separately; validation, as in the topic Validation and overfitting, estimates only their sum. And the split is for squared error. For 0–1 loss there are decompositions with terms of the same meaning, but they do not add up so simply.
What averaging can do
The table invites a fix for the variance: average many models. Take models trained by the same randomized procedure and predict with their mean . Each has the same average model , so by linearity of expectation
The bias does not change. Only the variance can fall. If the models were independent, the topic Expectation and variance would give the variance of their mean at once. Models trained on the same data are not independent, so the fall is smaller.
On Friedman's function, take 50 fully grown trees, each grown on a bootstrap sample of the same training set, and average them. The squared bias is , the variance instead of , and the expected test error , the best of the tree-based methods. Ridge on cubic features still does better here, , on this smooth function: the best family depends on the data.
The bias that averaging keeps is that of a bootstrap tree, not of a tree grown on the full set. A bootstrap sample repeats some points and misses others, so its tree is a little cruder: against .
Averaging attacks only one term. The topic Gradient boosting attacks the other: it combines high-bias models so that their sum has lower bias.
Double descent and what comes next
The U-shaped curve is not a law. Belkin and coauthors (2019) observed that when a model's capacity grows past the point where it fits the training data exactly, the interpolation threshold, the test error can peak there and then fall again. They showed it for random-feature models, neural networks and random forests.
A small example. Take points with 400 Gaussian features whose true weights decay, and fit least squares on the first features. For there are infinitely many exact fits, and the pseudoinverse from the topic The singular value decomposition picks the shortest one. Over 200 training sets the median excess test error is
| Excess error |
At the fit must pass through every noisy point with just enough weights, and the weights blow up. Past the threshold the error falls again, though in this example not below its value at . The decomposition is an identity and holds at every ; the check asks what does fail. Most settings of this topic's knobs stay below the threshold, where the classical U is the right picture. The flexible ends do not: 1-NN and a fully grown tree fit their training points exactly, so they sit at the threshold. Double descent matters most for very large models such as deep networks, which the course Deep Learning takes up.
Back to the trees. Fifty bagged trees cut the variance from to , about seven times, not fifty, because trees grown on resamples of the same data are correlated. The next topic, Bagging and random forests, derives how much variance an average of correlated models keeps. It then shows how a random forest lowers the correlation by giving each split only a random subset of the features.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Go deeper — optional
Code and labs do not count toward the main percentage or the certificate.
- Bias–variance on polynomial regressionoptional
Average predictions over seeds: bias², variance, MSE. Self-check in the notebook.
~25 min · easier on a computer · Bias–variance topic read.
Self-check in the notebook — not part of mastery or the certificate.
Answers are saved on the server and tied to this browser until you sign in.