Describing Data looked at data without a model. This post adds one: a function learned from examples, which predicts an outcome from inputs. What exactly is being learned? Why does a model that fits its training data better often predict new data worse? And how can we tell, without fooling ourselves?

The central idea is the bias–variance trade-off, and the post measures it rather than sketching it: polynomials fitted to 2,000 training sets, a k-nearest-neighbour classifier compared with the best possible one, and the place where the classic U-shaped curve stops telling the whole story.


1. What “Learning” Means

Arthur Samuel, who built a checkers program that improved by playing (Samuel, 1959), is usually credited with describing machine learning as giving computers “the ability to learn without being explicitly programmed” (the phrase is a later paraphrase, not a quote from his paper). Tom Mitchell (1997) made it testable: a program learns from experience with respect to a task and a performance measure if its performance on , measured by , improves with . For a spam filter that watches you label emails, is classifying emails, the share classified correctly, and your labels.

SettingWhat is givenGoalExamples
Supervised, regressioninputs and a numeric outcome predict price, travel time, blood pressure
Supervised, classificationinputs and a categorical outcome predict the classspam or not, digit 0–9, car / pedestrian / bike
Unsupervisedinputs onlyfind structure: groups, directions of variationcustomer segments, driving patterns
Reinforcementan environment that returns rewardslearn actions that maximise rewardgames, control, traffic-light timing

Statistical learning grew out of statistics and puts more weight on models, interpretation and uncertainty; machine learning grew out of artificial intelligence and puts more weight on large-scale prediction. The methods overlap almost entirely. In the nesting that is usually drawn, deep learning is a part of machine learning, which is a part of artificial intelligence.


2. Y = f(X) + ε

Supervised learning assumes that the outcome is a systematic function of the inputs plus noise:

is unknown, and the noise collects everything the inputs do not determine: measurement error, unrecorded variables, genuine randomness. Learning means estimating from training data , giving and predictions .

Xpredictorsf(X)systematic part+εnoiseYoutcomeY = f(X) + ε, with E[ε] = 0predictionŶ = f̂(X) for new Xf̂ may be a black box,if it predicts wellinferencewhich X matter, and how?needs an interpretable f̂and its uncertaintyThe reducible error comes from f̂ ≠ f and shrinks with better methods and more data; Var(ε) is the irreducible floor no method can beat.

For a fixed and input , the expected squared error splits into two parts:

A better method and more data can shrink the first part; nothing can shrink the second.

What is the “ideal” f? Under squared error, the best possible prediction at is the average outcome there, the regression function . That is a statement about squared error, not a law: under absolute error, the best prediction is the conditional median, and for classification with 0/1 loss it is the most probable class (the Bayes classifier of §8). The loss function decides what “best” means.


3. Two Reasons to Estimate f

Prediction: only matters. A bank that predicts default needs accurate predictions; may be a black box.

Inference: the relationship itself matters. Which inputs affect ? In which direction, and by how much? How much would sales rise if the TV budget doubled? This needs an whose form can be read, and an estimate of its uncertainty.

The two goals favour different methods (Shmueli, 2010; Breiman, 2001). Most of statistics has been about inference, most of machine learning about prediction, and many real questions need both.


4. Parametric and Non-parametric Methods

A parametric method first assumes a form for , for example a linear one,

and then estimates its parameters from the data (by least squares, in the next post). This turns an impossible problem (find any function) into an easy one (find a few numbers), at a price: if the assumed form is wrong, the estimate stays wrong however much data there is.

A non-parametric method makes no explicit assumption about the form of and lets the data shape it: splines, k-nearest neighbours, trees. It can fit far more shapes, and needs far more data to do it. How much more grows quickly with the number of inputs: with 10 inputs, a neighbourhood that contains 10% of the data spans about 80% of the range of each input, so “nearby” points are no longer near (the curse of dimensionality; Hastie et al., 2009, §2.5).

flexibility: how many shapes of f the method can fit →interpretability →subset selection, lassoleast squareslogistic regression, LDAdecision treegeneralised additive modelsk-nearest neighboursbagging, boosting, forestssupport vector machinesdeep neural networksPlacement is approximate (after James et al., 2021, Fig. 2.7): the trade-off is a tendency, and depends on how a method is tuned.

