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 d2d^2 variance and covariance calculations into a single matrix product: C=1nXTXC = \frac{1}{n}X^T X.

In this microlearning, we unpack both the column-inner-product view and the row-outer-product view of 1nXTX\frac{1}{n}X^T X, and clarify when to divide by nn versus n−1n - 1.

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 n×dn \times d matrix XX:

  • Rows (i=1,…,ni = 1, \dots, n): Individual mean-centered observations xiT∈R1×d\mathbf{x}_i^T \in \mathbb{R}^{1 \times d}.
  • Columns (j=1,…,dj = 1, \dots, d): Mean-centered feature vectors fj∈Rn×1\mathbf{f}_j \in \mathbb{R}^{n \times 1}, 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 XTX^T (size d×nd \times n) by XX (size n×dn \times d), we obtain a d×dd \times d matrix XTXX^T X that can be read in two complementary ways:

View 1 — Dot Products of Feature Columns:

Cjk=1n(fjTfk)=1n∑i=1nxijxikC_{jk} = \frac{1}{n} (\mathbf{f}_j^T \mathbf{f}_k) = \frac{1}{n} \sum_{i=1}^n x_{ij} x_{ik}

Entry (j,k)(j,k) of CC is the average dot product between feature column jj and feature column kk across all nn observations.

View 2 — Sum of Rank-1 Sample Outer Products:

C=1n∑i=1nxixiTC = \frac{1}{n} \sum_{i=1}^n \mathbf{x}_i \mathbf{x}_i^T

Each observation xi\mathbf{x}_i contributes a rank-1 d×dd \times d matrix xixiT\mathbf{x}_i \mathbf{x}_i^T pointing along xi\mathbf{x}_i, and CC 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 Cunbiased=1n−1XTXC_{\text{unbiased}} = \frac{1}{n-1}X^T X so that E[Cunbiased]=Σ\mathbb{E}[C_{\text{unbiased}}] = \Sigma when the mean is estimated from the sample. In optimization derivations, CMLE=1nXTXC_{\text{MLE}} = \frac{1}{n}X^T X is cleaner.

Does nn vs. n−1n-1 change PCA? Not the principal directions! Scaling a matrix by a positive constant α=nn−1\alpha = \frac{n}{n-1} scales every eigenvalue by α\alpha while leaving every unit eigenvector 100% identical, and even the explained variance ratios λk/∑jλj\lambda_k / \sum_j \lambda_j remain completely invariant because the constant cancels out in numerator and denominator.

4. Worked Step-by-Step Matrix Construction

Let X=[31−1−2−21]X = \begin{bmatrix} 3 & 1 \\ -1 & -2 \\ -2 & 1 \end{bmatrix} (n=3n=3 centered observations in d=2d=2 dimensions; verify that each column sums to 00).

  1. Compute XTXX^T X:

    XTX=[3−1−21−21][31−1−2−21]=[9+1+43+2−23+2−21+4+1]=[14336]X^T X = \begin{bmatrix} 3 & -1 & -2 \\ 1 & -2 & 1 \end{bmatrix} \begin{bmatrix} 3 & 1 \\ -1 & -2 \\ -2 & 1 \end{bmatrix} = \begin{bmatrix} 9+1+4 & 3+2-2 \\ 3+2-2 & 1+4+1 \end{bmatrix} = \begin{bmatrix} 14 & 3 \\ 3 & 6 \end{bmatrix}

  2. Divide by n=3n = 3 (or n−1=2n-1 = 2):

    C=13[14336]=[4.671.001.002.00]C = \frac{1}{3}\begin{bmatrix} 14 & 3 \\ 3 & 6 \end{bmatrix} = \begin{bmatrix} 4.67 & 1.00 \\ 1.00 & 2.00 \end{bmatrix}

5. Active Recall Check and Key Takeaways

Quick Check: Test Your Understanding

Question: If your dataset has n=10,000n = 10,000 observations and d=5d = 5 features, what are the dimensions of XTXX^T X versus XXTX X^T?

Answer: Since XX is 10,000×510,000 \times 5, the feature covariance product XTXX^T X is a tiny 5×55 \times 5 matrix, whereas the Gram matrix XXTX X^T is a massive 10,000×10,00010,000 \times 10,000 matrix.

  • Matrix Compactness: C=1nXTXC = \frac{1}{n}X^T X summarizes nn points in Rd\mathbb{R}^d into a symmetric d×dd \times d matrix.
  • Scale Invariance of Directions: Dividing by nn or n−1n-1 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

✓Step-by-step concepts & lessons
✓Active recall & adaptive feedback
✓Personalized spaced repetition
covariance-matrix
matrix-transpose
variance
covariance
pca
statistics