AI Knowledge Tree

Expectation and the law of the unconscious statistician

Define expectation, prove linearity, and compute E[g(X)] without first finding the distribution of g(X).

⏱ 30 minPrerequisites:Random variables and distributions

A distribution carries a lot of detail. Often the single most useful summary is its center of mass: the value the random variable takes on average over many trials. That is the expectation, and its single most useful property, linearity, holds with no independence assumptions at all.

Definition

The expectation (or mean) of a random variable XX is a probability-weighted average of its values[1]. For a discrete XX with PMF pXp_X,

E[X]=∑xx pX(x),\mathbb{E}[X] = \sum_x x\, p_X(x),

and for a continuous XX with PDF fXf_X,

E[X]=∫−∞∞x fX(x) dx,\mathbb{E}[X] = \int_{-\infty}^\infty x\, f_X(x)\, dx,

whenever the sum or integral converges absolutely. The expectation is a single number, often written μ\mu. It is the balance point of the distribution: the place where the PMF or PDF would balance on a knife edge.

The law of the unconscious statistician

To compute the expectation of g(X)g(X) for some function gg, one route is to find the distribution of the new random variable Y=g(X)Y = g(X), then average. The law of the unconscious statistician (LOTUS) says you can skip that step and weight g(x)g(x) by the distribution of XX directly[2]:

E[g(X)]=∑xg(x) pX(x)or∫−∞∞g(x) fX(x) dx.\mathbb{E}[g(X)] = \sum_x g(x)\, p_X(x) \qquad \text{or} \qquad \int_{-\infty}^\infty g(x)\, f_X(x)\, dx.

Why it holds (discrete case). Group the outcomes by the value yy that gg takes:

E[g(X)]=∑yy P(g(X)=y)=∑yy∑x:g(x)=ypX(x)=∑y∑x:g(x)=yg(x) pX(x)=∑xg(x) pX(x).\mathbb{E}[g(X)] = \sum_y y\, P(g(X) = y) = \sum_y y \sum_{x : g(x) = y} p_X(x) = \sum_y \sum_{x : g(x) = y} g(x)\, p_X(x) = \sum_x g(x)\, p_X(x).

The inner step replaces yy by g(x)g(x), valid because the sum only includes xx with g(x)=yg(x) = y. Summing over the groups recovers a sum over all xx.

Linearity of expectation

For any random variables X,YX, Y and constants a,ba, b,

E[aX+bY]=a E[X]+b E[Y].\mathbb{E}[aX + bY] = a\,\mathbb{E}[X] + b\,\mathbb{E}[Y].

Derivation (discrete, joint PMF pp). By LOTUS applied to the function g(x,y)=ax+byg(x, y) = ax + by,

E[aX+bY]=∑x,y(ax+by) p(x,y)=a∑x,yx p(x,y)+b∑x,yy p(x,y).\mathbb{E}[aX + bY] = \sum_{x, y} (ax + by)\, p(x, y) = a\sum_{x,y} x\, p(x,y) + b\sum_{x,y} y\, p(x,y).

The first double sum marginalizes out yy to give ∑xx pX(x)=E[X]\sum_x x\, p_X(x) = \mathbb{E}[X]; the second gives E[Y]\mathbb{E}[Y]. So the expectation of a sum is the sum of expectations, with no independence assumption. This is what makes linearity so powerful: it applies even to highly dependent variables.

A worked use of linearity

How many heads in nn flips of a coin with P(head)=qP(\text{head}) = q? Let XiX_i be the indicator that flip ii is heads, so E[Xi]=q\mathbb{E}[X_i] = q. The total is X=∑i=1nXiX = \sum_{i=1}^n X_i, and by linearity

E[X]=∑i=1nE[Xi]=nq,\mathbb{E}[X] = \sum_{i=1}^n \mathbb{E}[X_i] = nq,

with no need to touch the binomial PMF. Indicator random variables plus linearity solve a large fraction of counting-expectation problems.

The expectation is a distribution's balance point: the fulcrum at which its probability mass would sit level. A fair die balances at 3.5.
The expectation is a distribution's balance point: the fulcrum at which its probability mass would sit level. A fair die balances at 3.5.

Check it on arrays

python · numpy
import numpy as np

rng = np.random.default_rng(2)
N = 500_000

