Spread around the mean
Two coin games have the same mean. In game A you win or lose $1; in game B you win or lose $10. Both have expected winnings of 0, but B is far riskier.
The mean says nothing about how far the outcomes land from it.
The distance of a value from the mean is its deviation, . Deviations above and below the mean cancel, so their average is always 0. Squaring them first stops the cancelling.
Rule: Variance and standard deviation
The variance is the expected squared deviation, found by LOTUS (lesson 5.3.1) with . It is in squared units, such as dollars squared. The standard deviation (sigma) is back in the units of .
Game A has variance and standard deviation $1. Game B has variance 100 and standard deviation $10: ten times the spread.
For a fair die, and each face has probability :
| 1 | 2 | 3 | 4 | 5 | 6 | |
|---|---|---|---|---|---|---|
| 0.5 | 1.5 | 2.5 | ||||
| 6.25 | 2.25 | 0.25 | 0.25 | 2.25 | 6.25 |
The squares add up to 17.5, so the variance is . The standard deviation is dots.
Two distributions with mean 3. The bracket under each runs from one standard deviation below the mean to one above: 1.22 for the binomial, 2 for the flat one.
Three cases follow from the rule.
- Never negative. A variance is a sum of squares times probabilities.
- Zero only for a constant. If always takes the same value, every deviation is 0.
- Bernoulli(): . It is largest, 0.25, at , and 0 at or 1, where the outcome is certain.
Try it
variance applies LOTUS to the squared deviations. The die gives ; then its standard deviation.
The loop gives the Bernoulli variance for several . Without round, would print 0.09000000000000001: float noise in the last digit.
A shortcut: the mean of the square minus the square of the mean
Expanding the square gives a second formula, often quicker by hand.
Rule: Variance shortcut
The mean of the square minus the square of the mean: exactly the gap of lesson 5.3.1. So , with equality only for a constant.
Take equal to 0, 1 or 2 with probabilities 0.5, 0.3 and 0.2, so . Both formulas give 0.61:
- Definition: .
- Shortcut: , and .
For the die, (lesson 5.3.1), so the shortcut gives again.
On a computer the shortcut has a trap. Add to every face: the spread is still , but is about . A computer keeps about 16 digits (lesson 2.8.1), so subtracting two such nearly equal numbers leaves only rounding error.
Try it
The first lines compare the two formulas on the small example: both give 0.61.
The last lines shift the die by . np.var uses the definition and still gives 2.916667. The shortcut misses by more than 0.5; its exact digits are rounding noise and vary between computers.
Shifting and scaling
Change the units, or add a fee: many random variables are for some whose mean and variance you know.
Rule: Linear change
Adding moves every value and the mean by , so the deviations do not change. Multiplying by multiplies each deviation by and each squared deviation by .
A fee and a prize. Pay $20 to roll a die and win $10 for each dot, so you gain dollars. Then dollars. The variance is dollars squared, and the standard deviation is dollars.
- A negative . The other side of a bet, , has the same spread: . That is why takes .
- Units. In cents, has a mean and standard deviation 100 times larger, and a variance 10,000 times larger.
Standardising
Subtract the mean and divide by the standard deviation:
This is with and , so and . counts standard deviations from the mean and has no units.
For the die, runs from to 1.4639. Lesson 5.3.3 builds on .
Try it
The cell applies each change to the die's values and recomputes the mean and variance from scratch.
gives 15 and 291.6667, which is . Then , and the standardised with mean 0 and variance 1. Adding 0.0 turns a -0.0 into 0.0.
Linearity of expectation
The mean of a sum is the sum of the means. This is the most useful rule about expected values, because it needs nothing about how the variables relate.
Rule: Linearity of expectation
It holds for any random variables on the same experiment, independent or not, and with any constants .
Two dice. The total of two dice has mean , with no need for the table of 36 totals.
The top and the bottom. Opposite faces of a die add up to 7, so the face underneath is . The two faces are as dependent as can be, yet still.
Counting with indicators
An indicator is 1 if the event happens and 0 if not. It is a Bernoulli variable, so . A count is a sum of indicators, so its mean is a sum of probabilities.
Put 3 letters into 3 envelopes in a random order. How many land in their own envelope, on average? List the 6 orders, writing which letter each envelope gets:
| order | 123 | 132 | 213 | 231 | 312 | 321 |
|---|---|---|---|---|---|---|
| letters in the right envelope | 3 | 1 | 1 | 0 | 0 | 1 |
The average is . With indicators: each letter is in its own envelope with probability , so the mean count is .
The indicators are not independent: if two letters are right, so is the third. Linearity does not care. With letters the answer is , for every .
Try it
permutations lists every order of letters; the cell averages the number of matches over all of them for from 3 to 6. Each mean is exactly 1.
The last line shuffles 52 cards 100,000 times in one seeded run: 0.99767 cards land in their own place on average.
Variances add for independent variables
Variances do not always add. They do when the variables are independent: knowing one changes no probability about the other (lesson 5.1.3).
Rule: Variance of a sum
True when and are independent. Without independence it can fail; lesson 5.5.2 gives the general rule, with an extra covariance term.
| sum | independent? | variance |
|---|---|---|
| two separate dice | yes | |
| top face plus bottom face | no | 0, since the total is always 7 |
| no |
The last row is a common slip. A variable is not independent of itself, so is , by the scaling rule.
Try it
The 36 equally likely totals of two dice have variance . np.var divides by the number of values, as the rule's average does.
The top plus the bottom face is 7 every time, so its variance is 0. Doubling one die gives .
The binomial's mean and variance
A binomial variable counts the successes in independent trials, each a success with probability (lesson 5.2.2). It is a sum of indicators, , where is 1 when trial succeeds. Each is Bernoulli(), with mean and variance .
Rule: Binomial mean and variance
The mean comes from linearity alone. The variance needs the trials to be independent, so that the Bernoulli variances add.
Count the sixes in 10 rolls of a die: and . The mean is and the variance is . The standard deviation is about 1.1785.
The same rules give the whole of module 5.2:
| distribution | ||
|---|---|---|
| Bernoulli() | ||
The geometric and Poisson variances are stated, and checked below. Waiting for a six has mean 6 and variance 30, a standard deviation of about 5.48 rolls. A Poisson count has its variance equal to its mean.
Try it
The first line sums the 11 terms of the binomial pmf, then asks SciPy with .stats(moments="mv"), short for mean and variance. One seeded run of 100,000 binomial counts gives 1.67069 and 1.40058.
The last lines check the geometric with and the Poisson with by long sums.
Common mistakes
- Means always add; variances add only for independent variables. The top and bottom faces of a die have a total with variance 0, not .
- In the shift drops out and is squared. .
- A variance is never negative. , and .
- is not two independent copies. , not .
- Variance is in squared units. Compare spreads, and quote them, with the standard deviation.
- The shortcut needs , not . On a computer, use the definition when the mean is large compared with the spread.
Variance and linearity
0 of 13 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Summary card
- , in squared units; is in the units of . A variance is never negative, and 0 only for a constant.
- , and . has mean 0 and variance 1.
- Linearity: always. A count is a sum of indicators, so its mean is a sum of probabilities.
- for independent and only.
- Binomial: mean , variance . Geometric: and . Poisson: and .