Covariance geometry
Quick answer
A covariance matrix describes how numerical features vary together. With n samples as rows and d features as columns, subtract each column mean to get Z. For n > 1, sample covariance is C = Zᵀ Z / (n − 1), a d-by-d matrix. Diagonal entries are sample variances; off-diagonal entries are covariances. C is symmetric and positive semidefinite (PSD): every real vector u satisfies uᵀ C u ≥ 0. The quadratic form is the sample variance along u.
Calculate two-feature sample covariance
Three synthetic sensor readings are (1, 2), (2, 4), (3, 6). Means are 2 and 4; centered rows are (-1, -2), (0, 0), (1, 2). The sum of cross-products is 4, divided by n−1 = 2, giving covariance 2.
rows = [[1, 2], [2, 4], [3, 6]]x, y = zip(*rows)mx, my = sum(x)/len(x), sum(y)/len(y)print(sum((a-mx)*(b-my) for a,b in rows)/(len(rows)-1))Expected output:
2.0Positive covariance indicates that these readings tend to increase together. It is not evidence that one causes the other. Covariance depends on feature units.
Inspect the covariance geometry
Optional local lab (Python 3.13.5, NumPy 2.3.5, CPU, float64). np.cov(X, rowvar=False, ddof=1) treats columns as features and uses the sample denominator. Because uᵀ C u = ||Z u||²/(n−1), C cannot have negative eigenvalues in exact arithmetic. The fixture has perfectly dependent features, so one eigenvalue is zero.
import numpy as npX = np.array([[1., 2.], [2., 4.], [3., 6.]])C = np.cov(X, rowvar=False, ddof=1)w, Q = np.linalg.eigh(C)print(C.tolist())print(w.round(8).tolist())u = Q[:, -1]print(round(float(np.var((X-X.mean(axis=0)) @ u, ddof=1)), 8))Expected output:
[[1.0, 2.0], [2.0, 4.0]][0.0, 5.0]5.0The largest eigenvalue equals the variance along the corresponding unit eigenvector. The zero eigenvalue means no variation along the perpendicular direction; PSD does not mean positive definite or invertible.
Common mistakes
Use n−1 for this sample covariance definition, not n. Population covariance of an entire finite population uses n instead. Center columns, not rows, and fit any data-dependent centering or scaling only on training data when evaluating a predictive model. A tiny negative computed eigenvalue may be roundoff; a substantial negative value merits investigation.
Practice and next steps
Compute the covariance of a constant feature with a changing feature. Then predict why a covariance matrix of centered data may be singular. Next, factor rectangular matrices without forming covariance explicitly.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Covariance geometry — knowledge check
Apply matrix geometry to small numerical fixtures.
What is the shape of a covariance matrix for 8 rows and 3 features?
For 5 samples, what denominator is used here for sample covariance?
What is covariance with a constant feature?
What does PSD guarantee in exact arithmetic for covariance C?
How should the mean be chosen for a held-out predictive evaluation?