Sub topic 2
Variance, covariance and the covariance matrix
This chapter assumes you can use NumPy arrays, know the mean and variance, and read a feature matrix with samples as rows and features as columns. It builds the object PCA decomposes: the covariance matrix.
Variance: spread of one feature
The sample variance of a feature measures how far its values sit from their mean, on average squared:
var(x) = Σ (xᵢ − x̄)² / (n − 1)
The divisor n − 1 (Bessel's correction) gives an unbiased estimate when the mean is itself estimated from the data. NumPy's np.var divides by n unless you pass ddof=1, while np.cov divides by n − 1 by default. Mixing the two is a common source of small mismatches.
Covariance: how two features move together
Covariance extends the idea to a pair of features:
cov(x, y) = Σ (xᵢ − x̄)(yᵢ − ȳ) / (n − 1)
- A positive value means the features tend to be above their means together.
- A negative value means one tends to be high when the other is low.
- A value near zero means no linear relationship. Nonlinear dependence can still exist.
Covariance has the units of x times the units of y, so its size is hard to compare across feature pairs. Correlation fixes this by dividing by both standard deviations, which gives a value between −1 and 1.
The covariance matrix
For d features, collect all pairwise covariances in a d × d matrix C, where C[i, j] = cov(featureᵢ, featureⱼ).
- The diagonal holds the variances of each feature.
- The matrix is symmetric, because cov(x, y) = cov(y, x).
- It is positive semi-definite, so no direction has negative variance.
If Xc is the n × d data matrix with each column's mean subtracted, then:
C = XcᵀXc / (n − 1)
Worked example
The code below computes the covariance matrix by hand and with np.cov. By hand, x = [2, 4, 6, 8] and y = [1, 3, 5, 9] give var(x) = 20/3, cov(x, y) = 26/3 and var(y) = 35/3.
import numpy as np
X = np.array([[2.0, 1.0],
[4.0, 3.0],
[6.0, 5.0],
[8.0, 9.0]])
n = X.shape[0]
Xc = X - X.mean(axis=0)
C_manual = Xc.T @ Xc / (n - 1)
C_numpy = np.cov(X, rowvar=False)
print(C_manual)
print(np.allclose(C_manual, C_numpy))
print(np.allclose(C_manual, C_manual.T))The diagonal shows roughly 6.67 and 11.67, and the off-diagonal entries are both roughly 8.67. The positive off-diagonal value says the two features rise together, which is the redundancy PCA exploits.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Forgetting `rowvar=False` | Wrong matrix shape (n × n) | Pass `rowvar=False` |
| Using `np.var` next to `np.cov` | Values differ by a factor of n/(n − 1) | Use `ddof=1` |
| Not centering before `XᵀX` | Matrix includes the means, not just spread | Subtract column means first |
| Comparing raw covariances across features | Scale differences dominate | Standardize, or use correlation |
The last row leads into the next chapter, where centering and standardizing are treated directly.
Exercise
A feature matrix X has 500 samples and 8 features. What shape is its covariance matrix, which entries are the variances, and what does a covariance of zero between features 2 and 5 tell you?
Show answer
The matrix is 8 × 8. The variances are on the diagonal, C[i, i]. A covariance of zero means there is no linear relationship between features 2 and 5. They could still be related nonlinearly.