Skip to content

The unlikely happens too: chance and distributions

Flip virtual coins and watch the distribution appear: why the unlikely, repeated many times, ends up happening. AI course, 1.1.

Ramón Invarato
Once columnas de cristal de distintas alturas que dibujan una campana, con unas monedas al pie, sobre un suelo negro que las refleja, ante un fondo azul oscuro con una franja de luz cálida en el horizonte

An AI model answers with the most probable thing, not with certainties. To understand what that means, you first need one idea: the distribution, the shape that chance takes when it is repeated many times. And the unlikely is not impossible: repeat it enough times and it ends up happening.

Try this first

Open 10 coin flips in the Probability for AI tool. The chart answers one question: if you flip a coin 10 times, how many heads do you get? Each blue bar is a possible outcome, from 0 to 10 heads, and its height is the probability that it comes up. The tallest is the one at 5; the bars at the ends are so small that you can hardly see them.

Drawing of one round: ten coins, each heads or tails; in this one there are 6 heads, so it adds one to the bar at 6
The binomial of 10 coins in the Probability for AI tool before sampling: eleven blue bars, from 0 to 10 heads, the tallest at 5; at the bottom, the Sample button boxed

Click Sample. The tool runs the experiment for real: it flips the 10 coins 8000 times and records how many heads come up each time. The orange bars count how many times each number of heads came up, and they look a lot like the blue ones. (The dashed green line adds up the bars from left to right: it is the cumulative distribution, or CDF; you can remove it by unchecking CDF.)

Hover over the bar at 5 (or open the same coin with that bar already read). Below the chart it says P(X = 5) = 24.6% and how many of your 8000 rounds gave exactly 5 heads: about 1970. Now go to the far right, to 10, where the bar cannot even be seen (already read): P(X = 10) = 0.098%. And even so, in your 8000 rounds it has come up a few times; in the twenty times I clicked Sample to write this article, between 2 and 12.

The 10 coins flipped 8000 times in the Probability for AI tool: blue bars (probability) and orange bars (what came up); with the cursor at 5, P(X = 5) = 24.6%
With the cursor at 10, where the bar cannot even be seen: P(X = 10) = 0.098%, and even so it came up a few times in 8000 rounds

Finally, open the same coin, loaded, with 80% heads. The shape moves to the right and narrows a little.

The fair coin and the loaded coin side by side: the fair one, centered at 5 with deviation 1.581; the loaded one, at 8 and narrower, with 1.265

What just happened

A single round cannot be predicted. Many rounds can: they have a shape, and that shape is the distribution, the list of possible outcomes with the probability of each one. The one in this experiment, counting how many successes there are in a fixed number of attempts (here, counting heads), is called the binomial. It appears whenever hits are counted: how many of 100 customers buy, how many of a model’s answers are correct, or how many visitors click on each version of an A/B test. An A/B test shows two versions of something, A and B, to two groups of people assigned at random, and counts which one has more successes: more purchases, more clicks, fewer people leaving. Websites and apps use it to decide on a design, but so do software teams when they release a new version: they give it first to a part of the users and compare them with those who stay on the usual one before giving it to everyone. Each group is a binomial, and knowing how much each one varies is what tells you whether the difference is real, as explained in Is my improvement real?.

Two numbers summarize the shape, and the tool shows them on the left:

  • The mean, the center: with 10 flips and a 50% chance of heads, 5. In the binomial it is n⋅pn \cdot p (number of flips times the probability of heads): 10 × 0.5 = 5. With the loaded coin, 10 × 0.8 = 8.
  • The standard deviation (STD. in the tool), how far a result usually strays from the center: 1.581 for the fair coin. In the binomial it is n⋅p⋅(1−p)=2.5≈1.581\sqrt{n \cdot p \cdot (1 - p)} = \sqrt{2.5} \approx 1.581. With the loaded one, 1.6≈1.265\sqrt{1.6} \approx 1.265: the shape is narrower, as you saw.
The bars of the binomial of 10 flips with the mean and the deviation drawn. On the left, the fair coin: a red line at the mean, 5, and a double arrow of ± 1.58, one deviation, over a light blue band that runs from one deviation below the mean to one above. On the right, the loaded coin at 80%: the mean moves to 8 and the band, of ± 1.26, is narrower. At the bottom, the formulas: mean = n · p and deviation = √(n · p · (1 − p))

That the orange bars look like the blue ones is no coincidence: the more times you repeat an experiment, the closer the frequencies get to the probabilities (here, repeating is the number of rounds the tool plays at once, the N next to Sample: 8000 rounds look more like the blue bars than 200 do). This is the law of large numbers, and it was first proved, for experiments with two outcomes like a coin, by Jakob Bernoulli in his Ars Conjectandi, published in 1713 (Law of large numbers, Wikipedia).

An AI model always works with distributions. A language model, when choosing the next word, has in front of it a distribution over all the possible words, and both the good answers and the made-up ones come from how it chooses within it (explained in How it chooses the next word).

The classic mistake

You might think that “unlikely” is almost the same as “impossible”. Getting 10 heads out of 10 is 1 in 1024: on the chart the bar cannot even be seen. But in 8000 attempts, you should expect it to come up about 8000 / 1024 ≈ 8 times.

Drawing of ten coins, all heads: it happens once in every 1024 rounds, so in 8000 rounds you should expect to see it about 8 times

The calculation is always the same: probability × number of attempts. An AI system that makes a serious mistake in one case out of a thousand looks very reliable in a demo; if millions of people use it every day, that is thousands of serious mistakes every day.

Try it yourself

Open the normal distribution, the “bell curve”, which in AI is used to model noise and errors. Hover past 3 and read the probability that a value comes out above it. The questions below take you through calculating how many of the 8000 samples you expect there, and checking it with Sample.

The normal distribution in the Probability for AI tool, the bell centered at 0; with the cursor just past 3, the reading P(X > 3.006) = 0.13%

Test yourself

Open the normal with the cursor at 3. What is the probability that a value comes out above 3?

___%.

It is the reading next to the cursor, “P(X > 3)”.

See the answer

Answer: 0.13 ± 0.01.

Beyond three standard deviations there is very little bell left: a little more than 1 value in every 1000.

Test yourself

Before clicking Sample: of the 8000 samples, how many do you expect to come out above 3?

About ___.

See the answer

Answer: 10 ± 1.

8000 × 0.13% = 10.4. Now click Sample several times and look at the figure below the chart, “in your 8000 samples”: it comes out 5, 8, 11… With so few expected cases, chance weighs a lot. It is the same thing that happens with the coin below when there are few rounds.

And to see the law of large numbers at work, open the fair coin with just 200 rounds and click Sample several times in a row (each time it starts from zero with 200 new rounds). How much do the orange bars change from one time to the next? And with 8000?

Test yourself

What happens to the orange bars when going from 200 rounds to 8000?

  1. They change just as much from one time to the next: chance is the same.
  2. They change much less and stick to the probability bars.
  3. They change more, because there are more rounds that can come out odd.
See the answer

The answer is B.

This is the law of large numbers: the more rounds, the closer the frequencies get to the probabilities. The swing of a frequency goes down with the square root of the number of rounds: with 40 times more rounds, it moves about 6 times less (√40 ≈ 6.3).

Further reading

  • On Jarroba, La trampa del agente libre (in Spanish) uses the same idea in reverse: an AI agent that gets 95% of the steps right ends a 20-step task successfully only 35.8% of the time, because the probabilities of each step multiply.
  • Binomial distribution, Wikipedia: the formula for each bar and where it comes from.

“The unlikely happens too: chance and distributions” 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.