Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
binomial_coefficient_log.hpp
Go to the documentation of this file.
1 #ifndef STAN__MATH__FUNCTIONS__BINOMIAL_COEFFICIENT_LOG_HPP
2 #define STAN__MATH__FUNCTIONS__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 {
10 
61  template <typename T_N, typename T_n>
62  inline typename boost::math::tools::promote_args<T_N, T_n>::type
63  binomial_coefficient_log(const T_N N, const T_n n) {
64  using std::log;
65  using boost::math::lgamma;
66 
67  const double cutoff = 1000;
68  if ((N < cutoff) || (N - n < cutoff)) {
69  return lgamma(N + 1.0) - lgamma(n + 1.0) - lgamma(N - n + 1.0);
70  } else {
71  return n * log(N - n) + (N + 0.5) * log(N/(N-n))
72  + 1/(12*N) - n - 1/(12*(N-n)) - lgamma(n + 1.0);
73  }
74  }
75 
76  }
77 }
78 
79 #endif
boost::math::tools::promote_args< T_N, T_n >::type binomial_coefficient_log(const T_N N, const T_n n)
Return the log of the binomial coefficient for the specified arguments.
fvar< T > lgamma(const fvar< T > &x)
Definition: lgamma.hpp:15
double lgamma(double x)
Definition: lgamma.hpp:31
Eigen::Matrix< T, Rows, Cols > log(const Eigen::Matrix< T, Rows, Cols > &m)
Return the element-wise logarithm of the matrix or vector.
Definition: log.hpp:16
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15

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