Flexible methods can fit more shapes but are harder to interpret. A less flexible method is not only a fallback for inference: as the next sections show, it can also predict better.


5. Training Error and Test Error

For regression, the quality of a fit is measured by the mean squared error,

Computed on the training data, it rewards a model for memorising them. What matters is the MSE on new data the model has not seen, the test MSE. The experiment: the true function is on [0, 1], noise has standard deviation 0.3 (variance 0.09), and polynomials of degree 0 to 10 are fitted by least squares to 30 training points. Repeated for 2,000 independent training sets:

DegreeTraining MSETest MSE: mean · median
0 (a constant)0.4380.447 · 0.443
1 (a line)0.2660.309 · 0.302
30.0820.112 · 0.108
50.0720.143 · 0.114
70.0661.317 · 0.134
100.057474 · 0.267

The training MSE falls with every added degree: a more flexible model can always fit its own data at least as well. The test MSE falls until degree 3 and then rises: the model starts fitting the noise of its particular training set, which does not repeat in new data. This is overfitting. At high degrees, the mean test error is dominated by a few training sets whose points leave gaps near the edges, where a degree-10 polynomial swings wildly; the median shows the typical case.


6. The Bias–Variance Decomposition

Why does the test error turn around? Imagine drawing many training sets from the same population and fitting the same method to each. At a test input , the fits vary. The expected test error, averaged over training sets and over the noise of the test point , splits exactly into three parts (Geman et al., 1992):

The expectation is over the training sets: is random because the data it was fitted to are random.

  • Variance: how much changes from one training set to another. Flexible methods follow their data closely, so they have high variance.
  • Bias: how far the average fit is from the truth, the error of the assumptions themselves. A straight line fitted to a wave stays wrong with infinite data. Rigid methods have high bias.

Bias and variance are both properties of the method and the data together; neither is “accuracy” and neither is “flexibility”. Flexibility is the knob that trades one for the other. Measured over the 2,000 training sets, at 400 test inputs:

DegreeBias² · VarianceTheir sum + noise (0.09) vs. measured test MSE
00.343 · 0.0140.447 vs. 0.447
10.197 · 0.0220.309 vs. 0.309
30.005 · 0.0170.111 vs. 0.112
50.000 · 0.0520.142 vs. 0.143
70.001 · 1.2251.316 vs. 1.317

The decomposition is not an approximation: the three measured parts add up to the measured test error at every degree. From degree 0 to 3, bias falls much faster than variance grows, so the test error falls; beyond degree 3 the bias has nothing left to give and the variance takes over. This is the familiar U-shaped test error curve. Choosing the flexibility means finding its bottom, and since is unknown in practice, it has to be found from data (the resampling post).


7. Beyond the U: Double Descent

The U-shaped curve is often stated as a law: past some point, more flexibility always hurts. Modern neural networks broke it. They have far more parameters than training examples, fit their training data perfectly, and still predict well. Belkin et al. (2019) showed that the test error can fall again once a model is flexible enough to interpolate the data, a curve they called double descent.

It already happens in linear regression. The experiment: a linear model with 400 standard-normal features whose true coefficients decay as , 40 training points, noise variance 0.09, and least squares on the first features. When , infinitely many coefficient vectors fit the training data exactly; the one with the smallest norm is used (it is what the pseudo-inverse, and gradient descent started at zero, return). Median test MSE over 500 training sets:

Number of features pMedian test MSERegime
100.193the classic optimum
300.437variance rising
40 (= n)9.864interpolation threshold: exactly one exact fit
600.624second descent
4001.002many features, small norm

Two lessons. First, the U describes the under-parameterised regime; at the interpolation threshold the error peaks, because the only fit that matches every training point must bend to the noise, and beyond it the error can fall again because the minimum-norm solution spreads the noise thinly over many parameters. Second, the second descent does not automatically win: here the best model is still the classic one at . Whether it wins depends on the problem (Belkin, Hsu & Xu, 2020; Hastie et al., 2022), and in large neural networks it often does (Nakkiran et al., 2020). Bias and variance still add up to the test error everywhere; what changes is how the variance behaves past the threshold.


8. Training, Validation and Test Data

