Bounded Discrete Distributions

Bounded discrete probability functions have support on {0,,N} for some upper bound N.

Binomial distribution

Probability mass function

Suppose NN and θ[0,1], and n{0,,N}. Binomial(n | N,θ)=(Nn)θn(1θ)Nn.

Log probability mass function

logBinomial(n | N,θ)=logΓ(N+1)logΓ(n+1)logΓ(Nn+1)+nlogθ+(Nn)log(1θ),

Gradient of log probability mass function

θlogBinomial(n | N,θ)=nθNn1θ

Distribution statement

n ~ binomial(N, theta)

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

Available since 2.0

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

Available since 2.12

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

Available since 2.25

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

Available since 2.0

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

Available since 2.12

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

Available since 2.12

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.

Available since 2.18

Binomial distribution, logit parameterization

Stan also provides a version of the binomial probability mass function distribution with the chance of success parameterized on the unconstrained logistic scale.

Probability mass function

Suppose NN, αR, and n{0,,N}. Then BinomialLogit(n | N,α)=Binomial(n | N,logit1(α))=(Nn)(logit1(α))n(1logit1(α))Nn.

Log probability mass function

logBinomialLogit(n | N,α)=logΓ(N+1)logΓ(n+1)logΓ(Nn+1)+nloglogit1(α)+(Nn)log(1logit1(α)),

Gradient of log probability mass function

αlogBinomialLogit(n | N,α)=nlogit1(α)Nnlogit1(α)

Distribution statement

n ~ binomial_logit(N, alpha)

Increment target log probability density with binomial_logit_lupmf(n | N, alpha).

Available since 2.0

Stan functions

real binomial_logit_lpmf(ints n | ints N, reals alpha)
The log binomial probability mass of n successes in N trials given logit-scaled chance of success alpha

Available since 2.12

real binomial_logit_lupmf(ints n | ints N, reals alpha)
The log binomial probability mass of n successes in N trials given logit-scaled chance of success alpha dropping constant additive terms

Available since 2.25

Binomial-logit generalized linear model (Logistic Regression)

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

Probability mass function

Suppose NN, xRnm,αRn,βRm, and n{0,,N}. Then BinomialLogitGLM(n | N,x,α,β)=Binomial(n | N,logit1(αi+xiβ))=(Nn)(logit1(αi+1jmxijβj))n(1logit1(αi+1jmxijβj))Nn.

Distribution statement

n ~ binomial_logit_glm(N, x, alpha, beta)

Increment target log probability density with binomial_logit_glm_lupmf(n | N, x, alpha, beta).

Available since 2.34

Stan Functions

real binomial_logit_glm_lpmf(int n | int N, matrix x, real alpha, vector beta)
The log binomial probability mass of n given N trials and chance of success inv_logit(alpha + x * beta).

Available since 2.34

real binomial_logit_glm_lupmf(int n | int N, matrix x, real alpha, vector beta)
The log binomial probability mass of n given N trials and chance of success inv_logit(alpha + x * beta) dropping constant additive terms.

Available since 2.34

real binomial_logit_glm_lpmf(int n | int N, matrix x, vector alpha, vector beta)
The log binomial probability mass of n given N trials and chance of success inv_logit(alpha + x * beta).

Available since 2.34

real binomial_logit_glm_lupmf(int n | int N, matrix x, vector alpha, vector beta)
The log binomial probability mass of n given N trials and chance of success inv_logit(alpha + x * beta) dropping constant additive terms.

Available since 2.34

real binomial_logit_glm_lpmf(array[] int n | array[] int N, row_vector x, real alpha, vector beta)
The log binomial probability mass of n given N trials and chance of success inv_logit(alpha + x * beta).

Available since 2.34

real binomial_logit_glm_lupmf(array[] int n | array[] int N, row_vector x, real alpha, vector beta)
The log binomial probability mass of n given N trials and chance of success inv_logit(alpha + x * beta) dropping constant additive terms.

Available since 2.34

real binomial_logit_glm_lpmf(array[] int n | array[] int N, row_vector x, vector alpha, vector beta)
The log binomial probability mass of n given N trials and chance of success inv_logit(alpha + x * beta).

Available since 2.34

real binomial_logit_glm_lupmf(array[] int n | array[] int N, row_vector x, vector alpha, vector beta)
The log binomial probability mass of n given N trials and chance of success inv_logit(alpha + x * beta) dropping constant additive terms.

Available since 2.34

real binomial_logit_glm_lpmf(array[] int n | array[] int N, matrix x, real alpha, vector beta)
The log binomial probability mass of n given N trials and chance of success inv_logit(alpha + x * beta).

Available since 2.34

