Course contents 08 / 22

08 / 22 · Random Variables

Expectation and variance

A distribution summarized by two numbers: the expectation as a balance point and long-run average, linearity and the indicator trick (Secret Santa, shared birthdays, the coupon collector), the expectation of a function and Jensen's inequality, the variance and the 1/√n rule for averages, and moment and characteristic functions.

On this page 11

A distribution is a whole function, and most questions need much less: where is it centred, and how far from the centre do the values typically fall? The expectation and the variance answer these two questions. They are also the quantities machine learning optimizes and reports: a risk is an expected loss, an accuracy is an expected indicator, and the error bar of a benchmark is a standard deviation. This note defines both, shows the one property that makes expectations easy to compute, linearity, and derives the 1/n1/\sqrt n rule for averages that the opening note stated without proof.

Notation. E[X]E[X] is the expectation (mean) of XX, often μ\mu; Var⁡(X)\operatorname{Var}(X) its variance, σ2\sigma^2; σ=Var⁡(X)\sigma = \sqrt{\operatorname{Var}(X)} its standard deviation. X1,…,XnX_1, \dots, X_n are i.i.d. if they are independent and have the same distribution; Xˉn=(X1+⋯+Xn)/n\bar X_n = (X_1 + \dots + X_n)/n is their average.

Expectation#

The expectation weights every value by its probability:

E[X]=∑kk P(X=k)(discrete),E[X]=∫−∞∞x fX(x) dx(continuous).E[X] = \sum_k k\, P(X = k) \quad \text{(discrete)}, \qquad E[X] = \int_{-\infty}^{\infty} x\, f_X(x)\, dx \quad \text{(continuous)} .

A fair die has E[X]=(1+2+⋯+6)/6=3.5E[X] = (1 + 2 + \dots + 6)/6 = 3.5, a value it never shows. Two readings make the definition natural. Physically, E[X]E[X] is the balance point: a rod loaded with mass P(X=k)P(X = k) at position kk balances there. Statistically, it is the long-run average: the average of many independent copies of XX approaches E[X]E[X], which the law of large numbers will prove. The distributions of the previous notes have the means

DistributionBernoulli(p)\mathrm{Bernoulli}(p)Bin(n,p)\mathrm{Bin}(n, p)Geom(p)\mathrm{Geom}(p)Poisson(λ)\mathrm{Poisson}(\lambda)U(a,b)U(a, b)Exp(λ)\mathrm{Exp}(\lambda)N(μ,σ2)N(\mu, \sigma^2)
Meanppnpnp1/p1/pλ\lambda(a+b)/2(a+b)/21/λ1/\lambdaμ\mu

The mean is one of several centres. For a symmetric distribution the mean and the median coincide; for a skewed one they separate, and the mean is pulled toward the long tail:

Figure

Mode, median and mean of a skewed distribution

The log-normal density with mode, median and mean markedA skewed density peaking near 0.37, with median 1 and mean 1.65; the left half of the probability is shaded.0123456mode 0.37median 1.00mean 1.6550%The log-normal density with mode, median and mean markedA skewed density peaking near 0.37, with median 1 and mean 1.65; the left half of the probability is shaded.0123456mode 0.37median 1.00mean 1.6550%
The log-normal distribution Y = eˣ, X ~ N(0, 1). The density is highest at the mode e⁻¹ ≈ 0.368; the median e⁰ = 1 splits the probability in half; the mean e^(1/2) ≈ 1.649 is the balance point of the density (the triangle), and the long right tail pulls it to the right of the median.

This is why reports of incomes or house prices quote the median: a few very large values move the mean far more than they move the median.

Linearity#

For any random variables and constants,

E[aX+bY+c]=aE[X]+bE[Y]+c.E[aX + bY + c] = a E[X] + b E[Y] + c .

