Skip to content

Instantly share code, notes, and snippets.

@omarfsosa
Created January 27, 2021 16:06
Show Gist options
  • Save omarfsosa/12d2e0f25577b25668cdc14c89713824 to your computer and use it in GitHub Desktop.
Save omarfsosa/12d2e0f25577b25668cdc14c89713824 to your computer and use it in GitHub Desktop.
Sample from a negative binomial in terms of mean and precision parameters
import numpy as np
def neg_binomial_2_rng(mu, phi, size=None):
"""
RNG for the negative binomial parametrised in terms
of the mean and the precision.
mean = mu
variance = mu + (mu**2)/phi
"""
tau = np.random.gamma(phi, mu/phi, size=size)
return np.random.poisson(tau)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment