| Stan Math Library 2.12.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 namespace math {
11
12 template <typename T>
13 inline
14 fvar<T>
15 binomial_coefficient_log(const fvar<T>& x1, const fvar<T>& x2) {
16 using boost::math::digamma;
17 using std::log;
18 const double cutoff = 1000;
19 if ((x1.val_ < cutoff) || (x1.val_ - x2.val_ < cutoff)) {
20 return fvar<T>(binomial_coefficient_log(x1.val_, x2.val_),
21 x1.d_ * digamma(x1.val_ + 1)
22 - x2.d_ * digamma(x2.val_ + 1)
23 - (x1.d_ - x2.d_) * digamma(x1.val_ - x2.val_ + 1));
24 } else {
25 return fvar<T>(binomial_coefficient_log(x1.val_, x2.val_),
26 x2.d_ * log(x1.val_ - x2.val_)
27 + x2.val_ * (x1.d_ - x2.d_) / (x1.val_ - x2.val_)
28 + x1.d_ * log(x1.val_ / (x1.val_ - x2.val_))
29 + (x1.val_ + 0.5) / (x1.val_ / (x1.val_ - x2.val_))
30 * (x1.d_ * (x1.val_ - x2.val_)
31 - (x1.d_ - x2.d_) * x1.val_)
32 / ((x1.val_ - x2.val_) * (x1.val_ - x2.val_))
33 - x1.d_ / (12.0 * x1.val_ * x1.val_)
34 - x2.d_
35 + (x1.d_ - x2.d_) / (12.0 * (x1.val_ - x2.val_)
36 * (x1.val_ - x2.val_))
37 - digamma(x2.val_ + 1) * x2.d_);
38 }
39 }
40
41 template <typename T>
42 inline
43 fvar<T>
44 binomial_coefficient_log(const fvar<T>& x1, const double x2) {
45 using boost::math::digamma;
46 using std::log;
47 const double cutoff = 1000;
48 if ((x1.val_ < cutoff) || (x1.val_ - x2 < cutoff)) {
49 return fvar<T>(binomial_coefficient_log(x1.val_, x2),
50 x1.d_ * digamma(x1.val_ + 1)
51 - x1.d_ * digamma(x1.val_ - x2 + 1));
52 } else {
53 return fvar<T>(binomial_coefficient_log(x1.val_, x2),
54 x2 * x1.d_ / (x1.val_ - x2)
55 + x1.d_ * log(x1.val_ / (x1.val_ - x2))
56 + (x1.val_ + 0.5) / (x1.val_ / (x1.val_ - x2))
57 * (x1.d_ * (x1.val_ - x2) - x1.d_ * x1.val_)
58 / ((x1.val_ - x2) * (x1.val_ - x2))
59 - x1.d_ / (12.0 * x1.val_ * x1.val_)
60 + x1.d_ / (12.0 * (x1.val_ - x2) * (x1.val_ - x2)));
61 }
62 }
63
64 template <typename T>
65 inline
66 fvar<T>
67 binomial_coefficient_log(const double x1, const fvar<T>& x2) {
68 using boost::math::digamma;
69 using std::log;
70 const double cutoff = 1000;
71 if ((x1 < cutoff) || (x1 - x2.val_ < cutoff)) {
72 return fvar<T>(binomial_coefficient_log(x1, x2.val_),
73 -x2.d_ * digamma(x2.val_ + 1)
74 - x2.d_ * digamma(x1 - x2.val_ + 1));
75 } else {
76 return fvar<T>(binomial_coefficient_log(x1, x2.val_),
77 x2.d_ * log(x1 - x2.val_)
78 + x2.val_ * -x2.d_ / (x1 - x2.val_)
79 - x2.d_
80 - x2.d_ / (12.0 * (x1 - x2.val_) * (x1 - x2.val_))
81 + x2.d_ * (x1 + 0.5) / (x1 - x2.val_)
82 - digamma(x2.val_ + 1) * x2.d_);
83 }
84 }
85 }
86 }
87 #endif
core.hpp
stan::math::fvar::d_
T d_
Definition: fvar.hpp:14
stan::math::binomial_coefficient_log
fvar< T > binomial_coefficient_log(const fvar< T > &x1, const fvar< T > &x2)
Definition: binomial_coefficient_log.hpp:15
stan
Definition: log_sum_exp.hpp:8
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
binomial_coefficient_log.hpp
stan::math::fvar::val_
T val_
Definition: fvar.hpp:13
stan::math::fvar
Definition: fvar.hpp:12
stan::math::digamma
fvar< T > digamma(const fvar< T > &x)
Definition: digamma.hpp:15

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