Stan Math Library  2.10.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 
4 #include <boost/math/special_functions/gamma.hpp>
5 #include <boost/math/tools/promotion.hpp>
6 
7 namespace stan {
8 
9  namespace math {
60  template <typename T_N, typename T_n>
61  inline typename boost::math::tools::promote_args<T_N, T_n>::type
62  binomial_coefficient_log(const T_N N, const T_n n) {
63  using std::log;
64  using boost::math::lgamma;
65  const double CUTOFF = 1000;
66  if (N - n < CUTOFF) {
67  T_N N_plus_1 = N + 1;
68  return lgamma(N_plus_1) - lgamma(n + 1) - lgamma(N_plus_1 - n);
69  } else {
70  typename boost::math::tools::promote_args<T_N, T_n>::type N_minus_n
71  = N - n;
72  double one_twelfth = 1.0 / 12;
73  return n * log(N_minus_n)
74  + (N + 0.5) * log(N / N_minus_n)
75  + one_twelfth / N
76  - n
77  - one_twelfth / N_minus_n
78  - lgamma(n + 1);
79  }
80  }
81 
82  }
83 }
84 #endif
fvar< T > binomial_coefficient_log(const fvar< T > &x1, const fvar< T > &x2)
fvar< T > lgamma(const fvar< T > &x)
Definition: lgamma.hpp:15
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15

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