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 | 20 | 30 | 40 | 50 |
|---|---|---|---|---|
| 0.2 | 0.5 | 0.2 | 0.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:
Dividing each count by 100 turns it into a probability. So the same average is each time multiplied by its probability, then added up:
This is the expected value, or expectation. We write it . It is a weighted sum, with the probabilities as weights:
Read it as: for each possible value , 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.
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
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.
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.
To measure spread, look at how far each outcome is from the mean, . 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:
Worked example
The variance of our shop's delivery times
Distances from 32: , , and .
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 (the Greek letter sigma):
For the three shops, 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 delivery times , the sample mean is:
The hat on (said "mu hat") marks it as an estimate. Plain (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.
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
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 deliveries. Do it again with new deliveries, and you get a slightly different average. How much do these averages vary?
Start with the sum of independent delivery times. For independent things, variances add. Each delivery has variance 76, so the sum has variance .
The average is the sum divided by . Dividing a random variable by divides its variance by , because variance is in squared units. So:
A simulation checks this. Take 10,000 averages for each , and measure how much they spread out:
| SD of 10,000 averages | ||
|---|---|---|
| 1 | 8.74 | 8.72 |
| 4 | 4.35 | 4.36 |
| 16 | 2.19 | 2.18 |
| 64 | 1.08 | 1.09 |
| 256 | 0.55 | 0.54 |
Each time 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
Start with any guess. Say . The first delivery is 30, so the error (sample minus estimate) is . Move all the way: . The average of one sample is that sample.
Each step works out the error, then moves a fraction of the way across it. With :
Read it as: new estimate = old estimate + step size × (new sample − old estimate). The bracket 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 samples using the average of the first :
This rule is the incremental mean update. We often drop the subscripts and write it with an arrow, which means "replace with":
The number (the Greek letter alpha) is the step size. It says what fraction of the error to move by. With , the steps shrink as you see more samples, and you get the plain average.
Where does the estimate start? You pick any first guess, . With , the first step is , so the guess is wiped out at once.
With a constant , the guess keeps a weight of after samples. That fades, but it never quite reaches 0. Reinforcement learning often starts from , so this matters there.
Step through the update below. The dashed arrow is the error. The orange arrow is the move, times the error. Try the step-size slider at "1/n", at 0.5 and at 1.
Starting guess: μ̂ = 0. Press play or step forward.
Samples: 30 40 20 30 50
With , 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 , 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 ? 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 , 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.
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
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 (orange) follows the change. But it never stops wobbling. With , its last 50 values range from 21.8 to 27.9.
- At the start, the constant- estimate climbs up from the guess of 0. The plain average jumps to the first sample at once.
- Big : fast to react, very noisy. Small : smooth, slow to react. At it copies the dots.
So settles down and is right when nothing changes. A constant 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
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 . Rewrite it as . So after each step, the old estimate keeps a share of its weight, and the new sample gets . A sample from steps ago ends up with weight:
With , the newest sample has weight 0.1. One from 10 steps ago has . 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 is treated the same way. After samples it keeps weight . All the weights add up to 1:
With and a first guess of 0, the guess still has 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 , 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:
For our shop with , that's minutes. A smaller means a smaller wobble, but a slower reaction.
With , 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 .
- updates an estimate one sample at a time. With it is the sample mean.
- A constant step size tracks changes but never fully settles.
Section 2 shows how to compute these in PyTorch.