Skip to content

ProbabilityLesson 4 of 6

The average, and the swing

Expectation, variance, and standard deviation

Take every value a random quantity X can land on, multiply each value by its probability, and add. That’s the expectation — the average, computed before anything happens:

E[X]=xxpX(x)\mathbb E[X]=\sum_x x\,p_X(x)

Decoding: E[X]\mathbb E[X] is “the expectation of X”. \sum means “add these up”, one term per possible value. Each term is a value xx times pX(x)p_X(x), the probability that X lands exactly on xx. Every value × its chance, summed.

Picture the distribution as weights on the number line: E[X]\mathbb E[X] is its balance point.

A coin scored Heads = 1, Tails = 0: E[X]=1×1/2+0×1/2=1/2\mathbb E[X] = 1 \times 1/2 + 0 \times 1/2 = 1/2.

The coin can never show 1/2. The expectation needn’t be a possible outcome — it’s the long-run average per flip: flip a thousand times, divide the total score by a thousand, and you’ll hover near 1/2.

A four-sided die D, faces 1 through 4, each with probability 1/4. We can write the sum as a table with a totals row — one row per value, and the expectation is the sum of the last column:

valueprobabilitycontribution: value × probability
11/41/4
21/42/4
31/43/4
41/44/4
sum110/4 = 5/2
Roll and keep rolling. The running average hunts for one number — the expectation. The shaded band is one standard error of the sample mean on either side of the true mean.

For a continuous quantity, swap \sum for \int (an integral): E[X]=xfX(x)dx\mathbb E[X]=\int_{-\infty}^{\infty} x\,f_X(x)\,dx. Slice the number line into slivers of width dxdx. Each sliver holds a small probability — fX(x)dxf_X(x) \cdot dx, density × width. Each sliver contributes value × its sliver of probability — and contributions left of zero count negative. Add them all up.

Uniform on [−1, 1] — every point between −1 and 1 equally likely — has density 1/2. Its average is 0 by symmetry: every value has an equally likely negative twin, so the slivers left of 0 exactly cancel the slivers right of 0.

E[X]=11x12dx=0\mathbb E[X]=\int_{-1}^1 x\cdot\frac12\,dx=0

The expectation is the center. Variance is the average squared miss from that center:

Var[X]=E[(XE[X])2]\operatorname{Var}[X] = \mathbb E[(X - \mathbb E[X])^2]

Read it inside out: XE[X]X - \mathbb E[X] is the miss. Why square? Because raw misses average to exactly zero — E[XE[X]]=0\mathbb E[X - \mathbb E[X]] = 0 is what “balance point” means — so their average can’t tell a tight spread from a wild one. Squaring makes every miss count, big ones hardest; the outer EE averages them. For the die, whose center is 5/2, that’s the same kind of table — miss, square it, weight it, total:

valuemiss: value − 5/2squared miss× probability 1/4
1−3/29/49/16
2−1/21/41/16
3+1/21/41/16
4+3/29/49/16
sum20/16 = 5/4

All four faces have equal probability, but faces 1 and 4 contribute nine times as much to the variance as faces 2 and 3: their distance from the mean is three times larger, and 32=93^2=9.

For finite variance, expanding the square gives a useful shortcut: Var[X]=E[X2](E[X])2\operatorname{Var}[X] = \mathbb E[X^2] - (\mathbb E[X])^2 — the average of the square minus the square of the average. No misses to compute:

  • Die: E[D2]=(1+4+9+16)/4=30/4\mathbb E[D^2] = (1 + 4 + 9 + 16)/4 = 30/4, so Var[D]=30/4(5/2)2=30/425/4=5/4\operatorname{Var}[D] = 30/4 - (5/2)^2 = 30/4 - 25/4 = 5/4 — same answer as the table.
  • Coin: E[X2]=12×1/2+02×1/2=1/2\mathbb E[X^2] = 1^2 \times 1/2 + 0^2 \times 1/2 = 1/2, so Var[X]=1/2(1/2)2=1/21/4=1/4\operatorname{Var}[X] = 1/2 - (1/2)^2 = 1/2 - 1/4 = 1/4.
  • Uniform on [−1, 1]: E[X2]=11x212dx=1223=13\mathbb E[X^2]=\int_{-1}^1 x^2\cdot\frac12\,dx=\frac12\cdot\frac23=\frac13 — the area under x2x^2 from −1 to 1 works out to 2/3 — so Var[X]=1/302=1/3\operatorname{Var}[X] = 1/3 - 0^2 = 1/3.

