All problems
Polya Urn Variance
Problem
Variance of the number of red balls after n draws from a Polya urn that starts with r red and b blue balls:
Var = n * r * b * (r + b + n) / ((r + b)^2 * (r + b + 1)).
Return 0.0 when the urn is empty or n < 0.
Hint
The draws are positively correlated, so the variance exceeds the binomial case.
Test cases
case 1
in: [1,1,1]
expected: 0.25
case 2
in: [1,1,2]
expected: 0.6666666666666666
case 3
in: [2,3,5]
expected: 2
case 4
in: [0,5,3]
expected: 0
Starter codePython
def polya_urn_variance(r, b, n):
# Your code here
pass4 test cases · run your code against them in the editor