Skip to content
BIASED

Expectation and variance

Expectation and its linearity, the expectation of a function (LOTUS) and Jensen's inequality, variance and standard deviation, shifting and scaling, moments and shape, and expected loss as the target of learning.

The centre of a distribution

The topic Functions of random variables showed how to get the full distribution of Y=g(X)Y = g(X): through the CDF, or with the change of variables pY(y)=pX(x)/∣g′(x)∣p_Y(y) = p_X(x)/\lvert g'(x)\rvert. But a full distribution is a whole function. To compare two models, report a benchmark or state what a model should learn, we usually want one or two numbers: where the distribution sits and how wide it is. This topic builds both.

The first number is the expectation. For a discrete XX it weights each value by its probability:

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

For a continuous XX the sum becomes an integral against the density:

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

A fair die gives E[X]=(1+2+⋯+6)/6=3.5\mathbb E[X] = (1 + 2 + \dots + 6)/6 = 3.5, a value it never shows. So the expectation is not "the typical value". It has two other readings.

  • Balance point. Put mass pX(x)p_X(x) at each point xx of a weightless rod. The rod balances at E[X]\mathbb E[X].
  • Long-run average. Average many independent copies of XX, and the average settles near E[X]\mathbb E[X]. The topic The law of large numbers makes this precise.

For the Poisson distribution the sum takes one line:

E[X]=∑k≥1k e−λλkk!=λ∑k≥1e−λλk−1(k−1)!=λ.\mathbb E[X] = \sum_{k \ge 1} k \, e^{-\lambda} \frac{\lambda^k}{k!} = \lambda \sum_{k \ge 1} e^{-\lambda} \frac{\lambda^{k-1}}{(k-1)!} = \lambda .

So λ\lambda is exactly the average count, as promised in Discrete distributions. The other families from the previous topics have these means:

Distribution E[X]\mathbb E[X]
Bern⁡(p)\operatorname{Bern}(p) pp
Bin⁡(n,p)\operatorname{Bin}(n, p) npnp
Geom⁡(p)\operatorname{Geom}(p) 1/p1/p
Pois⁡(λ)\operatorname{Pois}(\lambda) λ\lambda
U(a,b)U(a, b) (a+b)/2(a + b)/2
Exp⁡(λ)\operatorname{Exp}(\lambda) 1/λ1/\lambda
N(μ,σ2)\mathcal N(\mu, \sigma^2) μ\mu

The expected wait for a six is Geom⁡(1/6)\operatorname{Geom}(1/6), so 6 rolls on average.

An expectation can fail to exist: the sum or integral must converge absolutely. The Cauchy distribution, with density 1/(π(1+x2))1/(\pi(1 + x^2)), has tails so heavy that ∫∣x∣ p(x) dx=∞\int \lvert x \rvert \, p(x) \, dx = \infty. Its running averages never settle down.

Linearity and indicators

The most useful property of expectation is linearity. For any random variables XX, YY and numbers aa, bb, cc,

E[aX+bY+c]=a E[X]+b E[Y]+c,\mathbb E[aX + bY + c] = a \, \mathbb E[X] + b \, \mathbb E[Y] + c ,

and the same holds for a sum of any number of terms:

E[X1+⋯+Xn]=E[X1]+⋯+E[Xn].\mathbb E[X_1 + \dots + X_n] = \mathbb E[X_1] + \dots + \mathbb E[X_n] .

No independence is needed. On a finite Ω\Omega the expectation is ∑ωX(ω) P({ω})\sum_\omega X(\omega) \, P(\{\omega\}), and a sum over outcomes splits term by term. That is the whole proof, and it never asks how XX and YY are related.

Linearity is strongest with indicators. The indicator 1A\mathbb 1_A equals 1 when the event AA happens and 0 otherwise, so

E[1A]=1⋅P(A)+0⋅P(Ac)=P(A).\mathbb 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 expected value is a sum of probabilities, however the events depend on each other.

  • Binomial mean. X∼Bin⁡(n,p)X \sim \operatorname{Bin}(n, p) is 1A1+⋯+1An\mathbb 1_{A_1} + \dots + \mathbb 1_{A_n}, so E[X]=np\mathbb E[X] = np. No sum over the pmf is needed.
  • Errors on a test set. A classifier with error rate 0.080.08 is expected to make 500⋅0.08=40500 \cdot 0.08 = 40 mistakes on 500 test examples. This holds even when the examples are not independent, say near-duplicates.
  • Shared birthdays. Among 23 people there are (232)=253\binom{23}{2} = 253 pairs, and each pair shares a birthday with probability 1/3651/365. The expected number of matching pairs is 253/365≈0.69253/365 \approx 0.69, although the pairs are far from independent.

Linearity gives the mean of a count. The probability that the count is zero, as in the birthday problem from Outcomes, events and counting, is a different and harder question.

The expectation of a function

