Skip to content

Instantly share code, notes, and snippets.

@PDiracDelta
Last active November 7, 2021 17:17
Show Gist options
  • Save PDiracDelta/98585ce0e8c62673bf13c1843f2f7215 to your computer and use it in GitHub Desktop.
Save PDiracDelta/98585ce0e8c62673bf13c1843f2f7215 to your computer and use it in GitHub Desktop.
Python3 log likelihood of Negative Binomial for binned data
def log_likelihood_binned(params, *args):
r, p = params
occurrences_binned = args[0]
observed_values = np.arange(len(X_binned))
N = np.sum(occurrences_binned)
result = np.sum(np.multiply(occurrences_binned, gammaln(observed_values + r))) \
- np.sum(np.multiply(occurrences_binned, np.log(factorial(observed_values)))) \
- N * (gammaln(r)) \
+ np.sum(np.multiply(occurrences_binned, observed_values * np.log(1 - (p if p < 1 else 1 - infinitesimal)))) \
+ N * r * np.log(p)
return -result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment