Maths toolkit12 / 17

Lesson 12 · Probability and information

Averages you can trust: expectation, variance and learning from samples

The problem

A customer asks how long their pizza will take. You can't know for sure. The shop's spinner from the last lesson says 20, 30, 40 or 50 minutes, with these probabilities:

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

You want one number to quote. You also want to know how far off that number is likely to be. And in real life, nobody hands you the table. You only see deliveries, one at a time. Can you learn the right number from them?

This lesson answers all three questions. The last one is the start of how machines learn from experience. Reinforcement learning uses it on almost every step.

Expectation: the long-run average

Imagine 100 deliveries that follow the spinner exactly: 20 take 20 minutes, 50 take 30, 20 take 40 and 10 take 50. Their average is:

20×20+50×30+20×40+10×50100=3200100=32 minutes\frac{20 \times 20 + 50 \times 30 + 20 \times 40 + 10 \times 50}{100} = \frac{3200}{100} = 32 \text{ minutes}

Dividing each count by 100 turns it into a probability. So the same average is each time multiplied by its probability, then added up:

20×0.2+30×0.5+40×0.2+50×0.1=4+15+8+5=3220 \times 0.2 + 30 \times 0.5 + 40 \times 0.2 + 50 \times 0.1 = 4 + 15 + 8 + 5 = 32

This is the expected value, or expectation. We write it E[T]\mathbb{E}[T]. It is a weighted sum, with the probabilities as weights:

E[T]=∑tt P(T=t)\mathbb{E}[T] = \sum_t t \, P(T = t)

Read it as: for each possible value tt, multiply it by its probability, then add them up.

The balance point

Picture each bar as a weight sitting on a seesaw at its time. The expectation is where you'd put the pivot to make the seesaw balance. Drag the bars below and watch the pivot move.

2030405000.20.40.60.81delivery time (minutes)probabilitymean 32.0

Drag a bar’s dot up or down. The other bars shrink or grow to keep the total at 1.

20 × 0.20 + 30 × 0.50 + 40 × 0.20 + 50 × 0.10

E[T] = 32.0 minutes

Each bar is a weight on a seesaw. The green triangle is the pivot that balances it: the expected value. Drag a bar's dot to change its probability.

Here is what to notice:

  • Raise the 50-minute bar, and the pivot slides right. A rare long delay pulls the average up.
  • Put all the weight on 30, and the expected value is exactly 30.
  • The expected value doesn't have to be a possible outcome. No delivery takes 32 minutes on this spinner. 32 is the average, not a forecast for any one trip.

Predict first

You change the spinner so 20 and 50 minutes each have probability 0.5. What is the expected delivery time?

Variance: how spread out?

Here are three shops. All have an expected delivery time of 32 minutes. But you'd rather order from some of them than others.

Pick a shop in the figure. The shaded band shows how far the times typically stray from 32.

0Shop0Market0Bridge0Park0Station0School1Hill

After 0 rounds: 1 of 7 junctions have heard about the jam at Hill.

Only the jammed junction has a value. Press the step button.

Each round, every junction: new value = 0.5 × own value + 0.5 × neighbours’ average. The same rule at every junction. Tap a junction to move the jam.

Round 0 of 3
Three shops with the same mean, 32 minutes, and different spreads. The table works out the variance: square each distance from the mean, weight it by its probability, and add.

To measure spread, look at how far each outcome is from the mean, t−32t - 32. Some distances are negative, and they would cancel the positive ones. So square them first, which makes them all positive. Then take the expected value of those squares.

That's the variance:

Var(T)=E[(T−E[T])2]=∑t(t−E[T])2 P(T=t)\mathrm{Var}(T) = \mathbb{E}\big[(T - \mathbb{E}[T])^2\big] = \sum_t (t - \mathbb{E}[T])^2 \, P(T = t)

Worked example

The variance of our shop's delivery times

  1. Distances from 32: 20−32=−1220 - 32 = -12, 30−32=−230 - 32 = -2, 40−32=840 - 32 = 8 and 50−32=1850 - 32 = 18.

Squaring changes the units: minutes become "minutes squared", which is hard to picture. So we usually take the square root. That's the standard deviation, written σ\sigma (the Greek letter sigma):