The test error is only an honest estimate if the test data played no part in building the model, including in choosing it. A clean protocol splits the data once, at random, into three parts:

training60%validation20%test20%one data set, split at random before anything is fitted1. fitevery candidate modelon the training set2. choosedegree, K, features …by validation error3. refitthe chosen modelon training + validation4. reportits error, once,on the test setChoosing on the test set, or re-using it after looking, turns the test error into a training error: it becomes optimistic.With little data, cross-validation replaces the single validation set (the resampling post); the test set stays untouched either way.
  1. Training set: fit every candidate model.
  2. Validation set: compare the candidates, choose the degree, the K, the features.
  3. Test set: report the chosen model’s error, once.

Comparing models by their training error picks the most flexible one every time (degree 10 has the lowest training MSE above). Comparing them on the test set is subtler: whichever model looks best on a finite test set was partly lucky on it, so its error there is optimistic (Cawley & Talbot, 2010). In the experiment, choosing the degree on a 30-point test set reported a test MSE of 0.103; the same models’ error on 5,000 fresh points was 0.119, 16% higher. After choosing, the test set has become part of the training process.

With little data, a single validation split wastes observations and depends on which points it happened to contain; cross-validation repeats the split and averages (the resampling post). The rule stays: whatever is used to choose cannot also be used to report.


9. Classification: Error Rate, the Bayes Classifier and k-Nearest Neighbours

For a categorical outcome, the usual measure is the error rate, the share of misclassified observations:

where is 1 when its condition holds and 0 otherwise. (Without the it is the number of errors, not a rate.) As with MSE, it is the test error rate that matters.

The Bayes classifier. If the conditional probabilities were known, the best possible classifier would assign each to its most probable class. Its error rate, the Bayes error rate, is the classification counterpart of : where the classes overlap, even the perfect rule makes mistakes. In practice the probabilities are unknown, and every classifier is an attempt to approximate this rule.

k-nearest neighbours (Fix & Hodges, 1951; Cover & Hart, 1967) is the simplest such attempt: to classify , find the training points closest to it and take a majority vote. is its flexibility knob, running the other way from a polynomial’s degree: follows every training point, large averages over many. The experiment uses the two-class mixture of Hastie et al. (2009, §2.3.3): each class is a mixture of 10 Gaussian clusters, 200 training points, so the true probabilities, and with them the Bayes classifier, are known exactly.

KTraining error · test error (20 training sets)
10.000 · 0.336memorises the training set
50.210 · 0.298
15 or 300.239 or 0.256 · 0.280the best K here
1500.296 · 0.310almost a straight boundary
Bayes classifier0.255 (test)the floor

The same pattern as for regression: the training error rises with (from 0 at ), the test error falls and then rises, and its minimum (0.280) stays above the Bayes error rate (0.255), which no classifier can beat on average.


10. Common Claims, Checked

ClaimVerdict
”The ideal f(x) is E(Y given X = x).”Under squared error. Under absolute error it is the conditional median; for classification with 0/1 loss, the most probable class (the Bayes classifier). The loss decides what “ideal” means.
”Bias means low accuracy, variance means flexibility.”Both are properties of a method fitted to data: bias is how far the average fit is from the truth, variance how much fits differ between training sets. Flexibility is the knob that trades them.
”Test MSE = variance + bias² + noise” (for one data set).It holds for the expected test error, averaged over training sets and test noise, and exactly so: 0.005 + 0.017 + 0.090 = 0.111 against a measured 0.112 at degree 3.
”A more flexible model fits the data better, so it is better.”It always fits its training data at least as well (training MSE 0.438 → 0.057). The test MSE was lowest at degree 3 and reached a mean of 474 at degree 10.
”Training data are used to fit and to compare models.”Comparing on training error always picks the most flexible model. Compare on a validation set or by cross-validation.
”Choosing a model by its test error is fine.”It makes the reported error optimistic: 0.103 reported vs. 0.119 on fresh data (16% higher) in the experiment. Choose on validation data, report on the test set once.
”Error rate = Σ I(yᵢ ≠ ŷᵢ).”That is the number of errors. The error rate divides by n.
”Past the optimum, more flexibility always increases test error.”Not always: at the interpolation threshold p = n the error peaked (median 9.9) and then fell again to 0.62 (double descent). Here the classic optimum (0.19 at p = 10) still won; in large neural networks the second descent often does.
”Non-parametric methods just need more data.”They need much more, and how much grows with the number of inputs (the curse of dimensionality).
”K = 1 nearest neighbour is perfect: zero training error.”Zero training error and the worst test error of the flexible settings (0.336 vs. 0.280 at K = 15–30). No classifier beat the Bayes error rate of 0.255.

