#120Covariance matrixEasy

Covariance matrix

Background

The covariance matrix summarises how a set of variables vary together: entry (i,j)(i, j) is the covariance between feature ii and feature jj, and the diagonal holds each feature's variance. It is the foundation of PCA, Gaussian models, whitening, and the Mahalanobis distance.

Problem statement

Implement calculate_covariance_matrix(vectors) where vectors[i] is the list of observations for feature ii. Return the symmetric matrix of sample covariances:

Σij=1n1k=1n(xikxˉi)(xjkxˉj)\Sigma_{ij} = \frac{1}{n - 1}\sum_{k=1}^{n}\big(x_{ik} - \bar{x}_i\big)\big(x_{jk} - \bar{x}_j\big)

where nn is the number of observations per feature and xˉi\bar{x}_i is feature ii's mean.

Input

  • vectorslist[list[float]] of shape (n_features, n_observations): each inner list holds one feature's values across all observations.

Output

Returns a list[list[float]] of shape (n_features, n_features): the symmetric sample covariance matrix.

Examples

Example 1

Input:  [[1, 2, 3], [4, 5, 6]]
Output: [[1.0, 1.0], [1.0, 1.0]]

Explanation: feature 0 has mean 2 and feature 1 has mean 5. Each variance is (1)2+02+122=1\frac{(-1)^2 + 0^2 + 1^2}{2} = 1, and the covariance is (1)(1)+0+(1)(1)2=1\frac{(-1)(-1) + 0 + (1)(1)}{2} = 1.

Constraints

  • Use the sample covariance: divide by n1n - 1, where nn is the number of observations.
  • The matrix is symmetric: Σij=Σji\Sigma_{ij} = \Sigma_{ji}.
  • Every vectors[i] has the same length nn.

Notes

  • Mind the orientation: rows are features, columns are observations — the transpose of the usual (n_samples, n_features) design matrix (this matches np.cov with rowvar=True).
  • Dividing by n1n - 1 (Bessel's correction) gives an unbiased estimate of the population covariance from a sample.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Reference example: [[1,2,3],[4,5,6]] -> all ones
  • Sample: three features, matches np.cov
  • Reference: single feature diagonal equals sample variance