σ=Var(T)\sigma = \sqrt{\mathrm{Var}(T)}

For the three shops, σ\sigma is 8.72 minutes (ours), 4 minutes (steady) and 14.70 minutes (all-or-nothing). Same average, very different promises to a customer.

Predict first

Every delivery from our shop gets 5 minutes slower. What happens to the variance?

Learning from samples

So far you've had the spinner's table. In real life, you don't. You see deliveries, one at a time, and want to estimate the expected value from them.

The obvious estimate is the average of what you've seen. If you've seen nn delivery times x1,x2,…,xnx_1, x_2, \dots, x_n, the sample mean is:

μ^n=1n∑i=1nxi\hat\mu_n = \frac{1}{n} \sum_{i=1}^{n} x_i

The hat on μ^\hat\mu (said "mu hat") marks it as an estimate. Plain μ\mu (the Greek letter mu) is the true expected value, here 32. In general, a hat over a symbol means "our estimate of it, from data".

Does the average settle?

Press play. Each grey dot is one delivery time drawn from the spinner. The blue line is the average of all the deliveries so far.

024681020304050number of deliveries so farminutestrue mean 32

Grey dots: each delivery’s time. Blue line: the average of all deliveries so far. The axis zooms out as samples arrive.

Delivery 1 took 40 minutes.

Average of the first 1: 40.00 minutes

Step 0 of 23
The running average wobbles a lot at first, then settles close to 32. The horizontal axis zooms out as samples arrive.

Here is what to notice:

  • Early on, one unusual delivery swings the average a lot. After 10 deliveries, the three runs are at 29, 28 and 28 minutes.
  • Later, a new delivery barely moves it. After 1,000 deliveries, the three runs are at 31.89, 31.79 and 32.03.
  • Every run settles near 32, along a different path.

This is the law of large numbers. As you average more independent samples, the sample mean tends to settle closer to the expected value. It's why "long-run average" and "expected value" mean the same thing.

Call the wobble how far the average typically lands from 32: its standard deviation. The wobble shrinks slowly. To halve it, you need four times as many samples. The Deeper aside below shows why.

DeeperWhy the average wobbles like 1/√nSpreads don't add, but variances do. A table of 10,000 simulated runs shows it. · 4 min

Average nn deliveries. Do it again with nn new deliveries, and you get a slightly different average. How much do these averages vary?

Start with the sum of nn independent delivery times. For independent things, variances add. Each delivery has variance 76, so the sum has variance 76n76n.

The average is the sum divided by nn. Dividing a random variable by nn divides its variance by n2n^2, because variance is in squared units. So:

Var(μ^n)=76nn2=76nsoSD of μ^n=σn=8.72n\mathrm{Var}(\hat\mu_n) = \frac{76 n}{n^2} = \frac{76}{n} \qquad\text{so}\qquad \text{SD of } \hat\mu_n = \frac{\sigma}{\sqrt{n}} = \frac{8.72}{\sqrt{n}}

A simulation checks this. Take 10,000 averages for each nn, and measure how much they spread out:

nnSD of 10,000 averages8.72/n8.72 / \sqrt{n}
18.748.72
44.354.36
162.192.18
641.081.09
2560.550.54

Each time nn goes up 4 times, the spread halves. Standard deviations don't add; variances do. That's why the square root appears.

Updating the average one sample at a time

You don't want to store every delivery time and re-add them all each time a new one arrives. There's a shortcut. You only need the current average and the count.

Worked example

A running average of 30, 40, 20, 30 and 50 minutes

  1. Start with any guess. Say μ^0=0\hat\mu_0 = 0. The first delivery is 30, so the error (sample minus estimate) is 30−0=3030 - 0 = 30. Move all the way: 0+1×30=300 + 1 \times 30 = 30. The average of one sample is that sample.

Each step works out the error, then moves a fraction of the way across it. With αn=1/n\alpha_n = 1/n:

μ^n=μ^n−1+αn (xn−μ^n−1)\hat\mu_n = \hat\mu_{n-1} + \alpha_n \, (x_n - \hat\mu_{n-1})

