Lesson 12 of 16 · Random Variables
Variance
Variance and standard deviation measure spread: the shortcut formula, what happens when you scale and add random variables, and why averages vary less.
- 7 min read
- Intermediate
- Reviewed
- Calculate the variance and standard deviation of a random variable
- Explain why two random variables with the same mean can be very different
- Use the rules for scaling and adding random variables
- Explain why averages vary less than single values, by a factor of √n
Same average, different risk
Lesson 11 ended with three games, all with an expected value of $50: a sure $50, a coin flip for $0 or $100, and a 1% chance of $5,000. Nobody would call them the same. What differs is how spread out the outcomes are.
import math
def mean_and_variance(pmf):
mean = sum(x * p for x, p in pmf.items())
variance = sum((x - mean) ** 2 * p for x, p in pmf.items())
return mean, variance
games = {
"sure thing ($50)": {50: 1.0},
"coin flip ($0 or $100)": {0: 0.5, 100: 0.5},
"long shot ($0 or $5,000)": {0: 0.99, 5000: 0.01},
}
for name, pmf in games.items():
mean, variance = mean_and_variance(pmf)
print(f"{name:25} mean = {mean:4.0f} variance = {variance:8.0f} sd = {math.sqrt(variance):5.1f}")The squaring makes far-off values count a lot: the long shot's rare $5,000 gives it a standard deviation of almost $500, while the sure thing has none at all.
There's also a shortcut that's often quicker by hand:
Var(X) = E[X²] − (E[X])²For a die, E[X²] = (1 + 4 + 9 + 16 + 25 + 36) ÷ 6 = 91/6, so Var(X) = 91/6 − 3.5² ≈ 2.917, the same answer.
Scaling and adding
So the total of two dice has variance 2.917 + 2.917 = 5.833 and standard deviation √5.833 ≈ 2.415. The chart shows the same value:
Why averages are steadier
Averaging is one of the most useful things you can do with random values. The average of n independent rolls has a variance n times smaller, so its standard deviation shrinks by a factor of √n:
sd(average of n) = σ / √nimport math
import random
import statistics
sd_one_die = math.sqrt(35 / 12) # the standard deviation of one die, about 1.708
random.seed(12)
for n in (1, 4, 16, 64):
# 5,000 times: roll n dice and take their average
averages = [statistics.fmean(random.randint(1, 6) for _ in range(n)) for _ in range(5_000)]
print(f"n = {n:2}: sd of the average = {statistics.pstdev(averages):.3f} (theory: {sd_one_die / math.sqrt(n):.3f})")Averaging 4 dice halves the spread; averaging 64 dice cuts it by a factor of 8. That's why a survey of 1,000 people beats a survey of 10, and why measurements are repeated and averaged.
Common mistakes
Calculate the mean, variance and standard deviation of the number of support tickets per hour.
import math
# X = the number of support tickets that arrive in an hour (from lesson 9)
pmf = {0: 0.1, 1: 0.3, 2: 0.3, 3: 0.2, 4: 0.1}
# Calculate the mean, the variance and the standard deviation of X.
# Print all three on one line: the mean and variance rounded to 2 decimals, the sd to 3.
X has standard deviation 2. What is the standard deviation of 5X + 10?
Summary
- Variance Var(X) = E[(X − μ)²] measures spread; the standard deviation σ is its square root.
- Shortcut: Var(X) = E[X²] − (E[X])².
- Var(aX + b) = a² × Var(X), and for independent random variables variances add.
- The average of n independent values has standard deviation σ / √n.
Next: the Bernoulli and binomial distributions.
Finished reading? Tick it off to track your progress.