SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 5.3.1 Expectation and LOTUSConcept · 30 min
      2. 5.3.2 Variance, and linearity of expectationConcept · 35 min
      3. 5.3.3 Skewness and kurtosisConcept · 30 min
      4. 5.3.4 Practice set: expectation and momentsPractice set · 20 min
      5. 5.3.5 Case study: return moments and value at riskCase study · 35 min

Level 5, Probability and statistics 5.3 Expectation and moments Lesson 2 of 5

ConceptLesson 5.3.2 in Level 5, Probability and statistics. About 35 minutes.

Variance, and linearity of expectation

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 μ=E[X]\mu = \mathbb{E}[X] is its deviation, x−μx - \mu. Deviations above and below the mean cancel, so their average is always 0. Squaring them first stops the cancelling.

Rule: Variance and standard deviation

Var⁡(X)=E[(X−μ)2]σX=Var⁡(X)\begin{gathered} \operatorname{Var}(X) = \mathbb{E}\big[(X - \mu)^2\big] \\[4pt] \sigma_X = \sqrt{\operatorname{Var}(X)} \end{gathered}

The variance is the expected squared deviation, found by LOTUS (lesson 5.3.1) with g(x)=(x−μ)2g(x) = (x - \mu)^2. It is in squared units, such as dollars squared. The standard deviation σX\sigma_X (sigma) is back in the units of XX.

Game A has variance 12⋅12+12⋅(−1)2=1\frac{1}{2} \cdot 1^2 + \frac{1}{2} \cdot (-1)^2 = 1 and standard deviation $1. Game B has variance 100 and standard deviation $10: ten times the spread.

For a fair die, μ=3.5\mu = 3.5 and each face has probability 16\frac{1}{6}:

xx123456
x−μx - \mu−2.5-2.5−1.5-1.5−0.5-0.50.51.52.5
(x−μ)2(x - \mu)^26.252.250.250.252.256.25

The squares add up to 17.5, so the variance is 17.56=3512≈2.9167\frac{17.5}{6} = \frac{35}{12} \approx 2.9167. The standard deviation is 35/12≈1.7078\sqrt{35/12} \approx 1.7078 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 XX always takes the same value, every deviation is 0.
  • Bernoulli(pp): Var⁡(X)=p(1−p)\operatorname{Var}(X) = p(1 - p). It is largest, 0.25, at p=12p = \frac{1}{2}, and 0 at p=0p = 0 or 1, where the outcome is certain.

Try it

variance applies LOTUS to the squared deviations. The die gives 3512\frac{35}{12}; then its standard deviation.

The loop gives the Bernoulli variance p(1−p)p(1 - p) for several pp. Without round, p=0.1p = 0.1 would print 0.09000000000000001: float noise in the last digit.

⌘/Ctrl + Enter runs the cell

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

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

The mean of the square minus the square of the mean: exactly the gap of lesson 5.3.1. So E[X2]≥(E[X])2\mathbb{E}[X^2] \ge (\mathbb{E}[X])^2, with equality only for a constant.

Take XX equal to 0, 1 or 2 with probabilities 0.5, 0.3 and 0.2, so μ=0.7\mu = 0.7. Both formulas give 0.61:

  • Definition: 0.49×0.5+0.09×0.3+1.69×0.2=0.245+0.027+0.338=0.610.49 \times 0.5 + 0.09 \times 0.3 + 1.69 \times 0.2 = 0.245 + 0.027 + 0.338 = 0.61.
  • Shortcut: E[X2]=0+0.3+0.8=1.1\mathbb{E}[X^2] = 0 + 0.3 + 0.8 = 1.1, and 1.1−0.72=0.611.1 - 0.7^2 = 0.61.

For the die, E[X2]=916\mathbb{E}[X^2] = \frac{91}{6} (lesson 5.3.1), so the shortcut gives 916−494=3512\frac{91}{6} - \frac{49}{4} = \frac{35}{12} again.

On a computer the shortcut has a trap. Add 10810^8 to every face: the spread is still 3512\frac{35}{12}, but E[X2]\mathbb{E}[X^2] is about 101610^{16}. 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 10810^8. 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.

⌘/Ctrl + Enter runs the cell

Shifting and scaling

Change the units, or add a fee: many random variables are aX+baX + b for some XX whose mean and variance you know.

Rule: Linear change

E[aX+b]=a E[X]+bVar⁡(aX+b)=a2Var⁡(X)σaX+b=∣a∣ σX\begin{gathered} \mathbb{E}[aX + b] = a\,\mathbb{E}[X] + b \\[4pt] \operatorname{Var}(aX + b) = a^2 \operatorname{Var}(X) \\[4pt] \sigma_{aX + b} = |a|\,\sigma_X \end{gathered}

