Lesson 11 · Probability and information
Chance: probability and distributions
The problem
Your pizza shop promises delivery within 35 minutes. Some deliveries take 20 minutes. Some take 50. You can't say in advance how long the next one will take.
But you can say how often each time happens. Out of your last 1,000 deliveries, about 200 took 20 minutes and about 500 took 30.
Those shares let you answer useful questions. How often are we late? Is lateness worse in heavy traffic? A delivery was late: how likely is it that traffic was the cause?
This lesson turns "how often" into numbers you can calculate with. The ML models later in the book use these same numbers. They predict probabilities rather than single answers.
A spinner for delivery times
Picture a spinner, like the one in a board game. It is split into four slices, one for each delivery time. A bigger slice means that time happens more often. Spin it, and where the pointer stops tells you how long the next delivery takes.
Drag the round handles on the edge of the spinner below to change the slice sizes.
Drag the round handles on the edge to resize the slices.
Total: 0.20 + 0.50 + 0.20 + 0.10 = 1.00
P(late) = P(40 min) + P(50 min) = 0.20 + 0.10 = 0.30
A slice that covers a fifth of the circle comes up about a fifth of the time. That share is the outcome's probability. A probability is a number from 0 to 1. A probability of 0 means "never", and 1 means "always".
We write for "the probability that a delivery takes 30 minutes is 0.5". The letter stands for probability.
Two rules always hold, whatever shape you give the spinner:
- Every probability is between 0 and 1. A slice can't be bigger than the whole circle, or smaller than nothing.
- The probabilities of all the outcomes add up to 1. The slices fill the circle exactly once.
Random variables and distributions
The outcome of a spin is a number: 20, 30, 40 or 50 minutes. A number that comes from a chance process is called a random variable. We name it with a capital letter. Here is the time the next delivery takes.
Capital is the delivery time before you know it. Small stands for one particular value it could take, like 30. So reads "the probability that the delivery time equals ".
The list of possible values, with a probability for each, is the random variable's distribution. For the shop's spinner:
| Delivery time | 20 | 30 | 40 | 50 |
|---|---|---|---|---|
| 0.2 | 0.5 | 0.2 | 0.1 |
The distribution is the whole spinner in one table. It is the full description of what might do.
A distribution over a fixed list of outcomes, with any probability for each, is called a categorical distribution. Every spinner is one. The outcomes are often labels, like pizza toppings or the moves in a game. We often number them 0, 1, 2, … and keep the probabilities in a list.
A preview of reinforcement learning
An agent that picks moves at random, with a probability for each move, follows a categorical distribution. In reinforcement learning, the agent spins its own spinner to choose a move. Learning means reshaping the slices so that good moves come up more often.
Events: is this delivery late?
You promise delivery within 35 minutes. So a delivery is late if it takes 40 or 50 minutes. "Late" is not one outcome. It's a group of outcomes. A group like this is called an event.
To get the probability of an event, add up the probabilities of the outcomes inside it:
In the figure, the "late" event is the arc drawn around the 40 and 50 slices. Reshape the spinner and watch the sum change.
With sum notation, the probability of an event is:
Read it as: for each outcome inside the event, take its probability, then add them all up.
Predict first
You drag the spinner so that 30 minutes takes up 0.8 of the circle. Can P(late) still be 0.3?
Spinning many times
A probability describes the long run. One spin tells you very little: it lands on one slice, and that's all. Spin many times, and the share of spins that land in each slice gets close to the slice's size.
Press play below. The figure spins the shop's spinner 1,000 times. The bars show the share of spins so far that landed on each time. The dashed lines show the slice sizes.
Bars: share of spins so far (the number on top is the count). Dashed lines: slice sizes.
Press play to start spinning.
Here is what to notice:
- After 1 spin, one bar is at 1 and the rest are at 0. A share from a single spin is a poor guess.
- After 1,000 spins, every bar is within a few hundredths of its slice size.
- Press "New run" to get different spins. The early steps look different every time. The late steps look almost the same.
The figure spins with a single random number between 0 and 1. That number says how far round the circle the pointer stops. The slices sit end to end: 20 minutes covers 0 to 0.2, 30 minutes covers 0.2 to 0.7, and so on. So lands in the 40-minute slice, which covers 0.7 to 0.9. This is one common way to draw from a categorical distribution.
Two things at once: joint probability
The shop also records the traffic on each trip. Here are the last 100 deliveries, counted by traffic and by whether they were late:
| Late | On time | Total | |
|---|---|---|---|
| Heavy traffic | 12 | 8 | 20 |
| Light traffic | 16 | 64 | 80 |
| Total | 28 | 72 | 100 |
The chance that two things both happen is their joint probability. We write it . From the table, 12 of the 100 deliveries were in heavy traffic and late:
Dividing every cell by 100 gives the joint probability of each pair. The four numbers, 0.12, 0.08, 0.16 and 0.64, add up to 1.
Learning something: conditional probability
Overall, 28 of the 100 deliveries were late, so .
Now a driver heads out, and you know the traffic is heavy. Should you still expect a 0.28 chance of lateness? No. You've learned something, so you only care about the heavy-traffic deliveries. There were 20, and 12 of them were late:
This is a conditional probability. The bar reads as "given". So is "the probability of late, given heavy traffic".
The trick is to zoom in. Knowing the traffic is heavy throws away the 80 light-traffic deliveries. The count on top stays 12, but the count underneath shrinks from 100 to 20.
In the figure, each dot is one delivery. Pick a group to zoom in on, and the dots outside it fade.
Late: 28 of 100, so P(late) = 0.28
Late and heavy traffic: 12 of 100, so P(late and heavy) = 0.12
Dividing both counts by 100 doesn't change the answer: . The top is a joint probability, and the bottom is the probability of the group. So the general rule is:
With our numbers: .
P(late | heavy) is not P(heavy | late)
Swapping the two sides changes the group you zoom in on.
: among heavy-traffic trips, 60% were late. : among late trips, 43% had heavy traffic. The top count is the same 12. The group underneath is different. Try both buttons in the figure.
Independence: when knowing doesn't help
Switch the figure to "Split by pineapple". Of the same 100 deliveries, 25 had pineapple on the pizza. Seven of those 25 were late:
That's exactly the overall rate, . Knowing that the order had pineapple tells you nothing about lateness. When that happens, the two things are independent.
For independent things, the joint probability is the product of the separate ones:
And indeed, 7 of the 100 deliveries were both. Traffic is different: , but the real joint probability is 0.12. So traffic and lateness are not independent.
Predict first
Two fair coins are tossed separately. What is the probability that both land heads?
Turning it around: Bayes' rule
A customer complains that their pizza was late. What's the chance that traffic was heavy? You want . But the numbers you usually know go the other way: how common heavy traffic is, and how likely lateness is in each kind of traffic.
Bayes' rule turns a conditional probability around. Here is how it works with our numbers.
Worked example
P(heavy | late), from what the shop knows
Start with what you know. Heavy traffic happens on 20% of trips: . In heavy traffic, 60% of trips are late. In light traffic, 20% are.
Written as a formula, with for the cause (heavy traffic) and for the evidence (late):
The top is "cause and evidence together". The bottom is "the evidence, from either cause".
In the figure, the square is all deliveries. Its width is split by traffic. In each column, the dark part is the late trips. Bayes' rule asks: what share of the dark area is on the left?
Strong colour = late. Each area is a probability; the whole square is 1.
P(late) = 0.20 × 0.60 + 0.80 × 0.20 = 0.12 + 0.16 = 0.28
P(heavy | late) = 0.12 ÷ 0.28 = 0.43
Try this: drag P(heavy traffic) down to 0.05. Lateness is still three times as likely in heavy traffic. But now drops to about 0.14. A rare cause stays fairly unlikely, even after you see evidence for it. The left column is too thin to hold much of the dark area.
Predict first
Heavy traffic makes lateness 3 times as likely. Heavy traffic happens on 10% of trips. What share of late deliveries happened in heavy traffic?
DigressionThe base-rate fallacy: why a positive test often isn't scaryA 90%-accurate test for a rare condition is usually wrong when it says yes. · 3 min
A condition affects 1 person in 100. A test for it says "positive" for 90% of people who have it. It also says "positive" for 10% of people who don't. You test positive. How worried should you be?
Count it out for 1,000 people:
- 10 have the condition. The test catches 9 of them.
- 990 don't. The test wrongly flags 10% of them: 99 people.
So 108 people test positive, and only 9 of them have the condition. That's , about 8%.
Bayes' rule gives the same answer: .
The trap is to ignore how rare the condition is. That rarity is the base rate. To see it in the figure above, set P(heavy traffic) to 0.01, P(late | heavy) to 0.9 and P(late | light) to 0.1. Read "heavy" as "has the condition" and "late" as "tests positive". The left column is very thin.
Common misconceptions
"A probability of 0.3 means it happens 3 times in 10." Only in the long run. In 10 actual deliveries you might see 1 late, or 5. The spinning figure shows how far short runs can stray.
"Independent means the events can't both happen." No. "Can't both happen" is a different idea. If one happens, you know the other didn't, so they are strongly linked, not independent. Late and pineapple are independent, and 7 deliveries were both.
"The conditional probability is the joint probability." counts against all 100 deliveries. counts against only the 20 heavy ones. Same top, different bottom.
Recap
- A probability is a long-run share, between 0 and 1. The probabilities of all outcomes add to 1.
- A random variable is a number that comes from chance. Its distribution lists each value's probability.
- An event's probability is the sum of its outcomes' probabilities.
- zooms in on the cases where happened: .
- Independent things don't inform each other, so their joint probability is a product.
- Bayes' rule turns into . A rare cause stays fairly rare, even after evidence.
Section 2 shows how to compute these in PyTorch.