The standard deviation is Var(X)\sqrt{\operatorname{Var}(X)}x\sqrt{\phantom{x}} is the square root, which undoes the squaring, so the answer is back in the same units as X. That’s the swing’s size on X’s own scale.

These are rules for expectation and variance. The quantities below have finite means and variances.

Linearity: the expectation of a sum is the sum of the expectations, even when the variables depend on each other. For three fair coin scores, E[X1+X2+X3]=1/2+1/2+1/2=3/2\mathbb E[X_1+X_2+X_3]=1/2+1/2+1/2=3/2. This also holds if all three scores record the same flip: then their sum is either 0 or 3, each with probability 1/21/2, so its expectation is still 3/23/2.

When the pieces are independent — one’s outcome tells you nothing about the other’s — variances add too.

  • Three coins: Var[X1+X2+X3]=1/4+1/4+1/4=3/4\operatorname{Var}[X_1 + X_2 + X_3] = 1/4 + 1/4 + 1/4 = 3/4.
  • Die + coin: E[D+X]=5/2+1/2=3\mathbb E[D + X] = 5/2 + 1/2 = 3 and Var[D+X]=5/4+1/4=3/2\operatorname{Var}[D + X] = 5/4 + 1/4 = 3/2.
  • It even mixes kinds. Coin + uniform on [−1, 1]: E[X+U]=1/2+0=1/2\mathbb E[X + U] = 1/2 + 0 = 1/2 and Var[X+U]=1/4+1/3=3/12+4/12=7/12\operatorname{Var}[X + U] = 1/4 + 1/3 = 3/12 + 4/12 = 7/12.

Linearity of expectation needs no independence. For variance, the full rule is Var(X+Y)=Var(X)+Var(Y)+2Cov(X,Y)\operatorname{Var}(X+Y)=\operatorname{Var}(X)+\operatorname{Var}(Y)+2\operatorname{Cov}(X,Y). Independence makes covariance zero; zero covariance is already sufficient, even without independence. We will define covariance in two at once.

Go deeper: where the shortcut comes from

Write mm for E[X]\mathbb E[X] — it’s just a number. Expand the square: (Xm)2=X22mX+m2(X - m)^2 = X^2 - 2mX + m^2. Take the expectation term by term (that’s linearity), letting the constant mm slide out: Var[X]=E[X2]2mE[X]+m2=E[X2]2m2+m2=E[X2]m2\operatorname{Var}[X] = \mathbb E[X^2] - 2m \cdot \mathbb E[X] + m^2 = \mathbb E[X^2] - 2m^2 + m^2 = \mathbb E[X^2] - m^2. The shortcut follows from expanding the square and applying linearity.

A fresh distribution each round: find the average, work the shortcut in two steps, or add two pieces.

A probability is an expectation in disguise

Section titled “A probability is an expectation in disguise”

Write 1{X=2}\mathbf1_{\{X=2\}} for the indicator — a flag that reads 1 when X = 2 and 0 otherwise (the braces hold the condition being flagged). Its expectation follows by considering its two possible values:

E[1{X=2}]=1Pr(X=2)+0Pr(X2)=Pr(X=2)\mathbb E[\mathbf1_{\{X=2\}}]=1\Pr(X=2)+0\Pr(X\ne2)=\Pr(X=2)

The average of a flag is the fraction of the time the flag is up — so any probability can be rewritten as an expectation. Accuracy on a random example can therefore be written as the expectation of a correct-answer indicator. For other loss functions, expected loss is the probability-weighted average of the per-example loss; the loss itself need not be a probability. When a paper writes Exp[]\mathbb E_{x\sim p}[\ldots ] — the \sim reads “drawn from” — it is asking for exactly this page’s move: the weighted average of whatever sits in the brackets, weights given by p.

Center and spread: two numbers per distribution. Next, four distributions so common they have names.

Definition

Read the full glossary entry →