SCIENCE IN MOTION

Chance Machine

hover a bin to compare it with the theory
space pause · 1 one ball · D +1000 · hold F speed up · ←→ p · ↑↓ rows · P Pascal · M sound · R reset · H menu
⚄ Chance Machine · Eduardo Correia

Add up random things

Pick a source of randomness. Draw n values from it and take their average. Repeat many times and make a histogram of the averages. Whatever the source looks like, the averages form a bell. This is the central limit theorem.

averages so far

Histogram of the averages

Yellow: the averages you collected. Pink: the normal curve with the same mean and standard deviation σ/√n.

averages of n values normal curve mean μ

The source (n = 1)

How far from a bell?

A bell has skewness 0 (it is symmetric) and excess kurtosis 0 (its tails are "normal"). For averages of n values, the skewness shrinks like 1/√n and the kurtosis like 1/n.

skewness
excess kurtosis
|skewness| |excess kurtosis| measured now

Q-Q plot: is it a bell?

Each dot compares a quantile of your averages (standardized) with the same quantile of a perfect bell. If the dots lie on the diagonal, the shape is normal. Curved ends mean skewed or heavy tails.

Why a bell?

An average of many values is rarely extreme: to get a very high average, all the values must be high at the same time, which is unlikely. Values near the middle can be made in many more ways. Count the ways and the bell appears, like the paths on the Galton board.

The lottery is the hardest case: almost always 0, rarely 10. It is very skewed, so it needs a large n before the bell shows up. Try it with n = 5 and n = 50.

The theorem needs a source with a finite variance. Some wild distributions (like the Cauchy) never turn into a bell: their averages are as spread as a single value. Everything in this tab has a finite variance.

The average settles down

Flip a coin over and over and follow the fraction of heads. At first it jumps around. Then it gets closer and closer to 50%. This is the law of large numbers. Each line is a different person flipping their own coin.

flips each

The dashed line is the true value. The shaded funnel is ±2 standard errors (2σ/√n): about 95% of the people should be inside it at any moment.

But the difference grows!

The gambler's fallacy

The ratio converges, the difference does not

After 10 000 flips, heads and tails typically differ by about 80. That sounds like a lot, but 80 out of 10 000 is less than 1%. The fraction goes to 50% because the difference grows only like √n, while n grows much faster.

So luck is not "corrected". A coin that gave 10 heads in a row has the same 50% chance on the next flip. The past is not cancelled; it is diluted by the many flips that come after it.

Measure π with randomness

Error × number of tries

Both axes are logarithmic. Yellow: the error of your estimate. The dashed line has slope −½: the typical error falls like 1/√N. Ten times more tries give only about three times less error.

|estimate − π| typical error (theory)

Why it works, and why it is slow

Each dart is a yes/no experiment with a fixed chance q. The fraction of yes answers estimates q, with a typical error of √(q(1 − q)/N). That √N is the price of randomness: 10 000 darts give about 2 correct digits, a million about 3.

It is still one of the most useful ideas in science. When a problem has too many dimensions to compute directly (neutrons in a reactor, light in a film scene, risk in finance), you simulate many random cases and count. The method was named after the Monte Carlo casino by Ulam and von Neumann in the 1940s.

Buffon's needle (1777) was the first problem of this kind. A needle of length l falls on lines spaced d apart (with l ≤ d). It crosses a line with probability 2l/(πd), so π ≈ 2l·N / (d·crossings).

The birthday paradox

How many people must be in a room for two of them to share a birthday, with a 50% chance? Most people guess about 180. The answer is 23. Build random groups and see for yourself (365 equally likely days, no leap years).

chance of a shared birthday

One group, on the calendar

one person shared birthday

Chance × group size

Pink: someone shares a birthday (exact formula). Dots: your simulated groups. Blue: someone shares YOUR birthday, a much rarer event.

any two people match simulated someone matches you

Why our intuition fails

We think about ourselves: "will someone share my birthday?" That is rare: with 23 people, only about 6%. But the question is about any pair. Pairs grow like n²/2, so 23 people make 253 pairs, and each pair is a new chance to match.

The same math protects your passwords and your bank: a computer "fingerprint" (a hash) with N possible values starts to collide after only about √N tries. Security engineers call it the birthday attack.

Stay or switch?

A car is behind one door, goats are behind the others. You pick a door. The host, who knows where the car is, opens the other doors except one, always showing goats. Then he asks: do you stay with your door or switch to the last closed one?

chance to win if you switch

The game

Scoreboard

Wins over many games

Share of games won so far by each strategy (automatic games). Dashed: the theory.

always stay always switch

Still not convinced? Use 100 doors

You pick door 37. The host, who knows everything, opens 98 doors with goats and leaves only door 37 and door 64 closed. Is it really 50/50? Your door had a 1-in-100 chance when you picked it, and nothing has changed that. Door 64 survived a careful search by the host: it holds the other 99 in 100.

The host's knowledge is the key. If he opens doors at random and just happens to show only goats, switching wins only half of those games. Tick the box above and check it. Many great mathematicians got this wrong when Marilyn vos Savant published the puzzle in 1990.

The drunkard's walk

Each walker flips a coin at every step to choose a direction. Hundreds of them start together at the same point and slowly spread out. This is how a drop of ink spreads in water: diffusion.

