Skip to content

A sample is not the whole truth: minimal statistics

Why the number from a sample is not the true one, how much it changes, and why pure chance can look like a trend. AI course, 1.3.

Ramón Invarato
Un tarro de cristal lleno de cuentas y, delante, tres cucharas con cuentas, cada una con una mezcla algo distinta, sobre un suelo negro que las refleja, ante un fondo azul oscuro con una franja de luz cálida en el horizonte

What we know about an AI system usually comes from trying it on a few examples: a sample. And the number a sample gives is not the true value, because another sample, drawn from the same place, would give a different number. For the same reason, a series of random results can look like a trend.

Drawing: a population of 400 values with its true mean, 1.05, marked by a red line. Below, three samples of 10 values drawn from it: their means are 0.60, 0.88 and 1.33, and none of them matches the true one

Try this first

Open the CLT simulator in the Statistics tool (CLT is the central limit theorem, explained below), with the Exponencial population (exponential; the menu keeps the Spanish spelling), sample size 30, 3000 samples and seed 1. When it opens, the tool runs the experiment by itself. The population is an invented list of values that follow an exponential distribution: many small values and a few very large ones, like the minutes between one customer and the next in a shop or the time a server takes to answer. On the left, Starting population: how those values are spread. On the right, Sample means: the result of the experiment.

The tool’s exponential population has a mean of 1: it is the true value, the one we would like to find out. The tool writes it at the top as “Population: 1 · σ 1”: the population mean is 1 and its standard deviation, σ (the Greek letter sigma), is also 1; the dot only separates the two figures. The standard deviation is how far a value usually strays from the mean, as explained in Chance and distributions. The true mean is also marked by a red line on the chart on the right.

The tool acts as if it did not know it: it draws 30 values at random and computes the mean of those 30, which is all you would have in a real case. And it repeats that 3000 times. If a sample always told the truth, the 3000 means would all come out as 1. They don’t: the chart on the right shows how they are spread, some above and some below. Above the charts are the numbers that summarize it: mean of means, 0.995, and SE observed, 0.184 (the standard error: how far those means usually stray from their center, which is almost at the true 1; it is explained below, with a worked example).

Now press Compare with n×4: the tool repeats the experiment with samples of 120 and writes underneath “Before: n=30, SE 0.183 → Now: n=120, SE 0.091” (there the tool gives the theoretical SE, explained below; the observed one was 0.184). The SE drops to about half (the same simulator with 120, already run). With 300, to 0.057.

The CLT simulator in the Statistics tool with the exponential population. Top, samples of 30: the very skewed starting population on the left; the 3000 means in a bell shape on the right, with the red line at 1; and boxed, “Population: 1 · σ 1”, mean of means 0.995, SE observed 0.184. Bottom, after pressing Compare with n×4: “Before: n=30, SE 0.183 → Now: n=120, SE 0.091”, and on the same axis the bell of the means comes out half as wide

Finally, open the random walk, with 200 steps and seed 1. The green line goes up, goes down, goes up again and ends high, at +10. It looks like the chart of a stock on the market, with its good and bad streaks. Behind it, in gray, other walks with the same coin: each one ends somewhere different, some high and some low. Each of its 200 steps is a coin flip: +1 if it lands heads, −1 if it lands tails.

Drawing: twelve coins in a row, each head adds a step up and each tail a step down; the line of the position goes up, goes down and ends at +4 with nothing pushing it
The random walk in the Statistics tool, 200 steps with seed 1: the green line goes up, goes down and ends at +10, like a stock chart; behind it, in gray, other walks with the same coin that end between −16 and +20

What just happened

The population is everything you are interested in (all the customers, all the questions that will be asked of a model, and so on) and the sample is the part of the total that you study. The mean of a sample resembles that of the population, but it is not the same, and it changes from one sample to another.

In AI this happens every time a model is evaluated. Its true score would be the one it would get on all the possible questions; the one you see comes from the few on which you test it.

Drawing: the same model, which truly gets 80% of the questions right, evaluated with three tests of 30 questions, each shown as green and red squares. The scores come out 73%, 87% and 80%, around the true 80% and within a band of plus or minus 7 points, the standard error

How much it changes is measured with the standard error (SE): how far the mean of a sample usually strays from the true mean. It has a short formula, the one the tool calls “SE theoretical”: the spread of the population (σ, sigma) divided by the square root of the sample size (n).

SE=σnSE = \frac{\sigma}{\sqrt{n}}

In this population σ is 1, so with 30 values we get 1 / √30 ≈ 0.183; with 120, 1 / √120 ≈ 0.091; with 300, 0.058, within a thousandth of the 0.057 you saw. And note what the root says: to cut the error in half you need four times as much data, not twice.

Tip

To do it by hand

