Expectation & variance
Step 15 of 18 in Foundations · Concept · Easy
Expectation is the long-run average; variance is the spread around it. Loss functions are expectations, and much of ML is about controlling variance.
The lesson
The expectation E[X] is the average weighted by probability: Σ x·P(x). It is what the mean of many samples settles on. Variance is the average squared distance from it: Var[X] = E[(X − E[X])²] = E[X²] − E[X]². The standard deviation is its square root, back in the units of X.
- E[X] = (1 + 2 + 3 + 4 + 5 + 6) / 6 = 3.5
- E[X²] = (1 + 4 + 9 + 16 + 25 + 36) / 6 = 91/6 ≈ 15.17
- Var[X] = 15.17 − 3.5² = 35/12 ≈ 2.92
Rules worth memorising. E[X + Y] = E[X] + E[Y], always. Var[X + Y] = Var[X] + Var[Y] only when X and Y are independent. E[aX] = a·E[X], but Var[aX] = a²·Var[X]. So two dice have mean 7 and variance 35/6.
A training loss is an expectation estimated from a batch. Bigger batches give a less noisy estimate, which is the next point.
Common mistake. Averaging n independent samples divides the variance by n, so the standard deviation falls by √n, not n. Four times the data buys half the noise.
Check yourself
A bet pays +10 with probability 0.3 and −5 otherwise. What is its expected value?
Show the answer
-0.5. 0.3 × 10 + 0.7 × (−5) = 3 − 3.5 = −0.5.
Var[X] = 4. What is Var[3X]?
Show the answer
36. Var[aX] = a²·Var[X] = 9 × 4 = 36.
X has standard deviation 10. What is the standard deviation of the mean of 25 independent copies?
Show the answer
2. 10 / √25 = 2.
Build it
Dice by formula and simulation
Compute E[X] and Var[X] for the sum of two dice by formula, then by simulation.
Done when: Formula: 7 and 35/6; simulation agrees within 1%.
Runs in your browser: write the code on the map and the tests mark it.
Resources
- Wikipedia: Expected value Article
- Seeing Theory Article
- StatQuest Video
- Think Bayes (free book) Book
Earn this star
On the map, your answers are marked and you log what you built. Finish the step and its star lights.
Open this step on the map