Skip to content
DeepForgeby svx
All problems
MediumLinear Algebrala-075

Correlation Matrix from Covariance

Problem

Convert a covariance matrix C into a correlation matrix:

R[i][j] = C[i][j] / sqrt(C[i][i] * C[j][j])

Return None if any variance C[i][i] is 0.

Hint

The result is the covariance rescaled by the standard deviations.

Test cases

case 1
in: [[[4,2],[2,4]]]
expected: [[1,0.5],[0.5,1]]
case 2
in: [[[1,2],[2,4]]]
expected: [[1,1],[1,1]]
case 3
in: [[[1,0],[0,4]]]
expected: [[1,0],[0,1]]
case 4
in: [[[1,0.5],[0.5,0]]]
expected: null
Starter codePython
def correlation_matrix(C):
    # Your code here
    pass

4 test cases · run your code against them in the editor