Expectation
Expected value for discrete and continuous variables, linearity, and the law of the unconscious statistician.
Assumes you know
Expectation
Intuition first
The expected value is the long-run average. Roll a fair die many times and the mean of your rolls settles near 3.5 — even though 3.5 never appears on any face.
That last point is worth keeping: an expectation need not be an attainable value. It is the centre of mass of the distribution, the balance point if you placed weights equal to the probabilities along a ruler.
The property that makes expectation the workhorse of probability is linearity: the expected value of a sum is the sum of the expected values, always, whether or not the variables are independent. No other summary statistic is that well behaved, and problems that look hopeless directly often collapse once you write the quantity as a sum and take expectations term by term.
Definitions
Each value weighted by how likely it is.
| Symbol | Meaning | Read aloud |
|---|---|---|
| E[X] | Expected value of X | E of X |
| μ or μₓ | Common alternative notation for E[X] | mu |
| E[g(X)] | Expected value of a function of X | E of g of X |
| E[X | Y] | Conditional expectation — a separate lesson | E of X given Y |
Linearity
For any random variables and any constants :
Proof of linearity, and why independence is not neededAdvanced
Work with the discrete case; the continuous case replaces sums with integrals identically.
By definition, using the joint PMF :
Split the sum:
In the first term does not depend on the inner index, so factor it out and marginalise:
The step is just marginalisation — summing a joint distribution over one variable. It holds for any joint distribution, dependent or not. The second term gives symmetrically.
Independence never appeared. Contrast with the product rule, where it is essential:
For dependent variables , and the covariance term is exactly what independence kills.
The law of the unconscious statistician
To find you do not need the distribution of — just weight by the distribution of :
Solved problem 1 · Expectation of a die, and of its square
A fair six-sided die is rolled. Find , , and .
Step 1 — E[X] by definition
Each value has probability :
Step 2 — E[X²] using LOTUS
Weight the squares by the same probabilities:
Step 3 — check that it is not the square of the mean
The difference is the variance of a fair die, .
Step 4 — E[3X + 2] by linearity
Verify the hard way, computing the distribution of :
Answer
; ; . Note 3.5 is not a face of the die — expectations need not be attainable.
Solved problem 2 · Linearity making a hard problem easy
Ten letters are placed at random into ten addressed envelopes, one per envelope. What is the expected number of letters in the correct envelope?
Step 1 — why the direct route is unpleasant
Let be the number of correct matches. Finding requires counting permutations with exactly fixed points, which involves derangements:
Correct, and far more work than necessary.
Step 2 — write X as a sum of indicators
Define if letter lands in its own envelope, 0 otherwise. Then
Step 3 — expectation of a single indicator
Letter is equally likely to land in any of the 10 envelopes, so
Step 4 — sum by linearity
Step 5 — note what was not needed
The indicators are not independent — if nine letters are correct the tenth must be too. That would wreck any variance calculation, but linearity does not care, so the answer stands.
Answer
Exactly 1 letter on average, and remarkably this is independent of : with a million letters the expected number of correct matches is still 1.
When expectation does not exist
Not every distribution has a finite mean. The Cauchy distribution,
has , so is undefined. Sample means of Cauchy draws never settle — they keep jumping, because the tails are heavy enough that extreme values keep arriving.
import numpy as np
rng = np.random.default_rng(0)
# Die: empirical vs theoretical.
rolls = rng.integers(1, 7, 500_000)
print(f"E[X] {rolls.mean():.4f} theory 3.5")
print(f"E[X²] {np.mean(rolls**2):.4f} theory {91/6:.4f}")
# Envelope problem: linearity says the answer is 1 for any n.
for n in (10, 100, 10_000):
matches = [np.sum(rng.permutation(n) == np.arange(n)) for _ in range(2000)]
print(f"n={n:<6} mean matches {np.mean(matches):.4f} theory 1.0")
# Cauchy: the sample mean never converges.
print("\nrunning mean of Cauchy draws (no finite expectation):")
c = rng.standard_cauchy(1_000_000)
for k in (10**3, 10**4, 10**5, 10**6):
print(f" first {k:>7,} draws: {c[:k].mean():+10.3f}")Exercise 1
A game costs £2 to play. You roll a die and win £1 per pip. Should you play?
Show solutionHide solution
Expected winnings are pounds, and the cost is £2, so expected profit per play is
Yes, play — the game is favourable by £1.50 per roll on average.
Two caveats worth stating. Expectation is a long-run average: on any single play you lose money whenever the roll is 1 or 2, which happens a third of the time. And expectation ignores risk entirely. A game with expected profit £1.50 but a small chance of a catastrophic loss may be one to decline, which is why utility theory weights outcomes by more than their monetary value.
Here, with bounded outcomes between £1 and £6 and a cost of £2, the risk is trivial and the expectation is decisive.
Exercise 2
A fair coin is flipped 100 times. Use indicators to find the expected number of times the pattern appears in consecutive positions (overlapping allowed).
Show solutionHide solution
There are 99 adjacent pairs of positions: .
Define if positions and are both heads. Two independent fair flips give
The total count is , so by linearity
The indicators are dependent — and share the flip at position , so knowing one raises the chance of the other. Linearity is unaffected, which is exactly why this approach works where a variance calculation would need the covariances.
Exercise 3
Show that for any random variable with finite second moment, and say when equality holds.
Show solutionHide solution
Consider the variance, which is an expectation of a squared quantity and therefore non-negative:
Expand the square and use linearity, remembering is a constant:
So , which rearranges to the claim.
Equality requires , meaning has expectation zero. Since it is non-negative, it must be zero with probability 1 — so is constant, almost surely.
This is the simplest instance of Jensen's inequality, with the convex function , and the identity derived along the way is the computational formula used throughout the next lesson.
Next: Conditional Expectation, where the expectation itself becomes a random variable.