Read it as: new estimate = old estimate + step size × (new sample − old estimate). The bracket xn−μ^n−1x_n - \hat\mu_{n-1} is the error: how far the new sample is from the current estimate.

Why is this the same as the plain average? Write the average of nn samples using the average of the first n−1n - 1:

μ^n=(n−1) μ^n−1+xnnold total, plus the new sample=n−1n μ^n−1+1nxnsplit the fraction=μ^n−1−1nμ^n−1+1nxnn−1n=1−1n=μ^n−1+1n (xn−μ^n−1)collect the 1n\begin{aligned} \hat\mu_n &= \frac{(n-1)\,\hat\mu_{n-1} + x_n}{n} && \text{old total, plus the new sample} \\ &= \frac{n-1}{n}\,\hat\mu_{n-1} + \frac{1}{n} x_n && \text{split the fraction} \\ &= \hat\mu_{n-1} - \frac{1}{n}\hat\mu_{n-1} + \frac{1}{n} x_n && \tfrac{n-1}{n} = 1 - \tfrac{1}{n} \\ &= \hat\mu_{n-1} + \frac{1}{n}\,(x_n - \hat\mu_{n-1}) && \text{collect the } \tfrac{1}{n} \end{aligned}

This rule is the incremental mean update. We often drop the subscripts and write it with an arrow, which means "replace with":

μ^←μ^+α (x−μ^)\hat\mu \leftarrow \hat\mu + \alpha\,(x - \hat\mu)

The number α\alpha (the Greek letter alpha) is the step size. It says what fraction of the error to move by. With α=1/n\alpha = 1/n, the steps shrink as you see more samples, and you get the plain average.

Where does the estimate start? You pick any first guess, μ^0\hat\mu_0. With α=1/n\alpha = 1/n, the first step is 1/1=11/1 = 1, so the guess is wiped out at once.

With a constant α\alpha, the guess keeps a weight of (1−α)n(1 - \alpha)^n after nn samples. That fades, but it never quite reaches 0. Reinforcement learning often starts from μ^0=0\hat\mu_0 = 0, so this matters there.

Step through the update below. The dashed arrow is the error. The orange arrow is the move, α\alpha times the error. Try the step-size slider at "1/n", at 0.5 and at 1.

0102030405060minutesμ̂ = 0

Starting guess: μ̂ = 0. Press play or step forward.

Samples: 30 40 20 30 50

Step 0 of 10
The update rule, one sample at a time, starting from a guess of 0. First the error appears (dashed). Then the estimate moves by α × error (orange).

With α=0.5\alpha = 0.5, the estimates are 15, 27.5, 23.75, 26.875 and 38.4375. The first step only gets halfway from 0 to 30, and the starting guess still drags the estimate down. With 1/n1/n, they are the running averages 30, 35, 30, 30, 34.

Predict first

Use a constant step size α = 1 instead. What does the estimate equal after each sample?

Choosing the step size

Why would you ever use anything other than 1/n1/n? Because the world changes. Say that after 100 deliveries the shop hires faster drivers, and every trip gets 6 minutes shorter. The true average drops from 32 to 26.

The plain average still counts the old, slow deliveries as much as the new ones. A constant step size, like α=0.1\alpha = 0.1, always moves 10% of the way toward the newest sample. Old samples fade out.

Both estimates start from a guess of 0. Move the slider and compare them.

05010015020001020304050number of deliveries so farminutesfaster drivers start
━ α = 1/n (plain average)━ α = 0.10 (constant)╍ true mean

After delivery 100 (true mean 32): 1/n gives 32.6, α gives 31.9

After delivery 200 (true mean 26): 1/n gives 28.6, α gives 23.2

Grey dots: delivery times. After delivery 100, the true mean (green dashes) drops from 32 to 26. Blue: the plain average (α = 1/n). Orange: a constant step size.

Here is what to notice:

  • The plain average (blue) is smooth, but slow to react. After 200 deliveries it's still at 28.6, though the true mean has been 26 for 100 deliveries.
  • A constant α\alpha (orange) follows the change. But it never stops wobbling. With α=0.1\alpha = 0.1, its last 50 values range from 21.8 to 27.9.
  • At the start, the constant-α\alpha estimate climbs up from the guess of 0. The plain average jumps to the first sample at once.
  • Big α\alpha: fast to react, very noisy. Small α\alpha: smooth, slow to react. At α=1\alpha = 1 it copies the dots.

