Maths toolkit11 / 17

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.

20 min20%30 min50%40 min20%50 min10%late

Drag the round handles on the edge to resize the slices.

2030405000.20.40.60.81delivery time (minutes)probability0.200.500.200.10

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

The spinner for delivery times. Each slice's share of the circle is that time's probability. The bars show the same numbers. However you reshape it, the total stays 1.

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 P(30 min)=0.5P(\text{30 min}) = 0.5 for "the probability that a delivery takes 30 minutes is 0.5". The letter PP 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 TT is the time the next delivery takes.

Capital TT is the delivery time before you know it. Small tt stands for one particular value it could take, like 30. So P(T=t)P(T = t) reads "the probability that the delivery time equals tt".

The list of possible values, with a probability for each, is the random variable's distribution. For the shop's spinner:

Delivery time tt20304050
P(T=t)P(T = t)0.20.50.20.1

The distribution is the whole spinner in one table. It is the full description of what TT 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:

P(late)=P(T=40)+P(T=50)=0.2+0.1=0.3P(\text{late}) = P(T = 40) + P(T = 50) = 0.2 + 0.1 = 0.3

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 AA is:

P(A)=∑t in AP(T=t)P(A) = \sum_{t \text{ in } A} P(T = t)

Read it as: for each outcome tt 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.

20 min20%30 min50%40 min20%50 min10%2030405000.20.40.60.81delivery time (minutes)share of spins

Bars: share of spins so far (the number on top is the count). Dashed lines: slice sizes.

Press play to start spinning.

Step 0 of 12
Early on, the shares jump around: after one spin, one outcome has 100%. After hundreds of spins, each bar sits close to its dashed line.

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 uu 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 u=0.75u = 0.75 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:

LateOn timeTotal
Heavy traffic12820
Light traffic166480
Total2872100

The chance that two things both happen is their joint probability. We write it P(heavy and late)P(\text{heavy and late}). From the table, 12 of the 100 deliveries were in heavy traffic and late:

P(heavy and late)=12100=0.12P(\text{heavy and late}) = \frac{12}{100} = 0.12

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 P(late)=0.28P(\text{late}) = 0.28.

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:

P(late∣heavy)=1220=0.6P(\text{late} \mid \text{heavy}) = \frac{12}{20} = 0.6

This is a conditional probability. The bar ∣\mid reads as "given". So P(late∣heavy)P(\text{late} \mid \text{heavy}) 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.

heavy trafficlight trafficfilled = late, hollow = on time

Late: 28 of 100, so P(late) = 0.28

Late and heavy traffic: 12 of 100, so P(late and heavy) = 0.12

Zooming in. 'Only heavy traffic' keeps 20 dots, and 12 of them are filled (late). 'Only late' keeps 28 dots, and 12 of them are squares (heavy). Same 12, different groups underneath.

Dividing both counts by 100 doesn't change the answer: 12/20=(12/100)÷(20/100)=0.12÷0.2012/20 = (12/100) \div (20/100) = 0.12 \div 0.20. The top is a joint probability, and the bottom is the probability of the group. So the general rule is:

P(A∣B)=P(A and B)P(B)P(A \mid B) = \frac{P(A \text{ and } B)}{P(B)}

With our numbers: P(late∣heavy)=0.12/0.20=0.6P(\text{late} \mid \text{heavy}) = 0.12 / 0.20 = 0.6.

P(late | heavy) is not P(heavy | late)

Swapping the two sides changes the group you zoom in on.

P(late∣heavy)=12/20=0.6P(\text{late} \mid \text{heavy}) = 12/20 = 0.6: among heavy-traffic trips, 60% were late. P(heavy∣late)=12/28≈0.43P(\text{heavy} \mid \text{late}) = 12/28 \approx 0.43: 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:

P(late∣pineapple)=725=0.28P(\text{late} \mid \text{pineapple}) = \frac{7}{25} = 0.28

That's exactly the overall rate, P(late)=0.28P(\text{late}) = 0.28. 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:

P(late and pineapple)=P(late)×P(pineapple)=0.28×0.25=0.07P(\text{late and pineapple}) = P(\text{late}) \times P(\text{pineapple}) = 0.28 \times 0.25 = 0.07

And indeed, 7 of the 100 deliveries were both. Traffic is different: P(late)×P(heavy)=0.28×0.2=0.056P(\text{late}) \times P(\text{heavy}) = 0.28 \times 0.2 = 0.056, 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 P(heavy∣late)P(\text{heavy} \mid \text{late}). 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

  1. Start with what you know. Heavy traffic happens on 20% of trips: P(heavy)=0.2P(\text{heavy}) = 0.2. In heavy traffic, 60% of trips are late. In light traffic, 20% are.

Written as a formula, with HH for the cause (heavy traffic) and EE for the evidence (late):

P(H∣E)=P(E∣H) P(H)P(E)P(H \mid E) = \frac{P(E \mid H)\, P(H)}{P(E)}

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?

heavylight trafficlate 0.12late 0.16on time 0.64

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

Bayes' rule as areas. Each area is a probability. P(heavy | late) is the dark area on the left divided by all the dark area.

Try this: drag P(heavy traffic) down to 0.05. Lateness is still three times as likely in heavy traffic. But now P(heavy∣late)P(\text{heavy} \mid \text{late}) 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 9/108≈0.0839 / 108 \approx 0.083, about 8%.

Bayes' rule gives the same answer: P(has it∣+)=0.9×0.010.9×0.01+0.1×0.99=0.0090.108≈0.083P(\text{has it} \mid +) = \dfrac{0.9 \times 0.01}{0.9 \times 0.01 + 0.1 \times 0.99} = \dfrac{0.009}{0.108} \approx 0.083.

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." P(heavy and late)=0.12P(\text{heavy and late}) = 0.12 counts against all 100 deliveries. P(late∣heavy)=0.6P(\text{late} \mid \text{heavy}) = 0.6 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.
  • P(A∣B)P(A \mid B) zooms in on the cases where BB happened: P(A and B)/P(B)P(A \text{ and } B) / P(B).
  • Independent things don't inform each other, so their joint probability is a product.
  • Bayes' rule turns P(E∣H)P(E \mid H) into P(H∣E)P(H \mid E). A rare cause stays fairly rare, even after evidence.

Section 2 shows how to compute these in PyTorch.