BRAGA

02 Writing02-02 Notes02-02-436

Sample Covariance Matrix

Published 16 Apr 202612 min read
Contents
  1. 01Deep Breakdown
  2. 02Flowchart
  3. 03Real-world use
  4. 04The formula, piece by piece
  5. 05The formula in words
  6. 06Critical Concepts
  7. 07Step-by-step calculation (tiny example)
  8. 08Code examples
  9. 09Self-check: mathematical verification
  10. 10Common Pitfalls and Edge Cases
  11. 11References and Standard Results
S=1n−1X⊤XS=\frac{1}{n-1}X^\top X
(1)

We are working on the sample covariance matrix for centered multivariate data, defined by

S=1n−1X⊤X,S=\frac{1}{n-1}X^\top X,
(2)

where XX is the centered data matrix with nn observations and pp features. Under the maximum-likelihood convention for Gaussian models, one may instead use

1nX⊤X.\frac{1}{n}X^\top X.
(3)

The sample covariance matrix is symmetric, summarizes variances and pairwise covariances, and its eigenvectors give the principal directions in PCA.

Deep Breakdown

The sample covariance matrix is one of the core objects in multivariate statistics, PCA, and factor-style dimensionality reduction. It summarizes how features vary on their own and how they vary together.

Suppose we have a dataset with:

  • nn observations,
  • pp numerical features.

After data centering, each column has mean zero. That means each value now represents deviation from the feature mean rather than raw level.

If the centered data matrix is:

X∈Rn×p,X \in \mathbb{R}^{n \times p},
(4)

then the sample covariance matrix is:

S=1n−1X⊤XS=\frac{1}{n-1}X^\top X
(5)

This matrix has size:

p×pp \times p
(6)

so it summarizes relationships between features, not between observations. Each diagonal entry of SS is a sample variance:

Sjj=sample variance of feature jS_{jj}=\text{sample variance of feature } j
(7)

Each off-diagonal entry is a sample covariance:

Sjk=sample covariance between features j and kS_{jk}=\text{sample covariance between features } j \text{ and } k
(8)

So the covariance matrix tells us:

  • which features vary a lot,
  • which features tend to move together,
  • which features move in opposite directions,
  • how the variance is distributed across feature space.

This is exactly why PCA builds on it. PCA seeks directions in feature space along which the centered data have maximum variance. Those directions are the eigenvectors of the covariance matrix, and the amount of variance explained along each direction is given by the corresponding eigenvalue. So the covariance matrix is the bridge between raw centered data and principal components. Why the factor 1n−1\frac{1}{n-1}? That is the usual unbiased sample-covariance scaling in classical statistics. It corrects for the fact that the mean was estimated from the sample itself. In contrast, the MLE version under a multivariate Gaussian model uses:

1nX⊤X\frac{1}{n}X^\top X
(9)

Both conventions produce the same eigenvectors, because they differ only by a scalar factor. The eigenvalues are rescaled, but the principal directions are unchanged.

This is important for PCA:

  • using 1n−1\frac{1}{n-1} or 1n\frac{1}{n} changes variance magnitudes slightly,
  • but it does not change the PCA directions.

The sample covariance matrix is also symmetric:

S⊤=SS^\top = S
(10)

This guarantees real eigenvalues and orthogonal eigenvectors, which makes PCA mathematically clean and computationally stable. In DA/DS work, the sample covariance matrix matters in:

  • PCA,
  • factor analysis foundations,
  • multivariate exploratory analysis,
  • feature redundancy detection,
  • dimensionality reduction pipelines,
  • risk models and portfolio-style covariance analysis.

It is also closely related to the correlation matrix. If features are on very different scales, analysts often standardize first and then compute PCA on the correlation matrix instead of the covariance matrix.

Flowchart

Start with rawdata matrixCenter eachfeature columnto mean zeroObtain centeredmatrix XCompute X'XScale by 1 / (n -1) to get samplecovariancematrix SRead diagonalentries asvariancesReadoff-diagonalentries ascovariancesComputeeigenvalues andeigenvectors ofSUseeigenvectors asprincipaldirections inPCA

Real-world use

Where it is used

The sample covariance matrix is used whenever multivariate numerical data must be summarized in terms of variance and co-movement. This is common in PCA, factor analysis, feature engineering, finance, marketing analytics, and scientific data analysis.

Problem it solves

It solves the problem of describing how multiple variables vary together in a compact matrix form. Looking at each feature separately misses the structure of joint variation across variables.

How it solves the problem

It computes all pairwise covariances among centered features and packages them into a symmetric matrix. This makes it possible to analyze multivariate variation geometrically and algebraically through eigen-decomposition or SVD.

Example in a business

