Stan Math Library  2.20.0
reverse mode automatic differentiation
binomial_coefficient_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_FUN_BINOMIAL_COEFFICIENT_LOG_HPP
2 #define STAN_MATH_PRIM_SCAL_FUN_BINOMIAL_COEFFICIENT_LOG_HPP
3 
8 #include <boost/math/tools/promotion.hpp>
9 
10 namespace stan {
11 namespace math {
62 template <typename T_N, typename T_n>
63 inline typename boost::math::tools::promote_args<T_N, T_n>::type
64 binomial_coefficient_log(const T_N N, const T_n n) {
65  using std::log;
66  const double CUTOFF = 1000;
67  if (N - n < CUTOFF) {
68  const T_N N_plus_1 = N + 1;
69  return lgamma(N_plus_1) - lgamma(n + 1) - lgamma(N_plus_1 - n);
70  } else {
71  typename boost::math::tools::promote_args<T_N, T_n>::type N_minus_n = N - n;
72  const double one_twelfth = inv(12);
73  return multiply_log(n, N_minus_n) + multiply_log((N + 0.5), N / N_minus_n)
74  + one_twelfth / N - n - one_twelfth / N_minus_n - lgamma(n + 1);
75  }
76 }
77 
78 } // namespace math
79 } // namespace stan
80 #endif
fvar< T > binomial_coefficient_log(const fvar< T > &x1, const fvar< T > &x2)
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition: lgamma.hpp:21
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
fvar< T > inv(const fvar< T > &x)
Definition: inv.hpp:12

     [ Stan Home Page ] © 2011–2018, Stan Development Team.