real binomial_logit_glm_lupmf(array[] int n | array[] int N, matrix x, real alpha, vector beta)
The log binomial probability mass of n given N trials and chance of success inv_logit(alpha + x * beta) dropping constant additive terms.

Available since 2.34

real binomial_logit_glm_lpmf(array[] int n | array[] int N, matrix x, vector alpha, vector beta)
The log binomial probability mass of n given N trials and chance of success inv_logit(alpha + x * beta).

Available since 2.34

real binomial_logit_glm_lupmf(array[] int n | array[] int N, matrix x, vector alpha, vector beta)
The log binomial probability mass of n given N trials and chance of success inv_logit(alpha + x * beta) dropping constant additive terms.

Available since 2.34

Beta-binomial distribution

Probability mass function

If NN, αR+, and βR+, then for n0,,N, BetaBinomial(n | N,α,β)=(Nn)B(n+α,Nn+β)B(α,β), where the beta function B(u,v) is defined for uR+ and vR+ by B(u,v)=Γ(u) Γ(v)Γ(u+v).

Distribution statement

n ~ beta_binomial(N, alpha, beta)

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

Available since 2.0

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

Hypergeometric distribution

Probability mass function

If aN, bN, and N{0,,a+b}, then for n{max(0,Nb),,min(a,N)}, Hypergeometric(n | N,a,b)=(an)(bNn)(a+bN).

Distribution statement

n ~ hypergeometric(N, a, b)

Increment target log probability density with hypergeometric_lupmf(n | N, a, b).

Available since 2.0

Stan functions

real hypergeometric_lpmf(int n | int N, int a, int b)
The log hypergeometric probability mass of n successes in N trials given total success count of a and total failure count of b

Available since 2.12

real hypergeometric_lupmf(int n | int N, int a, int b)
The log hypergeometric probability mass of n successes in N trials given total success count of a and total failure count of b dropping constant additive terms

Available since 2.25

int hypergeometric_rng(int N, int a, int b)
Generate a hypergeometric variate with N trials, total success count of a, and total failure count of b; may only be used in transformed data and generated quantities blocks

Available since 2.18

Categorical distribution

Probability mass functions

If NN, N>0, and if θRN forms an N-simplex (i.e., has nonnegative entries summing to one), then for y{1,,N}, Categorical(y | θ)=θy. In addition, Stan provides a log-odds scaled categorical distribution, CategoricalLogit(y | β)=Categorical(y | softmax(β)). See the definition of softmax for the definition of the softmax function.

Distribution statement

y ~ categorical(theta)

Increment target log probability density with categorical_lupmf(y | theta) dropping constant additive terms.

Available since 2.0

Distribution statement

y ~ categorical_logit(beta)

Increment target log probability density with categorical_logit_lupmf(y | beta).

Available since 2.4

Stan functions

All of the categorical distributions are vectorized so that the outcome y can be a single integer (type int) or an array of integers (type array[] int).

real categorical_lpmf(ints y | vector theta)
The log categorical probability mass function with outcome(s) y in 1:N given N-vector of outcome probabilities theta. The parameter theta must have non-negative entries that sum to one, but it need not be a variable declared as a simplex.

Available since 2.12

real categorical_lupmf(ints y | vector theta)
The log categorical probability mass function with outcome(s) y in 1:N given N-vector of outcome probabilities theta dropping constant additive terms. The parameter theta must have non-negative entries that sum to one, but it need not be a variable declared as a simplex.

Available since 2.25

real categorical_logit_lpmf(ints y | vector beta)
The log categorical probability mass function with outcome(s) y in 1:N given log-odds of outcomes beta.

Available since 2.12

real categorical_logit_lupmf(ints y | vector beta)
The log categorical probability mass function with outcome(s) y in 1:N given log-odds of outcomes beta dropping constant additive terms.

Available since 2.25

int categorical_rng(vector theta)
Generate a categorical variate with N-simplex distribution parameter theta; may only be used in transformed data and generated quantities blocks

Available since 2.0

int categorical_logit_rng(vector beta)
Generate a categorical variate with outcome in range 1:N from log-odds vector beta; may only be used in transformed data and generated quantities blocks

Available since 2.16

Categorical logit generalized linear model (softmax regression)

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

Note that the implementation does not put any restrictions on the coefficient matrix β. It is up to the user to use a reference category, a suitable prior or some other means of identifiability. See Multi-logit in the Stan User’s Guide.

Probability mass functions

If N,M,KN, N,M,K>0, and if xRM×K,αRN,βRKN, then for y{1,,N}M, CategoricalLogitGLM(y | x,α,β)=1iMCategoricalLogit(yi | α+xiβ)=1iMCategorical(yi | softmax(α+xiβ)). See the definition of softmax for the definition of the softmax function.