Written as a weighted sum over outcomes rather than over values, the expectation is linear in the random variable, so the proof is one line, and it needs no independence. Its power comes from indicators: E[1A]=1⋅P(A)+0⋅P(Ac)=P(A)E[\mathbb 1_A] = 1 \cdot P(A) + 0 \cdot P(A^c) = P(A). A count of events is a sum of indicators, so its expectation is a sum of probabilities, however the events depend on each other.

  • Binomial mean. X∼Bin(n,p)X \sim \mathrm{Bin}(n, p) is a sum of nn indicators, each with probability pp: E[X]=npE[X] = np, without touching the PMF.
  • Secret Santa. The axioms note needed inclusion–exclusion to find that nobody draws their own name with probability close to 1/e1/e. The expected number of people who draw their own name is a one-liner: person ii does with probability 1/n1/n, so the expectation is n⋅1n=1n \cdot \frac1n = 1, for every group size.
  • Shared birthdays. Among 23 people there are (232)=253\binom{23}{2} = 253 pairs, each sharing a birthday with probability 1/3651/365; the expected number of shared pairs is 253/365=0.69253/365 = 0.69.
  • The coupon collector. How many rolls of a die until every face has appeared? Split the wait into six stages, one per new face; each stage is geometric, and linearity adds their means:
Figure

How many rolls it takes to see every face of a die

A waterfall of the six expected waitsSix bars of length 1, 1.2, 1.5, 2, 3 and 6, each starting where the previous one ended; 14.7 in total.0510151st new facep = 6/6 per roll112nd new facep = 5/6 per roll1.223rd new facep = 4/6 per roll1.534th new facep = 3/6 per roll245th new facep = 2/6 per roll356th new facep = 1/6 per roll66all six faces: 14.7 rollsA waterfall of the six expected waitsSix bars of length 1, 1.2, 1.5, 2, 3 and 6, each starting where the previous one ended; 14.7 in total.0510151st new facep = 6/6 per roll112nd new facep = 5/6 per roll1.223rd new facep = 4/6 per roll1.534th new facep = 3/6 per roll245th new facep = 2/6 per roll356th new facep = 1/6 per roll66all six faces: 14.7 rolls
The expected wait for each next new face. With k − 1 faces seen, a new one appears with probability (7 − k)/6, and the wait for it is 6/(7 − k) rolls on average (a geometric distribution). By linearity of expectation the waits add up: 1 + 1.2 + 1.5 + 2 + 3 + 6 = 14.7. The last face costs 6 rolls, more than the first four together.

With nn equally likely coupons the same argument gives n(1+12+⋯+1n)≈n(ln⁡n+0.58)n (1 + \frac12 + \dots + \frac1n) \approx n (\ln n + 0.58): about 519 packs to complete a collection of 100 stickers, most of them spent on the last few.

The expectation of a function#

To find E[g(X)]E[g(X)], there is no need to find the distribution of g(X)g(X) first: weight gg of each value by the probability of the value,

E[g(X)]=∑kg(k) P(X=k),E[g(X)]=∫g(x) fX(x) dx.E[g(X)] = \sum_k g(k)\, P(X = k), \qquad E[g(X)] = \int g(x)\, f_X(x)\, dx .

For the log-normal Y=eXY = e^X with X∼N(0,1)X \sim N(0, 1) of the previous note, completing the square gives

E[eX]=∫exe−x2/22π dx=e1/2∫e−(x−1)2/22π dx=e1/2=1.6487,E[e^X] = \int e^x \frac{e^{-x^2/2}}{\sqrt{2\pi}}\, dx = e^{1/2} \int \frac{e^{-(x-1)^2/2}}{\sqrt{2\pi}}\, dx = e^{1/2} = 1.6487 ,

the mean in the figure above. It is not eE[X]=e0=1e^{E[X]} = e^0 = 1: in general E[g(X)]≠g(E[X])E[g(X)] \ne g(E[X]). For a convex gg (a function that bends upward) the inequality has a direction, Jensen’s inequality:

E[g(X)]≥g(E[X]).E[g(X)] \ge g(E[X]) .

