Chapter 4: Expectation and variance

Previous: PS 03 Discrete Random Variables | PS 00 Course Map | Next: PS 05 Continuous Random Variables

The big idea

Two numbers summarize most of what you need about a random variable. The expectation is its probability-weighted average: the balance point, and the long-run average. The variance is the average squared distance from that average: how spread out it is. Expectation is linear even for dependent variables, which makes hard problems easy. And averaging independent measurements shrinks the noise by a factor of .

In this chapter you will learn to
  1. Compute expectations, including of functions of a random variable.
  2. Use linearity of expectation and indicator variables to solve counting problems.
  3. Compute variance and standard deviation, using the shortcut formula.
  4. Apply the rules for variances of scaled and summed variables.
  5. Explain and use the square-root law for averages.
  6. Use Markov’s and Chebyshev’s inequalities, and conditional expectation.
Before you start

You need PMFs and the named distributions from PS 03 Discrete Random Variables.


4.1 Expectation

Definition

Definition: expectation

The expectation (expected value, mean) of a discrete random variable is

Each possible value, weighted by its probability, summed.

Often written (mu).

Three ways to think about it

  1. Weighted average. Values that are more likely count more.
  2. Balance point. Draw the PMF as weights on a ruler; the expectation is where the ruler balances.
  3. Long-run average. Repeat the experiment many times and average the results; you approach (made precise in PS 08 Law of Large Numbers and Central Limit Theorem).

Worked examples

Worked example 4.1: a die.

You can never roll 3.5. The expectation is an average, not a prediction of one outcome, and not necessarily a possible value.

Worked example 4.2: a bet. Win 10 with probability 0.1; lose 1 with probability 0.9.

On average you gain 0.1 per bet, even though you lose 90% of the time. The rare big win carries the average.

Worked example 4.3: Bernoulli. .


4.2 Expectation of a function

To average , weight each by the probability of :

Rule (law of the unconscious statistician)

You do not need to find the distribution of first.

Worked example 4.4. For a die, .

in general

but . “Average, then square” is not “square, then average”.

Equality holds when is linear, . For other functions it usually fails. This is a frequent source of errors in reasoning about averages, for example averaging speeds or ratios.


4.3 Linearity of expectation

Linearity of expectation

For any random variables , and constants :

This holds whether or not and are independent.

It is the most useful fact in probability, because it lets you break a complicated quantity into simple pieces whose expectations you can find separately, without ever working out the full distribution.

Worked example 4.5: sum of two dice. . No need for the 36-outcome table.

Worked example 4.6: binomial mean. A Binomial() count is a sum of Bernoulli variables, , each with mean . So . One line, instead of a long sum with binomial coefficients.

Indicator variables

An indicator variable is 1 if event happens and 0 otherwise. Its expectation is simply the probability of the event:

The indicator method

To find the expected number of things that happen: write the count as a sum of indicators, one per thing, then add up their probabilities.

Worked example 4.7: the hat-check problem. people check their hats; the hats are handed back in random order. What is the expected number of people who get their own hat?

Let if person gets their own hat. Each person is equally likely to receive any of the hats, so . The count is , and

One person on average, whatever is. The are dependent (if the first people got their own hats, so did the last), but linearity does not care.

Worked example 4.8: distinct values. Roll a die 6 times. Expected number of distinct faces seen? Let if face appears at least once. . So the expected number of distinct faces is . Rolling 6 times usually shows only about 4 different faces.


4.4 Variance and standard deviation

Why we need a second number

The values and (each equally likely) both have mean 5, but the second is far more spread out. Variance measures spread.

Definition: variance

The standard deviation is .

Why square the deviations? Without squaring, positive and negative deviations cancel ( always). Squaring makes them all count, and penalizes large deviations more.

Why take the square root? Variance has squared units (metres squared, if is in metres). The standard deviation is back in the original units, so it is the one to report.

The shortcut formula

Expanding the square and using linearity:

Shortcut

“Mean of the square minus square of the mean.”

Worked examples

Worked example 4.9: a die. , :

Worked example 4.10: Bernoulli. (since , ), so and .

Worked example 4.11: the two spreads. : deviations , variance 1, . : deviations , variance 25, .


4.5 Rules for variance

Scaling and shifting

  • Shifting by moves every value equally; the spread does not change.
  • Scaling by stretches every deviation by , so squared deviations by .

Example. Temperatures in Celsius have . In Fahrenheit (), .

Sums of independent variables

Variances add for independent variables

If and are independent,

Notice the second line: variances add even when you subtract. Uncertainty never cancels; combining two uncertain quantities, in either direction, makes the result more uncertain.

