Back in the Regression lecture, a 13-parameter wiggle threaded perfectly through a month of ice-cream data, then fell apart on a fresh month. Today we hold out a validation set properly and watch the same story play out, degree by degree, instead of relying on one lucky (or unlucky) comparison.
import warningsimport numpy as npimport matplotlib.pyplot as pltimport scienceplotswarnings.filterwarnings("ignore", message="The fit may be poorly conditioned")plt.style.use(["science", "no-latex"])TEAL, CARDINAL, GRAY ="#009090", "#9c1b33", "#c9c9c9"rng = np.random.default_rng(9)sigma =5hidden_f =lambda t: 60-0.03* (t -85) **2n_train, n_val =15, 60temps_train = rng.uniform(58, 100, size=n_train)sales_train = hidden_f(temps_train) + rng.normal(0, sigma, size=n_train)temps_val = rng.uniform(58, 100, size=n_val)sales_val = hidden_f(temps_val) + rng.normal(0, sigma, size=n_val)print(f"training on {n_train} days, validating on {n_val} fresh days")
training on 15 days, validating on 60 fresh days
Fit a polynomial of each degree \(0, 1, 2, \ldots\) to the training days only, and score it on both sets.
best degree by validation error: 2 (the hidden function is degree 2)
training error there: 24.1, validation error there: 32.8
Training error never goes back up, exactly the claim we proved in class, and past degree 2 it slides below the gray noise floor \(\sigma^2 = 25\), which no error on fresh data can do. That is the optimism from the reading: each parameter we add lets the fit absorb a little more of the training noise. Validation error bottoms out near the true degree of 2, just above the floor, and then climbs. Past degree 12 the training curve runs off the bottom of the plot toward zero and the validation curve leaves through the top: at \(\text{degree} = n_{\text{train}} - 1\) there is one parameter per training day, and the fit interpolates the noise exactly.
What if we had more training data?
More data should let a higher-degree model generalize before it starts overfitting. Try n_train_new = 60 below, then try 25.
Punchline: overfitting is a property of a model relative to how much data it has to work with. The same degree-6 polynomial that is already chasing noise on 15 days sits near the floor on 60. Next lecture leaves regression for a new kind of label, and the train/validation discipline built today follows every model for the rest of the course.