Suppose an analyst has customer features such as annual spend, purchase frequency, average basket size, and return rate. The covariance matrix reveals which features vary the most and which move together. PCA can then use this matrix to compress those features into a few latent behavioral dimensions for segmentation or visualization.

The formula, piece by piece

Symbol Meaning
nn Number of observations
pp Number of features
XX Centered data matrix with shape n×pn \times p
X⊤X^\top Transpose of the centered data matrix
SS Sample covariance matrix
SjkS_{jk} Covariance between features jj and kk
SjjS_{jj} Variance of feature jj
λℓ\lambda_\ell Eigenvalue of SS
vℓv_\ell Eigenvector of SS

Canonical sample covariance formula for centered data:

S=1n−1X⊤XS=\frac{1}{n-1}X^\top X
(11)

MLE covariance version:

ΣMLE=1nX⊤X\Sigma_{\mathrm{MLE}}=\frac{1}{n}X^\top X
(12)

Entrywise covariance formula:

Sjk=1n−1∑i=1nxijxikS_{jk}=\frac{1}{n-1}\sum_{i=1}^{n}x_{ij}x_{ik}
(13)

where the data are already centered.

Diagonal variance entry:

Sjj=1n−1∑i=1nxij2S_{jj}=\frac{1}{n-1}\sum_{i=1}^{n}x_{ij}^2
(14)

Symmetry property:

S⊤=SS^\top=S
(15)

Eigen-decomposition for PCA:

Svℓ=λℓvℓSv_\ell=\lambda_\ell v_\ell
(16)

The formula in words

Take the centered data matrix XX.

Multiply its transpose by itself:

X⊤XX^\top X
(17)

This forms all pairwise inner products between centered feature columns. Then divide by n−1n-1 to get the sample covariance matrix.

The result is a matrix where:

  • the diagonal tells you how much each feature varies,
  • the off-diagonal tells you whether two features tend to move together or in opposite directions.

Because PCA looks for directions of maximum variance, it uses the eigenvectors of this matrix as its principal directions.

Critical Concepts

  • The covariance matrix requires centered data. Without centering, the standard covariance interpretation is wrong.
  • The covariance matrix is symmetric. This guarantees real eigenvalues and orthogonal eigenvectors.
  • Diagonal entries are variances. These measure variability of individual features.
  • Off-diagonal entries are covariances. These measure joint movement between pairs of features.
  • PCA directions come from eigenvectors of the covariance matrix. This is the main reason the matrix matters in dimensionality reduction.
  • The 1/(n−1)1/(n-1) and 1/n1/n conventions differ by scale only. They do not change principal directions.
  • Covariance is scale-dependent. If variables are on very different scales, PCA on the correlation matrix may be more appropriate.

Step-by-step calculation (tiny example)

Consider the centered two-feature data matrix:

X=[−1−20012]X= \begin{bmatrix} -1 & -2\\ 0 & 0\\ 1 & 2 \end{bmatrix}
(18)

There are:

n=3n=3
(19)

observations and 2 features.

Step 1, compute X⊤X^\top

X⊤=[−101−202]X^\top= \begin{bmatrix} -1 & 0 & 1\\ -2 & 0 & 2 \end{bmatrix}
(20)

Step 2, compute X⊤XX^\top X

X⊤X=[−101−202][−1−20012]=[2448]X^\top X= \begin{bmatrix} -1 & 0 & 1\\ -2 & 0 & 2 \end{bmatrix} \begin{bmatrix} -1 & -2\\ 0 & 0\\ 1 & 2 \end{bmatrix} = \begin{bmatrix} 2 & 4\\ 4 & 8 \end{bmatrix}
(21)

Step 3, divide by n−1n-1 Since

n−1=2n-1=2
(22)

the sample covariance matrix is:

S=12[2448]=[1224]S=\frac{1}{2} \begin{bmatrix} 2 & 4\\ 4 & 8 \end{bmatrix} = \begin{bmatrix} 1 & 2\\ 2 & 4 \end{bmatrix}
(23)

Step 4, interpret the entries The diagonal entries are

S11=1,S22=4S_{11}=1,\qquad S_{22}=4
(24)

So feature 2 has larger variance than feature 1.

The off-diagonal entries are:

S12=S21=2S_{12}=S_{21}=2
(25)

So the features move together positively.

Tiny plain-language reading

The two centered features are strongly aligned. One varies about four times as much as the other, and they increase or decrease together.

PCA implication

Because the covariance matrix has strong positive covariance, the first principal component will point roughly along the direction where both features increase together.

Code examples

Beginner friendly Example [Python])

