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 is a measurable function from a sample space to (or ). In ML we almost never construct explicitly — what we manipulate are the induced distributions . Two kinds matter:
- Discrete: takes values in a countable set; is a probability mass function with .
- Continuous: takes values in ; is a probability density function with . Density values 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 under :
Three properties that we use without comment from here on:
- Linearity. for any , whether or not and are independent. Linearity is the single most useful identity in this course.
- Constant. .
- Independence ⇒ factorisation. If , then . The converse is false.
ML example: the empirical risk on a training set of size is
where is the empirical distribution placing mass 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
Expanding the square and using linearity gives the formula we'll use constantly:
Derivation, no skipped steps. Let . Then
Standard deviation: .
Two properties:
- Scaling. . The shift drops out (variance is translation-invariant); the scale gets squared (variance scales like length-squared).
- Independence ⇒ additivity. If , . 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,
Correlation is the unit-free version: .
For a vector random variable with mean , the covariance matrix is
By construction 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 ,
In words: average over given , then average that conditional expectation over . The two-step average equals the one-step average.
Quick proof (continuous case):
ML example: the noise term in a regression model is often given as . The tower property then gives 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.
Let be a Bernoulli random variable with and .
- Compute and from scratch.
- Let . Compute and .
- Suppose are i.i.d. Bernoulli() and . Use linearity to get , and independence to get . Verify that .