Stan Math Library  2.20.0
reverse mode automatic differentiation
log_inv_logit_diff.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_SCAL_FUN_LOG_INV_LOGIT_DIFF_HPP
2 #define STAN_MATH_REV_SCAL_FUN_LOG_INV_LOGIT_DIFF_HPP
3 
4 #include <stan/math/rev/meta.hpp>
5 #include <stan/math/rev/core.hpp>
10 
11 namespace stan {
12 namespace math {
13 
14 /*
15  * Returns the natural logarithm of the difference of the
16  * inverse logits of the specified arguments and its gradients.
17  *
18  \f[
19  \mathrm{log\_inv\_logit\_diff}(x,y) =
20  \ln\left(\frac{1}{1+\exp(-x)}-\frac{1}{1+\exp(-y)}\right)
21  \f]
22 
23  \f[
24  \frac{\partial }{\partial x} = -\frac{e^x}{e^y-e^x}-\frac{e^x}{e^x+1}
25  \f]
26 
27  \f[
28  \frac{\partial }{\partial x} = -\frac{e^y}{e^x-e^y}-\frac{e^y}{e^y+1}
29  \f]
30  *
31  * @tparam T1 Type of x argument
32  * @tparam T2 Type of y argument
33  * @param a Argument
34  * @param b Argument
35  * @return Result of log difference of inverse logits of arguments
36  * and gradients
37  */
38 namespace internal {
40  public:
42  : op_vv_vari(log_inv_logit_diff(avi->val_, bvi->val_), avi, bvi) {}
43  void chain() {
44  avi_->adj_
45  -= adj_ * (inv(expm1(bvi_->val_ - avi_->val_)) + inv_logit(avi_->val_));
46 
47  bvi_->adj_
48  -= adj_ * (inv(expm1(avi_->val_ - bvi_->val_)) + inv_logit(bvi_->val_));
49  }
50 };
51 
53  public:
55  : op_vd_vari(log_inv_logit_diff(avi->val_, b), avi, b) {}
56  void chain() {
57  avi_->adj_ -= adj_ * (inv(expm1(bd_ - avi_->val_)) + inv_logit(avi_->val_));
58  }
59 };
60 
62  public:
64  : op_dv_vari(log_inv_logit_diff(a, bvi->val_), a, bvi) {}
65  void chain() {
66  bvi_->adj_ -= adj_ * (inv(expm1(ad_ - bvi_->val_)) + inv_logit(bvi_->val_));
67  }
68 };
69 } // namespace internal
70 
71 inline var log_inv_logit_diff(const var& a, double b) {
73 }
74 
75 inline var log_inv_logit_diff(const var& a, const var& b) {
77 }
78 
79 inline var log_inv_logit_diff(double a, const var& b) {
81 }
82 
83 } // namespace math
84 } // namespace stan
85 #endif
fvar< T > inv_logit(const fvar< T > &x)
Returns the inverse logit function applied to the argument.
Definition: inv_logit.hpp:20
The variable implementation base class.
Definition: vari.hpp:30
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
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
fvar< T > expm1(const fvar< T > &x)
Definition: expm1.hpp:13
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:45
fvar< T > log_inv_logit_diff(const fvar< T > &x, const fvar< T > &y)
Returns fvar with the natural logarithm of the difference of the inverse logits of the specified argu...
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:44
fvar< T > inv(const fvar< T > &x)
Definition: inv.hpp:12

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