Stan Math Library  2.11.0
reverse mode automatic differentiation
grad_inc_beta.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_SCAL_FUN_GRAD_INC_BETA_HPP
2 #define STAN_MATH_FWD_SCAL_FUN_GRAD_INC_BETA_HPP
3 
11 #include <stan/math/fwd/core.hpp>
14 #include <cmath>
15 
16 namespace stan {
17  namespace math {
18 
19  // Gradient of the incomplete beta function beta(a, b, z)
20  // with respect to the first two arguments, using the
21  // equivalence to a hypergeometric function.
22  // See http://dlmf.nist.gov/8.17#ii
23  template<typename T>
31  using stan::math::log1m;
32 
33  stan::math::fvar<T> c1 = log(z);
34  stan::math::fvar<T> c2 = log1m(z);
35  stan::math::fvar<T> c3 = exp(lbeta(a, b)) * inc_beta(a, b, z);
36 
37  stan::math::fvar<T> C = exp(a * c1 + b * c2) / a;
38 
39  stan::math::fvar<T> dF1 = 0;
40  stan::math::fvar<T> dF2 = 0;
41 
42  if (value_of(value_of(C)))
43  stan::math::grad_2F1(dF1, dF2, a + b,
45  a + 1, z);
46 
47  g1 = (c1 - 1.0 / a) * c3 + C * (dF1 + dF2);
48  g2 = c2 * c3 + C * dF1;
49  }
50 
51  }
52 }
53 #endif
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:16
fvar< T > lbeta(const fvar< T > &x1, const fvar< T > &x2)
Definition: lbeta.hpp:16
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
fvar< T > inc_beta(const fvar< T > &a, const fvar< T > &b, const fvar< T > &x)
Definition: inc_beta.hpp:20
void grad_inc_beta(stan::math::fvar< T > &g1, stan::math::fvar< T > &g2, stan::math::fvar< T > a, stan::math::fvar< T > b, stan::math::fvar< T > z)
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
fvar< T > log1m(const fvar< T > &x)
Definition: log1m.hpp:16
void grad_2F1(T &gradA, T &gradC, T a, T b, T c, T z, T precision=1e-6)
Definition: grad_2F1.hpp:13

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