Probability and Statistics for Machine Learning

Covariance

Covariance as the quantity that measures how variables move together and reveals the orientation of a data cloud.

Lesson
12

Goal

Understand covariance as a measurement of how two variables vary together, and understand the covariance matrix as the object that captures the shape, scale, and orientation of a data cloud.

Motivation

PCA tells us to compute eigenvectors of the covariance matrix. This lesson explains why that advice makes sense.

If variance tells us how spread out one variable is, covariance tells us how two variables move together. Once we collect all those pairwise relationships into a matrix, the matrix starts behaving like a geometric summary of the dataset.

Prerequisites

Notation / Symbol Table

SymbolPlain-English nameMeaning
xix_ix sampleThe ii-th observed value of the first variable.
yiy_iy sampleThe ii-th observed value of the second variable.
μx\mu_xx meanAverage of the x-values.
μy\mu_yy meanAverage of the y-values.
Var(X)\operatorname{Var}(X)varianceSpread of one variable around its mean.
Cov(X,Y)\operatorname{Cov}(X, Y)covarianceHow two variables move together around their means.
Σ\Sigmacovariance matrixMatrix collecting variances and pairwise covariances.

For two variables, covariance is

Cov(X,Y)=1ni=1n(xiμx)(yiμy).\operatorname{Cov}(X, Y) = \frac{1}{n} \sum_{i=1}^{n} (x_i - \mu_x)(y_i - \mu_y).

Core Ideas

Variance is spread of one variable

Variance measures how far values typically sit from their mean:

Var(X)=1ni=1n(xiμx)2.\operatorname{Var}(X) = \frac{1}{n} \sum_{i=1}^{n} (x_i - \mu_x)^2.

The square does two jobs:

Covariance generalizes variance

Covariance uses the same idea, but with two variables:

Cov(X,Y)=1ni=1n(xiμx)(yiμy).\operatorname{Cov}(X, Y) = \frac{1}{n} \sum_{i=1}^{n} (x_i - \mu_x)(y_i - \mu_y).

Interpretation:

Variance is just the special case

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

The covariance matrix records the shape of the cloud

For two coordinates, the covariance matrix is

Σ=[Var(X)Cov(X,Y)Cov(Y,X)Var(Y)].\Sigma = \begin{bmatrix} \operatorname{Var}(X) & \operatorname{Cov}(X, Y) \\ \operatorname{Cov}(Y, X) & \operatorname{Var}(Y) \end{bmatrix}.

This matrix tells us:

That is why covariance is the bridge from raw data to geometry.

Symmetry is not an accident

Because real-number multiplication commutes,

(xiμx)(yiμy)=(yiμy)(xiμx),(x_i - \mu_x)(y_i - \mu_y) = (y_i - \mu_y)(x_i - \mu_x),

so

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

That is why every covariance matrix is symmetric.

Once symmetry appears, eigenvectors become natural again. Symmetric matrices have orthogonal eigenvectors, and those eigenvectors become the principal axes of the data cloud.

Covariance and centered data matrices

If AA is the centered data matrix with one sample per row, then

Σ=1nATA.\Sigma = \frac{1}{n} A^T A.

So the covariance matrix is not a mysterious new object. It is a familiar linear-algebra construction: multiply centered data by its transpose, then average.

This is the exact reason PCA and SVD fit together so cleanly.

Worked Example

Suppose

Σ=[9664].\Sigma = \begin{bmatrix} 9 & 6 \\ 6 & 4 \end{bmatrix}.

Read it entry by entry:

Geometrically, the cloud is stretched along a positively sloped diagonal direction. It is not aligned with the original axes.

That is exactly the situation where PCA helps: rotate into the natural axes of the covariance matrix instead of keeping the original feature axes.

Why This Shows Up In ML

Covariance appears throughout machine learning because models care about structure, not just isolated features.

It helps us:

A covariance matrix is one of the first compact summaries that tells you what a dataset is doing geometrically.

Exercises

  1. If two variables have large positive covariance, what should the point cloud look like?

  2. Why are the diagonal entries of a covariance matrix always variances?

  3. Why must the covariance matrix be symmetric?

  4. If

Σ=[16001],\Sigma = \begin{bmatrix} 16 & 0 \\ 0 & 1 \end{bmatrix},

what does the cloud look like, and which direction is the first principal component?

  1. Zero covariance means the variables are uncorrelated. Why does that not automatically imply independence?

Implementation Exercise

Implement a TypeScript function that takes an array of two-dimensional samples and returns:

Then print the matrix for one dataset whose coordinates are visibly correlated and one whose coordinates are not. Compare the off-diagonal entries.

Reflection Questions