Skip to content
DeepForgeby svx
All problems
HardProbabilitypr-375

Covariance from Joint Table

Problem

For a joint distribution given by pairs [x, y, p], the covariance is E[XY] - E[X] E[Y].

Given the list of [x, y, probability] rows, return the covariance.

Hint

Compute the two expectations and the cross expectation first.

Test cases

case 1
in: [[[0,0,0.25],[0,1,0.25],[1,0,0.25],[1,1,0.25]]]
expected: 0
case 2
in: [[[1,2,0.5],[3,4,0.5]]]
expected: 1
case 3
in: [[[1,1,0.5],[2,4,0.5]]]
expected: 0.75
Starter codePython
def covariance_from_joint_table(rows):
    # Your code here
    pass

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