Probability and Statistics for Machine Learning

Covariance Geometry

Covariance as geometry: an inner-product viewpoint that turns correlation into cosine, PCA into diagonalization, and whitening into a rotate-and-rescale operation.

Lesson
14

Goal

Understand covariance as geometry: once variables are centered, covariance behaves like an inner product, correlation behaves like cosine, PCA becomes diagonalization, and whitening becomes a rotate-and-rescale change of coordinates.

Motivation

The previous lesson showed that probability can be written in linear-algebra language. This lesson tightens that connection.

Instead of thinking of covariance as just another formula, we will treat it as the structure that gives random variables lengths, angles, and orthogonality. Once that clicks, several later ideas stop feeling unrelated.

Prerequisites

Notation / Symbol Table

SymbolPlain-English nameMeaning
X~\tilde{X} or X~centered XXThe mean-zero version of random variable XX.
Y~\tilde{Y} or Y~centered YYThe mean-zero version of random variable YY.
Cov(X,Y)\operatorname{Cov}(X, Y)covarianceInner-product-like pairing of centered random variables.
Var(X)\operatorname{Var}(X)varianceCovariance of XX with itself.
ρ(X,Y)\rho(X, Y)correlationNormalized covariance, analogous to cosine.
Σ\Sigmacovariance matrixGram matrix built from pairwise covariances.
VΛVTV \Lambda V^TeigendecompositionRotation into principal directions followed by axis scaling.
Λ1/2\Lambda^{-1/2}whitening scaleRescales principal directions to unit variance.

We will use

X,Y=E[X~Y~]=Cov(X,Y)\langle X, Y \rangle = E[\tilde{X}\tilde{Y}] = \operatorname{Cov}(X, Y)

as the key bridge between probability and geometry.

Core Ideas

Centered random variables behave like vectors

Once we center random variables, we can compare them in a way that ignores their means and focuses only on variation.

That makes the pairing

X,Y=E[X~Y~]\langle X, Y \rangle = E[\tilde{X}\tilde{Y}]

feel like a dot product:

So covariance is not only a statistic. It is the geometric structure on this vector space.

Variance is the induced squared norm

If covariance plays the role of inner product, then variance plays the role of squared length:

Var(X)=Cov(X,X).\operatorname{Var}(X) = \operatorname{Cov}(X, X).

This is the same relationship as

v2=vv\|v\|^2 = v \cdot v

in ordinary Euclidean space.

Correlation is cosine

Normalize covariance by the lengths of the variables:

ρ(X,Y)=Cov(X,Y)Var(X)Var(Y).\rho(X, Y) = \frac{\operatorname{Cov}(X, Y)} {\sqrt{\operatorname{Var}(X)}\sqrt{\operatorname{Var}(Y)}}.

That is exactly the same pattern as

cos(θ)=xyxy.\cos(\theta) = \frac{x \cdot y}{\|x\|\|y\|}.

So correlation measures angle-like alignment:

The covariance matrix is a Gram matrix

For a random vector with coordinates X1,X2,,XnX_1, X_2, \ldots, X_n, the covariance matrix is

Σij=Cov(Xi,Xj).\Sigma_{ij} = \operatorname{Cov}(X_i, X_j).

That means Σ\Sigma is the Gram matrix formed by pairing each coordinate with every other coordinate.

This is why it is symmetric and why diagonalization is the right move. We are looking for the basis in which the geometric structure becomes as simple as possible.

PCA is a rotation into the natural basis

Because covariance matrices are symmetric, they admit an orthogonal eigendecomposition:

Σ=VΛVT.\Sigma = V \Lambda V^T.

The columns of VV are orthogonal principal directions. In that basis, the covariance matrix is diagonal:

VTΣV=Λ.V^T \Sigma V = \Lambda.

PCA is not distorting the data. It is choosing the basis in which the covariance geometry is already aligned with the axes.

Whitening rescales the geometry

After PCA, the covariance becomes diagonal but the variances may still be unequal. Whitening applies one more transformation:

z=Λ1/2VT(xμ).z = \Lambda^{-1/2} V^T (x - \mu).

Interpretation:

  1. rotate into principal directions;
  2. divide each direction by its standard deviation.

After whitening, every principal direction has variance 11, so the covariance becomes the identity matrix.

Worked Example

Suppose

Var(X)=25,Var(Y)=9,Cov(X,Y)=12.\operatorname{Var}(X) = 25,\qquad \operatorname{Var}(Y) = 9,\qquad \operatorname{Cov}(X, Y) = 12.

Then the covariance lengths are

X=25=5,Y=9=3.\|X\| = \sqrt{25} = 5,\qquad \|Y\| = \sqrt{9} = 3.

The correlation is

ρ(X,Y)=1253=45.\rho(X, Y) = \frac{12}{5 \cdot 3} = \frac{4}{5}.

So the angle between the centered variables is

θ=cos1(45).\theta = \cos^{-1}\left(\frac{4}{5}\right).

This is the same geometry you already know from a 33-44-55 triangle: the variables are fairly aligned, but not identical.

That is the payoff of this lesson. A familiar geometric computation now has a statistical interpretation.

Why This Shows Up In ML

This viewpoint matters in machine learning because many standard preprocessing and modeling steps are really geometric operations in disguise.

Examples:

Once the covariance geometry is visible, many algorithms become easier to motivate and easier to debug.

Exercises

  1. If
Var(X)=4,Var(Y)=16,Cov(X,Y)=6,\operatorname{Var}(X) = 4,\qquad \operatorname{Var}(Y) = 16,\qquad \operatorname{Cov}(X, Y) = -6,

compute the correlation and say whether the corresponding angle is acute or obtuse.

  1. Build the covariance matrix from those three quantities. Why must the matrix be symmetric?

  2. Explain, without calculation, why PCA makes the covariance matrix diagonal in the principal-component basis.

  3. In your own words, what does whitening change that PCA alone does not?

Implementation Exercise

Create a tiny TypeScript experiment with a two-dimensional dataset.

  1. Compute the covariance matrix.
  2. Hard-code or compute its orthonormal eigenvectors.
  3. Rotate the centered points into the principal basis.
  4. Rescale each rotated coordinate by the reciprocal of its standard deviation.

Print the covariance before PCA, after rotation, and after whitening. The goal is to see the sequence:

Reflection Questions