MoonSay /Tutorials/Machine Learning Interview #2
Contents
  1. 01Introduction
  2. 02Setting and notation
  3. 03Intuition
  4. 04The decomposition and its proof
  5. 05A warm-up you can do by hand
  6. 06The experiment
  7. 07Models where every term has a formula
  8. 08Diagnosing bias and variance in practice
  9. 09Fixing high bias and high variance
  10. 10Beyond the classical picture
  11. 11Interview questions and model answers
  12. 12Summary
  13. References

06The experiment: polynomial regression

We now measure all three terms for a real learning procedure, as in the companion video.

6.1Setup

The truth is f(x)=\sin(2.3\pi x+0.2) on [0,1]; the noise is Gaussian with \sigma=0.3, so \sigma^2=0.09; each training set has n=15 inputs spread evenly over [0,1] with a small random jitter. The learning procedure is “fit a polynomial of degree d by least squares”. Since we know f, we can estimate bias and variance directly by Monte Carlo:

  1. Draw R=500 independent training sets \D_1,\dots,\D_R.
  2. Fit the procedure to each, obtaining \fhat_1,\dots,\fhat_R, and evaluate them on a fine grid of inputs.
  3. Estimate the average model, \fbar(x)\approx\frac1R\sum_r\fhat_r(x).
  4. Estimate \Bias^2(x)=(\fbar(x)-f(x))^2 and \Var(x)\approx\frac1R\sum_r(\fhat_r(x)-\fbar(x))^2, and average both over the grid.

With 500 training sets, the Monte Carlo error in these averages is small compared with the differences we discuss.

6.2Three models

Figure 4 shows twenty fits for three degrees, together with one of the training sets. The straight line (degree 1) barely changes between training sets, but every fit misses the curve in the same way: high bias, low variance. The degree-12 polynomial bends to pass near every training point, so each training set produces a wildly different curve, yet on average it follows the truth: low bias, high variance. Degree 4 sits in between: its fits stay close to the curve and close to each other.

Twenty least-squares fits from twenty training sets (thin lines), the average of 500 fits (purple) and the true f (dashed) for degrees 1, 4 and 12; the dots are one training set of n=15 points. Data: Monte Carlo simulation computed for this tutorial, replicating the companion video.
Figure 4. Twenty least-squares fits from twenty training sets (thin lines), the average of 500 fits (purple) and the true f (dashed) for degrees 1, 4 and 12; the dots are one training set of n=15 points. Data: Monte Carlo simulation computed for this tutorial, replicating the companion video.
Try itTurn the complexity knob

Choose the polynomial degree. Left: twenty least-squares fits from twenty training sets of n=15 points, their average and the true curve. Right: the measured terms from Table 1, with test error =\Bias^2+\Var+\sigma^2. Same simulation as the figures above and the video.

6.3All twelve degrees

Table 1 lists the measured terms for every degree from 1 to 12. Bias falls quickly as the degree grows: by degree 5 the average fit is essentially exact. Variance rises slowly at first and then explodes, because with only fifteen points a degree-12 polynomial has almost as many coefficients as there are data. Test error, the sum of the three terms, is smallest at degree 5. Training error falls monotonically all the way, which is the optimism of Section 4.3 in action.

degree d123456789101112
\Bias^2.410.236.047.011.001.000.000.000.000.000.000.002
\Var.012.018.023.028.033.039.045.056.070.104.206.771
test error.512.344.160.128.124.129.135.146.160.194.297.863
training error.504.331.119.073.057.050.045.038.032.025.020.013
Table 1. Measured terms (averaged over x) for the experiment of Figure 4; test error =\Bias^2+\Var+\sigma^2 with \sigma^2=0.09; training error is the mean over the 500 training sets. Data: computed for this tutorial from the same simulation as the companion video.

6.4The U-shaped curve

Figure 5 plots the same quantities against the degree. As flexibility grows, bias falls because the model can represent more of f, and variance rises because the model increasingly fits the noise of each particular sample. Their sum plus the noise floor is the famous U-shaped test-error curve. To the left of its minimum the model underfits; to the right it overfits. The dashed training-error curve keeps falling throughout, which is exactly why it cannot be used to choose the model.

The tradeoff curve for the polynomial experiment: bias^2 falls, variance rises, and test error (=\Bias^2+\Var+\sigma^2) is U-shaped with its minimum at degree 5; at degree 12 it reaches 0.86, off the scale. Training error (dashed) keeps falling. Data: computed for this tutorial (Table 1).
Figure 5. The tradeoff curve for the polynomial experiment: bias^2 falls, variance rises, and test error (=\Bias^2+\Var+\sigma^2) is U-shaped with its minimum at degree 5; at degree 12 it reaches 0.86, off the scale. Training error (dashed) keeps falling. Data: computed for this tutorial (Table 1).

6.5What changes with more data

Variance is a finite-sample phenomenon: with more data, every fitted model is pinned down more firmly. Figure 6 repeats the experiment (with fresh random draws) for n=15, 30, 60 and 120 points per training set. The variance of the degree-12 fit collapses from 0.66 to 0.039, 0.018 and 0.009, and the best achievable test error falls from 0.124 to 0.108, 0.099 and 0.095, approaching the noise floor of 0.09. In this example the best degree stays at 5 for every n, because degree 5 already has almost no bias; with a more complicated truth, more data would also move the optimum toward more flexible models.

Test error versus degree for four training-set sizes (500 training sets each; values above 0.4 are clipped). More data lowers the variance of every flexible model. Data: computed for this tutorial.
Figure 6. Test error versus degree for four training-set sizes (500 training sets each; values above 0.4 are clipped). More data lowers the variance of every flexible model. Data: computed for this tutorial.

Pause and think. Table 1 shows the bias of degree 12 (0.002) slightly above that of degree 8. Shouldn’t a more flexible model always have less bias?

Show the answer

Not exactly. Bias is measured for the whole procedure, including least squares on only 15 points. Very high-degree fits are numerically unstable near the ends of the interval, and their average does not converge perfectly to f there. Monotone bias is a tendency, not a theorem.