Expectation, variance and covariance
The average a random variable settles to, how spread out it is, and whether two of them move together. Their matrix form is the covariance matrix PCA takes apart.
1. The idea
The expectation 𝔼[X] is the average value X takes, weighting each value by how likely it is: for a fair die, (1 + 2 + … + 6) / 6 = 3.5. It is what you get by averaging many draws, which is why a batch average estimates it.
The variance, Var(X) = 𝔼[(X − 𝔼[X])²], is the average squared distance from that mean: how spread out X is. Its square root, the standard deviation σ, is in the same units as X. Initialization schemes such as Xavier and He are chosen so that the variance of activations stays about the same from layer to layer.
For two variables, the covariance Cov(X, Y) = 𝔼[(X − 𝔼X)(Y − 𝔼Y)] says whether they move together (positive), in opposite directions (negative) or unrelated (around 0). All the pairwise covariances of a vector make the covariance matrix Σ, the matrix PCA diagonalises. Divide by both standard deviations and you get the correlation ρ, between −1 and 1.
2. Try it: shape a cloud
Set the two spreads and the correlation. The ellipse covers about 86% of the cloud (two standard deviations in 2-D), and the numbers below are measured from these 150 points.
3. The math
4. The code (python)
import numpy as np
faces = np.arange(1, 7)
print(faces.mean(), round(faces.var(), 4)) # 3.5 2.9167
rng = np.random.default_rng(0)
rolls = rng.integers(1, 7, size=100_000)
print(round(rolls.mean(), 3)) # 3.498
z = rng.normal(size=(2, 50_000)) # x2 built to correlate with x1 at rho = 0.6
x1 = 1.2 * z[0]
x2 = 0.8 * (0.6 * z[0] + np.sqrt(1 - 0.6**2) * z[1])
print(np.cov(x1, x2).round(2).tolist()) # [[1.44, 0.58], [0.58, 0.65]]
print(round(np.corrcoef(x1, x2)[0, 1], 2)) # 0.65. You’ll see this in
- Core Course chapter 13: initialization and normalization keeping the variance steady layer by layer
- Module 3: PCA
- Normalization layers in the catalog
6. Check yourself
Finished this chapter?
What changed in ML architectures this week
New models worth knowing, benchmark moves, and one architecture explained in plain words. Free, once a week, unsubscribe anytime.