Skip to content
DeepForgeby svx
All problems
MediumProbabilitypr-173

Berkson's Paradox Covariance

Problem

Berkson's paradox: A and B are independent with probabilities p_a and p_b, but the data only include cases where A or B occurred. Return the conditional covariance between A and B among selected cases:

P(AB | selected) - P(A | selected) * P(B | selected)

which is negative unless one of the probabilities is 0 or 1. Return 0.0 when the selection probability is 0.

Hint

Condition on the union A or B and use P(selected) = p_a + p_b - p_a p_b.

Test cases

case 1
in: [0.5,0.5]
expected: -0.1111111111111111
case 2
in: [0.2,0.3]
expected: -0.1735537190082645
case 3
in: [1,0.5]
expected: 0
case 4
in: [0,0.5]
expected: 0
Starter codePython
def berksons_covariance(p_a, p_b):
    # Your code here
    pass

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