Distribution statement

y ~ categorical_logit_glm(x, alpha, beta)

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

Available since 2.23

Stan functions

real categorical_logit_glm_lpmf(int y | row_vector x, vector alpha, matrix beta)
The log categorical probability mass function with outcome y in 1:N given N-vector of log-odds of outcomes alpha + x * beta.

Available since 2.23

real categorical_logit_glm_lupmf(int y | row_vector x, vector alpha, matrix beta)
The log categorical probability mass function with outcome y in 1:N given N-vector of log-odds of outcomes alpha + x * beta dropping constant additive terms.

Available since 2.25

real categorical_logit_glm_lpmf(int y | matrix x, vector alpha, matrix beta)
The log categorical probability mass function with outcomes y in 1:N given N-vector of log-odds of outcomes alpha + x * beta.

Available since 2.23

real categorical_logit_glm_lupmf(int y | matrix x, vector alpha, matrix beta)
The log categorical probability mass function with outcomes y in 1:N given N-vector of log-odds of outcomes alpha + x * beta dropping constant additive terms.

Available since 2.25

real categorical_logit_glm_lpmf(array[] int y | row_vector x, vector alpha, matrix beta)
The log categorical probability mass function with outcomes y in 1:N given N-vector of log-odds of outcomes alpha + x * beta.

Available since 2.23

real categorical_logit_glm_lupmf(array[] int y | row_vector x, vector alpha, matrix beta)
The log categorical probability mass function with outcomes y in 1:N given N-vector of log-odds of outcomes alpha + x * beta dropping constant additive terms.

Available since 2.25

real categorical_logit_glm_lpmf(array[] int y | matrix x, vector alpha, matrix beta)
The log categorical probability mass function with outcomes y in 1:N given N-vector of log-odds of outcomes alpha + x * beta.

Available since 2.23

real categorical_logit_glm_lupmf(array[] int y | matrix x, vector alpha, matrix beta)
The log categorical probability mass function with outcomes y in 1:N given N-vector of log-odds of outcomes alpha + x * beta dropping constant additive terms.

Available since 2.25

Discrete range distribution

Probability mass functions

If l,uZ are lower and upper bounds (lu), then for any integer y{l,,u}, DiscreteRange(y | l,u)=1ul+1.

Distribution statement

y ~ discrete_range(l, u)

Increment the target log probability density with discrete_range_lupmf(y | l, u) dropping constant additive terms.

Available since 2.26

Stan functions

All of the discrete range distributions are vectorized so that the outcome y and the bounds l, u can be a single integer (type int) or an array of integers (type array[] int).

real discrete_range_lpmf(ints y | ints l, ints u)
The log probability mass function with outcome(s) y in l:u.

Available since 2.26

real discrete_range_lupmf(ints y | ints l, ints u)
The log probability mass function with outcome(s) y in l:u dropping constant additive terms.

Available since 2.26

real discrete_range_cdf(ints y | ints l, ints u)
The discrete range cumulative distribution function for the given y, lower and upper bounds.

Available since 2.26

real discrete_range_lcdf(ints y | ints l, ints u)
The log of the discrete range cumulative distribution function for the given y, lower and upper bounds.

Available since 2.26

real discrete_range_lccdf(ints y | ints l, ints u)
The log of the discrete range complementary cumulative distribution function for the given y, lower and upper bounds.

Available since 2.26

ints discrete_range_rng(ints l, ints u)
Generate a discrete variate between the given lower and upper bounds; 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.26

Ordered logistic distribution

Probability mass function

