Stan Math Library  2.20.0
reverse mode automatic differentiation
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/meta.hpp>
5 #include <stan/math/rev/core.hpp>
10 #include <limits>
11 
12 namespace stan {
13 namespace math {
14 
15 namespace internal {
16 class gamma_p_vv_vari : public op_vv_vari {
17  public:
19  : op_vv_vari(gamma_p(avi->val_, bvi->val_), avi, bvi) {}
20  void chain() {
21  using std::exp;
22  using std::fabs;
23  using std::log;
24 
25  if (is_inf(avi_->val_)) {
26  avi_->adj_ += std::numeric_limits<double>::quiet_NaN();
27  bvi_->adj_ += std::numeric_limits<double>::quiet_NaN();
28  return;
29  }
30  if (is_inf(bvi_->val_)) {
31  avi_->adj_ += std::numeric_limits<double>::quiet_NaN();
32  bvi_->adj_ += std::numeric_limits<double>::quiet_NaN();
33  return;
34  }
35 
36  // return zero derivative as gamma_p is flat
37  // to machine precision for b / a > 10
38  if (std::fabs(bvi_->val_ / avi_->val_) > 10)
39  return;
40 
42  bvi_->adj_
43  += adj_
44  * std::exp(-bvi_->val_ + (avi_->val_ - 1.0) * std::log(bvi_->val_)
45  - lgamma(avi_->val_));
46  }
47 };
48 
49 class gamma_p_vd_vari : public op_vd_vari {
50  public:
51  gamma_p_vd_vari(vari* avi, double b)
52  : op_vd_vari(gamma_p(avi->val_, b), avi, b) {}
53  void chain() {
54  if (is_inf(avi_->val_)) {
55  avi_->adj_ += std::numeric_limits<double>::quiet_NaN();
56  return;
57  }
58  if (is_inf(bd_)) {
59  avi_->adj_ += std::numeric_limits<double>::quiet_NaN();
60  return;
61  }
62 
63  // return zero derivative as gamma_p is flat
64  // to machine precision for b / a > 10
65  if (std::fabs(bd_ / avi_->val_) > 10)
66  return;
67 
69  }
70 };
71 
72 class gamma_p_dv_vari : public op_dv_vari {
73  public:
74  gamma_p_dv_vari(double a, vari* bvi)
75  : op_dv_vari(gamma_p(a, bvi->val_), a, bvi) {}
76  void chain() {
77  if (is_inf(ad_)) {
78  bvi_->adj_ += std::numeric_limits<double>::quiet_NaN();
79  return;
80  }
81  if (is_inf(bvi_->val_)) {
82  bvi_->adj_ += std::numeric_limits<double>::quiet_NaN();
83  return;
84  }
85 
86  // return zero derivative as gamma_p is flat to
87  // machine precision for b / a > 10
88  if (std::fabs(bvi_->val_ / ad_) > 10)
89  return;
90 
91  bvi_->adj_ += adj_
92  * std::exp(-bvi_->val_ + (ad_ - 1.0) * std::log(bvi_->val_)
93  - lgamma(ad_));
94  }
95 };
96 } // namespace internal
97 
98 inline var gamma_p(const var& a, const var& b) {
99  return var(new internal::gamma_p_vv_vari(a.vi_, b.vi_));
100 }
101 
102 inline var gamma_p(const var& a, double b) {
103  return var(new internal::gamma_p_vd_vari(a.vi_, b));
104 }
105 
106 inline var gamma_p(double a, const var& b) {
107  return var(new internal::gamma_p_dv_vari(a, b.vi_));
108 }
109 
110 } // namespace math
111 } // namespace stan
112 #endif
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition: lgamma.hpp:21
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
gamma_p_vd_vari(vari *avi, double b)
Definition: gamma_p.hpp:51
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
The variable implementation base class.
Definition: vari.hpp:30
gamma_p_vv_vari(vari *avi, vari *bvi)
Definition: gamma_p.hpp:18
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:33
friend class var
Definition: vari.hpp:32
const double val_
The value of this variable.
Definition: vari.hpp:38
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: gamma_p.hpp:53
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:11
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: gamma_p.hpp:76
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:45
gamma_p_dv_vari(double a, vari *bvi)
Definition: gamma_p.hpp:74
int is_inf(const fvar< T > &x)
Returns 1 if the input&#39;s value is infinite and 0 otherwise.
Definition: is_inf.hpp:20
fvar< T > gamma_p(const fvar< T > &x1, const fvar< T > &x2)
Definition: gamma_p.hpp:15
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:44
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: gamma_p.hpp:20
return_type< T1, T2 >::type grad_reg_lower_inc_gamma(const T1 &a, const T2 &z, double precision=1e-10, int max_steps=1e5)
Computes the gradient of the lower regularized incomplete gamma function.

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