Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
owens_t.hpp
Go to the documentation of this file.
1 #ifndef STAN__AGRAD__REV__FUNCTIONS__OWENS__T_HPP
2 #define STAN__AGRAD__REV__FUNCTIONS__OWENS__T_HPP
3 
4 #include <math.h>
5 #include <stan/agrad/rev/var.hpp>
11 #include <boost/math/special_functions/owens_t.hpp>
12 
13 namespace stan {
14  namespace agrad {
15 
16  namespace {
17 
18  using stan::math::pi;
19  class owens_t_vv_vari : public op_vv_vari {
20  public:
21  owens_t_vv_vari(vari* avi, vari* bvi) :
22  op_vv_vari(boost::math::owens_t(avi->val_, bvi->val_), avi, bvi) {
23  }
24  void chain() {
27  const double neg_avi_sq_div_2 = -stan::math::square(avi_->val_) * 0.5;
28  const double one_p_bvi_sq = 1.0 + stan::math::square(bvi_->val_);
29 
30  avi_->adj_ += adj_ * ::erf(bvi_->val_ * avi_->val_ * INV_SQRT_2)
31  * std::exp(neg_avi_sq_div_2) * INV_SQRT_TWO_PI * -0.5;
32  bvi_->adj_ += adj_ * std::exp(neg_avi_sq_div_2 * one_p_bvi_sq)
33  / (one_p_bvi_sq * 2.0 * pi());
34  }
35  };
36 
37  class owens_t_vd_vari : public op_vd_vari {
38  public:
39  owens_t_vd_vari(vari* avi, double b) :
40  op_vd_vari(boost::math::owens_t(avi->val_, b), avi, b) {
41  }
42  void chain() {
45  using stan::math::square;
46 
47  avi_->adj_ += adj_ * ::erf(bd_ * avi_->val_ * INV_SQRT_2)
48  * std::exp(-square(avi_->val_) * 0.5)
49  * INV_SQRT_TWO_PI * -0.5;
50  }
51  };
52 
53  class owens_t_dv_vari : public op_dv_vari {
54  public:
55  owens_t_dv_vari(double a, vari* bvi) :
56  op_dv_vari(boost::math::owens_t(a, bvi->val_), a, bvi) {
57  }
58  void chain() {
61  using stan::math::square;
62  const double one_p_bvi_sq = 1.0 + stan::math::square(bvi_->val_);
63 
64  bvi_->adj_ += adj_ * std::exp(-0.5 * square(ad_)
65  * one_p_bvi_sq)
66  / (one_p_bvi_sq * 2.0 * pi());
67  }
68  };
69  }
70 
82  inline var owens_t(const var& h,
83  const var& a) {
84  return var(new owens_t_vv_vari(h.vi_, a.vi_));
85  }
86 
98  inline var owens_t(const var& h,
99  const double& a) {
100  return var(new owens_t_vd_vari(h.vi_, a));
101  }
102 
114  inline var owens_t(const double& h,
115  const var& a) {
116  return var(new owens_t_dv_vari(h, a.vi_));
117  }
118 
119  }
120 }
121 #endif
T square(const T x)
Return the square of the specified argument.
Definition: square.hpp:22
const double INV_SQRT_TWO_PI
Definition: constants.hpp:150
fvar< T > erf(const fvar< T > &x)
Definition: erf.hpp:17
fvar< T > square(const fvar< T > &x)
Definition: square.hpp:15
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:40
fvar< T > owens_t(const fvar< T > &x1, const fvar< T > &x2)
Definition: owens_t.hpp:17
const double INV_SQRT_2
The value of 1 over the square root of 2, .
Definition: constants.hpp:26
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:27
double pi()
Return the value of pi.
Definition: constants.hpp:77
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:16

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