Skip to content
DeepForgeby svx
All problems
MediumStatisticsst-004

Pearson Correlation

Problem

Compute the Pearson correlation coefficient between x and y:

r = cov(x, y) / (std(x) * std(y))

Return 0.0 if either standard deviation is 0.

Hint

Covariance divided by the product of standard deviations. Range: [-1, 1].

Test cases

case 1
in: [[1,2,3,4],[2,4,6,8]]
expected: 1
case 2
in: [[1,2,3],[3,2,1]]
expected: -1
case 3
in: [[1,2,3,4,5],[2,4,1,3,5]]
expected: 0.5
case 4
in: [[5,5,5],[1,2,3]]
expected: 0
Starter codePython
def pearson_correlation(x, y):
    # Your code here
    pass

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