4.1 Constructing the Sample Covariance Matrix C = (1/n)XᵀX from Centered Data
When working with multivariable datasets, writing out separate summation formulas for every feature's variance and every pair of features' covariance quickly becomes unmanageable. Linear algebra compresses all variance and covariance calculations into a single matrix product: .
In this microlearning, we unpack both the column-inner-product view and the row-outer-product view of , and clarify when to divide by versus .
In this Article
1. Layout of the Centered Data Matrix X
By standard statistical and machine learning convention (such as scikit-learn and NumPy), our centered dataset is stored as an matrix :
- Rows (): Individual mean-centered observations .
- Columns (): Mean-centered feature vectors , where the sum of entries in each column is zero.
Feature 1 Feature 2 ... Feature d
[ x_11 x_12 ... x_1d ] <-- Observation 1 (row x_1^T)
X = [ x_21 x_22 ... x_2d ] <-- Observation 2 (row x_2^T)
[ : : ... : ]
[ x_n1 x_n2 ... x_nd ] <-- Observation n (row x_n^T)
(n x d matrix, column means = 0)
2. Two Algebraic Views of XᵀX: Feature Inner Products vs. Sample Outer Products
When we multiply (size ) by (size ), we obtain a matrix that can be read in two complementary ways:
View 1 — Dot Products of Feature Columns:
Entry of is the average dot product between feature column and feature column across all observations.
View 2 — Sum of Rank-1 Sample Outer Products:
Each observation contributes a rank-1 matrix pointing along , and is their average.
3. Dividing by n vs. n − 1: Why PCA Eigenvectors Never Change
In statistics courses, sample covariance is often defined with Bessel's correction so that when the mean is estimated from the sample. In optimization derivations, is cleaner.
Does vs. change PCA? Not the principal directions! Scaling a matrix by a positive constant scales every eigenvalue by while leaving every unit eigenvector 100% identical, and even the explained variance ratios remain completely invariant because the constant cancels out in numerator and denominator.
4. Worked Step-by-Step Matrix Construction
Let ( centered observations in dimensions; verify that each column sums to ).
- Compute :
- Divide by (or ):
5. Active Recall Check and Key Takeaways
Quick Check: Test Your Understanding
Question: If your dataset has observations and features, what are the dimensions of versus ?
Answer: Since is , the feature covariance product is a tiny matrix, whereas the Gram matrix is a massive matrix.
- Matrix Compactness: summarizes points in into a symmetric matrix.
- Scale Invariance of Directions: Dividing by or leaves all eigenvectors and explained variance ratios unchanged.
- Canonical References: See Wikipedia Reference on Sample Mean and Covariance.
Take a Quiz Based on This Article
Test your understanding with AI-generated questions tailored to this content
Adaptive Learning with SynLearn
Explore this article through guided practice that adapts to your answers