Maths toolkit13 / 17

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.

0102030405060708000.10.20.30.40.50.6delivery time (minutes)share of deliveries

Each bar: the share of the 10,000 deliveries in that bin.

Bins 10 minutes wide. Tallest bar: 30–40 min.

share 0.470

Narrower bins: step 0 of 4
10,000 delivery times. As the bins narrow, each bar holds fewer deliveries, so share-per-bar shrinks toward 0. Divide by the bin width instead, and the bars settle onto a curve.

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 0.346÷5=0.0690.346 \div 5 = 0.069 per minute.
  • With 2-minute bins, it's 0.164÷2=0.0820.164 \div 2 = 0.082.
  • With 1-minute bins, it's 0.084÷1=0.0840.084 \div 1 = 0.084.

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 p(t)p(t) 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):

P(a<T<b)=∫abp(t) dtP(a < T < b) = \int_a^b p(t)\, dt

Read it as: the chance that the delivery time is between aa and bb is the area under the density from aa to bb. Note the two letters. Small pp is the density, a height. Capital PP 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: p(t)=0.8 plight(t)+0.2 pheavy(t)p(t) = 0.8\, p_{\text{light}}(t) + 0.2\, p_{\text{heavy}}(t). 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.

01020304050607000.050.1delivery time t (minutes)density p(t)01020304050607000.51delivery time t (minutes)CDF F(t)

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

Top: the delivery-time density, with the area between two times shaded. Bottom: the CDF, the running area from the left. The shaded area equals how much the CDF rises between the two times.

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 tt. This is the cumulative distribution function, or CDF:

F(t)=P(T≤t)=∫−∞tp(s) dsF(t) = P(T \le t) = \int_{-\infty}^{t} p(s)\, ds

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:

P(25<T<35)=F(35)−F(25)=0.769−0.091=0.678P(25 < T < 35) = F(35) - F(25) = 0.769 - 0.091 = 0.678

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

010203040506000.10.2xdensityμ = 30

Dark band: within 1σ of μ. Light band: within 2σ.

Peak height 1 ÷ (σ√(2π)) = 0.080

Area within 1σ: 0.683 · within 2σ: 0.954

Normal: move μ to slide the bell and σ to widen it. The shaded bands always hold about 68% and 95% of the area. Uniform: a flat density whose height is 1 ÷ width.

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 U(2,6)\mathcal{U}(2, 6).

The total area must be 1, and the width is 4. So the height is 1/4=0.251/4 = 0.25. In general:

p(x)=1b−afor a≤x≤bp(x) = \frac{1}{b - a} \quad \text{for } a \le x \le b

Later, in the auctions section, a bidder's value is often uniform between 0 and 100. Its density is 1/100=0.011/100 = 0.01, and the chance the value is over 80 is the area 20×0.01=0.220 \times 0.01 = 0.2.

The normal distribution

The bell curve is the normal distribution. It has two settings:

  • μ\mu, the mean, which sets where the peak is
  • σ\sigma, the standard deviation, which sets how wide the bell is

We write it N(μ,σ2)\mathcal{N}(\mu, \sigma^2). The second number is the variance, σ2\sigma^2. Its density uses the exponential:

p(x)=1σ2πexp⁡ ⁣(−(x−μ)22σ2)p(x) = \frac{1}{\sigma\sqrt{2\pi}} \exp\!\left(-\frac{(x - \mu)^2}{2\sigma^2}\right)

Read it from the inside out. (x−μ)/σ(x - \mu)/\sigma measures how many standard deviations xx is from the mean. Square it, halve it and make it negative. Then exp⁡\exp 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 μ\mu, and the bell slides without changing shape.
  • Make σ\sigma 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 1σ1\sigma of the mean, and about 95% (0.954) within 2σ2\sigma.

For light-traffic trips, N(30,42)\mathcal{N}(30, 4^2), 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 Δt\Delta t near time tt holds probability of about p(t) Δtp(t)\,\Delta t. Multiply by tt, add up all the slices, and let them get thin. That sum becomes an integral:

E[T]=∫t p(t) dt\mathbb{E}[T] = \int t \, p(t)\, dt

