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
- Lesson 6: Eigenvectors and Eigenvalues.
- Lesson 10: Singular Value Decomposition.
- Lesson 11: Principal Component Analysis.
Notation / Symbol Table
| Symbol | Plain-English name | Meaning |
|---|---|---|
| x sample | The -th observed value of the first variable. | |
| y sample | The -th observed value of the second variable. | |
| x mean | Average of the x-values. | |
| y mean | Average of the y-values. | |
| variance | Spread of one variable around its mean. | |
| covariance | How two variables move together around their means. | |
| covariance matrix | Matrix collecting variances and pairwise covariances. |
For two variables, covariance is
Core Ideas
Variance is spread of one variable
Variance measures how far values typically sit from their mean:
The square does two jobs:
- positive and negative deviations do not cancel;
- larger deviations count more heavily.
Covariance generalizes variance
Covariance uses the same idea, but with two variables:
Interpretation:
- positive covariance means the variables tend to rise and fall together;
- negative covariance means one tends to rise when the other falls;
- covariance near zero means there is no strong linear relationship.
Variance is just the special case
The covariance matrix records the shape of the cloud
For two coordinates, the covariance matrix is
This matrix tells us:
- how wide the cloud is;
- how tall the cloud is;
- how tilted the cloud is.
That is why covariance is the bridge from raw data to geometry.
Symmetry is not an accident
Because real-number multiplication commutes,
so
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 is the centered data matrix with one sample per row, then
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
Read it entry by entry:
- , so the first coordinate has more spread than the second.
- , so the second coordinate is less spread out.
- , so the coordinates tend to increase together.
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:
- understand feature redundancy;
- diagnose scale and correlation in tabular data;
- motivate PCA and whitening;
- describe Gaussian distributions;
- reason about conditioning in optimization.
A covariance matrix is one of the first compact summaries that tells you what a dataset is doing geometrically.
Exercises
-
If two variables have large positive covariance, what should the point cloud look like?
-
Why are the diagonal entries of a covariance matrix always variances?
-
Why must the covariance matrix be symmetric?
-
If
what does the cloud look like, and which direction is the first principal component?
- 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:
- the mean of each coordinate;
- the centered samples;
- the covariance matrix.
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
- When you look at an off-diagonal covariance entry, do you think “formula” or “tilt of the cloud” first?
- Why is covariance the right object for explaining PCA?
- What is the cleanest difference between covariance near zero and true statistical independence?
- If you changed coordinate systems, what would stay the same: the matrix entries, the geometry, or both?
Completed Exercises
Handwritten work submitted after completing this lesson.