markdown
import numpy as np # Centered data matrix with 3 observations and 2 features
X = np.array([ [-1, -2], [0, 0], [1, 2]
], dtype=float) n = X.shape[0]
S = (1 / (n - 1)) * (X.T @ X) print("Centered data matrix X:")
print(X)
print("Sample covariance matrix S:")
print(S)

Explanation :

  • X.T @ X computes all pairwise column inner products.
  • Dividing by n - 1 turns those into sample covariances.
  • The resulting matrix summarizes feature variances and covariances.

Manual check:

S=[1224]S= \begin{bmatrix} 1 & 2\\ 2 & 4 \end{bmatrix}
(26)

Beginner friendly Example [R])

r
# Centered data matrix with 3 observations and 2 features
X <- matrix( c( -1, -2, 0, 0, 1, 2 ), nrow = 3, byrow = TRUE
) n <- nrow(X)
S <- (1 / (n - 1)) * t(X) %*% X
cat("Centered data matrix X:\n")
print(X)
cat("Sample covariance matrix S:\n")
print(S)

Explanation :

  • t(X) %*% X computes the feature cross-products.
  • Multiplying by 1 / (n - 1) gives the sample covariance matrix.
  • This matrix is the standard input to covariance-based PCA.

Final Professional Example [Python])

markdown
from __future__ import annotations from dataclasses import dataclass
from typing import Sequence import numpy as np RANDOM_SEED = 42 @dataclass(frozen=True)
class CovarianceAnalysisData: data_matrix: Sequence[Sequence[float]] feature_names: Sequence[str] def validate(self) -> None: if len(self.data_matrix) < 2: raise ValueError("At least two observations are required.") if len(self.feature_names) == 0: raise ValueError("At least one feature name is required.") feature_count = len(self.feature_names) for row in self.data_matrix: if len(row) != feature_count: raise ValueError("Each row must match the number of feature names.") def center_columns(data_matrix: np.ndarray) -> tuple[np.ndarray, np.ndarray]: column_means = np.mean(data_matrix, axis=0) centered_matrix = data_matrix - column_means return centered_matrix, column_means def sample_covariance_matrix(centered_matrix: np.ndarray) -> np.ndarray: observation_count = centered_matrix.shape[0] if observation_count < 2: raise ValueError("At least two observations are required.") return (centered_matrix.T @ centered_matrix) / (observation_count - 1) def summarize_covariance_analysis(data: CovarianceAnalysisData) -> None: data.validate() X = np.array(data.data_matrix, dtype=float) X_centered, column_means = center_columns(X) covariance_matrix = sample_covariance_matrix(X_centered) eigenvalues, eigenvectors = np.linalg.eigh(covariance_matrix) sort_index = np.argsort(eigenvalues)[::-1] eigenvalues = eigenvalues[sort_index] eigenvectors = eigenvectors[:, sort_index] print("Sample covariance matrix analysis") print("-" * 72) print("Feature means:") for feature_name, mean_value in zip(data.feature_names, column_means): print(f"{feature_name}: {mean_value:.6f}") print("-" * 72) print("Centered matrix:") print(X_centered) print("-" * 72) print("Sample covariance matrix:") print(covariance_matrix) print("-" * 72) print("Eigenvalues:") print(eigenvalues) print("-" * 72) print("Eigenvectors (principal directions):") print(eigenvectors) def main() -> None: # Customer behavior example: # Columns: annual_spend, purchase_frequency, basket_size data = CovarianceAnalysisData( feature_names=["annual_spend", "purchase_frequency", "basket_size"], data_matrix=[ [1200.0, 12.0, 100.0], [1500.0, 15.0, 110.0], [1800.0, 18.0, 130.0], [1600.0, 14.0, 115.0], ], ) summarize_covariance_analysis(data) if __name__ == "__main__": main()

What this professional example teaches:

  1. The covariance matrix is computed from centered data, not raw data.
  2. Its diagonal entries are feature variances and its off-diagonal entries are covariances.
  3. Eigen-decomposition of the covariance matrix gives PCA directions and explained-variance magnitudes.
  4. This is the standard covariance-based route to PCA.

In business data, it reveals which behavioral variables move together before dimensionality reduction.

Self-check: mathematical verification

Re-derivation from the covariance definition

For centered features jj and kk, the sample covariance is:

Sjk=1n−1∑i=1nxijxikS_{jk}=\frac{1}{n-1}\sum_{i=1}^{n}x_{ij}x_{ik}
(27)

If we stack all centered observations into the matrix:

X=[x11⋯x1p⋮⋱⋮xn1⋯xnp]X= \begin{bmatrix} x_{11} & \cdots & x_{1p}\\ \vdots & \ddots & \vdots\\ x_{n1} & \cdots & x_{np} \end{bmatrix}
(28)