With g(x)=x2g(x) = x^2 it says E[X2]≥(E[X])2E[X^2] \ge (E[X])^2. With the concave log⁡\log it reverses, E[log⁡X]≤log⁡E[X]E[\log X] \le \log E[X], and this one line gives the evidence lower bound of the variational autoencoder and the fact that the KL divergence of the entropy note is never negative. In practice it is a warning: the average of a loss over predictions is not the loss of the average prediction.

Variance#

The variance measures the typical squared distance from the mean:

Var⁡(X)=E[(X−μ)2]=E[X2]−μ2.\operatorname{Var}(X) = E\big[(X - \mu)^2\big] = E[X^2] - \mu^2 .

The second form follows from linearity: E[X2−2μX+μ2]=E[X2]−2μ2+μ2E[X^2 - 2\mu X + \mu^2] = E[X^2] - 2\mu^2 + \mu^2. For a die, E[X2]=91/6E[X^2] = 91/6, so Var⁡(X)=91/6−3.52=35/12≈2.92\operatorname{Var}(X) = 91/6 - 3.5^2 = 35/12 \approx 2.92 and σ≈1.71\sigma \approx 1.71. The standard deviation is in the units of XX, which makes it the number to report; the variance is the one that adds up.

DistributionBernoulli(p)\mathrm{Bernoulli}(p)Bin(n,p)\mathrm{Bin}(n, p)Geom(p)\mathrm{Geom}(p)Poisson(λ)\mathrm{Poisson}(\lambda)U(a,b)U(a, b)Exp(λ)\mathrm{Exp}(\lambda)N(μ,σ2)N(\mu, \sigma^2)
Variancep(1−p)p(1-p)np(1−p)np(1-p)(1−p)/p2(1-p)/p^2λ\lambda(b−a)2/12(b-a)^2/121/λ21/\lambda^2σ2\sigma^2

Two rules do most of the work. Shifting does not change the spread and scaling multiplies it: Var⁡(aX+b)=a2Var⁡(X)\operatorname{Var}(aX + b) = a^2 \operatorname{Var}(X). And for independent XX and YY, variances add: Var⁡(X+Y)=Var⁡(X)+Var⁡(Y)\operatorname{Var}(X + Y) = \operatorname{Var}(X) + \operatorname{Var}(Y); the joint distributions note proves it and shows the extra covariance term for dependent variables.

The 1/n1/\sqrt n rule. For i.i.d. X1,…,XnX_1, \dots, X_n with variance σ2\sigma^2, the two rules give

Var⁡(Xˉn)=1n2⋅nσ2=σ2n,sd⁡(Xˉn)=σn.\operatorname{Var}(\bar X_n) = \frac{1}{n^2} \cdot n \sigma^2 = \frac{\sigma^2}{n}, \qquad \operatorname{sd}(\bar X_n) = \frac{\sigma}{\sqrt n} .

The average is nn times less variable than one observation in variance, but only n\sqrt n times in standard deviation. A classifier with true accuracy 0.90.9, measured on 1,000 independent test examples, gets a measured accuracy with standard deviation 0.9⋅0.1/1000=0.0095\sqrt{0.9 \cdot 0.1 / 1000} = 0.0095, about one percentage point; halving that requires 4,000 examples.

Moment and characteristic functions#

Optional depth: moments beyond the variance, and the transforms that turn sums into products and prove the central limit theorem. The rest of the note does not depend on it.

The mean and the variance are built from the first two moments E[Xk]E[X^k], k=1,2k = 1, 2. The third and fourth standardized moments, E[(X−μ)3]/σ3E[(X - \mu)^3]/\sigma^3 and E[(X−μ)4]/σ4E[(X - \mu)^4]/\sigma^4, are the skewness and the kurtosis: lopsidedness, and how much of the variance comes from rare large deviations.

All the moments are packed into one function, the moment generating function

