Automatic Differentiation
 
Loading...
Searching...
No Matches

◆ binomial_coefficient_log() [2/3]

template<typename T_n , typename T_k , require_all_stan_scalar_t< T_n, T_k > * = nullptr>
return_type_t< T_n, T_k > stan::math::binomial_coefficient_log ( const T_n  n,
const T_k  k 
)
inline

Return the log of the binomial coefficient for the specified arguments.

The binomial coefficient, \({n \choose k}\), read "n choose k", is defined for \(0 \leq k \leq n\) by

\({n \choose k} = \frac{n!}{k! (n-k)!}\).

This function uses Gamma functions to define the log and generalize the arguments to continuous n and k.

\( \log {n \choose k} = \log \ \Gamma(n+1) - \log \Gamma(k+1) - \log \Gamma(n-k+1)\).

\[ \mbox{binomial\_coefficient\_log}(x, y) = \begin{cases} \textrm{error} & \mbox{if } y > x + 1 \textrm{ or } y < -1 \textrm{ or } x < -1\\ \ln\Gamma(x+1) & \mbox{if } -1 < y < x + 1 \\ \quad -\ln\Gamma(y+1)& \\ \quad -\ln\Gamma(x-y+1)& \\[6pt] \textrm{NaN} & \mbox{if } x = \textrm{NaN or } y = \textrm{NaN} \end{cases} \]

\[ \frac{\partial\, \mbox{binomial\_coefficient\_log}(x, y)}{\partial x} = \begin{cases} \textrm{error} & \mbox{if } y > x + 1 \textrm{ or } y < -1 \textrm{ or } x < -1\\ \Psi(x+1) & \mbox{if } 0\leq y \leq x \\ \quad -\Psi(x-y+1)& \\[6pt] \textrm{NaN} & \mbox{if } x = \textrm{NaN or } y = \textrm{NaN} \end{cases} \]

\[ \frac{\partial\, \mbox{binomial\_coefficient\_log}(x, y)}{\partial y} = \begin{cases} \textrm{error} & \mbox{if } y > x + 1 \textrm{ or } y < -1 \textrm{ or } x < -1\\ -\Psi(y+1) & \mbox{if } 0\leq y \leq x \\ \quad +\Psi(x-y+1)& \\[6pt] \textrm{NaN} & \mbox{if } x = \textrm{NaN or } y = \textrm{NaN} \end{cases} \]

This function is numerically more stable than naive evaluation via lgamma.

Template Parameters
T_ntype of the first argument
T_ktype of the second argument
Parameters
ntotal number of objects.
knumber of objects chosen.
Returns
log (n choose k).

Definition at line 81 of file binomial_coefficient_log.hpp.