Lesson 13 · Probability and information
Continuous chance: densities and the normal distribution
The problem
So far, the shop's spinner gave delivery times of 20, 30, 40 or 50 minutes. Real deliveries don't snap to tens. One takes 31.7 minutes, another 28.25, another 33.0412.
A time like this can be any number in a range. A random variable like this is a continuous random variable. You can't list its outcomes in a table, because there are infinitely many.
Worse, any one exact value has probability 0.
What's the chance that a delivery takes exactly 31.7000… minutes, to every decimal place? It's 0. Yet some delivery time always happens. So probability must live somewhere other than single points. This lesson shows where: in areas.
From histogram to density
Here are 10,000 delivery times from the shop's records. A histogram groups them into bins and draws a bar for each. Start with the tab "Height = share", and press play to make the bins narrower.
Each bar: the share of the 10,000 deliveries in that bin.
Bins 10 minutes wide. Tallest bar: 30–40 min.
share 0.470
Predict first
The bins halve in width. With height = share, what happens to the tallest bar?
The fix is to divide each share by the bin's width. That gives share per minute. Switch to the tab "Height = share ÷ width" and step through again.
- With 5-minute bins, the tallest bar is per minute.
- With 2-minute bins, it's .
- With 1-minute bins, it's .
The heights stop shrinking and settle onto the orange curve. Its height near 30 minutes is about 0.084. That curve is the density.
Now each bar's area is height × width, and that's the share of deliveries in it. So for bins of any width, area is probability. That stays true as the bars shrink into the curve.
Probability is area under the density
A probability density is a curve with one job: the area under it between two values is the probability of landing between them. As an integral (the area under the curve):
Read it as: the chance that the delivery time is between and is the area under the density from to . Note the two letters. Small is the density, a height. Capital is a probability, an area. Two rules follow from "area is probability":
- The density is never negative.
- The total area under it is 1. Every delivery takes some time.
The shop's density has a bump near 30 minutes and a long tail to the right. Most trips take 25 to 35 minutes. Trips in heavy traffic can take much longer.
DigressionWhere this density comes fromTwo bell curves mixed together: one for light traffic, one for heavy. · 2 min
The shop's model mixes two kinds of trip. On 80% of trips the traffic is light. Then the time follows a bell curve centred on 30 minutes, with a standard deviation of 4. On the other 20%, traffic is heavy. Then the curve is centred on 40 minutes, with a standard deviation of 8.
The density is the weighted sum: . The heavy part makes the right-hand tail. The bell curves themselves are normal distributions, explained below.
Like any model, it's a simplification. For example, it gives negative times a tiny chance: less than 1 in 10 million. That's small enough to ignore.
Areas and the CDF
Drag the two orange dots below to choose a range of times. The shaded area is the probability of a delivery time in that range.
Drag the orange dots on the top axis. The thick orange bar below is the rise of the CDF between them.
F(35) − F(25) = 0.769 − 0.091
P(25 < T < 35) = shaded area = 0.678
From 25 to 35 minutes, the area is 0.678. So about 68% of deliveries take between 25 and 35 minutes.
The lower plot is the running area: the area under the density from the far left up to each time . This is the cumulative distribution function, or CDF:
The CDF is an accumulation function, like the scooter's odometer in the integrals lesson. It starts at 0 and climbs to 1. To get the area between two times, subtract:
Here is what to notice:
- The CDF climbs steeply where the density is tall, near 30 minutes. That's where most deliveries land.
- Where the density is near zero, the CDF is flat.
- The slope of the CDF is the density. That's the fundamental theorem of calculus again: the slope of a running area is the height of the curve.
Predict first
A density can be taller than 1 somewhere. Does that break the rule that probabilities are at most 1?
Two shapes you'll meet everywhere
Dark band: within 1σ of μ. Light band: within 2σ.
Peak height 1 ÷ (σ√(2π)) = 0.080
Area within 1σ: 0.683 · within 2σ: 0.954
The uniform distribution
When a driver arrives at a door, they wait for the customer. Say the wait is anywhere from 2 to 6 minutes, with no time more likely than another. The density is flat between 2 and 6, and 0 outside. This is the uniform distribution, written .
The total area must be 1, and the width is 4. So the height is . In general:
Later, in the auctions section, a bidder's value is often uniform between 0 and 100. Its density is , and the chance the value is over 80 is the area .
The normal distribution
The bell curve is the normal distribution. It has two settings:
- , the mean, which sets where the peak is
- , the standard deviation, which sets how wide the bell is
We write it . The second number is the variance, . Its density uses the exponential:
Read it from the inside out. measures how many standard deviations is from the mean. Square it, halve it and make it negative. Then gives 1 at the mean and falls off fast on both sides. The number in front scales the curve so its area is 1.
Here is what to notice in the figure:
- Change , and the bell slides without changing shape.
- Make bigger, and the bell gets wider and shorter. The area stays 1, so it has to.
- Whatever you choose, about 68% of the area (0.683) lies within of the mean, and about 95% (0.954) within .
For light-traffic trips, , that means about 68% of them take 26 to 34 minutes.
Expectation as an integral
For the spinner, the expected value was a sum: each value times its probability. For a density, the same idea uses thin slices. A slice of width near time holds probability of about . Multiply by , add up all the slices, and let them get thin. That sum becomes an integral:
This is the expectation as an integral. It's the area under the curve . Move the slider to fill that area with more rectangles, like a Riemann sum.
Each slice: t × p(t) × 10.00 min
Total area ≈ 29.96 minutes
With 2 slices the total is 4.21, and with 4 it's 55.19. Both are far off, because wide rectangles miss the bump or overshoot it. With 8 slices it's 29.96, and from 16 slices on it's 32.00. So the expected delivery time is 32 minutes, the same as the spinner's.
The variance works the same way, with in place of : . For the shop's two-part density there's a shortcut. The variance equals , and each normal part has :
So the standard deviation is minutes. The spinner in the last lesson had the same mean, 32, but a variance of 76. Same average, less spread.
Monte Carlo: estimate by sampling
Often you can't work out the integral. The density may have no neat formula, or there may be hundreds of variables. But you can usually draw samples. Then the law of large numbers gives a way out: the average of many samples gets close to the expected value.
Press play. The ticks on the top axis are sampled delivery times. The orange line below is the running average.
Ticks on the axis: the sampled times.
After 1 sample: estimate 29.61 min
The average of the sampled times. The true value is 32.00 min.
Here is what to notice:
- After 10 samples, the estimate of is 31.06. After 5,000, it's 32.08. The true value is 32.
- The other tab estimates the probability , the shaded tail area. After 5,000 samples, the share above 40 is 0.107. The true area is 0.105.
- A probability is an expected value too. Score each sample 1 if it's over 40 minutes and 0 if not. The average of those scores is the share of samples over 40.
This is a Monte Carlo estimate. To estimate the expected value of anything, , draw samples and average:
With you estimate the mean. With if , else 0, you estimate a probability. It's the sample mean from the last lesson, applied to . Its error shrinks like , as it did there.
Why ML cares
Reinforcement learning wants expected rewards, but it can't integrate over everything that might happen. So it plays, collects samples and averages them. That's a Monte Carlo estimate, and Monte Carlo methods in RL are named after it. Training on random mini-batches of data relies on the same idea.
DeeperWhy bell curves appear everywhereAverage a few spins of any spinner and a bell appears. This is the central limit theorem. · 4 min
The shop's spinner looks nothing like a bell. It has four separate bars, and it's lopsided. Now average spins of it. What does the distribution of the average look like?
The figure works out the exact distribution of the average of spins. With 2 spins, the average can be 20, 25, 30, …, 50. For example, it's 20 only if both spins give 20, with probability . The orange curve is a normal with the same mean, 32, and the same standard deviation, .
Mean 32 · SD 8.72 ÷ √1 = 8.72 minutes
By or so, the bars follow the bell closely. This is the central limit theorem. Average many independent random values, and the result is close to normal, for almost any shape you start with.
Why? There are few ways to get an extreme average: every spin has to be extreme in the same direction. There are many ways to land near the middle, because highs and lows cancel. As grows, the counting of "ways" takes on the same bell shape every time. In the figure, slide up and watch the middle bars grow while the end bars shrink.
So a quantity made of many small independent effects tends to look normal. Measurement errors and heights are common examples. It's also why the wobble of a Monte Carlo estimate looks like a bell around the true value.
Common misconceptions
"The density at 30 minutes is the probability of 30 minutes." No. The probability of exactly 30 minutes is 0. The density, about 0.084 per minute near 30, says how much probability is packed nearby: about for the minute around it.
" has standard deviation 16." The second number is the variance. The standard deviation is .
"P(T < 35) and P(T ≤ 35) are different." For a continuous variable they're equal. The single point 35 adds no area.
Recap
- A continuous random variable can take any value in a range. Any single exact value has probability 0.
- A density's area over a range is the probability of that range. Its height is probability per unit, and can be above 1.
- The CDF is the running area, , and .
- The uniform is flat. The normal is a bell with about 68% of its area within and 95% within .
- . When you can't integrate, average samples instead: a Monte Carlo estimate.
Section 2 shows how to compute these in PyTorch.