All courses
Courses › How LLMs really work · foundation

Random variables and expectation

Compute the expected value and variance of a random variable as weighted averages.

Builds on: Probability and distributions, Reading formulas: indices and sums

▶ Watch this lesson Start the whole course

What the animation says

A game of chance

Here is a game. One play pays zero, two, or ten euros. Zero comes up half the time, two thirty percent, ten twenty percent. One play tells you almost nothing. You might win ten, you might win zero. So we judge it over many plays: the average per play, and how far single plays swing around it. Quick guess: across many plays, what do you earn per play on average?

Weighted average

It's two euros sixty. Picture a hundred plays. Fifty pay zero, thirty pay two, twenty pay ten. That's zero plus sixty plus two hundred: two hundred sixty euros over a hundred plays, so two sixty per play. Each value counts in proportion to its chance. That's the expected value, a weighted average. It needn't be a possible payout. A fair die averages three and a half. Now measure each payout's distance from two sixty, with its sign. What's the weighted average of those distances?

Square, then weight

Zero. Minus two sixty, minus point six, plus seven point four: weighted, they cancel exactly. So we square the distances first. Six seventy-six, point three six, fifty-four seventy-six. Then weight them by their chances, not a plain average, and add. Three thirty-eight, plus point one oh eight, plus ten point nine five two: fourteen point four four. That's the variance. Tap the payout that supplies most of that spread.

Shortcut and standard deviation

The ten: far from the mean, so its squared distance dominates. About seventy-six percent. A shortcut gives the same variance: weight the squared payouts, giving twenty-one point two. Then subtract the mean squared, six seventy-six. Fourteen point four four again. Careful: weighting the squares is not the same as squaring the mean. Variance is in euros squared. Its square root, three point eight euros, is the standard deviation: the typical swing around the mean.

What is the 'typical' value of a chance?

A game pays €0, €2 or €10, each with its own chance. Over many plays, what do you earn per play on average? And how far do single plays swing around that average? These two numbers, **average** and **spread**, are how we judge anything that makes uncertain guesses: not by one lucky or unlucky case, but by its behaviour across many cases.

Weighted average, and spread around it

A **random variable** is a number attached to each outcome of a chance event, such as a payout, a die face or the size of an error. Its **expected value** (mean) is the long-run average. Each value counts in proportion to its probability, so it is a **weighted average**. It need not be a possible value: a fair six-sided die averages 3.5. **Variance** measures spread. It is the weighted average of the *squared* distances from the mean. We square because the plain signed distances always average to exactly zero: misses above and below would cancel.

The two formulas

$X$ takes values $x_1,\dots,x_n$ with probabilities $p_1,\dots,p_n$ (summing to 1). $$\mu=\mathbb{E}[X]=\sum_{i=1}^n p_i\,x_i$$ $$\mathrm{Var}[X]=\sum_{i=1}^n p_i\,(x_i-\mu)^2=\mathbb{E}[X^2]-\mu^2$$ Here $\mathbb{E}[X^2]=\sum_i p_i x_i^2$: you weight the squared values, which is not the same as squaring the mean. Standard deviation $=\sqrt{\mathrm{Var}[X]}$, and it is in X's own units.

Worked: payouts 0, 2, 10

$X=0,2,10$ with $p=0.5,0.3,0.2$. **Mean:** $0.5\cdot0+0.3\cdot2+0.2\cdot10=0+0.6+2=2.6$ **Deviations from 2.6:** $-2.6,\,-0.6,\,7.4$. Squared: $6.76,\,0.36,\,54.76$ **Var:** $0.5\cdot6.76+0.3\cdot0.36+0.2\cdot54.76$ $=3.38+0.108+10.952=14.44$ **Check:** $\mathbb{E}[X^2]=0.3\cdot4+0.2\cdot100=21.2$, and $21.2-2.6^2=14.44$ ✓ **Standard deviation:** $\sqrt{14.44}=3.8$. The rare 10 supplies most of the spread: $10.952$ of the $14.44$, about 76%.

Remember

**Expected value** is the probability-weighted average of the values. **Variance** is the probability-weighted average of the squared distance from that mean (equivalently $\mathbb{E}[X^2]-\mu^2$). Its square root, the **standard deviation**, is the typical swing around the mean.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.