# Unbounded Discrete Distributions

The unbounded discrete distributions have support over the natural numbers (i.e., the non-negative integers).

## Negative binomial distribution

For the negative binomial distribution Stan uses the parameterization described in Gelman et al. (2013). For alternative parameterizations, see section negative binomial glm.

### Probability mass function

If $$\alpha \in \mathbb{R}^+$$ and $$\beta \in \mathbb{R}^+$$, then for $$n \in \mathbb{N}$$, $\begin{equation*} \text{NegBinomial}(n~|~\alpha,\beta) = \binom{n + \alpha - 1}{\alpha - 1} \, \left( \frac{\beta}{\beta+1} \right)^{\!\alpha} \, \left( \frac{1}{\beta + 1} \right)^{\!n} \!. \end{equation*}$

The mean and variance of a random variable $$n \sim \text{NegBinomial}(\alpha,\beta)$$ are given by $\begin{equation*} \mathbb{E}[n] = \frac{\alpha}{\beta} \ \ \text{ and } \ \ \text{Var}[n] = \frac{\alpha}{\beta^2} (\beta + 1). \end{equation*}$

### Sampling statement

n ~ neg_binomial(alpha, beta)

Increment target log probability density with neg_binomial_lupmf(n | alpha, beta).

Available since 2.0

### Stan functions

real neg_binomial_lpmf(ints n | reals alpha, reals beta)
The log negative binomial probability mass of n given shape alpha and inverse scale beta

Available since 2.12

real neg_binomial_lupmf(ints n | reals alpha, reals beta)
The log negative binomial probability mass of n given shape alpha and inverse scale beta dropping constant additive terms

Available since 2.25

real neg_binomial_cdf(ints n | reals alpha, reals beta)
The negative binomial cumulative distribution function of n given shape alpha and inverse scale beta

Available since 2.0

real neg_binomial_lcdf(ints n | reals alpha, reals beta)
The log of the negative binomial cumulative distribution function of n given shape alpha and inverse scale beta

Available since 2.12

real neg_binomial_lccdf(ints n | reals alpha, reals beta)
The log of the negative binomial complementary cumulative distribution function of n given shape alpha and inverse scale beta

Available since 2.12

R neg_binomial_rng(reals alpha, reals beta)
Generate a negative binomial variate with shape alpha and inverse scale beta; may only be used in transformed data and generated quantities blocks. alpha $$/$$ beta must be less than $$2 ^ {29}$$. For a description of argument and return types, see section vectorized function signatures.

Available since 2.18

## 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.

### 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 $\begin{equation*} \text{NegBinomial2}(n \, | \, \mu, \phi) = \binom{n + \phi - 1}{n} \, \left( \frac{\mu}{\mu+\phi} \right)^{\!n} \, \left( \frac{\phi}{\mu+\phi} \right)^{\!\phi} \!. \end{equation*}$

The mean and variance of a random variable $$n \sim \text{NegBinomial2}(n~|~\mu,\phi)$$ are $\begin{equation*} \mathbb{E}[n] = \mu \ \ \ \text{ and } \ \ \ \text{Var}[n] = \mu + \frac{\mu^2}{\phi}. \end{equation*}$ 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$$.

### 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

### 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

## Negative binomial distribution (log alternative parameterization)

Related to the parameterization in section negative binomial, alternative parameterization, the following parameterization uses a log mean parameter $$\eta = \log(\mu)$$, defined for $$\eta \in \mathbb{R}$$, $$\phi \in \mathbb{R}^+$$, so that for $$n \in \mathbb{N}$$, $\begin{equation*} \text{NegBinomial2Log}(n \, | \, \eta, \phi) = \text{NegBinomial2}(n | \exp(\eta), \phi). \end{equation*}$ This alternative may be used for sampling, as a function, and for random number generation, but as of yet, there are no CDFs implemented for it. This is especially useful for log-linear negative binomial regressions.

### Sampling statement

n ~ neg_binomial_2_log(eta, phi)

Increment target log probability density with neg_binomial_2_log_lupmf(n | eta, phi).

Available since 2.3

### Stan functions

real neg_binomial_2_log_lpmf(ints n | reals eta, reals phi)
The log negative binomial probability mass of n given log-location eta and inverse overdispersion parameter phi.

Available since 2.20

real neg_binomial_2_log_lupmf(ints n | reals eta, reals phi)
The log negative binomial probability mass of n given log-location eta and inverse overdispersion parameter phi dropping constant additive terms.

Available since 2.25

R neg_binomial_2_log_rng(reals eta, reals phi)
Generate a negative binomial variate with log-location eta and inverse overdispersion control phi; may only be used in transformed data and generated quantities blocks. eta must be less than $$29 \log 2$$. For a description of argument and return types, see section vectorized function signatures.

Available since 2.18

## Negative-binomial-2-log generalized linear model (negative binomial regression)

