the full guide

Expectation and variance

The expected value of a random variable is its long-run average: each outcome weighted by its probability. Variance measures how spread out the outcomes are around that average, and the standard deviation is the square root of the variance.

10 min Intermediate

What you need: The earlier topics of this module.

Definition

The expected value of a random variable is its long-run average: each outcome weighted by its probability. Variance measures how spread out the outcomes are around that average, and the standard deviation is the square root of the variance.

In simple words

A game pays ₹100 on heads and nothing on tails. You won't ever win exactly ₹50 in one toss, yet on average you win ₹50 a toss. That average is the expected value. A second game that always pays ₹50 has the same expected value but zero variance, because there's no risk.

Why it matters

Reinforcement learning maximises expected reward. Training minimises expected loss. The bias-variance trade-off, batch normalisation and weight initialisation all rely on variance. When a paper writes E[...], it means this.

How it works

  • Expectation is linear: E[aX + bY] = aE[X] + bE[Y], and that holds even if X and Y are dependent. This trick shortens many proofs.
  • Variance is not linear: Var(aX) = a²Var(X). Doubling a value multiplies its variance by four.
  • For independent X and Y, Var(X + Y) = Var(X) + Var(Y).

The maths

Expected value (discrete)
Each possible value
The chance of that value
Variance
The mean, E[X]
Standard deviation
Covariance

For one roll of a fair die:

E[X]  = (1+2+3+4+5+6) / 6 = 3.5
E[X²] = (1+4+9+16+25+36) / 6 = 91/6 ≈ 15.17
Var   = 15.17 − 3.5² = 15.17 − 12.25 ≈ 2.92
σ     = √2.92 ≈ 1.71

In code

expectation.py
import numpy as np

faces = np.arange(1, 7)
probabilities = np.full(6, 1 / 6)

mean = np.sum(faces * probabilities)
variance = np.sum((faces - mean) ** 2 * probabilities)
print("Expected value:", round(mean, 2))
print("Variance:", round(variance, 2))
print("Standard deviation:", round(np.sqrt(variance), 2))

rng = np.random.default_rng(0)
rolls = rng.integers(1, 7, size=100_000)
print("Simulated mean:", round(rolls.mean(), 2), "variance:", round(rolls.var(), 2))

Output

Expected value: 3.5
Variance: 2.92
Standard deviation: 1.71
Simulated mean: 3.5 variance: 2.91

Common mistakes

  • Expecting the expected value to actually occur. A die never shows 3.5.
  • Reading variance in the original units. It is in squared units, which is why standard deviation is easier to interpret.

Check yourself

Question 1

1. A ticket costs nothing, and pays ₹100 with probability 0.2 and ₹0 otherwise. What is its expected value?

Question 2

2. If X has variance 4, what is the variance of 3X?

Summary

  • Expected value: the probability-weighted average.
  • Variance: the average squared distance from the mean.
  • Standard deviation: the square root of variance, in the original units.

Finished reading?

Sources: Deisenroth, Faisal & Ong, Mathematics for Machine Learning