Skip to content

One dataset, from rows to a final prediction

Here is a small, invented dataset. One input xx goes with one observed output yy. We chose the numbers so you can check the entire calculation by hand.

Training recordInput xOutput y
100
213
324
433

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.

A constant model predicts cc everywhere. Under squared error, its best choice is the average training output:

c=0+3+4+34=52.c=\frac{0+3+4+3}{4}=\frac52.

Its four squared errors are 25/4,1/4,9/4,1/425/4,1/4,9/4,1/4. They sum to 9; their average is 9/4=2.259/4=2.25.

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.

Inspect each candidate on the same training rows. Open validation when ready, choose the model, then reveal its final test result.

A line predicts h(x)=a+bxh(x)=a+bx. The intercept aa is added to every prediction; bb multiplies the input. Our training loss is

L(a,b)=14[a2+(a+b3)2+(a+2b4)2+(a+3b3)2].L(a,b)=\frac14\left[a^2+(a+b-3)^2+(a+2b-4)^2+(a+3b-3)^2\right].

Each term comes from one table row. For instance, record 3 has x=2,y=4x=2,y=4, so its prediction error is a+2b4a+2b-4.

To fit the line, choose both parameters to minimize that expression. Its partial derivatives are

La=2a+3b5,Lb=3a+7b10.\frac{\partial L}{\partial a}=2a+3b-5,\qquad \frac{\partial L}{\partial b}=3a+7b-10.

Setting both equal to zero gives a=b=1a=b=1. You can check by substitution: 2+35=02+3-5=0 and 3+710=03+7-10=0. This squared-error objective is a strictly convex quadratic, so the stationary point is its unique minimum.

The line 1+x1+x predicts (1,2,3,4)(1,2,3,4). Observed minus predicted gives (1,1,1,1)(-1,1,1,-1). Every squared error is 1, so training MSE is 1.

Where did the derivatives come from?

For one row, the error is a+bxiyia+bx_i-y_i. Differentiating its square gives twice the error. The derivative of the inside is 1 with respect to aa, and xix_i with respect to bb:

La=24i(a+bxiyi),Lb=24i(a+bxiyi)xi.\frac{\partial L}{\partial a}=\frac24\sum_i(a+bx_i-y_i),\qquad \frac{\partial L}{\partial b}=\frac24\sum_i(a+bx_i-y_i)x_i.

The row totals are xi=6\sum x_i=6, yi=10\sum y_i=10, xi2=14\sum x_i^2=14, and xiyi=20\sum x_iy_i=20. Substituting gives the two derivatives above. This is the chain rule applied to the actual rows.

Solving the equations found the completed fit. A gradient method can approach it through updates.

Start at (a,b)=(0,0)(a,b)=(0,0), whose training MSE is 34/4=8.534/4=8.5. The gradient there is (5,10)(-5,-10). With learning rate 0.10.1:

(a,b)new=(0,0)0.1(5,10)=(0.5,1).(a,b)_{\text{new}}=(0,0)-0.1(-5,-10)=(0.5,1).

The new line predicts (0.5,1.5,2.5,3.5)(0.5,1.5,2.5,3.5) and has training MSE 1.251.25. 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.

The quadratic q(x)=4xx2q(x)=4x-x^2 predicts (0,3,4,3)(0,3,4,3), 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 (0,1)(0,1) and (3,5)(3,5).

CandidatePredictionsSquared errorsValidation MSE
Constant 2.52.5, 2.52.25, 6.254.25
Line 1+x1, 40, 10.5
Quadratic 4x−x²0, 31, 42.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.

Choose the line using validation. Keep its parameters fixed, then open the test rows.

The final test calculation

The final test observations are (1,1)(1,1) and (2,4)(2,4). 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

XUnif{0,1,2,3},Y=1+X+ε,X\sim\operatorname{Unif}\{0,1,2,3\},\qquad Y=1+X+\varepsilon,

where independent noise ε\varepsilon is equally likely to be 1,0,1-1,0,1. 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 1+x1+x. Our fitted line happens to match it exactly in this constructed sample. Its expected squared error is therefore

E[ε2]=1+0+13=23.\mathbb E[\varepsilon^2]=\frac{1+0+1}{3}=\frac23.

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.

Put an intercept column of ones beside the inputs:

A=[10111213],θ=[ab],Aθ=[aa+ba+2ba+3b].A=\begin{bmatrix}1&0\\1&1\\1&2\\1&3\end{bmatrix},\quad \theta=\begin{bmatrix}a\\b\end{bmatrix},\quad A\theta=\begin{bmatrix}a\\a+b\\a+2b\\a+3b\end{bmatrix}.

The output vector is y=[0,3,4,3]Ty=[0,3,4,3]^\mathsf T. The squared Euclidean norm Aθy2\lVert A\theta-y\rVert^2 adds the squares of its four entries. Then the loss is 14Aθy2\frac14\lVert A\theta-y\rVert^2. 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.

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.

Definition

Read the full glossary entry →