EasyLinear Algebra

Compute Covariance Matrix

Linear Algebra · Data Processing

Easy

Problem

Compute the sample covariance matrix without using np.cov. First center each feature:

X_c = X - \mu

Then compute:

\Sigma = \frac{X_c^{\mathsf T}X_c}{N-1}

Here, X has N samples and D features, \mu is the vector of feature means, X_c is the centered data, and \Sigma is the D \times D sample covariance matrix. Return \Sigma as a NumPy array.

Theory

A covariance matrix captures how multiple variables change together. For a dataset with D features, the covariance matrix is a D \times D symmetric matrix where each entry (i, j) represents the covariance between feature i and feature j. The diagonal entries contain the variances of individual features.


Why Covariance Matters

Understanding Relationships: Covariance reveals whether variables move together (positive covariance), move oppositely (negative covariance), or are independent (zero covariance).

Dimensionality Reduction: Principal Component Analysis (PCA) uses the covariance matrix eigendecomposition to find directions of maximum variance.

Multivariate Statistics: Many statistical tests and models assume multivariate normal distributions characterized by mean vectors and covariance matrices.

Portfolio Theory: In finance, the covariance matrix of asset returns determines portfolio risk and optimal allocation strategies.


Mathematical Definition

For a dataset X with N samples and D features, the covariance between features i and j is:

\text{Cov}(X_i, X_j) = \frac{1}{N-1} \sum_{k=1}^{N} (x_{ki} - \bar{x}_i)(x_{kj} - \bar{x}_j)

Where:

Special case - Variance: When i = j:

\text{Var}(X_i) = \text{Cov}(X_i, X_i) = \frac{1}{N-1} \sum_{k=1}^{N} (x_{ki} - \bar{x}_i)^2


Matrix Formulation

Given a centered data matrix \tilde{X} (where each column has mean zero):

\Sigma = \frac{1}{N-1} \tilde{X}^T \tilde{X}

Steps to compute:

  1. Compute the mean of each feature (column)
  2. Subtract the mean from each column to center the data
  3. Compute \tilde{X}^T \tilde{X} (matrix multiplication)
  4. Divide by N-1

Properties of Covariance Matrices

Symmetry: \Sigma_{ij} = \Sigma_{ji} because \text{Cov}(X_i, X_j) = \text{Cov}(X_j, X_i)

Positive Semi-Definite: For any vector v, v^T \Sigma v \geq 0. This means all eigenvalues are non-negative.

Diagonal Elements: Always non-negative (variances cannot be negative)

Dimensions: For D features, the matrix is D \times D with D(D+1)/2 unique values


Interpreting Covariance Values

Positive covariance (\Sigma_{ij} > 0):

Negative covariance (\Sigma_{ij} < 0):

Zero covariance (\Sigma_{ij} = 0):

Magnitude interpretation:


Sample vs Population Covariance

Population covariance (divide by N):

\Sigma_{pop} = \frac{1}{N} \sum_{k=1}^{N} (x_{ki} - \bar{x}_i)(x_{kj} - \bar{x}_j)

Sample covariance (divide by N-1):

\Sigma_{sample} = \frac{1}{N-1} \sum_{k=1}^{N} (x_{ki} - \bar{x}_i)(x_{kj} - \bar{x}_j)


Worked Example

Dataset (3 samples, 2 features: Height in cm and Weight in kg):

Step 1 - Calculate means:

\bar{Height} = \frac{170 + 180 + 175}{3} = 175

\bar{Weight} = \frac{65 + 75 + 70}{3} = 70

Step 2 - Center the data (subtract means):

Step 3 - Compute covariances:

\text{Var(Height)} = \frac{(-5)^2 + 5^2 + 0^2}{3-1} = \frac{50}{2} = 25

\text{Var(Weight)} = \frac{(-5)^2 + 5^2 + 0^2}{3-1} = \frac{50}{2} = 25

\text{Cov(Height, Weight)} = \frac{(-5)(-5) + (5)(5) + (0)(0)}{3-1} = \frac{50}{2} = 25

Step 4 - Assemble the covariance matrix:

\Sigma = \begin{bmatrix} 25 & 25 \\ 25 & 25 \end{bmatrix}

Interpretation: Perfect positive covariance - height and weight move together proportionally in this dataset.


Covariance vs Correlation

Covariance is scale-dependent:

Correlation is normalized covariance:

\rho_{ij} = \frac{\text{Cov}(X_i, X_j)}{\sigma_i \sigma_j}


Numerical Stability Considerations

Catastrophic cancellation: When values are large but differences are small, floating-point precision can cause errors. Computing (x - \bar{x}) for large x and \bar{x} may lose significant digits.

Two-pass vs one-pass algorithms:

Welford's algorithm: Numerically stable online algorithm for computing variance/covariance incrementally


Where Covariance Matrices Show Up

Examples

Example 1

Input
X = [[1, 2], [2, 3], [3, 4]]
Output
[[1.0, 1.0], [1.0, 1.0]]
Explanation
Both features vary together by the same amount after centering.

Example 2

Input
X = [[1, 0], [0, 1]]
Output
[[0.5, -0.5], [-0.5, 0.5]]

Hints

  1. Use X - np.mean(X, axis=0) to center every feature.
  2. Use centered.T @ centered before dividing by X.shape[0] - 1.

Requirements

Constraints

Starter Code

import numpy as np

def covariance_matrix(X: list) -> np.ndarray:
    """
    Returns the covariance matrix as a NumPy array.
    """
    # Write code here
    pass

Test Cases

CaseMatches
3x2 matrixpublic
2x2 matrix (minimum)public