All problems
Negative Binomial Log-Likelihood
Problem
Compute the negative binomial log likelihood for a count k with dispersion r and success probability p: lgamma(k + r) - lgamma(r) - lgamma(k + 1) + r * ln(1 - p) + k * ln(p).
Return 0.0 for invalid parameters: r <= 0, k < 0, or p outside the open interval (0, 1).
Hint
The dispersion r lets the variance exceed the mean, unlike Poisson.
Test cases
case 1
in: [0,1,0.5]
expected: -0.6931471805599453
case 2
in: [1,1,0.5]
expected: -1.3862943611198906
case 3
in: [2,2,0.5]
expected: -1.6739764335716711
case 4
in: [1,1,0]
expected: 0
Starter codePython
import math
def negative_binomial_loglik(k, r, p):
# Your code here
pass4 test cases · run your code against them in the editor