This is an old version, view current version.

## 13.1 Binomial distribution

### 13.1.1 Probability mass function

Suppose $$N \in \mathbb{N}$$ and $$\theta \in [0,1]$$, and $$n \in \{0,\ldots,N\}$$. $\text{Binomial}(n~|~N,\theta) = \binom{N}{n} \theta^n (1 - \theta)^{N - n}.$

### 13.1.2 Log probability mass function

$\begin{eqnarray*} \log \text{Binomial}(n~|~N,\theta) & = & \log \Gamma(N+1) - \log \Gamma(n + 1) - \log \Gamma(N- n + 1) \\[4pt] & & { } + n \log \theta + (N - n) \log (1 - \theta), \end{eqnarray*}$

### 13.1.3 Gradient of log probability mass function

$\frac{\partial}{\partial \theta} \log \text{Binomial}(n~|~N,\theta) = \frac{n}{\theta} - \frac{N - n}{1 - \theta}$

### 13.1.4 Sampling statement

n ~ binomial(N, theta)

Increment target log probability density with binomial_lupmf(n | N, theta).

### 13.1.5 Stan functions

real binomial_lpmf(ints n | ints N, reals theta)
The log binomial probability mass of n successes in N trials given chance of success theta

real binomial_lupmf(ints n | ints N, reals theta)
The log binomial probability mass of n successes in N trials given chance of success theta dropping constant additive terms

real binomial_cdf(ints n, ints N, reals theta)
The binomial cumulative distribution function of n successes in N trials given chance of success theta

real binomial_lcdf(ints n | ints N, reals theta)
The log of the binomial cumulative distribution function of n successes in N trials given chance of success theta

real binomial_lccdf(ints n | ints N, reals theta)
The log of the binomial complementary cumulative distribution function of n successes in N trials given chance of success theta

R binomial_rng(ints N, reals theta)
Generate a binomial variate with N trials and chance of success theta; may only be used in transformed data and generated quantities blocks. For a description of argument and return types, see section vectorized PRNG functions.