Automatic Differentiation
 
Loading...
Searching...
No Matches
binomial_coefficient_log.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_KERNELS_DEVICE_FUNCTIONS_BINOMIAL_COEFFICIENT_LOG_HPP
2#define STAN_MATH_OPENCL_KERNELS_DEVICE_FUNCTIONS_BINOMIAL_COEFFICIENT_LOG_HPP
3#ifdef STAN_OPENCL
4
6#include <string>
7
8namespace stan {
9namespace math {
10namespace opencl_kernels {
11
12// \cond
13static constexpr const char* binomial_coefficient_log_device_function
14 = "\n"
15 "#ifndef "
16 "STAN_MATH_OPENCL_KERNELS_DEVICE_FUNCTIONS_BINOMIAL_COEFFICIENT_LOG\n"
17 "#define "
18 "STAN_MATH_OPENCL_KERNELS_DEVICE_FUNCTIONS_BINOMIAL_COEFFICIENT_"
19 "LOG\n" STRINGIFY(
20 // \endcond
80 double binomial_coefficient_log(double n, double k) {
81 if (isnan(n) || isnan(k)) {
82 return NAN;
83 }
84
85 // Choosing the more stable of the symmetric branches
86 if (n > -1 && k > n / 2.0 + 1e-8) {
87 k = n - k;
88 }
89
90 double n_plus_1 = n + 1;
91 double n_plus_1_mk = n_plus_1 - k;
92
93 if (k == 0) {
94 return 0;
95 } else if (n_plus_1 < LGAMMA_STIRLING_DIFF_USEFUL) {
96 return lgamma(n_plus_1) - lgamma(k + 1) - lgamma(n_plus_1_mk);
97 } else {
98 return -lbeta(n_plus_1_mk, k + 1) - log1p(n);
99 }
100 }
101 // \cond
102 ) "\n#endif\n"; // NOLINT
103// \endcond
104
105} // namespace opencl_kernels
106} // namespace math
107} // namespace stan
108
109#endif
110#endif // BINOMIAL_COEFFICIENT_LOG_HPP
double lbeta(double a, double b)
Return the log of the beta function applied to the specified arguments.
Definition lbeta.hpp:62
double binomial_coefficient_log(double n, double k)
Return the log of the binomial coefficient for the specified arguments.
static constexpr double e()
Return the base of the natural logarithm.
Definition constants.hpp:20
fvar< T > log1p(const fvar< T > &x)
Definition log1p.hpp:12
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition lgamma.hpp:21
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
#define STRINGIFY(...)
Definition stringify.hpp:9