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 : through the CDF, or with the change of variables . 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 it weights each value by its probability:
For a continuous the sum becomes an integral against the density:
A fair die gives , a value it never shows. So the expectation is not "the typical value". It has two other readings.
- Balance point. Put mass at each point of a weightless rod. The rod balances at .
- Long-run average. Average many independent copies of , and the average settles near . The topic The law of large numbers makes this precise.
For the Poisson distribution the sum takes one line:
So is exactly the average count, as promised in Discrete distributions. The other families from the previous topics have these means:
| Distribution | |
|---|---|
The expected wait for a six is , so 6 rolls on average.
An expectation can fail to exist: the sum or integral must converge absolutely. The Cauchy distribution, with density , has tails so heavy that . Its running averages never settle down.
Linearity and indicators
The most useful property of expectation is linearity. For any random variables , and numbers , , ,
and the same holds for a sum of any number of terms:
No independence is needed. On a finite the expectation is , and a sum over outcomes splits term by term. That is the whole proof, and it never asks how and are related.
Linearity is strongest with indicators. The indicator equals 1 when the event happens and 0 otherwise, so
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. is , so . No sum over the pmf is needed.
- Errors on a test set. A classifier with error rate is expected to make mistakes on 500 test examples. This holds even when the examples are not independent, say near-duplicates.
- Shared birthdays. Among 23 people there are pairs, and each pair shares a birthday with probability . The expected number of matching pairs is , 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 took work. For the expectation of that work is unnecessary. Weight by the probabilities of itself:
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 by the value of .
For the die, , while . In general .
The log-normal from the previous topic shows the gap sharply. Take and . LOTUS with the normal density, after completing the square in the exponent, gives
while .
The direction of the gap depends on the shape of . This is Jensen's inequality: if is convex (curves upward, like or ), then
and for a concave , such as , 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 accuracy over random seeds, one always landing at and the other swinging between and . The variance measures the spread. Write . The variance is the expected squared distance from the mean:
Expand the square and apply linearity. This gives a formula that is usually quicker:
The variance is in squared units: squared seconds, squared dollars. Its square root, the standard deviation , is back in the units of . It is the number to report, and the error bar of a benchmark is a standard deviation.
For the die, LOTUS gave . So
A variance is an expectation of a square, so it is never negative. It is zero only when is a constant (with probability 1). Hence , which is Jensen's inequality for .
| Distribution | |
|---|---|
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 , linearity gives . Then , and
A shift moves the distribution without widening it. A scale stretches every deviation by and the variance by . A sign flip does not change the spread at all. Accuracy reported as a fraction with standard deviation becomes accuracy in percent with standard deviation , and its variance grows times.
Choose and . This is the standardization from the previous topic:
There it turned any normal into . 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 -th moment of is , and the -th central moment is . The variance is the second central moment. Two standardized moments describe the shape:
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 splits the probability in half; for a continuous , . 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 with has mode , median and mean .
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 is the cost of predicting when the truth is . The truth is random, so the quality of a prediction is its expected loss, also called the risk:
Start with the simplest model: one constant for every input, the baseline every model has to beat. Under the squared loss, write and expand. The cross term is by linearity, and what remains is
The risk is smallest at , and its minimum is . 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 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, .
A real model does better than a constant: it predicts from features , and its risk averages over pairs . That needs the distribution of two variables at once. Sums need it too: , but , 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 under the squared loss waits in Conditional expectation.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.