Lecture 3 · Animations
Honest evaluation, and classification metrics
Five widgets, in the order of the lecture. The first two draw what
LinearRegression().fit() computes — as a sum of squares in
the plane of the data, and as a projection in the space of the labels. The
third is the procedure that tells you whether a fitted model generalises:
$k$-fold cross-validation, one fold at a time. The last two move a
classifier’s threshold one instance at a time and trace the ROC and
precision/recall curves, then make the positive class rarer. Press
Step to advance one unit and read what happened.
All data on this page is synthetic — generated in your browser from a fixed seed, not the housing data or MNIST — so Reset always gives back the same picture.
1Least squares, as squares
Every residual $r_i = \hat y^{(i)} - y^{(i)}$ is drawn as a square of side $|r_i|$, so the shaded area is $\sum_i r_i^2 = m\cdot\mathrm{MSE}$. The green line is the normal-equation solution $\hat{\boldsymbol\theta}$, recomputed whenever the data change. Step adds one instance; drag any point to move it. The sliders move your line away from $\hat{\boldsymbol\theta}$, and the bar on the right shows what that costs.
With rows $\mathbf{x}^{(i)} = (1, x_i)$ stacked into $\mathbf{X}$,
$$\mathrm{MSE}(\boldsymbol\theta) = \tfrac{1}{m}\lVert\mathbf{X}\boldsymbol\theta - y\rVert_2^2,$$and setting the gradient to zero gives $\mathbf{X}^{\intercal}\mathbf{X}\hat{\boldsymbol\theta} = \mathbf{X}^{\intercal}y$. Here $\mathbf{X}^{\intercal}\mathbf{r} = (\sum_i r_i,\ \sum_i x_i r_i)$. For any $\boldsymbol\theta = \hat{\boldsymbol\theta} + \boldsymbol\Delta$, expanding the square and using $\mathbf{X}^{\intercal}\hat{\mathbf r} = \mathbf 0$,
$$\lVert\mathbf{X}\boldsymbol\theta - y\rVert^2 = \lVert\hat{\mathbf r}\rVert^2 + \lVert\mathbf{X}\boldsymbol\Delta\rVert^2.$$When $\mathbf{X}^{\intercal}\mathbf{X}$ is singular the widget uses $\mathbf{X}^{+} = (\mathbf{X}^{\intercal}\mathbf{X})^{+}\mathbf{X}^{\intercal}$, which follows from $\mathbf{X} = \mathbf{U}\boldsymbol\Sigma\mathbf{V}^{\intercal}$.
- With one instance, $m < n + 1 = 2$: every line through the point fits, and the pseudoinverse picks the one with the shortest $\boldsymbol\theta$. With two, the line passes through both and the area is zero.
- From three instances on, the area is positive unless the points happen to lie on one line, and $\mathbf{X}^{\intercal}\mathbf{r}$ stays at zero (to rounding) whatever you drag.
- Moving your line costs exactly $\lVert\mathbf{X}\boldsymbol\Delta\rVert^2$, and that does not depend on $y$ at all.
- Drag one point far from the line. Its square grows with the square of the distance, and the line swings towards it — one outlier can dominate the sum.
- At $m = 2$, drag one point until it is almost straight above the other. $\det\mathbf{X}^{\intercal}\mathbf{X} = (x_1 - x_2)^2$ heads to 0, the line through both turns towards vertical, and $\hat\theta_1$ grows without bound: near-singular, so a tiny move of one point swings $\hat{\boldsymbol\theta}$ wildly.
- Set $\Delta\theta_0 = 1$, $\Delta\theta_1 = 0$: the extra area is exactly $m$, one unit squared per instance.
2The normal equation is a projection
Three instances, so the label vector $y$ lives in $\mathbb{R}^3$: each axis on the left is one instance, not a feature. The columns of $\mathbf{X}$, the all-ones column $\mathbf{1}$ and the feature column $\mathbf{x}$, are two vectors in that space. Step builds the argument piece by piece; drag the left picture to turn it. On the right, the same fit in the usual picture.
$\mathbf{X}\boldsymbol\theta = \theta_0\mathbf{1} + \theta_1\mathbf{x}$ ranges over the column space of $\mathbf{X}$. The closest point of a subspace to $y$ is its orthogonal projection $\hat y$, so the residual $\mathbf{r} = \mathbf{X}\hat{\boldsymbol\theta} - y$ is orthogonal to both columns:
$$\mathbf{1}^{\intercal}\mathbf{r} = 0,\qquad \mathbf{x}^{\intercal}\mathbf{r} = 0,$$which together say $\mathbf{X}^{\intercal}(\mathbf{X}\hat{\boldsymbol\theta} - y) = \mathbf 0$.
Here $\det\mathbf{X}^{\intercal}\mathbf{X} = m\sum_i (x_i - \bar x)^2$, since $\mathbf{X}^{\intercal}\mathbf{X}$ has entries $m$, $\sum_i x_i$ and $\sum_i x_i^2$. It is zero exactly when $\mathbf{x}$ is a multiple of $\mathbf 1$.
- The residual meets the plane at a right angle, and both dot products in the side panel are zero to rounding.
- As the spread of $\mathbf{x}$ shrinks, the plane is still the plane but $\mathbf{x}$ lines up with $\mathbf{1}$, and $\hat{\boldsymbol\theta}$ grows without bound: near-singular.
- At spread 0 the plane collapses to a line. $\hat y$ is still unique; only $\boldsymbol\theta$ is not.
- Step to the end, then turn the picture until the plane is seen edge-on, as a line. $\mathbf{r}$ is then drawn at a right angle to it: the angle is real, not a trick of the view.
- Slide the spread from 1 down to 0.1: $\hat y$ does not move at all — for any nonzero spread the plane is the same plane — but $\hat\theta_1$ goes from $0.38$ to $3.81$. Then set it to 0: the plane collapses to a line and $\hat y$ jumps to $\bar y\,\mathbf 1$.
3k-fold cross-validation
Forty instances and three models: a constant (the training mean), a straight line by least squares, and a decision tree grown until every leaf holds one instance. Step runs one fold: the hollow points are held out, the three models are fitted on the rest, and each is scored by RMSE on the held-out points only. The right-hand plot collects the scores.
where $\mathrm{RMSE}_j$ scores the model trained without fold $j$ on fold $j$. The spread is the standard deviation of the $k$ fold scores. Two models are compared fold by fold: $d_j = \mathrm{RMSE}_j^{A} - \mathrm{RMSE}_j^{B}$, with the mean and spread of the $d_j$. In one dimension, a tree split halfway between neighbouring $x$ values until each leaf holds one instance predicts the $y$ of the nearest training instance.
- On the training rows the tree scores exactly 0. Cross-validated, it is indistinguishable from the straight line: $1.226$ against $1.229$.
- The constant's training and cross-validated scores are close, $1.90$ against $1.82$: it is too rigid to memorise anything.
- The fold scores scatter by more than the gap between the line and the tree. Read the paired difference, not the two means.
- Play all five folds and read the paired difference: the tree is better on some folds and worse on others.
- Press New shuffle a few times. The means move, and the ranking of tree and line can flip — which is what a difference smaller than the spread means.
- Switch to $k = 10$: twice the fits, and four held-out points per fold. The tree's fold scores now spread three times as widely, $\pm 0.48$ against $\pm 0.15$: an RMSE on four points is a noisy measurement.
4Tracing the ROC and PR curves
The same 200 instances, now ranked from the highest score down (the strip on top). Step lowers the threshold past the next instance in the ranking, flagging it, and each curve gains one vertex. Both curves come from the same sweep; they differ only in what they divide by. The faint curves show where the sweep will end.
Each negative $j$ flagged moves the ROC curve right by $1/N$ at height $\mathrm{TPR} = a_j/P$, where $a_j$ is the number of positives ranked above it. Adding up those strips,
$$\text{AUC} = \frac{1}{PN}\sum_{\text{negatives } j} a_j,$$the fraction of (positive, negative) pairs in the right order.
Average precision here is the un-interpolated sum
$\mathrm{AP} = \sum_k (R_k - R_{k-1})\,\text{precision}_k$ — each
positive adds $1/P$ times the precision at its own rank, which is what
Scikit-Learn's average_precision_score computes.
- The ROC curve only ever moves up or right: both its coordinates divide by numbers the threshold cannot touch.
- The PR curve moves up and to the right at a positive, and straight down at a negative: recall stays put while the flagged count grows.
- When the sweep ends, the accumulated area equals the pair count in the side panel exactly.
- Set the separation $\Delta$ to 0: both classes draw scores from the same distribution. The ROC curve wanders around the diagonal and AUC lands near $0.5$; precision ends at $p = 0.1$.
- Set $\Delta = 4$ and play: almost every positive is flagged before any negative, AUC is close to 1, and the PR curve stays high until recall is nearly 1.
5A rarer positive class
2,000 negatives and 200 positives, scored by one fixed classifier. Step deletes one positive at random — the scores of everything left are unchanged — so the base rate $p$ falls from 9.1% towards 1%. Solid curves are now; dashed curves are the start. The marker on each curve is the operating point at threshold $t$. The lower plot follows the accuracy of that detector against the classifier that never fires.
The classifier that never fires has $\mathrm{TP} = \mathrm{FP} = 0$, so its accuracy is $N/m = 1 - p$. For any detector,
$$\begin{aligned}\text{accuracy} - (1-p) &= \frac{\mathrm{TP}+\mathrm{TN}-N}{m} \\ &= \frac{\mathrm{TP}-\mathrm{FP}}{m},\end{aligned}$$using $\mathrm{TN} + \mathrm{FP} = N$. So a detector beats doing nothing on accuracy exactly when $\mathrm{TP} > \mathrm{FP}$, that is, when its precision is above $1/2$. Deleting positives leaves FPR untouched (no negative moves) and samples TPR from the same scores; precision $= \mathrm{TP}/(\mathrm{TP}+\mathrm{FP})$ has the shrinking $\mathrm{TP}$ against a fixed $\mathrm{FP}$.
- The ROC curve stays on top of its dashed starting curve, with only the jitter of fewer positives. AUC never leaves $0.953$–$0.963$.
- The PR curve sinks: average precision falls from $0.79$ to about $0.50$, with nothing about the classifier changed.
- At $t = 2$, FP stays at 55 throughout. The detector's accuracy meets the never-fires line at the step where TP falls to 55, and is below it from then on.
- Play to the end and compare the operating point on the two plots: on the ROC curve it has barely moved, on the PR curve precision has fallen from $0.72$ to $0.20$.
- Set $t$ to $3$ and play. Only 6 false positives, so the detector stays above the never-fires line all the way down to $p \approx 1\%$ — at the price of recall: it flags 61 of the 200 positives at the start, where $t = 2$ flags 144.