Learning outcomes
By the end of this lesson you should be able to: define a random variable and its probability distribution; compute an expected value as a probability-weighted sum; compute the variance and standard deviation of a random variable; and explain why the expected value is a long-run average rather than a value the variable can actually take.
Prerequisites
Lesson 4 (Probability rules). You need the probability rules, especially that the probabilities in a distribution sum to 1, because the expected value is a weighted average over them.
The big idea
The expected value is the most useful number in applied probability and the most routinely misread. It is not the outcome you should expect on any single attempt — a lottery ticket with an expected value of -0.60 does not lose 60 cents on the draw you buy. It is the average over many repetitions, and it is the right decision rule for choices made repeatedly. That is what separates insurance pricing, which averages thousands of policies, from gambling, which concentrates losses on individuals.
Why the weighted average is the right decision rule
Compute E(X) as the sum of x times p(x). Two properties make it behave like an average. First, it is linear: E(aX + b) = aE(X) + b, so adding a constant shifts the expectation by that constant and rescaling the variable rescales its expectation. Second, for independent variables E(XY) = E(X)E(Y) — a fact that fails loudly when the variables are dependent, which is why correlation has to be modelled before products of random variables can be analysed.
The variance uses the same weighting: Var(X) is the sum of (x - E(X)) squared times p(x), which is easier to compute as E(X squared) minus E(X) squared. The standard deviation is its square root. Both are measured around the expected value, exactly as the sample variance of lesson 3 is measured around the sample mean; the only difference is that here each value is weighted by its probability instead of being one observation among n.
Worked example 1: a die game with zero expected value
Roll a fair die. If it shows a six you win 10; otherwise you lose 2. Is the game worth playing? The expected value answers it, and the variance says how volatile it is.
import math
from fractions import Fraction as F
outcomes = [(10, F(1, 6)), (-2, F(5, 6))]
ev = sum(x * p for x, p in outcomes)
ev_sq = sum(x * x * p for x, p in outcomes)
var = ev_sq - ev * ev
print("payouts:", [x for x, _ in outcomes])
print("E(X) = sum x * p(x):", ev, "=", float(ev))
print("E(X^2) = sum x^2 * p(x):", ev_sq, "=", float(ev_sq))
print("Var(X) = E(X^2) - E(X)^2:", var, "=", float(var))
print("SD(X):", round(math.sqrt(float(var)), 4))payouts: [10, -2]
E(X) = sum x * p(x): 0 = 0.0
E(X^2) = sum x^2 * p(x): 20 = 20.0
Var(X) = E(X^2) - E(X)^2: 20 = 20.0
SD(X): 4.4721Method: E(X) = (1/6)(10) + (5/6)(-2) = 10/6 - 10/6 = 0, so the stake is a fair price for the game. Then E(X squared) = (1/6)(100) + (5/6)(4) = 100/6 + 20/6 = 120/6 = 20, so Var(X) = 20 - 0 squared = 20 and the standard deviation is the square root of 20, about 4.4721. A zero expected value does not mean a quiet game — it means wins and losses balance in the long run while individual plays are volatile.
Worked example 2: the raffle, priced honestly
A raffle sells 1000 tickets at 1 each and has one prize of 400. Include the ticket price as a cost and the expected profit is clearly negative — the arithmetic that makes lotteries a business.
from fractions import Fraction as F
p_win = F(1, 1000)
prize = 400
cost = 1
ev_win = p_win * prize
ev_profit = ev_win - cost
print("E(win):", ev_win, "=", float(ev_win))
print("E(profit per ticket) = E(win) - cost:", ev_profit, "=", round(float(ev_profit), 2))E(win): 2/5 = 0.4
E(profit per ticket) = E(win) - cost: -3/5 = -0.6Method: the chance of holding the winning ticket is 1/1000, so the expected prize money is (1/1000)(400) = 0.4 exactly. Deducting the 1 ticket price gives an expected profit of 0.4 - 1 = -0.6 per ticket. Over 1000 tickets the raffle takes in 1000 and pays out 400, so the house keeps 600 — the same -0.6 per ticket, now visible with no probability notation at all.
NoteInsurance is the same calculation with the sign reversed. If 1000 policyholders each face a 1/1000 chance of a 400 loss, the expected payout per policy is 0.4, so a premium of 1 is profitable in expectation while remaining individually bearable. Both sides of that trade price the same number.
Worked example 3: what expectation hides
Two investments can share an expected value and behave nothing alike. The table computes the summaries, and the standard deviation reveals how different the risk is.
| Outcome x | p(x) in investment P | p(x) in investment Q | x * p(x) contribution |
|---|---|---|---|
| -2 | 0.5 | 0.999 | P: -1.0, Q: -1.998 |
| 2 | 0.5 | 0.001 | P: 1.0, Q: 0.002 |
| E(X) | — | — | P: 0.0, Q: -1.996 |
| SD(X) | — | — | P: 2.0, Q: 0.13 |
Method: for investment P, E(X) = 0.5(-2) + 0.5(2) = 0 and the variance is 0.5(4) + 0.5(4) = 4, so the SD is 2. Investment Q pays 2 once in a thousand times and -2 otherwise: E(X) = 0.001(2) + 0.999(-2) = 0.002 - 1.998 = -1.996, while E(X squared) = 0.001(4) + 0.999(4) = 4, so Var(X) = 4 - 1.996 squared = 4 - 3.984 = 0.016 and the SD is about 0.13. Expectation alone would rank these two as close in scale; expectation together with variance separates them cleanly — P is a fair coin-flip, Q is a near-certain loss of about 2.
Common mistakes
Watch outTreating the expected value as a likely outcome. A random variable can have an expected value it never takes — the die game pays either 10 or -2 while its expectation is 0. Expectation is about the long run, not about one trial.
Watch outForgetting to subtract costs. An expected win of 0.4 for a 1 ticket is an expected loss of 0.6. Always state whether the expected value is gross or net of the price of entry.
Watch outAssuming the expectation of a product is the product of expectations. E(XY) = E(X)E(Y) holds for independent variables only; with dependent variables the product's expectation must be computed from the joint distribution.
Watch outIgnoring variance when comparing choices. Two options with the same expected value can differ enormously in risk, and the standard deviation of the random variable is what quantifies that risk.
Check your understanding
A bag holds 3 red and 2 blue balls; drawing a red wins 6 and drawing a blue wins 0. What is the expected value, and what does it mean?
For the die game above, why is the expected value zero while the standard deviation is 4.4721?
A variable takes values 1 and 9 with equal probability. What are its expected value and standard deviation?
Multiple choice
A random variable takes values 2, 4 and 6 with probabilities 0.5, 0.3 and 0.2. What is E(X)?
Which statement about an expected value is correct?
In the raffle with 1000 tickets, a 400 prize and a 1 entry cost, what is the expected profit per ticket?
Var(X) = E(X^2) - E(X)^2. For the die game paying 10 with probability 1/6 and -2 with probability 5/6, what is the variance?
A fair coin pays 3 on heads and -3 on tails. What is the standard deviation of the payoff?
Summary and next
A random variable summarises a random process as a number with a distribution over its values, and those probabilities sum to 1. Its expected value, the sum of x times p(x), is the long-run average and the right basis for repeated decisions. Its variance, E(X squared) minus E(X) squared, measures spread around that average, and the standard deviation is its square root. Expectation and variance together state both the payoff and the risk, which either number alone cannot do.
Next: distributions. Two distributions are used constantly — the binomial for counts of successes in a fixed number of yes/no trials, and the normal for continuous measurements — and each has formulas that turn its mean into a probability table, which is what makes statistical inference possible later.