MX(t)=E[etX],MX(k)(0)=E[Xk],M_X(t) = E\big[e^{tX}\big], \qquad M_X^{(k)}(0) = E[X^k] ,

since the kk-th derivative of etXe^{tX} at t=0t = 0 is XkX^k. Its main use is sums: for independent XX and YY, et(X+Y)=etXetYe^{t(X+Y)} = e^{tX} e^{tY} and the expectation of a product of independent variables factorizes, so MX+Y(t)=MX(t) MY(t)M_{X+Y}(t) = M_X(t)\, M_Y(t). A Poisson(λ\lambda) variable has M(t)=exp⁡(λ(et−1))M(t) = \exp(\lambda(e^t - 1)); multiplying two of them gives the MGF of Poisson(λ1+λ2\lambda_1 + \lambda_2), and since an MGF that exists near 0 determines the distribution, the sum of independent Poisson variables is Poisson. The normal has M(t)=exp⁡(μt+σ2t2/2)M(t) = \exp(\mu t + \sigma^2 t^2 / 2), and the same argument shows that sums of independent normals are normal.

The MGF does not exist for heavy-tailed distributions such as the log-normal, where E[etY]E[e^{tY}] is infinite for every t>0t > 0. The characteristic function ψX(t)=E[eitX]\psi_X(t) = E[e^{itX}] always exists. Here ii is the imaginary unit, i2=−1i^2 = -1, and eitX=cos⁡(tX)+isin⁡(tX)e^{itX} = \cos(tX) + i \sin(tX), so ψX(t)=E[cos⁡(tX)]+i E[sin⁡(tX)]\psi_X(t) = E[\cos(tX)] + i\, E[\sin(tX)] is a pair of ordinary expectations of bounded quantities, and ∣eitX∣=1\lvert e^{itX} \rvert = 1. It also determines the distribution and turns sums into products. Its other property is Lévy’s continuity theorem, the tool behind the proof of the central limit theorem: the distributions of XnX_n converge to the distribution of XX exactly when ψXn(t)→ψX(t)\psi_{X_n}(t) \to \psi_X(t) for every tt; and if ψXn(t)\psi_{X_n}(t) converges for every tt to a limit that is continuous at t=0t = 0, that limit is the characteristic function of some distribution, to which the XnX_n converge. The condition at 00 matters: N(0,n)N(0, n) has ψ(t)=e−nt2/2\psi(t) = e^{-nt^2/2}, which tends to 00 for every t≠0t \ne 0 and equals 11 at t=0t = 0. The limit jumps at 00, it is not a characteristic function, and the distributions spread out without converging to any distribution.

What changed#

A distributionIts summaries
Centrea whole PMF or densityE[X]E[X], a balance point and long-run average
SpreadVar⁡(X)\operatorname{Var}(X), σ\sigma
Sumsconvolution of distributionsmeans always add; variances add under independence; MGFs multiply
Averages of nnmean unchanged, standard deviation σ/n\sigma/\sqrt n

Limitations#

  • The expectation may not exist. The Cauchy distribution, the ratio of two independent standard normals, has a density but no mean: its averages do not settle however many samples are taken (see the code).
  • Means are sensitive to tails. A single extreme value can move a mean arbitrarily far; the median is robust, and for skewed quantities such as latencies, incomes or losses on rare inputs the mean alone misleads.
  • Two numbers do not describe a shape. Distributions with the same mean and variance can have very different tails, which is exactly where risk, failures and outliers live.

