This is an old version, view current version.

17.2 Negative binomial distribution (alternative parameterization)

Stan also provides an alternative parameterization of the negative binomial distribution directly using a mean (i.e., location) parameter and a parameter that controls overdispersion relative to the square of the mean. Section combinatorial functions, below, provides a second alternative parameterization directly in terms of the log mean.

17.2.1 Probability mass function

The first parameterization is for \(\mu \in \mathbb{R}^+\) and \(\phi \in \mathbb{R}^+\), which for \(n \in \mathbb{N}\) is defined as \[ \text{NegBinomial2}(n \, | \, \mu, \phi) = \binom{n + \phi - 1}{n} \, \left( \frac{\mu}{\mu+\phi} \right)^{\!n} \, \left( \frac{\phi}{\mu+\phi} \right)^{\!\phi} \!. \]

The mean and variance of a random variable \(n \sim \text{NegBinomial2}(n~|~\mu,\phi)\) are \[ \mathbb{E}[n] = \mu \ \ \ \text{ and } \ \ \ \text{Var}[n] = \mu + \frac{\mu^2}{\phi}. \] Recall that \(\text{Poisson}(\mu)\) has variance \(\mu\), so \(\mu^2 / \phi > 0\) is the additional variance of the negative binomial above that of the Poisson with mean \(\mu\). So the inverse of parameter \(\phi\) controls the overdispersion, scaled by the square of the mean, \(\mu^2\).

17.2.2 Sampling statement

n ~ neg_binomial_2(mu, phi)

Increment target log probability density with neg_binomial_2_lupmf(n | mu, phi).
Available since 2.3

17.2.3 Stan functions

real neg_binomial_2_lpmf(ints n | reals mu, reals phi)
The log negative binomial probability mass of n given location mu and precision phi.
Available since 2.20

real neg_binomial_2_lupmf(ints n | reals mu, reals phi)
The log negative binomial probability mass of n given location mu and precision phi dropping constant additive terms.
Available since 2.25

real neg_binomial_2_cdf(ints n, reals mu, reals phi)
The negative binomial cumulative distribution function of n given location mu and precision phi.
Available since 2.6

real neg_binomial_2_lcdf(ints n | reals mu, reals phi)
The log of the negative binomial cumulative distribution function of n given location mu and precision phi.
Available since 2.12

real neg_binomial_2_lccdf(ints n | reals mu, reals phi)
The log of the negative binomial complementary cumulative distribution function of n given location mu and precision phi.
Available since 2.12

R neg_binomial_2_rng(reals mu, reals phi)
Generate a negative binomial variate with location mu and precision phi; may only be used in transformed data and generated quantities blocks. mu must be less than \(2 ^ {29}\). For a description of argument and return types, see section vectorized function signatures.
Available since 2.18