One dataset, from rows to a final prediction
Here is a small, invented dataset. One input goes with one observed output . We chose the numbers so you can check the entire calculation by hand.
| Training record | Input x | Output y |
|---|---|---|
| 1 | 0 | 0 |
| 2 | 1 | 3 |
| 3 | 2 | 4 |
| 4 | 3 | 3 |
We also have two validation observations for choosing a model and two test observations for evaluating the final choice. Start with the four training rows.
Start with one prediction for every row
Section titled “Start with one prediction for every row”A constant model predicts everywhere. Under squared error, its best choice is the average training output:
Its four squared errors are . They sum to 9; their average is .
Keep the answers separate: the best constant is 2.5; its training MSE is 2.25. “Argmin” would ask for the first; “minimum loss” would ask for the second.
Let the prediction depend on the input
Section titled “Let the prediction depend on the input”A line predicts . The intercept is added to every prediction; multiplies the input. Our training loss is
Each term comes from one table row. For instance, record 3 has , so its prediction error is .
To fit the line, choose both parameters to minimize that expression. Its partial derivatives are
Setting both equal to zero gives . You can check by substitution: and . This squared-error objective is a strictly convex quadratic, so the stationary point is its unique minimum.
The line predicts . Observed minus predicted gives . Every squared error is 1, so training MSE is 1.
Where did the derivatives come from?
For one row, the error is . Differentiating its square gives twice the error. The derivative of the inside is 1 with respect to , and with respect to :
The row totals are , , , and . Substituting gives the two derivatives above. This is the chain rule applied to the actual rows.
Take one gradient step
Section titled “Take one gradient step”Solving the equations found the completed fit. A gradient method can approach it through updates.
Start at , whose training MSE is . The gradient there is . With learning rate :
The new line predicts and has training MSE . One step improved the fit; it has not yet reached the minimum of 1.
In the explorer, this update is a separate fitting demonstration. The model comparison uses each candidate’s completed fit.
A perfect training fit
Section titled “A perfect training fit”The quadratic predicts , exactly matching the training outputs. Its training MSE is zero.
That alone does not tell us whether it predicts new outputs well. Compare the three fitted candidates using the validation rows.
Work through the validation calculation
The validation observations are and .
| Candidate | Predictions | Squared errors | Validation MSE |
|---|---|---|---|
| Constant 2.5 | 2.5, 2.5 | 2.25, 6.25 | 4.25 |
| Line 1+x | 1, 4 | 0, 1 | 0.5 |
| Quadratic 4x−x² | 0, 3 | 1, 4 | 2.5 |
The line has the lowest validation error among these candidates. Two observations provide a small amount of evidence; this ranking is not guaranteed for every future sample.
Freeze the choice, then evaluate it
Section titled “Freeze the choice, then evaluate it”Choose the line using validation. Keep its parameters fixed, then open the test rows.
The final test calculation
The final test observations are and . The frozen line predicts 2 and 3. Its squared errors are 1 and 1, so final test MSE is 1.
We report that score. If we use it to change the model, these observations become part of selection and no longer provide an untouched test of that new choice.
Connect the measured errors to probability
Section titled “Connect the measured errors to probability”For this teaching example, we generated possible outputs using
where independent noise is equally likely to be . The fitting procedure receives the training rows, not this rule. The displayed rows are selected possible realizations chosen for easy arithmetic.
At each input, the population mean is . Our fitted line happens to match it exactly in this constructed sample. Its expected squared error is therefore
Training MSE was 1, validation MSE was 0.5, and test MSE was 1. Those scores used a few particular observations. The expectation averages all possible noise outcomes with their probabilities. Different numbers are expected here.
The same calculation in matrix notation
Section titled “The same calculation in matrix notation”Put an intercept column of ones beside the inputs:
The output vector is . The squared Euclidean norm adds the squares of its four entries. Then the loss is . It still makes four predictions, subtracts the four outputs, squares the errors, and averages them. Linear algebra explains how to read each shape and product.
Check your understanding
Section titled “Check your understanding”Why is the line’s validation error below the expected noise variance? Why did we choose the line even though the quadratic’s training error was zero?
Compare the experiments being measured
The validation error uses two realized outputs; the noise variance averages all possible outputs at a fixed input. A finite sample can lie below that expectation. The quadratic fits the training sample, but the line performed better on the separate validation observations used to choose the model.