So 1/n1/n settles down and is right when nothing changes. A constant α\alpha keeps tracking and never fully settles. Neither is better everywhere. You pick based on whether the thing you're estimating can change.

You'll see this rule again and again

μ^←μ^+α(x−μ^)\hat\mu \leftarrow \hat\mu + \alpha(x - \hat\mu) is the core of reinforcement learning. A bandit learner uses it to estimate how good each choice is. Monte Carlo methods use it to average returns. Temporal-difference learning uses it with a target that is itself an estimate. And when training neural networks, the step size is called the learning rate.

DeeperConstant step sizes: tracking vs settlingA constant α weights old samples less and less. That's why it tracks, and why it never stops wobbling. · 4 min

Unroll the update with a constant α\alpha. Rewrite it as μ^←(1−α) μ^+αx\hat\mu \leftarrow (1 - \alpha)\,\hat\mu + \alpha x. So after each step, the old estimate keeps a share 1−α1 - \alpha of its weight, and the new sample gets α\alpha. A sample from kk steps ago ends up with weight:

α(1−α)k\alpha (1 - \alpha)^k

With α=0.1\alpha = 0.1, the newest sample has weight 0.1. One from 10 steps ago has 0.1×0.910≈0.0350.1 \times 0.9^{10} \approx 0.035. One from 50 steps ago has about 0.0005. So the estimate is mostly an average of the last few dozen samples. That's why it can follow a change.

The starting guess μ^0\hat\mu_0 is treated the same way. After nn samples it keeps weight (1−α)n(1 - \alpha)^n. All the weights add up to 1:

(1−α)n⏟first guess+∑k=0n−1α(1−α)k=1\underbrace{(1 - \alpha)^n}_{\text{first guess}} + \sum_{k=0}^{n-1} \alpha (1 - \alpha)^k = 1

With α=0.1\alpha = 0.1 and a first guess of 0, the guess still has 0.910≈0.350.9^{10} \approx 0.35 of the weight after 10 samples. So the estimate is pulled about a third of the way toward 0. After 50 samples its weight is 0.950≈0.0050.9^{50} \approx 0.005, small but not zero.

It's also why it never settles. It's always an average of only a few dozen samples, so it always wobbles. If the true mean stays still, the wobble has a standard deviation of about:

σα2−α\sigma \sqrt{\frac{\alpha}{2 - \alpha}}

For our shop with α=0.1\alpha = 0.1, that's 8.72×0.1/1.9≈2.08.72 \times \sqrt{0.1 / 1.9} \approx 2.0 minutes. A smaller α\alpha means a smaller wobble, but a slower reaction.

With α=1/n\alpha = 1/n, the weights stay equal for every sample, and the wobble shrinks to zero. Steps that shrink like this, but not too fast, are what make an estimate settle on the right answer. Reinforcement learning states this as the Robbins–Monro conditions.

Common misconceptions

"The expected value is the most likely value." Not always. Our spinner's most likely time is 30 minutes, and its expected value is 32. The expected value can even be impossible, like 35 for a 20-or-50 spinner.

"Variance is in minutes." It's in minutes squared. The standard deviation is in minutes.

"A smaller step size is always better." Smaller steps wobble less but react slower. When the thing you're estimating changes, a step size that's too small stays stuck on old data.

Recap

  • The expected value is the probability-weighted average of the outcomes: the balance point.
  • Variance is the expected squared distance from the mean. The standard deviation is its square root, in the original units.
  • The sample mean tends to settle closer to the expected value as samples pile up (the law of large numbers). Its wobble shrinks like 1/n1/\sqrt{n}.
  • μ^←μ^+α(x−μ^)\hat\mu \leftarrow \hat\mu + \alpha(x - \hat\mu) updates an estimate one sample at a time. With α=1/n\alpha = 1/n it is the sample mean.
  • A constant step size tracks changes but never fully settles.

Section 2 shows how to compute these in PyTorch.