Stan Math Library
5.0.0
Automatic Differentiation
|
double stan::math::opencl_kernels::binomial_coefficient_log | ( | double | n, |
double | k | ||
) |
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.
n | total number of objects. |
k | number of objects chosen. |
Definition at line 80 of file binomial_coefficient_log.hpp.