Skip to content
Lambdian

Bias, variance and cross-validation

Why models fail to generalise, the error decomposition, and how cross-validation chooses complexity.

Animated explanation

The idea

Imagine retraining your model many times, each time on a fresh random training set from the same source. Two things can go wrong on average.

Bias: even averaged over all those training sets, the model is wrong, because its family is too simple (a line for a curve). Variance: the model changes a lot from one training set to the next, because it is flexible enough to fit each sample's noise.

Expected test error = bias² + variance + irreducible noise. Making a model more flexible trades bias for variance; regularisation and more data reduce variance. Since we cannot see the true function, we estimate where the sweet spot is with cross-validation.

Diagnosing from learning curves

Plot training and validation error as the training set grows.

  • High bias: both errors are high and close together. More data will not help; add features or capacity.
  • High variance: training error is low, validation error much higher, and the gap shrinks with more data. Get more data, regularise, or simplify.

k-fold cross-validation

Split the training data into kk folds (5 or 10). For each fold, train on the other k−1k-1 and validate on it. Average the kk validation scores. Every example is used for validation exactly once, so the estimate is steadier than one split, at kk times the cost.

Use stratified folds for classification, grouped folds when examples share a source, and forward-chaining splits for time series. To choose hyperparameters and report an unbiased score without a test set, use nested cross-validation.

Double descent, briefly

Very large models (deep networks) can show a second drop in test error past the point where they interpolate the training data. The classical U-curve still describes most tabular models and is the right default intuition.

The algorithm, step by step

  1. 1
    Shuffle the training data and cut it into kk equal folds.
  2. 2
    For each candidate setting and each fold jj: train on the other folds, compute the error on fold jj.
  3. 3
    Average the kk errors for each setting; pick the setting with the lowest mean (or the simplest within one standard error).
  4. 4
    Retrain on all training data with the chosen setting.

The maths

Decomposition (squared loss)

E[(y−f^(x))2]=Bias[f^(x)]2+Var[f^(x)]+σ2\mathbb{E}[(y-\hat f(x))^2] = \mathrm{Bias}[\hat f(x)]^2 + \mathrm{Var}[\hat f(x)] + \sigma^2

Bias

Bias[f^(x)]=ED[f^(x)]−f(x)\mathrm{Bias}[\hat f(x)] = \mathbb{E}_{\mathcal{D}}[\hat f(x)] - f(x)

Variance

Var[f^(x)]=ED[(f^(x)−ED[f^(x)])2]\mathrm{Var}[\hat f(x)] = \mathbb{E}_{\mathcal{D}}\big[(\hat f(x) - \mathbb{E}_{\mathcal{D}}[\hat f(x)])^2\big]

k-fold estimate

CVk=1k∑j=1k1∣Fj∣∑i∈FjL(yi,f^(−j)(xi))\mathrm{CV}_k = \frac1k\sum_{j=1}^k \frac{1}{|F_j|}\sum_{i\in F_j} L\big(y_i, \hat f^{(-j)}(x_i)\big)

Strengths and weaknesses

Strengths

  • Explains under- and overfitting with one picture
  • Cross-validation uses all data for both training and validation
  • Guides what to try next (data, features, regularisation)

Weaknesses

  • The exact decomposition holds for squared loss only
  • Cross-validation multiplies training cost by k
  • Wrong fold design (time, groups) silently leaks

Reach for it when…

Every time you choose a model, a hyperparameter or a feature set.

Check your understanding

Question 1

Q1Training error 2%, validation error 25%. What is the main problem?

Question 2

Q2Which change reduces variance without changing the model family?

Question 3

Q3Your data is daily sales over three years. How should you cross-validate?

Lambdian Lab

Measure bias and variance empirically

Estimate bias² and variance of polynomial fits by refitting on many bootstrap samples.

  • kfold_indices(n, k, seed): a list of k (train_idx, val_idx) pairs covering every index exactly once as validation.
  • bias_variance(x, y, x_test, f_test, degree, rounds, seed): resample the training data with replacement rounds times, fit a polynomial (np.polyfit), predict at x_test, and return (mean bias², mean variance) over the test points. f_test holds the true function values.
Open the labPython + NumPy runs in your browser. Sign-in keeps your code and attempts.