Skip to content
DeepForgeby svx
All problems
MediumStatisticsst-003

Covariance

Problem

Compute the population covariance of two equal-length lists x and y:

cov = sum((x_i - mean_x) * (y_i - mean_y)) / N

Hint

Positive when x and y move together; negative when they move opposite.

Test cases

case 1
in: [[1,2,3,4],[2,4,6,8]]
expected: 2.5
case 2
in: [[1,2,3],[3,2,1]]
expected: -0.6666666666666666
case 3
in: [[1,2,3],[1,2,3]]
expected: 0.6666666666666666
Starter codePython
def covariance(x, y):
    # Your code here
    pass

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