Summary

IdeaIn one line
LearningPerformance on a task improves with experience; supervised, unsupervised, reinforcement
Y = f(X) + εReducible error ( vs. ) can shrink; Var(ε) cannot
Prediction vs. inferenceAccurate vs. an interpretable relationship with its uncertainty
FlexibilityParametric methods assume a form; non-parametric ones need far more data
Bias–varianceExpected test error = variance + bias² + noise, measured exactly; flexibility trades them
Double descentPast p = n the test error can fall again; it does not always win
Honest evaluationFit on training, choose on validation, report on test once
ClassificationError rate; the Bayes classifier is the floor; kNN’s K is a flexibility knob

Next: Linear Regression, Read Correctly: Least Squares, Intervals and p-Values: fitting a line to 32 cars, and what its standard errors, confidence intervals, p-values and R² do and do not say.

The experiments: what_a_model_learns.py (numpy 2.1, seeded); the lab’s curves come from its output. The lab fits its polynomials in the browser by the same least squares in the Legendre basis; on a fixed test data set, its coefficients agree with numpy’s legfit to within 4 × 10⁻¹⁴, and its k-nearest-neighbour predictions match a numpy implementation on all 300 test points for K = 1, 4, 15 and 150.


References

Learning and its goals

  • Samuel, A. L. (1959). Some studies in machine learning using the game of checkers. IBM Journal of Research and Development, 3(3).
  • Mitchell, T. M. (1997). Machine Learning. McGraw-Hill.
  • Breiman, L. (2001). Statistical modeling: The two cultures. Statistical Science, 16(3).
  • Shmueli, G. (2010). To explain or to predict? Statistical Science, 25(3).
  • Wolpert, D. H. (1996). The lack of a priori distinctions between learning algorithms. Neural Computation, 8(7).
  • Sutton, R. S., & Barto, A. G. (2018). Reinforcement Learning: An Introduction (2nd ed.). MIT Press.

Bias, variance and double descent

  • Geman, S., Bienenstock, E., & Doursat, R. (1992). Neural networks and the bias/variance dilemma. Neural Computation, 4(1).
  • Belkin, M., Hsu, D., Ma, S., & Mandal, S. (2019). Reconciling modern machine-learning practice and the classical bias–variance trade-off. PNAS, 116(32).
  • Belkin, M., Hsu, D., & Xu, J. (2020). Two models of double descent for weak features. SIAM Journal on Mathematics of Data Science, 2(4).
  • Nakkiran, P., Kaplun, G., Bansal, Y., Yang, T., Barak, B., & Sutskever, I. (2020). Deep double descent: Where bigger models and more data hurt. ICLR.
  • Hastie, T., Montanari, A., Rosset, S., & Tibshirani, R. J. (2022). Surprises in high-dimensional ridgeless least squares interpolation. Annals of Statistics, 50(2).

Evaluation and model selection

  • Cawley, G. C., & Talbot, N. L. C. (2010). On over-fitting in model selection and subsequent selection bias in performance evaluation. Journal of Machine Learning Research, 11.

Nearest neighbours

  • Fix, E., & Hodges, J. L. (1951). Discriminatory analysis. Nonparametric discrimination: Consistency properties. Technical Report 4, USAF School of Aviation Medicine.
  • Cover, T., & Hart, P. (1967). Nearest neighbor pattern classification. IEEE Transactions on Information Theory, 13(1).

Textbooks

  • James, G., Witten, D., Hastie, T., & Tibshirani, R. (2021). An Introduction to Statistical Learning (2nd ed.). Springer.
  • Hastie, T., Tibshirani, R., & Friedman, J. (2009). The Elements of Statistical Learning (2nd ed.). Springer.