TExcelTAD
Home › Probability Theory › Variance and Standard Deviation

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 μ=E[X]\mu = E[X]:

Var⁡(X)=E[(X−μ)2]=E[X2]−μ2,σ(X)=Var⁡(X)\operatorname{Var}(X) = E\left[(X - \mu)^2\right] = E[X^2] - \mu^2, \qquad \sigma(X) = \sqrt{\operatorname{Var}(X)}

The standard deviation σ\sigma has the same units as XX. The second formula, "mean of the square minus square of the mean", is usually easier to compute. For a fair die, E[X2]=91/6E[X^2] = 91/6 and μ=7/2\mu = 7/2, so Var⁡(X)=916−494=3512≈2.92\operatorname{Var}(X) = \frac{91}{6} - \frac{49}{4} = \frac{35}{12} \approx 2.92 and σ≈1.71\sigma \approx 1.71.

python · try it yourself
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 a,ba, b:

  • Var⁡(aX+b)=a2Var⁡(X)\operatorname{Var}(aX + b) = a^2 \operatorname{Var}(X): shifting does not change spread; scaling multiplies the standard deviation by ∣a∣\lvert a \rvert.
  • If XX and YY are independent, Var⁡(X+Y)=Var⁡(X)+Var⁡(Y)\operatorname{Var}(X + Y) = \operatorname{Var}(X) + \operatorname{Var}(Y).

Unlike linearity of expectation, the sum rule for variance needs independence (or at least zero covariance). For X+X=2XX + X = 2X the variance is 4Var⁡(X)4\operatorname{Var}(X), not 2Var⁡(X)2\operatorname{Var}(X).

Variances of standard distributions

Distribution Mean Variance
Bernoulli(pp) pp p(1−p)p(1 - p)
Binomial(n,pn, p) npnp np(1−p)np(1 - p)
Geometric(pp) 1/p1/p (1−p)/p2(1 - p)/p^2
Poisson(λ\lambda) λ\lambda λ\lambda
Uniform on [a,b][a, b] (a+b)/2(a + b)/2 (b−a)2/12(b - a)^2/12
Exponential(λ\lambda) 1/λ1/\lambda 1/λ21/\lambda^2
Normal(μ,σ2\mu, \sigma^2) μ\mu σ2\sigma^2

The binomial row follows from the Bernoulli row by the sum rule, since a binomial is a sum of nn independent Bernoullis.

The square-root law

Let Xˉn\bar{X}_n be the average of nn independent copies of XX, each with standard deviation σ\sigma. Then

Var⁡(Xˉn)=nσ2n2=σ2n,σ(Xˉn)=σn\operatorname{Var}(\bar{X}_n) = \frac{n \sigma^2}{n^2} = \frac{\sigma^2}{n}, \qquad \sigma(\bar{X}_n) = \frac{\sigma}{\sqrt{n}}

Averages are less variable than single observations, but only by n\sqrt{n}: 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.

python · try it yourself
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 x1,…,xnx_1, \ldots, x_n, the sample variance divides by n−1n - 1, not nn: s2=1n−1∑(xi−xˉ)2s^2 = \frac{1}{n - 1}\sum (x_i - \bar{x})^2. Dividing by nn underestimates on average, because the data are closer to their own mean xˉ\bar{x} than to the true μ\mu. NumPy's var divides by nn unless you pass ddof=1; the Statistics course covers this.

python · try it yourself
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 (n∝σ2/δ2n \propto \sigma^2/\delta^2 to detect a difference δ\delta), 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 pp is the variance of a Bernoulli(pp) variable largest, and what is it?

Answer

p=1/2p = 1/2, variance 1/41/4.

Solution

p(1−p)p(1 - p) is a downward parabola with maximum at p=1/2p = 1/2.

Exercise 2: Temperature conversion

A temperature in Celsius has standard deviation 5. What is its standard deviation in Fahrenheit, F=1.8C+32F = 1.8C + 32?

Answer
Solution

σ(aX+b)=∣a∣σ(X)=1.8⋅5\sigma(aX + b) = \lvert a \rvert \sigma(X) = 1.8 \cdot 5. 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 3⋅35/123 \cdot 35/12.

Answer

35/4=8.7535/4 = 8.75.

Solution
python · try it yourself
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 Var(X)=4\mathrm{Var}(X) = 4, what is Var(3X+7)\mathrm{Var}(3X + 7)?

  1. 12
  2. 19
  3. 36
  4. 43

The standard deviation of an average of nn independent measurements is…

  1. σ\sigma
  2. σ/n\sigma/n
  3. σ/n\sigma/\sqrt{n}
  4. nσn\sigma