Capacity, Underfitting, Overfitting, and the Bias-Variance Trade-off

Chapter 10, Generalization, Bias, and Variance

Learning objectives

  • Name a model's capacity (kNN's k, a tree's depth) and say which way it moves bias and variance
  • Split a model's squared error over many training sets into the bias squared and the variance, exactly
  • Read underfitting as bias (the models agree and are wrong together) and overfitting as variance (each model follows its own draw)
  • Explain why the training error cannot choose capacity, and why the same k is a different model on more data

Two Errors, One Dial

A model's capacity trades two errors, and only data the model has not seen can find the balance. kNN with few neighbours follows every sample it was given, so a model fitted on another draw of samples calls the same rock differently: that disagreement is variance. With many neighbours it averages over rock unlike the sample's own, and every draw makes the same mistake: that shared mistake is bias. Thirty kNN models, each fitted on its own 300 samples drawn at random from the eight appraisal wells and called on Ogbon-1, measure both at every k. With one neighbour the squared error of their pay probability is 0.0173, two thirds of it variance; with 101 neighbours it is 0.0906, nearly all of it bias; at k = 11 it is least, 0.0082. The models' own training error cannot find that k: it is 0.000 at k = 1.

Capacity Bias VarianceInteractive figure, enable JavaScript to interact.

Thirty Training Sets

Bias and variance are properties of a way of fitting, not of one fitted model, so they are measured over many training sets. The data are the book's own learning set, all synthetic: Ogbon-1, the Petrophysics book's well, and eight hypothetical appraisal wells of the book's field, logged by Ogbon-1's own build and the book's own tool model, with pay by the generator's truth. Each of the thirty draws takes 300 of the appraisal wells' 9608 samples at random, seeded, without replacement, standardizes the porosity logs and log10 RT on those 300 alone, and fits kNN. Every model then calls Ogbon-1, which no draw holds, and gives each of its 1201 samples a probability of pay: the share of its k neighbours that are pay. Thirty models fitted on a phone would take too long, so the book's precomputed capacity table holds what they give.

Bias Squared Plus Variance

At a sample ii with truth yiy_i (1 for pay, 0 for other rock), the thirty probabilities pr(i)p_r(i) have a mean pˉ(i)\bar p(i). Their mean squared error splits exactly in two:

1Rβˆ‘r=1R(pr(i)βˆ’yi)2=(pΛ‰(i)βˆ’yi)2+1Rβˆ‘r=1R(pr(i)βˆ’pΛ‰(i))2.\displaystyle \frac{1}{R}\sum_{r=1}^{R}\big(p_r(i) - y_i\big)^2 = \big(\bar p(i) - y_i\big)^2 + \frac{1}{R}\sum_{r=1}^{R}\big(p_r(i) - \bar p(i)\big)^2.

The first term is the bias squared, the error of the average model; the second is the variance, how far the models scatter about it. Averaged over Ogbon-1's samples, the two add to the squared error in plate (a). The bias term here holds everything the mean model gets wrong, including what no model of these logs could get right: the truth is fixed, so there is no separate noise term.

Reading the Curves

With one neighbour, each model copies the label of the one sample nearest in the logs. Its bias squared is the least of any k, 0.0058, but its variance is 0.0116: the models disagree, and plate (c) shows their spread widest. More neighbours average the disagreement away: the variance falls to 0.0011 at k = 11, rises a little between k = 15 and k = 31, and falls to 0.0008 at k = 101. Meanwhile the bias squared climbs, slowly at first and then fast, to 0.0899 at k = 101, where a third of each model's samples vote on every call and the thirty models agree on the same wrong answer. Underfitting is that corner: high bias, low variance. Overfitting is the other: low bias, high variance. The sum is least between them.

What the Training Error Cannot Tell

Plate (b) counts calls rather than probabilities. On their own samples the models call 0.000 wrong at k = 1, since every sample is its own nearest neighbour, and 0.103 wrong at k = 101: the training error rises with k all the way. On Ogbon-1 they call 0.017 wrong at k = 1, 0.010 at k = 11, least at k = 21, 0.009, and 0.179 at k = 101. The training error says the most capacity is best; the held-out error says it is not. The squared error and the share wrong pick nearby k, not the same one: the share wrong scores only which side of 0.5 a probability falls.

The Same k on More Data

The darker line in plate (b) is Ogbon-1 held out of one kNN fitted on every sample of the eight appraisal wells, from the book's cross-validation table. At k = 51 it calls 0.009 of Ogbon-1 wrong, against 0.120 for the models fitted on 300 samples, and its error is least at k = 9, 0.005. Fifty-one neighbours are 17 % of 300 samples but 0.5 % of 9608: capacity is k against the data, and the same k is a different model on a different amount of data. Chapter 7's Section 7.2 showed the same trade-off as a tree's depth on the nine wells; here the two errors are measured apart.

What These Numbers Are

These numbers measure a way of fitting on one held-out well; they are not how well kNN finds pay. That score is leave one well out on all nine wells, and Section 10.2 asks why no other split can be trusted to give it. They are also kind: every well of the set carries Ogbon-1's beds, and one tool model logs them all.

This page is prerendered for SEO and accessibility. The interactive widgets above hydrate on JavaScript load.