Adding bb moves every value and the mean by bb, so the deviations do not change. Multiplying by aa multiplies each deviation by aa and each squared deviation by a2a^2.

A fee and a prize. Pay $20 to roll a die and win $10 for each dot, so you gain Y=10X−20Y = 10X - 20 dollars. Then E[Y]=10×3.5−20=15\mathbb{E}[Y] = 10 \times 3.5 - 20 = 15 dollars. The variance is 100×3512≈291.67100 \times \frac{35}{12} \approx 291.67 dollars squared, and the standard deviation is 10×1.7078≈17.0810 \times 1.7078 \approx 17.08 dollars.

  • A negative aa. The other side of a bet, −X-X, has the same spread: Var⁡(−X)=(−1)2Var⁡(X)\operatorname{Var}(-X) = (-1)^2\operatorname{Var}(X). That is why σ\sigma takes ∣a∣|a|.
  • Units. In cents, 100X100X 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:

Z=X−μσXZ = \frac{X - \mu}{\sigma_X}

This is aX+baX + b with a=1σXa = \frac{1}{\sigma_X} and b=−μσXb = -\frac{\mu}{\sigma_X}, so E[Z]=0\mathbb{E}[Z] = 0 and Var⁡(Z)=1\operatorname{Var}(Z) = 1. ZZ counts standard deviations from the mean and has no units.

For the die, ZZ runs from −1.4639-1.4639 to 1.4639. Lesson 5.3.3 builds on ZZ.

Try it

The cell applies each change to the die's values and recomputes the mean and variance from scratch.

Y=10X−20Y = 10X - 20 gives 15 and 291.6667, which is 100×3512100 \times \frac{35}{12}. Then −X-X, and the standardised ZZ with mean 0 and variance 1. Adding 0.0 turns a -0.0 into 0.0.

⌘/Ctrl + Enter runs the cell

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

E[X+Y]=E[X]+E[Y]E[∑iaiXi]=∑iai E[Xi]\begin{gathered} \mathbb{E}[X + Y] = \mathbb{E}[X] + \mathbb{E}[Y] \\[4pt] \mathbb{E}\Big[\sum_i a_i X_i\Big] = \sum_i a_i\,\mathbb{E}[X_i] \end{gathered}

It holds for any random variables on the same experiment, independent or not, and with any constants aia_i.

Two dice. The total of two dice has mean 3.5+3.5=73.5 + 3.5 = 7, 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 7−X7 - X. The two faces are as dependent as can be, yet E[X+(7−X)]=3.5+3.5=7\mathbb{E}[X + (7 - X)] = 3.5 + 3.5 = 7 still.

Counting with indicators

An indicator IAI_A is 1 if the event AA happens and 0 if not. It is a Bernoulli variable, so E[IA]=P(A)\mathbb{E}[I_A] = P(A). 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:

order123132213231312321
letters in the right envelope311001

The average is 66=1\frac{6}{6} = 1. With indicators: each letter is in its own envelope with probability 13\frac{1}{3}, so the mean count is 3×13=13 \times \frac{1}{3} = 1.

The indicators are not independent: if two letters are right, so is the third. Linearity does not care. With nn letters the answer is n×1n=1n \times \frac{1}{n} = 1, for every nn.

Try it

permutations lists every order of nn letters; the cell averages the number of matches over all of them for nn 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.

⌘/Ctrl + Enter runs the cell

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

Var⁡(X+Y)=Var⁡(X)+Var⁡(Y)\begin{gathered} \operatorname{Var}(X + Y) \\[4pt] = \operatorname{Var}(X) + \operatorname{Var}(Y) \end{gathered}

True when XX and YY are independent. Without independence it can fail; lesson 5.5.2 gives the general rule, with an extra covariance term.

sumindependent?variance
two separate diceyes3512+3512=356≈5.8333\frac{35}{12} + \frac{35}{12} = \frac{35}{6} \approx 5.8333
top face plus bottom faceno0, since the total is always 7
X+X=2XX + X = 2Xno4×3512=353≈11.674 \times \frac{35}{12} = \frac{35}{3} \approx 11.67

The last row is a common slip. A variable is not independent of itself, so Var⁡(X+X)\operatorname{Var}(X + X) is Var⁡(2X)=4Var⁡(X)\operatorname{Var}(2X) = 4\operatorname{Var}(X), by the scaling rule.

Try it

The 36 equally likely totals of two dice have variance 356\frac{35}{6}. 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 353\frac{35}{3}.

⌘/Ctrl + Enter runs the cell

The binomial's mean and variance

