TExcelTAD
Home › Math for Machine Learning › Expectation, Variance and Summary Statistics

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 gg of a random variable is the probability-weighted average:

EX[g(x)]=∑xg(x) p(x)(discrete),EX[g(x)]=∫g(x) p(x) dx(continuous)\mathbb{E}_X[g(x)] = \sum_x g(x)\,p(x) \quad \text{(discrete)}, \qquad \mathbb{E}_X[g(x)] = \int g(x)\,p(x)\,dx \quad \text{(continuous)}

The mean is E[X]\mathbb{E}[X]. Expectation is linear, with no independence needed:

E[aX+bY+c]=a E[X]+b E[Y]+c\mathbb{E}[aX + bY + c] = a\,\mathbb{E}[X] + b\,\mathbb{E}[Y] + c

but in general E[g(X)]≠g(E[X])\mathbb{E}[g(X)] \ne g(\mathbb{E}[X]) for nonlinear gg. (For convex gg, Jensen's inequality says E[g(X)]≥g(E[X])\mathbb{E}[g(X)] \ge g(\mathbb{E}[X]).)

Variance

V[X]=E[(X−E[X])2]=E[X2]−E[X]2\mathbb{V}[X] = \mathbb{E}\big[(X - \mathbb{E}[X])^2\big] = \mathbb{E}[X^2] - \mathbb{E}[X]^2

The standard deviation V[X]\sqrt{\mathbb{V}[X]} has the units of XX. Rules: V[aX+c]=a2V[X]\mathbb{V}[aX + c] = a^2\mathbb{V}[X] (shifts do not change spread), and for independent X,YX, Y: V[X+Y]=V[X]+V[Y]\mathbb{V}[X + Y] = \mathbb{V}[X] + \mathbb{V}[Y].

Monte Carlo: expectations as averages

The empirical mean of NN independent samples estimates E[X]\mathbb{E}[X], and its variance is V[X]/N\mathbb{V}[X]/N, so the error shrinks like 1/N1/\sqrt{N}. This is why the average training loss estimates the expected loss, and why mini-batch gradients are noisy but unbiased.

python · try it yourself
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 N−1N - 1

For data x1,…,xNx_1, \ldots, x_N the sample mean is xˉ=1N∑nxn\bar{x} = \frac{1}{N}\sum_n x_n. The sample variance with divisor NN is biased low, because xˉ\bar{x} was fitted to the same data; dividing by N−1N - 1 fixes that (Bessel's correction). NumPy's var divides by NN by default; ddof=1 gives N−1N - 1. For large NN the difference is negligible.

python · try it yourself
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; pandas std() defaults to ddof=1, NumPy to ddof=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 E[ℓ(f(x),y)]\mathbb{E}[\ell(f(\mathbf{x}), y)] and its Monte Carlo estimate, the training loss, are the subject of Empirical Risk Minimization.

Exercises

Exercise 1: Linearity

E[X]=2\mathbb{E}[X] = 2 and E[Y]=−1\mathbb{E}[Y] = -1. Find E[3X−4Y+5]\mathbb{E}[3X - 4Y + 5].

Answer
Solution

3⋅2−4⋅(−1)+5=6+4+5=153 \cdot 2 - 4 \cdot (-1) + 5 = 6 + 4 + 5 = 15. Linearity needs no independence.

Exercise 2: Variance of a Bernoulli

Find the variance of a Bernoulli(μ\mu) variable. For which μ\mu is it largest?

Answer

μ(1−μ)\mu(1 - \mu), largest at μ=0.5\mu = 0.5.

Solution

X2=XX^2 = X for X∈{0,1}X \in \{0, 1\}, so E[X2]=μ\mathbb{E}[X^2] = \mu and V[X]=μ−μ2\mathbb{V}[X] = \mu - \mu^2. The parabola peaks at μ=12\mu = \frac{1}{2} with value 14\frac{1}{4}.

Exercise 3: Averaging reduces variance (code)

X1,…,X25X_1, \ldots, X_{25} are independent with variance 9. What is the standard deviation of their mean? Confirm by simulation.

Answer

0.60.6.

Solution

V[Xˉ]=9/25\mathbb{V}[\bar{X}] = 9/25, so the standard deviation is 3/5=0.63/5 = 0.6.

python · try it yourself
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

V[2X+7]\mathbb{V}[2X + 7] equals…

  1. 2V[X]+72\mathbb{V}[X] + 7
  2. 4V[X]4\mathbb{V}[X]
  3. 2V[X]2\mathbb{V}[X]
  4. 4V[X]+494\mathbb{V}[X] + 49

The standard error of a mean of NN independent samples shrinks like…

  1. 1/N1/N
  2. 1/N1/\sqrt{N}
  3. 1/N21/N^2
  4. it does not shrink

Which statement is always true?

  1. E[XY]=E[X]E[Y]\mathbb{E}[XY] = \mathbb{E}[X]\mathbb{E}[Y]
  2. E[X+Y]=E[X]+E[Y]\mathbb{E}[X + Y] = \mathbb{E}[X] + \mathbb{E}[Y]
  3. V[X+Y]=V[X]+V[Y]\mathbb{V}[X + Y] = \mathbb{V}[X] + \mathbb{V}[Y]
  4. E[X2]=E[X]2\mathbb{E}[X^2] = \mathbb{E}[X]^2