steps taken

Where are they now?

Distance from the start × steps

Yellow: the root mean square distance of your walkers. Pink: √t. The walkers get away, but slowly: 4 times more steps, only twice as far.

Will the drunkard get home?

In 1D and in 2D, a random walker always comes back to the start sooner or later (with probability 1), even though it may take a very long time. In 3D it is different: the chance of ever coming back is only about 34%. George Pólya proved this in 1921. Someone summed it up as: "a drunk man will find his way home, but a drunk bird may get lost forever."

Random walks describe pollen grains shaken by water molecules (Brownian motion, explained by Einstein in 1905), the spreading of heat, and, roughly, the up-and-down of stock prices.

Can you fake randomness?

Type a sequence of 100 coin flips from your head, trying to look random: press H for heads and T for tails (or use the buttons). Then the machine compares it with real random sequences. Most people get caught.

Your sequence

Each square is a flip. The longest streak is outlined.

Longest streak

Number of switches

Randomness is clumpy

Real randomness has streaks and clusters, and our brains read them as patterns: a "hot hand" in basketball, a "lucky" slot machine, a cluster of illnesses in a town. Often it is just chance doing what chance does.

A teacher's trick: half the class flips a real coin 100 times, the other half invents the results. The teacher looks at the papers and points at the fakes in seconds: they have no streak of 6 or more.

1

What is a probability?

A probability is a number from 0 (never) to 1 (always). There are two ways to see it. Counting: a die has 6 equally likely faces, so a six has probability 1/6. Frequency: roll it many times and about 1/6 of the rolls are sixes. The law of large numbers says both views agree.

Two rules do most of the work. For results that cannot happen together, add. For independent events, multiply: two sixes in a row is 1/6 × 1/6 = 1/36.

2

The Galton board is a binomial

A ball that ends in bin k went right k times out of n. One such path has probability pk(1 − p)n−k, and there are C(n, k) such paths: the numbers of Pascal's triangle. The mean bin is np and the variance is np(1 − p).

3

Why the bell appears

When a result is the sum of many small, independent pushes, the extreme results need all the pushes to agree, and the middle results can be made in many ways. Abraham de Moivre found in 1733 that the binomial then looks like this curve: the normal distribution, or bell curve. Francis Galton built his board in 1873 to show it.

4

The central limit theorem

Take n independent values from the same distribution, with mean μ and standard deviation σ. Their average has mean μ and standard deviation σ/√n, and for large n its shape is close to normal, whatever the original shape was (if its variance is finite). This is why heights, measurement errors and test scores are often bell-shaped.

5

The law of large numbers

The average of many independent tries gets close to the true mean, and stays close. The typical error is σ/√n: to halve the error you need 4 times more tries. The law says nothing about the next try: there is no "due" result. Thinking otherwise is the gambler's fallacy.

6

Monte Carlo: compute by playing dice

If a number is a probability in disguise, you can estimate it by simulation. The darts measure the area of a circle; Buffon's needles measure the chance of crossing a line. The error falls like 1/√N, so the method is slow but works in any number of dimensions.

7

Famous surprises

Birthdays: 23 people give a 50.7% chance of a shared birthday, because there are 253 pairs. Monty Hall: switching wins 2/3 of the time, because the host's choice carries information. Streaks: in 100 flips, a streak of 6 or more happens about 80% of the time. In all three, the honest count beats the gut feeling.

8

Random walks and diffusion

A walk of t random ±1 steps is a sum of independent pushes, so its end point follows the bell again, with spread √t. The same √t appears everywhere: a smell spreads through a room, heat goes through a wall, a molecule wanders in a cell.

9

Where do "random" numbers come from?

A computer follows rules, so it cannot really flip a coin. It uses a pseudo-random generator: a formula that turns a starting number, the seed, into a long sequence that passes statistical tests for randomness. This lab uses one called mulberry32. The same seed always gives the same sequence, which is handy for repeating an experiment.

🏆

Challenges

  1. On the Galton board with p = 0.5, which bin gets the most balls with 12 rows? And with 11 rows?
  2. Set p = 0.1 and 4 rows. Does the normal curve still fit? Now try 20 rows. Why the difference?
  3. In the Bell curve tab, what is the smallest n that makes the lottery look like a bell on the Q-Q plot?
  4. Draw your own source with two far-apart spikes. How many values must you average to get one bell?
  5. Estimate π to 3 correct digits with darts. How many darts did it take?
  6. Find the smallest group with a 99% chance of a shared birthday.
  7. Play Monty Hall 20 times yourself with 10 doors, always switching. How many cars did you win?
  8. Write 100 "random" flips that the machine cannot catch.
  9. Make 1000 walkers take 400 steps. About how far from the start is a typical walker?
Eduardo Correia

About

Science in Motion is created and maintained by Eduardo Correia, sponsored by Apliant.

⌨️

Shortcuts

space pause / play (new group or game in Birthdays and Monty Hall) · D do a lot at once · R reset · N new seed · M sound · H hide/show the menu

Galton board: 1 one ball · hold F speed up · ←→ p · ↑↓ rows · P Pascal's triangle

Bell curve and Birthdays: ←→ n · Monty Hall: 1–9 pick a door, S stay, W switch · Streaks: H heads, T tails, Backspace undo