| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

gamma_p.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_SCAL_FUN_GAMMA_P_HPP
2 #define STAN_MATH_REV_SCAL_FUN_GAMMA_P_HPP
3
4 #include <stan/math/rev/core.hpp>
5 #include <stan/math/prim/scal/fun/gamma_p.hpp>
6 #include <boost/math/special_functions/gamma.hpp>
7 #include <boost/math/special_functions/digamma.hpp>
8 #include <valarray>
9
10 namespace stan {
11 namespace math {
12
13 namespace {
14 class gamma_p_vv_vari : public op_vv_vari {
15 public:
16 gamma_p_vv_vari(vari* avi, vari* bvi) :
17 op_vv_vari(gamma_p(avi->val_, bvi->val_),
18 avi, bvi) {
19 }
20 void chain() {
21 // return zero derivative as gamma_p is flat
22 // to machine precision for b / a > 10
23 if (std::fabs(bvi_->val_ / avi_->val_) > 10 ) return;
24
25 double u = gamma_p(avi_->val_, bvi_->val_);
26
27 double S = 0.0;
28 double s = 1.0;
29 double l = std::log(bvi_->val_);
30 double g = boost::math::tgamma(avi_->val_);
31 double dig = boost::math::digamma(avi_->val_);
32
33 int k = 0;
34 double delta = s / (avi_->val_ * avi_->val_);
35
36 while (std::fabs(delta) > 1e-6) {
37 S += delta;
38 ++k;
39 s *= -bvi_->val_ / k;
40 delta = s / ((k + avi_->val_) * (k + avi_->val_));
41 }
42
43 avi_->adj_ -= adj_ * ((u) * (dig - l)
44 + std::exp(avi_->val_ * l) * S / g);
45 bvi_->adj_ += adj_ * (std::exp(-bvi_->val_)
46 * std::pow(bvi_->val_, avi_->val_ - 1.0) / g);
47 }
48 };
49
50 class gamma_p_vd_vari : public op_vd_vari {
51 public:
52 gamma_p_vd_vari(vari* avi, double b) :
53 op_vd_vari(gamma_p(avi->val_, b),
54 avi, b) {
55 }
56 void chain() {
57 // return zero derivative as gamma_p is flat
58 // to machine precision for b / a > 10
59 if (std::fabs(bd_ / avi_->val_) > 10)
60 return;
61
62 double u = gamma_p(avi_->val_, bd_);
63
64 double S = 0.0;
65 double s = 1.0;
66 double l = std::log(bd_);
67 double g = boost::math::tgamma(avi_->val_);
68 double dig = boost::math::digamma(avi_->val_);
69
70 int k = 0;
71 double delta = s / (avi_->val_ * avi_->val_);
72
73 while (std::fabs(delta) > 1e-6) {
74 S += delta;
75 ++k;
76 s *= -bd_ / k;
77 delta = s / ((k + avi_->val_) * (k + avi_->val_));
78 }
79
80 avi_->adj_ -= adj_ * ((u) * (dig - l)
81 + std::exp(avi_->val_ * l) * S / g);
82 }
83 };
84
85 class gamma_p_dv_vari : public op_dv_vari {
86 public:
87 gamma_p_dv_vari(double a, vari* bvi) :
88 op_dv_vari(gamma_p(a, bvi->val_),
89 a, bvi) {
90 }
91 void chain() {
92 // return zero derivative as gamma_p is flat to
93 // machine precision for b / a > 10
94 if (std::fabs(bvi_->val_ / ad_) > 10 )
95 return;
96 bvi_->adj_ += adj_
97 * (std::exp(-bvi_->val_) * std::pow(bvi_->val_, ad_ - 1.0)
98 / boost::math::tgamma(ad_));
99 }
100 };
101 }
102
103 inline var gamma_p(const var& a,
104 const var& b) {
105 return var(new gamma_p_vv_vari(a.vi_, b.vi_));
106 }
107
108 inline var gamma_p(const var& a,
109 double b) {
110 return var(new gamma_p_vd_vari(a.vi_, b));
111 }
112
113 inline var gamma_p(double a,
114 const var& b) {
115 return var(new gamma_p_dv_vari(a, b.vi_));
116 }
117
118 }
119 }
120 #endif
core.hpp
stan::math::fabs
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
stan
Definition: log_sum_exp.hpp:8
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
stan::math::exp
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
stan::math::var::vi_
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:42
stan::math::gamma_p
fvar< T > gamma_p(const fvar< T > &x1, const fvar< T > &x2)
Definition: gamma_p.hpp:14
gamma_p.hpp
stan::math::e
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:94
stan::math::pow
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition: pow.hpp:17
stan::math::tgamma
fvar< T > tgamma(const fvar< T > &x)
Definition: tgamma.hpp:14
stan::math::digamma
fvar< T > digamma(const fvar< T > &x)
Definition: digamma.hpp:15

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