A binomial variable X∼Bin⁡(n,p)X \sim \operatorname{Bin}(n, p) counts the successes in nn independent trials, each a success with probability pp (lesson 5.2.2). It is a sum of indicators, X=I1+I2+⋯+InX = I_1 + I_2 + \cdots + I_n, where IjI_j is 1 when trial jj succeeds. Each IjI_j is Bernoulli(pp), with mean pp and variance p(1−p)p(1 - p).

Rule: Binomial mean and variance

E[X]=npVar⁡(X)=np(1−p)\begin{gathered} \mathbb{E}[X] = np \\[4pt] \operatorname{Var}(X) = np(1 - p) \end{gathered}

The mean comes from linearity alone. The variance needs the trials to be independent, so that the nn Bernoulli variances add.

Count the sixes in 10 rolls of a die: n=10n = 10 and p=16p = \frac{1}{6}. The mean is 106=53≈1.6667\frac{10}{6} = \frac{5}{3} \approx 1.6667 and the variance is 10×16×56=2518≈1.388910 \times \frac{1}{6} \times \frac{5}{6} = \frac{25}{18} \approx 1.3889. The standard deviation is about 1.1785.

The same rules give the whole of module 5.2:

distributionE[X]\mathbb{E}[X]Var⁡(X)\operatorname{Var}(X)
Bernoulli(pp)ppp(1−p)p(1 - p)
Bin⁡(n,p)\operatorname{Bin}(n, p)npnpnp(1−p)np(1 - p)
Geom⁡(p)\operatorname{Geom}(p)1p\frac{1}{p}1−pp2\frac{1 - p}{p^2}
Poisson⁡(λ)\operatorname{Poisson}(\lambda)λ\lambdaλ\lambda

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 p=16p = \frac{1}{6} and the Poisson with λ=2.5\lambda = 2.5 by long sums.

⌘/Ctrl + Enter runs the cell

Common mistakes

  1. Means always add; variances add only for independent variables. The top and bottom faces of a die have a total with variance 0, not 356\frac{35}{6}.
  2. In Var⁡(aX+b)\operatorname{Var}(aX + b) the shift drops out and aa is squared. Var⁡(3X+5)=9Var⁡(X)\operatorname{Var}(3X + 5) = 9\operatorname{Var}(X).
  3. A variance is never negative. Var⁡(−X)=Var⁡(X)\operatorname{Var}(-X) = \operatorname{Var}(X), and σaX=∣a∣ σX\sigma_{aX} = |a|\,\sigma_X.
  4. X+XX + X is not two independent copies. Var⁡(X+X)=4Var⁡(X)\operatorname{Var}(X + X) = 4\operatorname{Var}(X), not 2Var⁡(X)2\operatorname{Var}(X).
  5. Variance is in squared units. Compare spreads, and quote them, with the standard deviation.
  6. The shortcut needs E[X2]\mathbb{E}[X^2], not (E[X])2(\mathbb{E}[X])^2. On a computer, use the definition when the mean is large compared with the spread.

Variance and linearity

0 of 13 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 11.
  12. 12.
  13. 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, e is Euler's number, sqrt(x) is a square root.
  • sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for 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

  • Var⁡(X)=E[(X−μ)2]=E[X2]−μ2\operatorname{Var}(X) = \mathbb{E}[(X - \mu)^2] = \mathbb{E}[X^2] - \mu^2, in squared units; σX=Var⁡(X)\sigma_X = \sqrt{\operatorname{Var}(X)} is in the units of XX. A variance is never negative, and 0 only for a constant.
  • E[aX+b]=a E[X]+b\mathbb{E}[aX + b] = a\,\mathbb{E}[X] + b, Var⁡(aX+b)=a2Var⁡(X)\operatorname{Var}(aX + b) = a^2\operatorname{Var}(X) and σaX+b=∣a∣ σX\sigma_{aX + b} = |a|\,\sigma_X. Z=X−μσXZ = \frac{X - \mu}{\sigma_X} has mean 0 and variance 1.
  • Linearity: E[X+Y]=E[X]+E[Y]\mathbb{E}[X + Y] = \mathbb{E}[X] + \mathbb{E}[Y] always. A count is a sum of indicators, so its mean is a sum of probabilities.
  • Var⁡(X+Y)=Var⁡(X)+Var⁡(Y)\operatorname{Var}(X + Y) = \operatorname{Var}(X) + \operatorname{Var}(Y) for independent XX and YY only.
  • Binomial: mean npnp, variance np(1−p)np(1 - p). Geometric: 1p\frac{1}{p} and 1−pp2\frac{1 - p}{p^2}. Poisson: λ\lambda and λ\lambda.

Free further reading

Optional. This lesson covers what you need; these add more examples and practice. All are free to read.