Applications of Machine Learning

Lecture 2 · Animations

The end-to-end project

Three ways a number can mislead before any model is any good: a test score that was used to choose, a split that does not represent the data, and a correlation coefficient read as more than it measures. Each widget repeats an experiment many times, because each of these failures is about what happens across repetitions, not on one. Press Step for one more repetition and read what happened.

All data on this page is synthetic, generated in your browser from a fixed seed, so Reset always gives back the same picture.

1Choosing on the test set

A model chosen by its test score, run many times. $N$ candidate models are scored on one test set of $n$ instances, and the best score is reported. Every candidate here has no skill at all: each prediction is a fair coin, so every true accuracy is exactly $0.5$. The chosen candidate is then scored once more on a fresh test set. Each Step is one complete experiment.

The mathematics

Candidate $j$ gets $K_j \sim \mathrm{Binomial}(n, \tfrac12)$ test instances right, independently of the others, so its score $K_j/n$ has mean $\tfrac12$. The reported score is $M = \max_j K_j/n$. With $F$ the binomial distribution function, independence gives $P(nM < k) = F(k-1)^N$, and for a count $Z \ge 0$, $E[Z] = \sum_{k \ge 1} P(Z \ge k)$, so

$$E[M] = \frac1n\sum_{k=1}^{n}\Big(1 - F(k-1)^N\Big).$$

This is the dashed curve. It grows with $N$, because $F(k-1)^N$ falls as $N$ grows. The fresh score uses instances that played no part in the choice, so its mean is $\tfrac12$ whatever was chosen.

Watch for
  • The reported mean settles near the exact $E[M] = 0.593$ for $N = 20$, $n = 100$, while the fresh mean settles near $0.5$, the truth.
  • Which candidate wins is pure chance. Nothing about the winner is better; it was luckier on these $n$ instances.
  • The gap is a statement about repetitions. In a single experiment the fresh score can even beat the reported one.
Try this
  1. Set $N = 1$: no choice is made, and the two histograms become the same distribution, centred on $0.5$.
  2. Set $N = 5$, say five values of one hyperparameter: $E[M] = 0.558$ on 100 instances. Then $N = 200$: $0.636$, from coin flips.
  3. With $N = 20$, raise $n$ to 1,000: $E[M]$ falls to $0.530$. A larger test set shrinks the bias, but never removes it.

2Random against stratified splits

A synthetic population of districts with a median income each, cut into income bands as in the deck: below 1.5, 1.5–3, 3–4.5, 4.5–6, and above 6. Each Step draws a 20% test set twice: once purely at random, once stratified on the band. The left plot shows how far each band's share of the test set is from its share of the whole population; the right plot keeps the worst band of every draw.

The mathematics

Band $k$ holds $n_k$ of the $m$ districts, a share $q_k = n_k/m$. A test set of $T = f m$ districts with $c_k$ from band $k$ has share $\hat q_k = c_k/T$, and the error shown is $100\,(\hat q_k - q_k)/q_k$ per cent.

Drawn at random, $c_k$ is hypergeometric, with mean $T q_k$ and variance $T q_k(1-q_k)\frac{m-T}{m-1}$. So the relative error has standard deviation about $\sqrt{(1-q_k)(1-f)/(f\,m\,q_k)}$: worst for the smallest band, and shrinking like $1/\sqrt m$.

Stratified, $c_k$ is $f n_k$ rounded to a whole number (largest remainders, so that $\sum_k c_k = T$). The only error left is the rounding.

Watch for
  • The random split's worst band is most often the smallest one, below 1.5, with about 4% of the districts: in 191 of the first 300 draws.
  • The stratified split lands on the same proportions on every draw: which districts it picks changes, how many from each band does not.
  • The random split's worst-band error is usually a few per cent, and now and then far more. One draw cannot tell you which you got.
Try this
  1. Shrink the population to 2,000 districts: the random split's errors roughly triple, as $1/\sqrt m$ says ($\sqrt{10} \approx 3.2$).
  2. Raise the number of bands to 10: every band is smaller, so the random split gets worse, while the stratified one stays within rounding.

3What the correlation coefficient sees

Pearson's $r$ for a growing sample. On the left, the data as they are, with the means $\bar x$ and $\bar y$ dashed. On the right, the same points in standard units: $r$ is the average of the products $z_x z_y$, so a point in the shaded top-right or bottom-left quarter pushes $r$ up (blue circle) and a point in the other two pulls it down (gold triangle). Step adds one point.

The mathematics $$r = \frac{\sum_i (x_i-\bar x)(y_i-\bar y)}{\sqrt{\sum_i (x_i-\bar x)^2\,\sum_i (y_i-\bar y)^2}},$$ $$r = \frac1m\sum_i z_{x,i}\,z_{y,i},$$

with $z_{x,i} = (x_i - \bar x)/s_x$ and $s_x$ the standard deviation with divisor $m$ (likewise for $y$). Replacing $y$ by $b\,y + c$ with $b > 0$ leaves every $z_{y,i}$, and so $r$, unchanged: $r$ cannot see the slope. If $y = b x$ exactly, every $z_{y,i} = z_{x,i}$ and $r = \frac1m\sum_i z_{x,i}^2 = 1$.

The points are drawn in mirror pairs, $x$ and $-x$ (on the circle, $\phi$ and $-\phi$). With an even $m$ on the parabola, $\bar x = 0$ and the two points of a pair share $y$, so their products $z_x z_y$ are equal and opposite.

Watch for
  • The right-hand picture does not change when you move the slope slider with the noise at 0. Neither does $r$.
  • On the parabola every point has a mirror image in the neighbouring quarter: their products cancel, and $r$ is exactly 0 whenever $m$ is even, although $y$ is a function of $x$.
  • With few points $r$ jumps about; it settles as $m$ grows. A correlation is a measurement on a sample, with a spread.
Try this
  1. On the line, set the noise to 0, then the slope to 0.01 and to 100: $r = 1.000$ both times. Inches against nanometres.
  2. Switch to the parabola with noise 0 and step: $y$ is exactly $x^2$, and $r$ reads $0.000$ at every even $m$. The circle does the same.
  3. Play to 150 points on the line with noise 0.5, then slide the slope from 1 down to 0.1: the relationship is the same shape, but $r$ falls from $0.71$ to $0.01$. With the noise fixed, a shallower line accounts for less of $y$'s spread; $r$ measures that, not how steep the line is.