# E[X] for a die
X = rng.integers(1, 7, size=N)
print("E[die]:", X.mean(), "  exact 3.5")

# LOTUS: E[X^2] computed by weighting g(x)=x^2, no distribution of X^2 needed
print("E[X^2]:", (X**2).mean(), "  exact", sum(x**2 for x in range(1, 7)) / 6)

# Linearity without independence: Y = X, so X + Y = 2X are perfectly dependent
Y = X
print("E[X + Y]:", (X + Y).mean(), "  E[X]+E[Y]:", X.mean() + Y.mean())

# Indicators + linearity: heads in n=10 flips, q=0.3
q, n = 0.3, 10
flips = (rng.random(size=(N, n)) < q)
total = flips.sum(axis=1)
print("E[heads]:", total.mean(), "  n*q:", n * q)

The die mean lands on 3.5, LOTUS gives the right second moment, linearity holds even for the perfectly dependent pair, and the indicator sum averages to nqnq.

Exercises

Exercise 1. A fair six-sided die is rolled once. Use LOTUS to compute E[X2]\mathbb{E}[X^2] directly, without finding the distribution of X2X^2.

Solution

Step 1. LOTUS weights g(x)=x2g(x) = x^2 by the PMF of XX, which is pX(x)=1/6p_X(x) = 1/6 for each x∈{1,…,6}x \in \{1, \dots, 6\}:

E[X2]=∑x=16x2⋅16=16(1+4+9+16+25+36).\mathbb{E}[X^2] = \sum_{x=1}^{6} x^2 \cdot \tfrac{1}{6} = \tfrac{1}{6}\left(1 + 4 + 9 + 16 + 25 + 36\right).

Step 2. Sum the squares and divide:

E[X2]=916≈15.1667.\mathbb{E}[X^2] = \tfrac{91}{6} \approx 15.1667.

Computing the distribution of X2X^2 (values 1,4,9,16,25,361, 4, 9, 16, 25, 36, each with probability 1/61/6) gives the same sum; LOTUS skips that bookkeeping.

Exercise 2. Draw a card position uniformly: a hat holds nn cards numbered 11 to nn, you draw all nn in random order. Let XX be the number of cards that land in their own position (a "fixed point"). Use indicator variables and linearity to show E[X]=1\mathbb{E}[X] = 1 for every nn.

Solution

Step 1. Define indicators. Let Xi=1X_i = 1 if card ii lands in position ii, and 00 otherwise. Then X=∑i=1nXiX = \sum_{i=1}^n X_i.

Step 2. Each card is equally likely to land in any of the nn positions, so P(Xi=1)=1/nP(X_i = 1) = 1/n, giving E[Xi]=1/n\mathbb{E}[X_i] = 1/n.

Step 3. Apply linearity (the XiX_i are dependent, but linearity needs no independence):

E[X]=∑i=1nE[Xi]=n⋅1n=1.\mathbb{E}[X] = \sum_{i=1}^n \mathbb{E}[X_i] = n \cdot \tfrac{1}{n} = 1.

On average exactly one card matches its position, regardless of nn. The dependence among the indicators never enters the mean.

Exercise 3 (code). Verify Exercise 2 by simulation: for several values of nn, shuffle 1,…,n1, \dots, n many times and confirm the average number of fixed points is close to 1.

Solution
python · numpy
import numpy as np

rng = np.random.default_rng(0)
trials = 500_000

for n in [3, 10, 52]:
    perms = rng.permuted(np.broadcast_to(np.arange(n), (trials, n)), axis=1)
    fixed = (perms == np.arange(n)).sum(axis=1)
    mean_fixed = fixed.mean()
    print(f"n={n:3d}  E[fixed points] = {mean_fixed:.4f}  (exact 1.0)")
    assert np.isclose(mean_fixed, 1.0, atol=0.02)

The average number of fixed points stays near 1 for every nn, matching the indicator-plus-linearity result.

That second moment is the raw material for the next lesson, which measures spread: variance.


References

  1. [1]Casella, G., & Berger, R. L. (2002). Statistical Inference (2nd ed.). Duxbury. §2.2, expected values and their properties.↩
  2. [2]Wasserman, L. (2004). All of Statistics. Springer. Ch. 3, expectation, linearity, and the law of the unconscious statistician. doi:10.1007/978-0-387-21736-9↩