In Functions of random variables, finding the distribution of Y=g(X)Y = g(X) took work. For the expectation of YY that work is unnecessary. Weight g(x)g(x) by the probabilities of XX itself:

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

This rule is nicknamed LOTUS, the law of the unconscious statistician: people use it without noticing that it needs a proof. In the discrete case the proof is to group the values xx by the value of g(x)g(x).

For the die, E[X2]=(1+4+9+16+25+36)/6=91/6≈15.17\mathbb E[X^2] = (1 + 4 + 9 + 16 + 25 + 36)/6 = 91/6 \approx 15.17, while (E[X])2=12.25(\mathbb E[X])^2 = 12.25. In general E[g(X)]≠g(E[X])\mathbb E[g(X)] \ne g(\mathbb E[X]).

The log-normal from the previous topic shows the gap sharply. Take X∼N(0,1)X \sim \mathcal N(0, 1) and Y=eXY = e^X. LOTUS with the normal density, after completing the square in the exponent, gives

E[eX]=∫−∞∞exe−x2/22π dx=e1/2∫−∞∞e−(x−1)2/22π dx=e1/2≈1.65,\mathbb E[e^X] = \int_{-\infty}^{\infty} e^x \frac{e^{-x^2/2}}{\sqrt{2\pi}} \, dx = e^{1/2} \int_{-\infty}^{\infty} \frac{e^{-(x-1)^2/2}}{\sqrt{2\pi}} \, dx = e^{1/2} \approx 1.65 ,

while eE[X]=e0=1e^{\mathbb E[X]} = e^0 = 1.

The direction of the gap depends on the shape of gg. This is Jensen's inequality: if gg is convex (curves upward, like x2x^2 or exe^x), then

E[g(X)]≥g(E[X]),\mathbb E[g(X)] \ge g(\mathbb E[X]),

and for a concave gg, such as log⁡\log, the inequality flips. In ML this is a daily warning: the average loss over predictions is not the loss of the average prediction. Jensen is also where the evidence lower bound (ELBO) comes from; the courses Bayesian Methods in Machine Learning and Generative Models use it constantly.

Variance: a measure of spread

The expectation says where a distribution sits, not how wide it is. Two training recipes can both average 0.800.80 accuracy over random seeds, one always landing at 0.800.80 and the other swinging between 0.600.60 and 1.001.00. The variance measures the spread. Write μ=E[X]\mu = \mathbb E[X]. The variance is the expected squared distance from the mean:

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

Expand the square and apply linearity. This gives a formula that is usually quicker:

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

The variance is in squared units: squared seconds, squared dollars. Its square root, the standard deviation σ=Var⁡(X)\sigma = \sqrt{\operatorname{Var}(X)}, is back in the units of XX. It is the number to report, and the error bar of a benchmark is a standard deviation.

For the die, LOTUS gave E[X2]=91/6\mathbb E[X^2] = 91/6. So

Var⁡(X)=916−3.52=3512≈2.92,σ≈1.71.\operatorname{Var}(X) = \frac{91}{6} - 3.5^2 = \frac{35}{12} \approx 2.92, \qquad \sigma \approx 1.71 .

A variance is an expectation of a square, so it is never negative. It is zero only when XX is a constant (with probability 1). Hence E[X2]≥(E[X])2\mathbb E[X^2] \ge (\mathbb E[X])^2, which is Jensen's inequality for g(x)=x2g(x) = x^2.

Distribution Var⁡(X)\operatorname{Var}(X)
Geom⁡(p)\operatorname{Geom}(p) (1−p)/p2(1 - p)/p^2
Pois⁡(λ)\operatorname{Pois}(\lambda) λ\lambda
U(a,b)U(a, b) (b−a)2/12(b - a)^2/12
Exp⁡(λ)\operatorname{Exp}(\lambda) 1/λ21/\lambda^2
N(μ,σ2)\mathcal N(\mu, \sigma^2) σ2\sigma^2

A Poisson count has variance equal to its mean, and an exponential wait has standard deviation equal to its mean. The Bernoulli variance is yours to find below. The binomial one waits for the next topic, where the variances of independent terms add.

Shifting and scaling

Change the units, and the mean and variance change in a predictable way. For Y=aX+bY = aX + b, linearity gives E[Y]=aμ+b\mathbb E[Y] = a\mu + b. Then Y−E[Y]=a(X−μ)Y - \mathbb E[Y] = a(X - \mu), and

Var⁡(aX+b)=E[a2(X−μ)2]=a2Var⁡(X),σaX+b=∣a∣ σX.\operatorname{Var}(aX + b) = \mathbb E\big[a^2 (X - \mu)^2\big] = a^2 \operatorname{Var}(X), \qquad \sigma_{aX + b} = \lvert a \rvert \, \sigma_X .

A shift bb moves the distribution without widening it. A scale aa stretches every deviation by ∣a∣\lvert a \rvert and the variance by a2a^2. A sign flip does not change the spread at all. Accuracy reported as a fraction with standard deviation 0.010.01 becomes accuracy in percent with standard deviation 11, and its variance grows 10410^4 times.