This rule needs independence (more precisely, zero covariance). The general formula with a covariance term is in PS 06 Joint Distributions and Covariance.

Standard deviations do not add. They combine like the sides of a right triangle:

Worked example 4.12. Two independent sensors each have noise mm. The difference of their readings has mm, not 6 and not 0.

Worked example 4.13: binomial variance. , independent Bernoullis each with variance : .


4.6 The square-root law for averages

This is one of the most important results in the course.

Take independent measurements , each with mean and variance . Their average is

Mean: by linearity, . The average aims at the right value.

Variance: the sum has variance (independence), and dividing by divides the variance by :

The square-root law

Averaging independent measurements reduces the noise by a factor of .

  • To halve the error, you need 4 times the data.
  • To gain one more decimal digit of accuracy, you need 100 times the data.

This single fact explains why careful evaluation is expensive, why mini-batch gradients are noisy, and why more data gives diminishing returns.

Worked example 4.14. A sensor has noise cm. To get the average’s noise down to cm, solve : readings. To get to cm: .


4.7 Two inequalities that need almost no assumptions

Markov’s inequality

For a non-negative random variable and any :

Why: (when the right side is ; otherwise it is ). Take expectations: .

Example. If a robot’s average task time is 30 s, at most of tasks can take 120 s or more, whatever the distribution.

Chebyshev’s inequality

For any random variable with finite variance, and any :

Why: apply Markov to the non-negative variable with .

at most this much probability lies or more from the mean
225%
311%
101%

These bounds are weak (for a normal distribution the true numbers are 5%, 0.3% and essentially 0), but they hold for every distribution, which makes them valuable in proofs.


4.8 Conditional expectation

is the average of in the world where : compute it with the conditional distribution of given .

Law of total expectation

The overall average is the weighted average of the averages within each case.

Worked example 4.15. A robot handles light boxes (60% of jobs, average 20 s) and heavy boxes (40%, average 45 s). Average job time: s.

Worked example 4.16 (geometric mean, by conditioning). Let be the number of trials until the first success. Condition on the first trial: with probability it succeeds (); with probability it fails, and because the process has no memory, you start over having used one trial:

Solve: . This “condition on the first step” trick is the same idea as the Bellman equation in reinforcement learning (09 Reinforcement Learning).


Summary

IdeaFormula
expectation
function
linearity (always)
indicators
variance
scaling
independent sumvariances add
average of sd
Chebyshev

Three facts to remember:

  1. Expectation is linear, always, even with dependence.
  2. Variances add for independent variables; standard deviations do not.
  3. Averaging samples cuts the noise by .

Exercises

Part A: warm-up

A1. takes values with probabilities . Find , and .

A2. If , find and .

A3. Roll 10 dice. Find the mean and variance of the total.

A4. Binomial(20, 0.3). Find and .

Part B: core

B1. A sensor has noise standard deviation 2 cm. How many independent readings must be averaged to reach 0.25 cm?

B2. A robot picks from a bin of 10 objects, 3 of which are fragile. It picks 4 at random without replacement. Find the expected number of fragile objects picked, using indicators.

B3. Two independent quantities: a link length with mean 30 cm and sd 0.2 cm, and a second link with mean 25 cm and sd 0.1 cm. Find the mean and sd of the total length.

B4. A game pays where is a die roll. What is a fair price to play? Compare with .

B5. Average task time is 40 s with standard deviation 5 s. Use Chebyshev to bound the probability that a task takes more than 55 s or less than 25 s.

B6. You flip a fair coin until you see two heads in a row. Using conditioning on the first one or two flips, find the expected number of flips.

Part C: explain and prove

C1. Prove from the definition.

C2. Prove that , and that it equals 0 only if is constant (with probability 1).

C3. Explain why in general, with a two-value example. Why does this matter when averaging speeds?

C4. Explain why the hat-check answer (1) does not depend on , in your own words.

Part D: practise with code

D1. Simulate 100,000 die rolls and compare the sample mean and variance with 3.5 and 2.917.

D2. Simulate the hat-check problem for (shuffle with rng.permutation(n) and count fixed points). Check the average is close to 1.

D3. For , draw 10,000 averages of standard normal samples and compute their standard deviation. Plot against on log-log axes. What slope do you see?

D4. Simulate B6 (flips until two heads in a row) and check that the average is near 6.


Where this is used

  • A training loss is an expectation over data. A mini-batch gradient estimates it, with noise shrinking like (05 Training Neural Policies).
  • Expected return is the objective of reinforcement learning, and value functions are conditional expectations (09 Reinforcement Learning).
  • Error budgets: independent error sources add in variance, which is how tolerances in a robot’s kinematic chain are combined.

Next: PS 05 Continuous Random Variables: measurements that can take any value.