Expectation, Variance and Summary Statistics
A whole distribution is hard to work with; often two numbers are enough: where it is centred (the mean) and how spread out it is (the variance). Expected values are also how losses are defined: the risk of a model is the expected loss over the data distribution, and training minimizes an average that estimates it.
Expectation
The expected value of a function of a random variable is the probability-weighted average:
The mean is . Expectation is linear, with no independence needed:
but in general for nonlinear . (For convex , Jensen's inequality says .)
Variance
The standard deviation has the units of . Rules: (shifts do not change spread), and for independent : .
Monte Carlo: expectations as averages
The empirical mean of independent samples estimates , and its variance is , so the error shrinks like . This is why the average training loss estimates the expected loss, and why mini-batch gradients are noisy but unbiased.
import numpy as np
rng = np.random.default_rng(0)
# X uniform on {1,...,6}: E[X] = 3.5, V[X] = 35/12
x = np.arange(1, 7); p = np.full(6, 1 / 6)
mean = (x * p).sum(); var = ((x - mean)**2 * p).sum()
print(f"exact: mean {mean}, var {var:.4f}, E[X^2] - E[X]^2 = {(x**2 * p).sum() - mean**2:.4f}")
for N in (10, 1000, 100000):
s = rng.integers(1, 7, size=N)
print(f"N={N:6d}: sample mean {s.mean():.4f}, sample var {s.var(ddof=1):.4f}")
# Jensen: E[X^2] >= E[X]^2
print("E[X^2] =", round((x**2 * p).sum(), 4), ">= (E[X])^2 =", mean**2)exact: mean 3.5, var 2.9167, E[X^2] - E[X]^2 = 2.9167
N= 10: sample mean 2.8000, sample var 3.2889
N= 1000: sample mean 3.6020, sample var 2.8845
N=100000: sample mean 3.4981, sample var 2.9103
E[X^2] = 15.1667 >= (E[X])^2 = 12.25
Sample statistics and the
For data the sample mean is . The sample variance with divisor is biased low, because was fitted to the same data; dividing by fixes that (Bessel's correction). NumPy's var divides by by default; ddof=1 gives . For large the difference is negligible.
import numpy as np
rng = np.random.default_rng(1)
true_var = 4.0
biased, unbiased = [], []
for _ in range(20000):
s = rng.normal(0, 2, size=5) # tiny samples make the bias visible
biased.append(s.var()); unbiased.append(s.var(ddof=1))
print(f"true variance {true_var}, mean of var(ddof=0) {np.mean(biased):.3f}, mean of var(ddof=1) {np.mean(unbiased):.3f}")
print("expected bias factor (N-1)/N =", 4 / 5)true variance 4.0, mean of var(ddof=0) 3.191, mean of var(ddof=1) 3.988
expected bias factor (N-1)/N = 0.8
In software
np.mean,np.var(ddof=...),np.std; pandasstd()defaults toddof=1, NumPy toddof=0, a common source of small mismatches.- Batch normalization uses the batch mean and variance, then running averages at inference time (Batch Normalization).
- The risk and its Monte Carlo estimate, the training loss, are the subject of Empirical Risk Minimization.
Exercises
Exercise 1: Linearity
and . Find .
Answer
Solution
. Linearity needs no independence.
Exercise 2: Variance of a Bernoulli
Find the variance of a Bernoulli() variable. For which is it largest?
Answer
, largest at .
Solution
for , so and . The parabola peaks at with value .
Exercise 3: Averaging reduces variance (code)
are independent with variance 9. What is the standard deviation of their mean? Confirm by simulation.
Answer
.
Solution
, so the standard deviation is .
import numpy as np
rng = np.random.default_rng(2)
means = rng.normal(0, 3, size=(100000, 25)).mean(axis=1)
print(round(means.std(), 2))0.6
Test yourself
equals…
The standard error of a mean of independent samples shrinks like…
- it does not shrink
Which statement is always true?