If KN with K>2, cRK1 such that ck<ck+1 for k{1,,K2}, and ηR, then for k{1,,K}, OrderedLogistic(k | η,c)={1logit1(ηc1)if k=1,logit1(ηck1)logit1(ηck)if 1<k<K,andlogit1(ηcK1)0if k=K. The k=K case is written with the redundant subtraction of zero to illustrate the parallelism of the cases; the k=1 and k=K edge cases can be subsumed into the general definition by setting c0= and cK=+ with logit1()=0 and logit1()=1.

Distribution statement

k ~ ordered_logistic(eta, c)

Increment target log probability density with ordered_logistic_lupmf(k | eta, c).

Available since 2.0

Stan functions

real ordered_logistic_lpmf(ints k | vector eta, vectors c)
The log ordered logistic probability mass of k given linear predictors eta, and cutpoints c.

Available since 2.18

real ordered_logistic_lupmf(ints k | vector eta, vectors c)
The log ordered logistic probability mass of k given linear predictors eta, and cutpoints c dropping constant additive terms.

Available since 2.25

int ordered_logistic_rng(real eta, vector c)
Generate an ordered logistic variate with linear predictor eta and cutpoints c; may only be used in transformed data and generated quantities blocks

Available since 2.0

Ordered logistic generalized linear model (ordinal regression)

Probability mass function

If N,M,KN with N,M>0, K>2, cRK1 such that ck<ck+1 for k{1,,K2}, and xRN×M,βRM, then for y{1,,K}N, OrderedLogisticGLM(y | x,β,c)=1iNOrderedLogistic(yi | xiβ,c)=1iN{1logit1(xiβc1)if y=1,logit1(xiβcy1)logit1(xiβcy)if 1<y<K,andlogit1(xiβcK1)0if y=K. The k=K case is written with the redundant subtraction of zero to illustrate the parallelism of the cases; the y=1 and y=K edge cases can be subsumed into the general definition by setting c0= and cK=+ with logit1()=0 and logit1()=1.

Distribution statement

y ~ ordered_logistic_glm(x, beta, c)

Increment target log probability density with ordered_logistic_lupmf(y | x, beta, c).

Available since 2.23

Stan functions

real ordered_logistic_glm_lpmf(int y | row_vector x, vector beta, vector c)
The log ordered logistic probability mass of y, given linear predictors x * beta, and cutpoints c. The cutpoints c must be ordered.

Available since 2.23

real ordered_logistic_glm_lupmf(int y | row_vector x, vector beta, vector c)
The log ordered logistic probability mass of y, given linear predictors x * beta, and cutpoints c dropping constant additive terms. The cutpoints c must be ordered.

Available since 2.25

real ordered_logistic_glm_lpmf(int y | matrix x, vector beta, vector c)
The log ordered logistic probability mass of y, given linear predictors x * beta, and cutpoints c. The cutpoints c must be ordered.

Available since 2.23

real ordered_logistic_glm_lupmf(int y | matrix x, vector beta, vector c)
The log ordered logistic probability mass of y, given linear predictors x * beta, and cutpoints c dropping constant additive terms. The cutpoints c must be ordered.

Available since 2.25

real ordered_logistic_glm_lpmf(array[] int y | row_vector x, vector beta, vector c)
The log ordered logistic probability mass of y, given linear predictors x * beta, and cutpoints c. The cutpoints c must be ordered.

Available since 2.23

real ordered_logistic_glm_lupmf(array[] int y | row_vector x, vector beta, vector c)
The log ordered logistic probability mass of y, given linear predictors x * beta, and cutpoints c dropping constant additive terms. The cutpoints c must be ordered.

Available since 2.25

real ordered_logistic_glm_lpmf(array[] int y | matrix x, vector beta, vector c)
The log ordered logistic probability mass of y, given linear predictors x * beta, and cutpoints c. The cutpoints c must be ordered.

Available since 2.23

real ordered_logistic_glm_lupmf(array[] int y | matrix x, vector beta, vector c)
The log ordered logistic probability mass of y, given linear predictors x * beta, and cutpoints c dropping constant additive terms. The cutpoints c must be ordered.

Available since 2.25

Ordered probit distribution

Probability mass function

If KN with K>2, cRK1 such that ck<ck+1 for k{1,,K2}, and ηR, then for k{1,,K}, OrderedProbit(k | η,c)={1Φ(ηc1)if k=1,Φ(ηck1)Φ(ηck)if 1<k<K,andΦ(ηcK1)0if k=K. The k=K case is written with the redundant subtraction of zero to illustrate the parallelism of the cases; the k=1 and k=K edge cases can be subsumed into the general definition by setting c0= and cK=+ with Φ()=0 and Φ()=1.

Distribution statement

k ~ ordered_probit(eta, c)

Increment target log probability density with ordered_probit_lupmf(k | eta, c).

Available since 2.19

Stan functions

real ordered_probit_lpmf(ints k | vector eta, vectors c)
The log ordered probit probability mass of k given linear predictors eta, and cutpoints c.

Available since 2.18

real ordered_probit_lupmf(ints k | vector eta, vectors c)
The log ordered probit probability mass of k given linear predictors eta, and cutpoints c dropping constant additive terms.

Available since 2.25

real ordered_probit_lpmf(ints k | real eta, vectors c)
The log ordered probit probability mass of k given linear predictor eta, and cutpoints c.

Available since 2.19

real ordered_probit_lupmf(ints k | real eta, vectors c)
The log ordered probit probability mass of k given linear predictor eta, and cutpoints c dropping constant additive terms.

Available since 2.19

int ordered_probit_rng(real eta, vector c)
Generate an ordered probit variate with linear predictor eta and cutpoints c; may only be used in transformed data and generated quantities blocks

Available since 2.18
Back to top