Chapter 4: Expectation and variance
Previous: PS 03 Discrete Random Variables | PS 00 Course Map | Next: PS 05 Continuous Random Variables
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 .
- Compute expectations, including of functions of a random variable.
- Use linearity of expectation and indicator variables to solve counting problems.
- Compute variance and standard deviation, using the shortcut formula.
- Apply the rules for variances of scaled and summed variables.
- Explain and use the square-root law for averages.
- Use Markov’s and Chebyshev’s inequalities, and conditional expectation.
You need PMFs and the named distributions from PS 03 Discrete Random Variables.
4.1 Expectation
Definition
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
- Weighted average. Values that are more likely count more.
- Balance point. Draw the PMF as weights on a ruler; the expectation is where the ruler balances.
- 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. .
takes values with probabilities . Find .
Answer. .
4.2 Expectation of a function
To average , weight each by the probability of :
You do not need to find the distribution of first.
Worked example 4.4. For a die, .
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
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:
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.
In a class of 30, each student’s birthday is one of 365 days at random. What is the expected number of pairs of students sharing a birthday?
Answer. pairs, each matching with probability : .
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.
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:
“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
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 :
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 | |
|---|---|
| 2 | 25% |
| 3 | 11% |
| 10 | 1% |
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 .
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
| Idea | Formula |
|---|---|
| expectation | |
| function | |
| linearity | (always) |
| indicators | |
| variance | |
| scaling | |
| independent sum | variances add |
| average of | sd |
| Chebyshev |
Three facts to remember:
- Expectation is linear, always, even with dependence.
- Variances add for independent variables; standard deviations do not.
- 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 .
A1. . . .
A2. ; .
A3. Mean ; variance .
A4. ; .
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.
B1. , so , .
B2. Indicator for each fragile object being picked: probability . Expected: .
B3. Mean cm; sd cm.
B4. . . The fair price is 15.17.
B5. 55 and 25 are from the mean: probability at most .
B6. Let be the expected number. First flip T (prob ): used 1, start over. First H then T (prob ): used 2, start over. HH (prob ): done in 2. So . Then , so and .
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.
C1. , so . Squaring and taking expectations: .
C2. It is the expectation of a square, which is non-negative. It is zero only if with probability 1, i.e. .
C3. or equally: , but . Driving equal distances at 30 and 90 km/h gives an average speed of 45, not 60, because the slow leg takes longer.
C4. More people means more chances for a match, but each person’s chance falls to . The two effects cancel exactly.
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.
D3. The points lie on a line of slope : the standard deviation is proportional to .
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.