Stan also supplies a single function for a generalized linear model with negative binomial likelihood and log link function, i.e. a function for a negative binomial regression. This provides a more efficient implementation of negative binomial regression than a manually written regression in terms of a negative binomial likelihood and matrix multiplication.

### Probability mass function

If $$x\in \mathbb{R}^{n\cdot m}, \alpha \in \mathbb{R}^n, \beta\in \mathbb{R}^m, \phi\in \mathbb{R}^+$$, then for $$y \in \mathbb{N}^n$$, $\begin{equation*} \text{NegBinomial2LogGLM}(y~|~x, \alpha, \beta, \phi) = \prod_{1\leq i \leq n}\text{NegBinomial2}(y_i~|~\exp(\alpha_i + x_i\cdot \beta), \phi). \end{equation*}$

### Sampling statement

y ~ neg_binomial_2_log_glm(x, alpha, beta, phi)

Increment target log probability density with neg_binomial_2_log_glm_lupmf(y | x, alpha, beta, phi).

Available since 2.19

### Stan functions

real neg_binomial_2_log_glm_lpmf(int y | matrix x, real alpha, vector beta, real phi)
The log negative binomial probability mass of y given log-location alpha + x * beta and inverse overdispersion parameter phi.

Available since 2.23

real neg_binomial_2_log_glm_lupmf(int y | matrix x, real alpha, vector beta, real phi)
The log negative binomial probability mass of y given log-location alpha + x * beta and inverse overdispersion parameter phi dropping constant additive terms.

Available since 2.25

real neg_binomial_2_log_glm_lpmf(int y | matrix x, vector alpha, vector beta, real phi)
The log negative binomial probability mass of y given log-location alpha + x * beta and inverse overdispersion parameter phi.

Available since 2.23

real neg_binomial_2_log_glm_lupmf(int y | matrix x, vector alpha, vector beta, real phi)
The log negative binomial probability mass of y given log-location alpha + x * beta and inverse overdispersion parameter phi dropping constant additive terms.

Available since 2.25

real neg_binomial_2_log_glm_lpmf(array[] int y | row_vector x, real alpha, vector beta, real phi)
The log negative binomial probability mass of y given log-location alpha + x * beta and inverse overdispersion parameter phi.

Available since 2.23

real neg_binomial_2_log_glm_lupmf(array[] int y | row_vector x, real alpha, vector beta, real phi)
The log negative binomial probability mass of y given log-location alpha + x * beta and inverse overdispersion parameter phi dropping constant additive terms.

Available since 2.25

real neg_binomial_2_log_glm_lpmf(array[] int y | row_vector x, vector alpha, vector beta, real phi)
The log negative binomial probability mass of y given log-location alpha + x * beta and inverse overdispersion parameter phi.

Available since 2.23

real neg_binomial_2_log_glm_lupmf(array[] int y | row_vector x, vector alpha, vector beta, real phi)
The log negative binomial probability mass of y given log-location alpha + x * beta and inverse overdispersion parameter phi dropping constant additive terms.

Available since 2.25

real neg_binomial_2_log_glm_lpmf(array[] int y | matrix x, real alpha, vector beta, real phi)
The log negative binomial probability mass of y given log-location alpha + x * beta and inverse overdispersion parameter phi.

Available since 2.18

real neg_binomial_2_log_glm_lupmf(array[] int y | matrix x, real alpha, vector beta, real phi)
The log negative binomial probability mass of y given log-location alpha + x * beta and inverse overdispersion parameter phi dropping constant additive terms.

Available since 2.25

real neg_binomial_2_log_glm_lpmf(array[] int y | matrix x, vector alpha, vector beta, real phi)
The log negative binomial probability mass of y given log-location alpha + x * beta and inverse overdispersion parameter phi.

Available since 2.18

real neg_binomial_2_log_glm_lupmf(array[] int y | matrix x, vector alpha, vector beta, real phi)
The log negative binomial probability mass of y given log-location alpha + x * beta and inverse overdispersion parameter phi dropping constant additive terms.

Available since 2.25

## Poisson distribution

### Probability mass function

If $$\lambda \in \mathbb{R}^+$$, then for $$n \in \mathbb{N}$$, $\begin{equation*} \text{Poisson}(n|\lambda) = \frac{1}{n!} \, \lambda^n \, \exp(-\lambda). \end{equation*}$

### Sampling statement

n ~ poisson(lambda)

Increment target log probability density with poisson_lupmf(n | lambda).

Available since 2.0

### Stan functions

real poisson_lpmf(ints n | reals lambda)
The log Poisson probability mass of n given rate lambda

Available since 2.12

real poisson_lupmf(ints n | reals lambda)
The log Poisson probability mass of n given rate lambda dropping constant additive terms

Available since 2.25

real poisson_cdf(ints n | reals lambda)
The Poisson cumulative distribution function of n given rate lambda

Available since 2.0

real poisson_lcdf(ints n | reals lambda)
The log of the Poisson cumulative distribution function of n given rate lambda

Available since 2.12

