Skip to content
DeepForgeby svx
All problems
EasyProbabilitypr-152

Polya Contagion Update

Problem

A Polya urn starts with r red and b blue balls. After observed_reds red balls appeared in observed_total draws, return the predictive probability that the next draw is red:

P = (r + observed_reds) / (r + b + observed_total).

Return 0.0 for inconsistent or empty inputs.

Hint

Replace each drawn ball and add one of the same color, so the proportions update by counts.

Test cases

case 1
in: [1,1,1,1]
expected: 0.6666666666666666
case 2
in: [2,3,0,0]
expected: 0.4
case 3
in: [1,1,2,4]
expected: 0.5
case 4
in: [0,0,0,0]
expected: 0
Starter codePython
def polya_contagion_probability(r, b, observed_reds, observed_total):
    # Your code here
    pass

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