This is the expectation as an integral. It's the area under the curve t×p(t)t \times p(t). Move the slider to fill that area with more rectangles, like a Riemann sum.

010203040506070800123delivery time t (minutes)t × p(t)

Each slice: t × p(t) × 10.00 min

Total area ≈ 29.96 minutes

The area under t × p(t) is the expected delivery time. Rectangles at the slice midpoints estimate it. By 16 slices, the total is 32.00 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 (t−μ)2(t - \mu)^2 in place of tt: Var(T)=∫(t−μ)2p(t) dt\mathrm{Var}(T) = \int (t - \mu)^2 p(t)\, dt. For the shop's two-part density there's a shortcut. The variance equals E[T2]−μ2\mathbb{E}[T^2] - \mu^2, and each normal part has E[T2]=σ2+μ2\mathbb{E}[T^2] = \sigma^2 + \mu^2:

E[T2]=0.8 (16+900)+0.2 (64+1600)=1065.6Var(T)=1065.6−322=41.6\begin{aligned} \mathbb{E}[T^2] &= 0.8\,(16 + 900) + 0.2\,(64 + 1600) = 1065.6 \\ \mathrm{Var}(T) &= 1065.6 - 32^2 = 41.6 \end{aligned}

So the standard deviation is 41.6≈6.45\sqrt{41.6} \approx 6.45 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.

01020304050607000.050.1delivery time (minutes)density

Ticks on the axis: the sampled times.

0246810203040number of samplesestimatetrue 32.00

After 1 sample: estimate 29.61 min

The average of the sampled times. The true value is 32.00 min.

Step 0 of 12
Monte Carlo. Each step draws more samples. The average of t estimates E[T]. The share of samples above 40 minutes estimates P(T > 40), an area under the density.

Here is what to notice:

  • After 10 samples, the estimate of E[T]\mathbb{E}[T] is 31.06. After 5,000, it's 32.08. The true value is 32.
  • The other tab estimates the probability P(T>40)P(T > 40), 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, E[g(T)]\mathbb{E}[g(T)], draw samples t1,…,tnt_1, \dots, t_n and average:

E[g(T)]≈1n∑i=1ng(ti)\mathbb{E}[g(T)] \approx \frac{1}{n} \sum_{i=1}^{n} g(t_i)

With g(t)=tg(t) = t you estimate the mean. With g(t)=1g(t) = 1 if t>40t > 40, else 0, you estimate a probability. It's the sample mean from the last lesson, applied to g(t)g(t). Its error shrinks like 1/n1/\sqrt{n}, 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 nn spins of it. What does the distribution of the average look like?

The figure works out the exact distribution of the average of nn 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 0.2×0.2=0.040.2 \times 0.2 = 0.04. The orange curve is a normal with the same mean, 32, and the same standard deviation, 8.72/n8.72 / \sqrt{n}.

203040500.000.020.04average of n delivery times (minutes)probability ÷ gap

Mean 32 · SD 8.72 ÷ √1 = 8.72 minutes

Bars: the exact distribution of the average of n spins, drawn so that bar areas are probabilities. Orange: a normal curve with mean 32 and SD 8.72 ÷ √n. Slide n from 1 to 30.

By n=10n = 10 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 nn grows, the counting of "ways" takes on the same bell shape every time. In the figure, slide nn 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 0.084×1=0.0840.084 \times 1 = 0.084 for the minute around it.

"N(30,16)\mathcal{N}(30, 16) has standard deviation 16." The second number is the variance. The standard deviation is 16=4\sqrt{16} = 4.

"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, F(t)=P(T≤t)F(t) = P(T \le t), and P(a<T<b)=F(b)−F(a)P(a < T < b) = F(b) - F(a).
  • The uniform is flat. The normal is a bell with about 68% of its area within 1σ1\sigma and 95% within 2σ2\sigma.
  • E[T]=∫t p(t) dt\mathbb{E}[T] = \int t\,p(t)\,dt. When you can't integrate, average samples instead: a Monte Carlo estimate.

Section 2 shows how to compute these in PyTorch.