Problems
Loading...
1 / 1

Compute Pearson Correlation Matrix

Data ProcessingLinear Algebra
Medium

Compute the Pearson correlation matrix from a dataset without using np.corrcoef. Correlation measures linear relationships between features, normalized by their standard deviations.

Pearson Correlation Formula:

ρij=Cov(Xi,Xj)σiσj\rho_{ij} = \frac{\text{Cov}(X_i, X_j)}{\sigma_i \sigma_j}

Matrix Form:

R=ΣσσTR = \frac{\Sigma}{\sigma \sigma^T}

Where: Σ = covariance matrix, σ = vector of standard deviations, R = correlation matrix

Function Arguments

  • X: list[list[float]] | np.ndarray - Dataset with shape (N, D)
Loading visualization...

Examples

Input: X=[[1, 2], [2, 4], [3, 6]]

Output: [[1.0, 1.0], [1.0, 1.0]] (perfect correlation)

Input: X=[[1, 6], [2, 4], [3, 2]]

Output: [[1.0, -1.0], [-1.0, 1.0]] (perfect negative correlation)

Input: X=[[1, 5], [2, 5], [3, 5]]

Output: [[1.0, NaN], [NaN, 1.0]] (zero variance in feature 2)

Hint 1

Start by computing the covariance matrix. Center data with X - np.mean() then use matrix multiplication.

Hint 2

Compute standard deviations with np.std(). Use np.outer() to create the denominator matrix.

Hint 3

Handle zero variance features by checking std_devs == 0. Set correlations involving these features to NaN, but keep diagonal as 1.0.

Requirements

  • Return np.ndarray of shape (D, D) with correlation values
  • Return None for invalid input (N < 2 or not 2D)
  • Must be vectorized (no loops over data points)
  • Cannot use np.corrcoef function
  • Handle zero variance features (return NaN for correlations involving them)

Constraints

  • Dataset size: N ≤ 10,000, D ≤ 1,000
  • Numerical precision: relative tolerance ≤ 1e-8
  • Libraries: NumPy only
Try Similar Problems