Stan Math Library: stan/math/fwd/scal/fun/binomial_coefficient_log.hpp Source File

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

binomial_coefficient_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_SCAL_FUN_BINOMIAL_COEFFICIENT_LOG_HPP
2 #define STAN_MATH_FWD_SCAL_FUN_BINOMIAL_COEFFICIENT_LOG_HPP
3
4 #include <stan/math/fwd/core.hpp>
5
6 #include <boost/math/special_functions/digamma.hpp>
7 #include <stan/math/prim/scal/fun/binomial_coefficient_log.hpp>
8
9 namespace stan {
10
11 namespace math {
12
13 template <typename T>
14 inline
15 fvar<T>
16 binomial_coefficient_log(const fvar<T>& x1, const fvar<T>& x2) {
17 using boost::math::digamma;
18 using std::log;
19 using stan::math::binomial_coefficient_log;
20 const double cutoff = 1000;
21 if ((x1.val_ < cutoff) || (x1.val_ - x2.val_ < cutoff)) {
22 return fvar<T>(binomial_coefficient_log(x1.val_, x2.val_),
23 x1.d_ * digamma(x1.val_ + 1)
24 - x2.d_ * digamma(x2.val_ + 1)
25 - (x1.d_ - x2.d_) * digamma(x1.val_ - x2.val_ + 1));
26 } else {
27 return fvar<T>(binomial_coefficient_log(x1.val_, x2.val_),
28 x2.d_ * log(x1.val_ - x2.val_)
29 + x2.val_ * (x1.d_ - x2.d_) / (x1.val_ - x2.val_)
30 + x1.d_ * log(x1.val_ / (x1.val_ - x2.val_))
31 + (x1.val_ + 0.5) / (x1.val_ / (x1.val_ - x2.val_))
32 * (x1.d_ * (x1.val_ - x2.val_)
33 - (x1.d_ - x2.d_) * x1.val_)
34 / ((x1.val_ - x2.val_) * (x1.val_ - x2.val_))
35 - x1.d_ / (12.0 * x1.val_ * x1.val_)
36 - x2.d_
37 + (x1.d_ - x2.d_) / (12.0 * (x1.val_ - x2.val_)
38 * (x1.val_ - x2.val_))
39 - digamma(x2.val_ + 1) * x2.d_);
40 }
41 }
42
43 template <typename T>
44 inline
45 fvar<T>
46 binomial_coefficient_log(const fvar<T>& x1, const double x2) {
47 using boost::math::digamma;
48 using std::log;
49 using stan::math::binomial_coefficient_log;
50 const double cutoff = 1000;
51 if ((x1.val_ < cutoff) || (x1.val_ - x2 < cutoff)) {
52 return fvar<T>(binomial_coefficient_log(x1.val_, x2),
53 x1.d_ * digamma(x1.val_ + 1)
54 - x1.d_ * digamma(x1.val_ - x2 + 1));
55 } else {
56 return fvar<T>(binomial_coefficient_log(x1.val_, x2),
57 x2 * x1.d_ / (x1.val_ - x2)
58 + x1.d_ * log(x1.val_ / (x1.val_ - x2))
59 + (x1.val_ + 0.5) / (x1.val_ / (x1.val_ - x2))
60 * (x1.d_ * (x1.val_ - x2) - x1.d_ * x1.val_)
61 / ((x1.val_ - x2) * (x1.val_ - x2))
62 - x1.d_ / (12.0 * x1.val_ * x1.val_)
63 + x1.d_ / (12.0 * (x1.val_ - x2) * (x1.val_ - x2)));
64 }
65 }
66
67 template <typename T>
68 inline
69 fvar<T>
70 binomial_coefficient_log(const double x1, const fvar<T>& x2) {
71 using boost::math::digamma;
72 using std::log;
73 using stan::math::binomial_coefficient_log;
74 const double cutoff = 1000;
75 if ((x1 < cutoff) || (x1 - x2.val_ < cutoff)) {
76 return fvar<T>(binomial_coefficient_log(x1, x2.val_),
77 -x2.d_ * digamma(x2.val_ + 1)
78 - x2.d_ * digamma(x1 - x2.val_ + 1));
79 } else {
80 return fvar<T>(binomial_coefficient_log(x1, x2.val_),
81 x2.d_ * log(x1 - x2.val_)
82 + x2.val_ * -x2.d_ / (x1 - x2.val_)
83 - x2.d_
84 - x2.d_ / (12.0 * (x1 - x2.val_) * (x1 - x2.val_))
85 + x2.d_ * (x1 + 0.5) / (x1 - x2.val_)
86 - digamma(x2.val_ + 1) * x2.d_);
87 }
88 }
89 }
90 }
91 #endif
core.hpp
stan::math::fvar::d_
T d_
Definition: fvar.hpp:15
stan::math::binomial_coefficient_log
fvar< T > binomial_coefficient_log(const fvar< T > &x1, const fvar< T > &x2)
Definition: binomial_coefficient_log.hpp:16
stan
Definition: log_sum_exp.hpp:8
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
binomial_coefficient_log.hpp
stan::math::fvar::val_
T val_
Definition: fvar.hpp:14
stan::math::fvar
Definition: fvar.hpp:13
stan::math::digamma
fvar< T > digamma(const fvar< T > &x)
Definition: digamma.hpp:16

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