Choose a=1/σa = 1/\sigma and b=−μ/σb = -\mu/\sigma. This is the standardization from the previous topic:

Z=X−μσ,E[Z]=0,Var⁡(Z)=1.Z = \frac{X - \mu}{\sigma}, \qquad \mathbb E[Z] = 0, \qquad \operatorname{Var}(Z) = 1 .

There it turned any normal into N(0,1)\mathcal N(0, 1). Now we see that it gives mean 0 and variance 1 for any distribution with a finite variance. The shape stays the same, though: a standardized exponential is still lopsided.

ML pipelines standardize every input feature this way: subtract its mean, divide by its standard deviation. A feature measured in millimetres and one measured in kilometres then live on the same scale, which helps gradient descent and makes a penalty on the weights treat all features alike.

Moments and shape

The mean and the variance are the first two of a family. The kk-th moment of XX is E[Xk]\mathbb E[X^k], and the kk-th central moment is E[(X−μ)k]\mathbb E[(X - \mu)^k]. The variance is the second central moment. Two standardized moments describe the shape:

skewness=E[(X−μ)3]σ3,kurtosis=E[(X−μ)4]σ4.\text{skewness} = \frac{\mathbb E[(X - \mu)^3]}{\sigma^3}, \qquad \text{kurtosis} = \frac{\mathbb E[(X - \mu)^4]}{\sigma^4}.

Skewness measures lopsidedness. It is zero for a symmetric distribution and positive when a long tail goes to the right, as for incomes, request latencies or the log-normal. Kurtosis measures how much of the variance comes from rare large deviations. The normal has kurtosis 3, so libraries often report the excess kurtosis, kurtosis minus 3. The exponential has skewness 2 and kurtosis 9.

The mean is not the only centre. The median mm splits the probability in half; for a continuous XX, FX(m)=1/2F_X(m) = 1/2. The mode is the most probable value: the peak of the pmf or of the density. For a symmetric distribution with one peak all three coincide. For a skewed one the mean is pulled toward the long tail. The log-normal Y=eXY = e^X with X∼N(0,1)X \sim \mathcal N(0, 1) has mode e−1≈0.37e^{-1} \approx 0.37, median e0=1e^0 = 1 and mean e1/2≈1.65e^{1/2} \approx 1.65.

That is why incomes and house prices are reported as medians: a few huge values move the mean a lot and the median hardly at all. The same goes for the latency of a model server. A handful of slow requests can dominate the mean, so engineers report the median and a high percentile next to it.

And a caution: two numbers do not fix a distribution. Two distributions with the same mean and the same variance can have very different tails, and the tails are where outliers and risk live.

Expected loss

Why does machine learning care so much about expectations? Because what a model should learn is itself an expectation. A loss L(y,y^)L(y, \hat y) is the cost of predicting y^\hat y when the truth is yy. The truth YY is random, so the quality of a prediction cc is its expected loss, also called the risk:

R(c)=E[L(Y,c)].R(c) = \mathbb E\big[L(Y, c)\big].

Start with the simplest model: one constant cc for every input, the baseline every model has to beat. Under the squared loss, write Y−c=(Y−μ)+(μ−c)Y - c = (Y - \mu) + (\mu - c) and expand. The cross term is 2(μ−c) E[Y−μ]=02(\mu - c) \, \mathbb E[Y - \mu] = 0 by linearity, and what remains is

E[(Y−c)2]=Var⁡(Y)+(μ−c)2.\mathbb E\big[(Y - c)^2\big] = \operatorname{Var}(Y) + (\mu - c)^2 .

The risk is smallest at c=μc = \mu, and its minimum is Var⁡(Y)\operatorname{Var}(Y). So the mean is the best constant under the squared loss, and the variance is the error no constant can remove. Other losses pick other centres. The absolute loss ∣Y−c∣\lvert Y - c \rvert is minimized by the median, which shares the median's indifference to outliers. The 0–1 loss, which charges 1 for any miss, is minimized by the mode, the most probable class. Its risk is the error rate, E[1{Y≠c}]=P(Y≠c)\mathbb E[\mathbb 1_{\{Y \ne c\}}] = P(Y \ne c).

A real model does better than a constant: it predicts YY from features XX, and its risk E[L(Y,f(X))]\mathbb E[L(Y, f(X))] averages over pairs (X,Y)(X, Y). That needs the distribution of two variables at once. Sums need it too: Var⁡(X+X)=Var⁡(2X)=4Var⁡(X)\operatorname{Var}(X + X) = \operatorname{Var}(2X) = 4 \operatorname{Var}(X), but Var⁡(X+(−X))=0\operatorname{Var}(X + (-X)) = 0, so the variance of a sum depends on how its terms move together. The next topic, Joint distributions and independence, provides that language: joint distributions, covariance, and the rule that variances of independent terms add. The best f(X)f(X) under the squared loss waits in Conditional expectation.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.