04 / Machine learning

Fit the signal.
Not the noise.

A more flexible model can follow the training data closely. That does not guarantee good predictions on data it has never seen.

Polynomial regression

20 training points · 200 test points
Fitted modelTrue signalTraining observation
Polynomial regression on noisy observationsThe fitted polynomial and known synthetic signal, with 20 noisy training observations.-1.501.5-1-0.500.51ResponseInput
Training MSE0.1023
Held-out MSE0.0997
Fitted coefficients4
Noise variance0.0625

A signal we can inspect.

The observations come from a known curve plus independent Gaussian noise. Training inputs are evenly spaced; held-out inputs are newly sampled uniformly from the same interval.

y=sin⁡(πx)+0.3x+εy=\sin(\pi x)+0.3x+\varepsilon
ε∼N(0,σ2),−1≤x≤1\varepsilon\sim\mathcal N(0,\sigma^2),\qquad -1\leq x\leq1

A polynomial model is fitted only to the 20 training observations. Its flexibility is controlled by the degree dd:

f^(x)=∑j=0dβjPj(x)\widehat f(x)=\sum_{j=0}^d\beta_j P_j(x)

The PjP_j are Legendre polynomials. They span the same degree-dd polynomial family as ordinary powers, with better numerical behavior on this interval.

Regularization penalizes nonconstant coefficients. The fit minimizes:

120∑i=120(yi−f^(xi))2+λ∑j=1dβj2\begin{gathered}\frac1{20}\sum_{i=1}^{20}\bigl(y_i-\widehat f(x_i)\bigr)^2\\+\lambda\sum_{j=1}^d\beta_j^2\end{gathered}

The intercept is not penalized. The penalty depends on this choice of basis.

Watch the error you did not fit.

  1. Set regularization to zero and increase the degree. Training error cannot increase for these nested least-squares models, apart from numerical rounding.
  2. Compare the held-out error. Extra flexibility may fit noise and hurt prediction.
  3. Add regularization or try a new sample. Which patterns survive?
MSE⁡=1N∑i=1N(yi−f^(xi))2\operatorname{MSE}=\frac1N\sum_{i=1}^N\bigl(y_i-\widehat f(x_i)\bigr)^2

Held-out MSE uses 200 observations excluded from fitting. The noise variance σ2\sigma^2 is the irreducible error in expectation for new noisy responses. A finite-sample MSE can fall below it.

This is a demonstration of generalization, not a measured bias–variance decomposition. A single test split can be lucky or unlucky.

Repeatedly choosing settings from test error turns the test set into validation data. A real workflow keeps a final, untouched test set. All data here are synthetic and generated locally.