Week 01 · Prediction, loss, and generalization

Compare a flexible fit with a simple baseline on data it did not see.

Subject contents · Week 2 →

Compare a flexible fit with a simple baseline on data it did not see.

Work through the problem

Training loss measures errors on examples used to fit a rule. Generalization risk is the expected loss on a new draw from the target population. A held-out set estimates that risk only to the extent that it represents the target population and stays outside model selection.

Fit y = [0, 3, 0] at x = [−1, 0, 1]. The degree-two interpolant is 3−3x² and has zero training error. At x = ±2, it predicts −9. If both new outcomes are zero, mean squared error is 81, versus 1 for the constant training mean of 1.

Run the Python lab

import numpy as np
x, y = np.array([-1,0,1]), np.array([0,3,0])
coef = np.polyfit(x,y,2)
test = np.array([-2,2])
errors = [np.mean(np.polyval(coef,test)**2), y.mean()**2]
print("Quadratic coefficients:", coef.round(3))
print("Test MSE, flexible and constant:", np.round(errors, 3))
assert np.allclose(errors, [81,1])

Output from the lab

Quadratic coefficients: [-3.  0.  3.]
Test MSE, flexible and constant: [81.  1.]

Practice

  1. When is a test at ±2 a fair comparison?

Check your reasoning

  1. When the intended use includes such inputs. A test inside the training range asks an interpolation question.

Subject contents · Week 2 →

Further reading · An Introduction to Statistical Learning