Expectation & Variance
16 min
Variance measures how spread out a random variable's outcomes are around its expected value — a random variable that's always exactly its mean has variance 0; one that swings wildly has high variance. The standard formula:
Var(X) = E[(X − E[X])²]
which expands algebraically to the equivalent, easier-to-compute form:
Var(X) = E[X²] − (E[X])²
E[X²] − (E[X])² is almost always the formula used in practice — computing E[(X − E[X])²] directly would require knowing E[X] before you could even start the sum.
Given actual sample data (rather than a known PMF), you compute the
same ideas slightly differently — the sample variance divides by n − 1,
not n (Bessel's correction), because using the sample's own mean
instead of the true population mean slightly underestimates spread,
and n - 1 corrects for that bias.
import statistics
data = [2, 4, 4, 4, 5, 5, 7, 9]
print(statistics.mean(data)) # 5 -- statistics.mean returns an int here since it computes exactly (via Fraction internally), not a float
print(statistics.variance(data)) # 4.571... -- sample variance (n-1)
print(statistics.stdev(data)) # 2.138... -- sample standard deviation
Standard deviation is just sqrt(variance) — it's preferred for
reporting because it's in the same units as the original data (variance
is in squared units, which is hard to interpret directly).
Write `variance(pmf)`, where `pmf` is a dict of numeric value → probability. Return Var(X) using Var(X) = E[X²] − (E[X])².
Write `sample_std(values)`, returning the sample standard deviation of a list of numbers (using Bessel's correction — divide by n−1, not n). Use `statistics.stdev`.
What does Var(X) = E[X²] − (E[X])² measure?
You can compute variance from a PMF using the E[X²] − (E[X])² shortcut, and compute sample standard deviation from raw data with Bessel's correction.