real poisson_lccdf(ints n | reals lambda)
The log of the Poisson complementary cumulative distribution function of n given rate lambda

Available since 2.12

R poisson_rng(reals lambda)
Generate a Poisson variate with rate lambda; may only be used in transformed data and generated quantities blocks. lambda must be less than $$2^{30}$$. For a description of argument and return types, see section vectorized function signatures.

Available since 2.18

## Poisson distribution, log parameterization

Stan also provides a parameterization of the Poisson using the log rate $$\alpha = \log \lambda$$ as a parameter. This is useful for log-linear Poisson regressions so that the predictor does not need to be exponentiated and passed into the standard Poisson probability function.

### Probability mass function

If $$\alpha \in \mathbb{R}$$, then for $$n \in \mathbb{N}$$, $\begin{equation*} \text{PoissonLog}(n|\alpha) = \frac{1}{n!} \, \exp \left(n\alpha - \exp(\alpha) \right). \end{equation*}$

### Sampling statement

n ~ poisson_log(alpha)

Increment target log probability density with poisson_log_lupmf(n | alpha).

Available since 2.0

### Stan functions

real poisson_log_lpmf(ints n | reals alpha)
The log Poisson probability mass of n given log rate alpha

Available since 2.12

real poisson_log_lupmf(ints n | reals alpha)
The log Poisson probability mass of n given log rate alpha dropping constant additive terms

Available since 2.25

R poisson_log_rng(reals alpha)
Generate a Poisson variate with log rate alpha; may only be used in transformed data and generated quantities blocks. alpha must be less than $$30 \log 2$$. For a description of argument and return types, see section vectorized function signatures.

Available since 2.18

## Poisson-log generalized linear model (Poisson regression)

Stan also supplies a single function for a generalized linear model with Poisson likelihood and log link function, i.e. a function for a Poisson regression. This provides a more efficient implementation of Poisson regression than a manually written regression in terms of a Poisson likelihood and matrix multiplication.

### Probability mass function

If $$x\in \mathbb{R}^{n\cdot m}, \alpha \in \mathbb{R}^n, \beta\in \mathbb{R}^m$$, then for $$y \in \mathbb{N}^n$$, $\begin{equation*} \text{PoissonLogGLM}(y|x, \alpha, \beta) = \prod_{1\leq i \leq n}\text{Poisson}(y_i|\exp(\alpha_i + x_i\cdot \beta)). \end{equation*}$

### Sampling statement

y ~ poisson_log_glm(x, alpha, beta)

Increment target log probability density with poisson_log_glm_lupmf(y | x, alpha, beta).

Available since 2.19

### Stan functions

real poisson_log_glm_lpmf(int y | matrix x, real alpha, vector beta)
The log Poisson probability mass of y given the log-rate alpha + x * beta.

Available since 2.23

real poisson_log_glm_lupmf(int y | matrix x, real alpha, vector beta)
The log Poisson probability mass of y given the log-rate alpha + x * beta dropping constant additive terms.

Available since 2.25

real poisson_log_glm_lpmf(int y | matrix x, vector alpha, vector beta)
The log Poisson probability mass of y given the log-rate alpha + x * beta.

Available since 2.23

real poisson_log_glm_lupmf(int y | matrix x, vector alpha, vector beta)
The log Poisson probability mass of y given the log-rate alpha + x * beta dropping constant additive terms.

Available since 2.25

real poisson_log_glm_lpmf(array[] int y | row_vector x, real alpha, vector beta)
The log Poisson probability mass of y given the log-rate alpha + x * beta.

Available since 2.23

real poisson_log_glm_lupmf(array[] int y | row_vector x, real alpha, vector beta)
The log Poisson probability mass of y given the log-rate alpha + x * beta dropping constant additive terms.

Available since 2.25

real poisson_log_glm_lpmf(array[] int y | row_vector x, vector alpha, vector beta)
The log Poisson probability mass of y given the log-rate alpha + x * beta.

Available since 2.23

real poisson_log_glm_lupmf(array[] int y | row_vector x, vector alpha, vector beta)
The log Poisson probability mass of y given the log-rate alpha + x * beta dropping constant additive terms.

Available since 2.25

real poisson_log_glm_lpmf(array[] int y | matrix x, real alpha, vector beta)
The log Poisson probability mass of y given the log-rate alpha + x * beta.

Available since 2.18

real poisson_log_glm_lupmf(array[] int y | matrix x, real alpha, vector beta)
The log Poisson probability mass of y given the log-rate alpha + x * beta dropping constant additive terms.

Available since 2.25

real poisson_log_glm_lpmf(array[] int y | matrix x, vector alpha, vector beta)
The log Poisson probability mass of y given the log-rate alpha + x * beta.

Available since 2.18

real poisson_log_glm_lupmf(array[] int y | matrix x, vector alpha, vector beta)
The log Poisson probability mass of y given the log-rate alpha + x * beta dropping constant additive terms.

Available since 2.25