With the three samples of the first drawing, whose means came out 0.60, 0.88 and 1.33:

  • The mean of each sample is the sum of its values divided by how many there are. With 10 values, the sum divided by 10.
  • The mean of the means is the same with the means: (0.60 + 0.88 + 1.33) / 3 = 2.81 / 3 ≈ 0.94.
  • The observed SE is how far those means usually stray from their own mean, their standard deviation. Subtract the mean of the means from each one, square it, take the mean of those squares and take the root: (0.60 − 0.94)² ≈ 0.116; (0.88 − 0.94)² ≈ 0.004; (1.33 − 0.94)² ≈ 0.152. Their mean is 0.272 / 3 ≈ 0.091, and the observed SE is 0.091≈0.30\sqrt{0.091} \approx 0.30.
  • The theoretical SE needs no samples: σ / √n. With σ = 1 and n = 30, 1 / 5.48 ≈ 0.183.

The tool does the same with 3000 means instead of 3, and that is why its observed SE, 0.184, lands so close to the theoretical one.

The standard deviation of the population against the standard error of the means, the three curves on the same axis from 0 to 3, with the true mean, 1, marked by a red line. Top, the exponential population, which drops from 0 and stretches to the right, with an arrow for σ = 1. Middle, the means of 3000 samples of 30, a narrow bell around 1, with an arrow for SE = 1 / √30 ≈ 0.183. Bottom, the means of samples of 120, a bell half as wide, with SE = 1 / √120 ≈ 0.091. In each one, in light blue, the band of one standard deviation on each side of the mean

The shape of the chart has its own story too. The exponential population is very skewed (many small values and a few very large ones), and yet the means are spread in the shape of a bell. This is the central limit theorem (CLT): the means of many samples tend toward that shape whatever the starting population (as long as its spread is finite). Its first version is the de Moivre–Laplace theorem, according to which the binomial can be approximated by a bell, and the modern form was not stated precisely until the 1920s (Central limit theorem, on Wikipedia).

Drawing in three histograms with the mean 1 marked in red. The exponential population, very skewed: many small values and few large ones. The means of samples of 2, still skewed. The means of samples of 30, in a narrow bell shape around 1

That leaves the seed. A computer does not toss coins: it generates numbers that look random with a formula that starts from an initial value, the seed. With the same seed the same sequence always comes out (Pseudorandom number generator, on Wikipedia). That is why the “random” values the tool computes on your computer match the ones you see in this article, and why seeds are used when training models: they make it possible to repeat an experiment exactly. Try putting seed 2 in the CLT: the summaries come out the same up to the third decimal place, because with 3000 samples the differences cancel out. Each single sample varies a lot; the summary of 3000, very little.

Drawing: three rows of 16 coins. The two with seed 1 come out identical, head for head; the one with seed 2 comes out different

The classic mistake

You might think that if a line rises and falls in streaks, something must be pushing it. The random walk has nothing behind it: each step is a coin flip, and it still draws “trends”. The tendency to see meaningful streaks in what is chance has a name, the clustering illusion, and famous examples: during the Second World War, Londoners built theories about where the German flying bombs were aimed, and an analysis from 1946 by R. D. Clarke found that the impacts fitted a random distribution and gave no support to the idea of clusters (“An application of the Poisson distribution”, Journal of the Institute of Actuaries, 1946).

In AI the same thing happens, in two ways. A model trained on little data can learn the noise as if it were a pattern (explained in Fitting a curve). And when comparing two models on a small sample of tests, a difference of a few points can be just chance (it has its own article: Is my improvement real?).

Try it yourself

Test yourself

In the CLT in Statistics, with the exponential population, set the sample size to 480. Before looking, predict with the formula above what the theoretical SE will come out as.

It will be ___.

The σ of this population is 1. And 480 is four times 120.

See the answer

Answer: 0.046 ± 0.002.

SE = σ/√n = 1/√480 ≈ 0.046. It is half of what it was with 120 (0.091), just as with 120 it was half of that with 30: to halve the error you have to quadruple the sample. The observed SE of the 3000 means, 0.045, stays close by.

In the random walk, raise p (step +1) to 0.55: now the coin lands heads 55% of the time, so there is a real upward trend. Run it several times with different seeds, looking only at the line, and then raise the steps to 2000.

Test yourself

When can you tell by eye a walk with p = 0.55 from one with p = 0.5?

  1. With 200 steps it is already clear: the line always goes up.
  2. Hardly with 200 steps; with 2000, yes.
  3. Never: with p = 0.55 the trend is too small.
See the answer

The answer is B.

With p = 0.55, each step goes up 0.1 on average, so the trend grows with the steps: 20 in 200 steps, 200 in 2000. The noise grows too, but more slowly, with the root: about 14 in 200 steps and about 45 in 2000. With 200, the trend is hidden by the noise; with 2000, the trend wins by a wide margin. It is the same root as in the SE.

Further reading

“A sample is not the whole truth: minimal statistics” by Ramón Invarato at jarroba.com, licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0. Registered at Safe Creative.

Ramón Invarato

I design and build data platforms and intelligent systems, and I enjoy sharing that experience here.

You may also like

Comments

Leave a comment


This site uses Akismet to reduce spam. Learn how your comment data is processed.