What you should remember#

  • E[X]=∑k P(X=k)E[X] = \sum k\, P(X = k) or ∫xf(x) dx\int x f(x)\, dx: a balance point and the long-run average.
  • Linearity: E[aX+bY+c]=aE[X]+bE[Y]+cE[aX + bY + c] = aE[X] + bE[Y] + c for any dependence; E[1A]=P(A)E[\mathbb 1_A] = P(A) turns expected counts into sums of probabilities.
  • E[g(X)]=∑g(k)P(X=k)E[g(X)] = \sum g(k) P(X = k); in general E[g(X)]≠g(E[X])E[g(X)] \ne g(E[X]), and E[g(X)]≥g(E[X])E[g(X)] \ge g(E[X]) for convex gg (Jensen).
  • Var⁡(X)=E[X2]−(E[X])2\operatorname{Var}(X) = E[X^2] - (E[X])^2; Var⁡(aX+b)=a2Var⁡(X)\operatorname{Var}(aX + b) = a^2 \operatorname{Var}(X); variances of independent variables add.
  • The average of nn i.i.d. variables has standard deviation σ/n\sigma/\sqrt n.
  • MGFs and characteristic functions turn sums of independent variables into products.

Code#

Each number of the note, computed exactly or by simulation:

import numpy as np, math
from scipy import stats

rng = np.random.default_rng(0)

# a fair die: the variance from the definition and as E[X^2] - (E[X])^2
die = np.arange(1, 7)
mean = die.mean()
print(mean, round(((die - mean) ** 2).mean(), 4), round((die**2).mean() - mean**2, 4))   # 3.5 2.9167 2.9167

# linearity with indicators: own names drawn among 20 people; shared-birthday pairs among 23
perm = np.argsort(rng.random((100_000, 20)), axis=1)
print(round((perm == np.arange(20)).sum(axis=1).mean(), 3), round(math.comb(23, 2) / 365, 4))   # 0.996 0.6932

# coupon collector: rolls until all six faces have appeared
rolls = []
for _ in range(20_000):
    seen, n = set(), 0
    while len(seen) < 6:
        seen.add(int(rng.integers(6))); n += 1
    rolls.append(n)
print(round(sum(6 / k for k in range(1, 7)), 2), round(float(np.mean(rolls)), 2))   # 14.7 14.71

# the expectation of a function: E[e^X] for X ~ N(0, 1), against e^E[X] = 1 and the median of e^X
x = rng.normal(size=1_000_000)
print(round(float(np.exp(x).mean()), 3), round(math.exp(0.5), 4), round(float(np.median(np.exp(x))), 3))   # 1.645 1.6487 0.998

# the 1/sqrt(n) rule: accuracy measured on 1,000 test examples when the true accuracy is 0.9
print(round(math.sqrt(0.9 * 0.1 / 1000), 4), round(float(rng.binomial(1000, 0.9, 100_000).std()) / 1000, 4))   # 0.0095 0.0095

# generating functions: Poisson(2) + Poisson(3), independent, is Poisson(5)
s = rng.poisson(2, 1_000_000) + rng.poisson(3, 1_000_000)
print(round(float(s.mean()), 3), round(float(s.var()), 3), round(float((s == 5).mean()), 4), round(stats.poisson(5).pmf(5), 4))   # 4.998 5.004 0.1756 0.1755

# no mean at all: averages of Cauchy samples do not settle
c = rng.standard_cauchy(1_000_000)
print([round(float(c[:n].mean()), 2) for n in (10**3, 10**4, 10**5, 10**6)])   # [0.83, 2.88, 0.4, -0.23]

Next#

The expectation and the variance describe one random variable. Data come in several at once: a person’s height and weight, a word and the next one, a feature vector and a label. The next note describes several random variables together: joint and marginal distributions, independence, and the covariance and correlation that measure, partly, how they move together.

References#

  • J. K. Blitzstein, J. Hwang. Introduction to Probability, 2nd ed. CRC Press, 2019. Chapter 4 (expectation, linearity, indicators, LOTUS, variance, the coupon collector), Chapter 6 (moments, moment generating functions).
  • W. Feller. An Introduction to Probability Theory and Its Applications, Vol. 1, 3rd ed. Wiley, 1968. Chapter IX (random variables, expectation), Chapter XI (generating functions).
  • A. N. Shiryaev. Probability-1, 3rd ed. Springer, 2016. Chapter 2, §6 (expectation as an integral), §12 (characteristic functions).