## 16.4 Beta-binomial distribution

### 16.4.1 Probability mass function

If $$N \in \mathbb{N}$$, $$\alpha \in \mathbb{R}^+$$, and $$\beta \in \mathbb{R}^+$$, then for $$n \in {0,\ldots,N}$$, $\begin{equation*} \text{BetaBinomial}(n~|~N,\alpha,\beta) = \binom{N}{n} \frac{\mathrm{B}(n+\alpha, N -n + \beta)}{\mathrm{B}(\alpha,\beta)}, \end{equation*}$ where the beta function $$\mathrm{B}(u,v)$$ is defined for $$u \in \mathbb{R}^+$$ and $$v \in \mathbb{R}^+$$ by $\begin{equation*} \mathrm{B}(u,v) = \frac{\Gamma(u) \ \Gamma(v)}{\Gamma(u + v)}. \end{equation*}$

### 16.4.2 Sampling statement

n ~ beta_binomial(N, alpha, beta)

Increment target log probability density with beta_binomial_lupmf(n | N, alpha, beta).
Available since 2.0

### 16.4.3 Stan functions

real beta_binomial_lpmf(ints n | ints N, reals alpha, reals beta)
The log beta-binomial probability mass of n successes in N trials given prior success count (plus one) of alpha and prior failure count (plus one) of beta
Available since 2.12

real beta_binomial_lupmf(ints n | ints N, reals alpha, reals beta)
The log beta-binomial probability mass of n successes in N trials given prior success count (plus one) of alpha and prior failure count (plus one) of beta dropping constant additive terms
Available since 2.25

real beta_binomial_cdf(ints n | ints N, reals alpha, reals beta)
The beta-binomial cumulative distribution function of n successes in N trials given prior success count (plus one) of alpha and prior failure count (plus one) of beta
Available since 2.0

real beta_binomial_lcdf(ints n | ints N, reals alpha, reals beta)
The log of the beta-binomial cumulative distribution function of n successes in N trials given prior success count (plus one) of alpha and prior failure count (plus one) of beta
Available since 2.12

real beta_binomial_lccdf(ints n | ints N, reals alpha, reals beta)
The log of the beta-binomial complementary cumulative distribution function of n successes in N trials given prior success count (plus one) of alpha and prior failure count (plus one) of beta
Available since 2.12

R beta_binomial_rng(ints N, reals alpha, reals beta)
Generate a beta-binomial variate with N trials, prior success count (plus one) of alpha, and prior failure count (plus one) of beta; may only be used in transformed data and generated quantities blocks. For a description of argument and return types, see section vectorized PRNG functions.
Available since 2.18