Skip to content
DeepForgeby svx
All problems
MediumProbabilitypr-071

Beta-Binomial PMF

Problem

Beta-binomial probability of k successes in n trials when the success probability is drawn from a Beta(alpha, beta) prior:

P(k) = C(n, k) * B(k + alpha, n - k + beta) / B(alpha, beta).

Use log-gamma functions to avoid overflow. Return 0.0 for invalid arguments.

Hint

Use lgamma for the Beta function values and exponentiate at the end.

Test cases

case 1
in: [1,2,1,1]
expected: 0.3333333333333332
case 2
in: [2,2,1,1]
expected: 0.3333333333333332
case 3
in: [0,2,1,1]
expected: 0.3333333333333332
case 4
in: [1,3,2,3]
expected: 0.34285714285714186
Starter codePython
import math


def beta_binomial_pmf(k, n, alpha, beta):
    # Your code here
    pass

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