then the (j,k)(j,k) entry of X⊤XX^\top X is:

(X⊤X)jk=∑i=1nxijxik(X^\top X)_{jk}=\sum_{i=1}^{n}x_{ij}x_{ik}
(29)

Therefore:

Sjk=1n−1(X⊤X)jkS_{jk}=\frac{1}{n-1}(X^\top X)_{jk}
(30)

Since this holds for every pair (j,k)(j,k), the full matrix form is:

S=1n−1X⊤XS=\frac{1}{n-1}X^\top X
(31)

This matches the canonical formula exactly.

Symmetry check Take the transpose

S⊤=(1n−1X⊤X)⊤S^\top = \left(\frac{1}{n-1}X^\top X\right)^\top
(32)

Using transpose rules:

S⊤=1n−1(X⊤X)⊤=1n−1X⊤X=SS^\top = \frac{1}{n-1}(X^\top X)^\top = \frac{1}{n-1}X^\top X = S
(33)

So the covariance matrix is symmetric, as stated.

Diagonal interpretation check For feature jj, the diagonal entry is

Sjj=1n−1∑i=1nxij2S_{jj}=\frac{1}{n-1}\sum_{i=1}^{n}x_{ij}^2
(34)

because the feature is centered. This is exactly the sample variance of feature jj. So the diagonal interpretation is correct.

PCA eigenvector connection check PCA solves for directions vv that maximize variance of the projected centered data.

This leads to the eigenvalue problem:

Sv=λvSv=\lambda v
(35)

Therefore the eigenvectors of SS are principal directions and the eigenvalues give the variances along those directions. This matches the stated PCA connection.

Scaling convention check If instead one uses

1nX⊤X\frac{1}{n}X^\top X
(36)

the result differs only by a positive scalar factor:

n−1n\frac{n-1}{n}
(37)

relative to the sample covariance matrix. Multiplying a matrix by a positive scalar rescales eigenvalues but does not change eigenvectors. So the note about unchanged principal directions is mathematically correct.

Verbal-to-algebra consistency

The verbal explanation says the covariance matrix summarizes variances and covariances, is symmetric, and provides eigenvectors for PCA. The algebra confirms all of these claims.

Structural verification

  • Opening restatement paragraph is present.
  • All required sections are present and non-empty.
  • Block equations use `
......
(38)

`.

  • Symbol-table entries use math mode such as $S$, $X$, $n$, and $\lambda_\ell$.
  • Mermaid uses flowchart TD and node text in ["..."].
  • Three code blocks are present: Python beginner, R beginner, Python professional.
  • Links to eigenvalues, PCA, factor analysis, and data centering are explicitly included.
  • No placeholder text remains.

Common Pitfalls and Edge Cases

  • Using raw rather than centered data: Standard covariance formulas assume deviations from the mean.
  • Confusing covariance with correlation: Covariance depends on scale, while correlation is scale-normalized.
  • Assuming the 1/(n−1)1/(n-1) versus 1/n1/n choice changes PCA directions: It changes magnitudes, not eigenvectors.
  • Ignoring feature scaling: Large-scale variables can dominate the covariance matrix and therefore the PCA solution.
  • Using too few observations: Covariance estimates become unstable when sample size is very small.
  • Interpreting covariance as causality: Positive or negative covariance indicates co-movement, not causal effect.
  • Assuming symmetry implies independence: Symmetry is a matrix property, not a statement about variables being independent.
  • Applying PCA without checking whether covariance structure is meaningful: Nonlinear or categorical structures may require different methods.

References and Standard Results

  • Sample covariance definition, S=1n−1X⊤XS=\frac{1}{n-1}X^\top X
  • MLE covariance version, 1nX⊤X\frac{1}{n}X^\top X
  • Entrywise covariance identity, Sjk=1n−1∑i=1nxijxikS_{jk}=\frac{1}{n-1}\sum_{i=1}^{n}x_{ij}x_{ik}
  • Variance as the diagonal of the covariance matrix
  • Symmetry property of covariance matrices
  • Eigen-decomposition equation, Svℓ=λℓvℓSv_\ell=\lambda_\ell v_\ell
  • PCA as variance maximization on centered data
  • Connection between covariance-based PCA and SVD of centered data

#machine-learning#machine-learning-algorithms#dimensionality-reduction-principal-component-analysis-pca

Keyboard shortcuts

Single keys, ignored while typing in a field or with Cmd, Ctrl or Alt held.

0
Home
1 2 3 4
Work, Writing, Tools, About
j / k
Next / previous section on this page
n / p
Next / previous project or essay
u
Up one level
l
Deutsch
t
Light or dark theme
?
This list
Esc
Close