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 folds (5 or 10). For each fold, train on the other and validate on it. Average the validation scores. Every example is used for validation exactly once, so the estimate is steadier than one split, at 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
- 1Shuffle the training data and cut it into equal folds.
- 2For each candidate setting and each fold : train on the other folds, compute the error on fold .
- 3Average the errors for each setting; pick the setting with the lowest mean (or the simplest within one standard error).
- 4Retrain on all training data with the chosen setting.
The maths
Decomposition (squared loss)
Bias
Variance
k-fold estimate
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…
Check your understanding
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.