Variance and Standard Deviation
Two investments can have the same expected return and very different risks. The expected value says where a random variable is centred; the variance says how far it typically strays from that centre.
Definition
With :
The standard deviation has the same units as . The second formula, "mean of the square minus square of the mean", is usually easier to compute. For a fair die, and , so and .
from fractions import Fraction
import math
def var(dist):
mu = sum(x * p for x, p in dist.items())
return sum((x - mu) ** 2 * p for x, p in dist.items()), mu
die = {x: Fraction(1, 6) for x in range(1, 7)}
v, mu = var(die)
print("die: mean", mu, " variance", v, " sd", round(math.sqrt(v), 4))
safe = {10: Fraction(1)}
risky = {0: Fraction(1, 2), 20: Fraction(1, 2)}
for name, d in [("safe", safe), ("risky", risky)]:
v, mu = var(d)
print(f"{name}: mean {mu}, sd {math.sqrt(v)}")die: mean 7/2 variance 35/12 sd 1.7078
safe: mean 10, sd 0.0
risky: mean 10, sd 10.0
The two investments both have mean 10, but standard deviations 0 and 10.
Rules
For constants :
- : shifting does not change spread; scaling multiplies the standard deviation by .
- If and are independent, .
Unlike linearity of expectation, the sum rule for variance needs independence (or at least zero covariance). For the variance is , not .
Variances of standard distributions
| Distribution | Mean | Variance |
|---|---|---|
| Bernoulli() | ||
| Binomial() | ||
| Geometric() | ||
| Poisson() | ||
| Uniform on | ||
| Exponential() | ||
| Normal() |
The binomial row follows from the Bernoulli row by the sum rule, since a binomial is a sum of independent Bernoullis.
The square-root law
Let be the average of independent copies of , each with standard deviation . Then
Averages are less variable than single observations, but only by : to halve the error you need 4 times the data. This is the root of the law of large numbers and of every "plus or minus" margin of error.
import numpy as np
rng = np.random.default_rng(4)
for n in [1, 4, 16, 64, 256]:
means = rng.integers(1, 7, size=(20_000, n)).mean(axis=1)
print(f"n={n:4d} sd of average of n dice: {means.std():.4f} theory: {np.sqrt(35 / 12) / np.sqrt(n):.4f}")n= 1 sd of average of n dice: 1.7128 theory: 1.7078
n= 4 sd of average of n dice: 0.8502 theory: 0.8539
n= 16 sd of average of n dice: 0.4286 theory: 0.4270
n= 64 sd of average of n dice: 0.2122 theory: 0.2135
n= 256 sd of average of n dice: 0.1064 theory: 0.1067
Sample variance
Given data , the sample variance divides by , not : . Dividing by underestimates on average, because the data are closer to their own mean than to the true . NumPy's var divides by unless you pass ddof=1; the Statistics course covers this.
import numpy as np
rng = np.random.default_rng(5)
samples = rng.integers(1, 7, size=(100_000, 5)) # many samples of size 5
print("average of var (divide by n): ", round(samples.var(axis=1).mean(), 4))
print("average of var (divide by n-1): ", round(samples.var(axis=1, ddof=1).mean(), 4))
print("true variance 35/12 =", round(35 / 12, 4))average of var (divide by n): 2.3364
average of var (divide by n-1): 2.9205
true variance 35/12 = 2.9167
In software
Variance drives capacity planning (peak load is mean plus a few standard deviations), alerting thresholds, A/B test sample sizes ( to detect a difference ), and portfolio risk. In ML, batch normalization standardises activations to mean 0 and variance 1, and variance reduction techniques make reinforcement learning and Monte Carlo estimates converge faster.
Exercises
Exercise 1: A Bernoulli maximum
For which is the variance of a Bernoulli() variable largest, and what is it?
Answer
, variance .
Solution
is a downward parabola with maximum at .
Exercise 2: Temperature conversion
A temperature in Celsius has standard deviation 5. What is its standard deviation in Fahrenheit, ?
Answer
Solution
. The shift by 32 does not matter.
Exercise 3: Sum of dice (code)
Compute the exact variance of the sum of three dice with fractions, and compare with .
Answer
.
Solution
from fractions import Fraction
from itertools import product
sums = [sum(r) for r in product(range(1, 7), repeat=3)]
n = len(sums)
mu = Fraction(sum(sums), n)
v = sum((s - mu) ** 2 for s in sums) / n
print(mu, v, 3 * Fraction(35, 12))21/2 35/4 35/4
Test yourself
If , what is ?
- 12
- 19
- 36
- 43
The standard deviation of an average of independent measurements is…