AI / ML Theory
Chapter 0 · 18 min

0.4 — Probability refresher: random variables, expectation, variance

Goal: State E[X], Var[X], and the law of total expectation in both discrete and continuous forms, and apply each to a one-line ML example.

1. The minimum vocabulary

A random variable XX is a measurable function from a sample space Ω\Omega to R\mathbb{R} (or Rd\mathbb{R}^d). In ML we almost never construct Ω\Omega explicitly — what we manipulate are the induced distributions p(x)p(x). Two kinds matter:

  • Discrete: XX takes values in a countable set; p(x)p(x) is a probability mass function with ∑xp(x)=1\sum_x p(x) = 1.
  • Continuous: XX takes values in Rd\mathbb{R}^d; p(x)p(x) is a probability density function with ∫p(x) dx=1\int p(x)\,dx = 1. Density values p(x)p(x) are not probabilities — only integrals over sets are.

The distinction is fundamental but the formulas to follow have parallel shapes; we'll write them side by side.

2. Expectation

The expectation of a function g(X)g(X) under pp:

Ep[g(X)]  =  {∑xg(x) p(x)discrete,∫g(x) p(x) dxcontinuous.\mathbb{E}_{p}[g(X)] \;=\; \begin{cases} \displaystyle\sum_x g(x)\, p(x) & \text{discrete}, \\[6pt] \displaystyle\int g(x)\, p(x)\, dx & \text{continuous}. \end{cases}

Three properties that we use without comment from here on:

  • Linearity. E[aX+bY]=a E[X]+b E[Y]\mathbb{E}[aX + bY] = a\,\mathbb{E}[X] + b\,\mathbb{E}[Y] for any a,b∈Ra, b \in \mathbb{R}, whether or not XX and YY are independent. Linearity is the single most useful identity in this course.
  • Constant. E[c]=c\mathbb{E}[c] = c.
  • Independence ⇒ factorisation. If X⊥YX \perp Y, then E[XY]=E[X] E[Y]\mathbb{E}[XY] = \mathbb{E}[X]\,\mathbb{E}[Y]. The converse is false.

ML example: the empirical risk on a training set of size mm is

R^(θ)=1m∑i=1mℓ(xi,yi;θ)  =  Ep^[ℓ(X,Y;θ)],\hat R(\theta) = \frac{1}{m}\sum_{i=1}^{m} \ell(x_i, y_i; \theta) \;=\; \mathbb{E}_{\hat p}[\ell(X, Y; \theta)],

where p^\hat p is the empirical distribution placing mass 1/m1/m on each training point. Expectation under the empirical distribution and "mean over the training set" are the same object. This will matter when we re-derive the cross-entropy loss as a sample-average MLE in §0.7.

3. Variance and standard deviation

Var[X]  =  E[(X−E[X])2].\mathrm{Var}[X] \;=\; \mathbb{E}\big[(X - \mathbb{E}[X])^2\big].

Expanding the square and using linearity gives the formula we'll use constantly:

  Var[X]=E[X2]−(E[X])2.  \boxed{\;\mathrm{Var}[X] = \mathbb{E}[X^2] - (\mathbb{E}[X])^2.\;}

Derivation, no skipped steps. Let μ=E[X]\mu = \mathbb{E}[X]. Then

Var[X]=E[(X−μ)2]=E[X2−2μX+μ2]=E[X2]−2μ E[X]+μ2=E[X2]−2μ2+μ2=E[X2]−μ2.\begin{aligned} \mathrm{Var}[X] &= \mathbb{E}[(X-\mu)^2] \\ &= \mathbb{E}[X^2 - 2\mu X + \mu^2] \\ &= \mathbb{E}[X^2] - 2\mu\,\mathbb{E}[X] + \mu^2 \\ &= \mathbb{E}[X^2] - 2\mu^2 + \mu^2 \\ &= \mathbb{E}[X^2] - \mu^2. \end{aligned}

Standard deviation: σX=Var[X]\sigma_X = \sqrt{\mathrm{Var}[X]}.

Two properties:

  • Scaling. Var[aX+b]=a2 Var[X]\mathrm{Var}[aX + b] = a^2\,\mathrm{Var}[X]. The shift bb drops out (variance is translation-invariant); the scale gets squared (variance scales like length-squared).
  • Independence ⇒ additivity. If X⊥YX \perp Y, Var[X+Y]=Var[X]+Var[Y]\mathrm{Var}[X + Y] = \mathrm{Var}[X] + \mathrm{Var}[Y]. Crucial for understanding why batch-norm and layer-norm work the way they do (Ch. 3).

4. Covariance and correlation

For two scalar random variables,

Cov(X,Y)  =  E[(X−E[X])(Y−E[Y])]  =  E[XY]−E[X] E[Y].\mathrm{Cov}(X, Y) \;=\; \mathbb{E}\big[(X - \mathbb{E}[X])(Y - \mathbb{E}[Y])\big] \;=\; \mathbb{E}[XY] - \mathbb{E}[X]\,\mathbb{E}[Y].

Correlation is the unit-free version: ρXY=Cov(X,Y)/(σXσY)∈[−1,1]\rho_{XY} = \mathrm{Cov}(X,Y) / (\sigma_X \sigma_Y) \in [-1, 1].

For a vector random variable X∈RdX \in \mathbb{R}^d with mean μ=E[X]\mu = \mathbb{E}[X], the covariance matrix is

Σ  =  E[(X−μ)(X−μ)⊤]  ∈  Rd×d.\Sigma \;=\; \mathbb{E}\big[(X-\mu)(X-\mu)^\top\big] \;\in\; \mathbb{R}^{d\times d}.

By construction Σ\Sigma is symmetric positive-semidefinite. The diagonal contains the per-component variances; off-diagonals are pairwise covariances. This is the object PCA diagonalises and that Gaussian likelihoods invert.

5. The law of total expectation (tower property)

For any random variables X,YX, Y,

  E[Y]  =  EX ⁣[E[Y∣X]].  \boxed{\;\mathbb{E}[Y] \;=\; \mathbb{E}_X\!\big[\mathbb{E}[Y \mid X]\big].\;}

In words: average over YY given XX, then average that conditional expectation over XX. The two-step average equals the one-step average.

Quick proof (continuous case):

EX[E[Y∣X]]=∫p(x)(∫y p(y∣x) dy)dx=∫ ⁣ ⁣∫y p(x) p(y∣x) dy dx=∫ ⁣ ⁣∫y p(x,y) dy dx=E[Y].\begin{aligned} \mathbb{E}_X[\mathbb{E}[Y \mid X]] &= \int p(x) \left( \int y \, p(y \mid x)\, dy \right) dx \\ &= \int\!\!\int y \, p(x)\, p(y \mid x)\, dy\, dx \\ &= \int\!\!\int y \, p(x, y)\, dy\, dx \\ &= \mathbb{E}[Y]. \end{aligned}

ML example: the noise term in a regression model is often given as E[ε∣X]=0\mathbb{E}[\varepsilon \mid X] = 0. The tower property then gives E[ε]=0\mathbb{E}[\varepsilon] = 0 unconditionally — but the converse is not true. This distinction is at the heart of why ordinary least squares gives an unbiased estimator under the conditional-mean-zero assumption.

6. Why this matters going forward

Almost every loss in ML is an expectation. Almost every regularisation trick is a variance manipulation. Cross-entropy (§0.7) is an expectation. KL divergence (§0.6) is an expectation. Dropout (Ch. 3) is a variance argument. Attention scores (Ch. 6) are normalised so that they form a probability distribution and have a well-defined expectation over values.

Keep linearity of expectation as your reflex move; it solves more problems than any other identity in this course.

Self-test

Let XX be a Bernoulli random variable with P(X=1)=p\mathbb{P}(X = 1) = p and P(X=0)=1−p\mathbb{P}(X = 0) = 1 - p.

  1. Compute E[X]\mathbb{E}[X] and Var[X]\mathrm{Var}[X] from scratch.
  2. Let Y=aX+bY = aX + b. Compute E[Y]\mathbb{E}[Y] and Var[Y]\mathrm{Var}[Y].
  3. Suppose X1,…,XnX_1, \dots, X_n are i.i.d. Bernoulli(pp) and S=∑iXiS = \sum_i X_i. Use linearity to get E[S]\mathbb{E}[S], and independence to get Var[S]\mathrm{Var}[S]. Verify that Var[S]=n p(1−p)\mathrm{Var}[S] = n\,p(1-p).