Skip to content
DeepForgeby svx
All problems
MediumProbabilitypr-157

Bertrand's Box Posterior

Problem

Each box holds items described by a [gold, silver] count pair, and a uniformly chosen box yields a gold item. Return the posterior probability that the next item drawn from the same box is also gold, weighting boxes by the probability g / (g + s) of producing the observed gold:

P = sum over boxes of w * (g - 1) / (g + s - 1) divided by sum of w, with w = g / (g + s).

Return 0.0 when no gold can be drawn.

Hint

Condition on which box was chosen; boxes with more gold items are more likely to have produced the draw.

Test cases

case 1
in: [[[2,0],[0,2],[1,1]]]
expected: 0.6666666666666666
case 2
in: [[[1,0],[0,1]]]
expected: 0
case 3
in: [[[1,1]]]
expected: 0
case 4
in: [[[3,1],[0,2]]]
expected: 0.6666666666666666
case 5
in: [[]]
expected: 0
Starter codePython
def bertrand_box_posterior(boxes):
    # Your code here
    pass

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