Skip to content
DeepForgeby svx
All problems
HardProbabilitypr-378

Conditional Expectation Discrete Table

Problem

For a joint distribution given as [x, y, probability] rows, E[X | Y = y0] = sum_{rows with y = y0} x p / sum_{rows with y = y0} p.

Given the rows and the target y0, return the conditional expectation.

Hint

Restrict to the matching rows and renormalize.

Test cases

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

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