Compute a Sample Covariance Matrix With NumPy Broadcasting

Quick Overview

Implement the unbiased sample covariance matrix of an (n, d) NumPy array by centering columns with broadcasting and forming one matrix product, without loops or np.cov. It tests broadcasting, the matrix form of covariance, Bessel's correction, and numerical stability.

Compute a Sample Covariance Matrix With NumPy Broadcasting

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `covariance_matrix(X)` that returns the `(d, d)` sample covariance matrix of `X` of shape `(n, d)` (rows are observations, columns are features). The implementation must: - Subtract column means via broadcasting to get centered `Xc`. - Return `Xc.T @ Xc / (n - 1)` (unbiased estimator). - Use vectorized NumPy only: no Python loops and no `np.cov`. ```python X = np.array([[1.0, 2.0], [3.0, 6.0], [5.0, 10.0]]) covariance_matrix(X) # array([[ 4., 8.], # [ 8., 16.]]) ``` ```hint Watch the shapes Check the shape of `X.mean(axis=0)` and how it lines up against an `(n, d)` array. Then check which of `Xc.T @ Xc` and `Xc @ Xc.T` comes out as `(d, d)`. ``` ### Constraints & Assumptions - `n ≥ 2` - `X` is a 2-D float array of shape `(n, d)` ### What a Strong Answer Covers - Centering each column by its mean with broadcasting, with the shapes involved. - Why entry `(i, j)` of `Xc.T @ Xc / (n - 1)` equals the sample covariance of features `i` and `j`. - The reason for dividing by `n - 1`. - Time and space complexity. - Why centering before the product is numerically safer than a one-pass sum-of-products formula. ### Follow-up Questions - How would you compute the correlation matrix from this? - When would you divide by `n` instead of `n - 1`? - How would you compute the same matrix when `X` is too large to fit in memory and arrives in chunks?

Overview: Implement the unbiased sample covariance matrix of an (n, d) NumPy array by centering columns with broadcasting and forming one matrix product, without loops or np.cov. It tests broadcasting, the matrix form of covariance, Bessel's correction, and numerical stability.

PracHub logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement covariance_matrix(X) that returns the (d, d) sample covariance matrix of X of shape (n, d) (rows are observations, columns are features).

The implementation must:

  • Subtract column means via broadcasting to get centered Xc .
  • Return Xc.T @ Xc / (n - 1) (unbiased estimator).
  • Use vectorized NumPy only: no Python loops and no np.cov .
X = np.array([[1.0, 2.0],
              [3.0, 6.0],
              [5.0, 10.0]])
covariance_matrix(X)
# array([[ 4.,  8.],
#        [ 8., 16.]])

Constraints & Assumptions

  • n ≥ 2
  • X is a 2-D float array of shape (n, d)

What a Strong Answer Covers Guidance

  • Centering each column by its mean with broadcasting, with the shapes involved.
  • Why entry (i, j) of Xc.T @ Xc / (n - 1) equals the sample covariance of features i and j .
  • The reason for dividing by n - 1 .
  • Time and space complexity.
  • Why centering before the product is numerically safer than a one-pass sum-of-products formula.

Follow-up Questions Guidance

  • How would you compute the correlation matrix from this?
  • When would you divide by n instead of n - 1 ?
  • How would you compute the same matrix when X is too large to fit in memory and arrives in chunks?