Stan Math Library  2.20.0
reverse mode automatic differentiation
grad_inc_beta.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_FUN_GRAD_INC_BETA_HPP
2 #define STAN_MATH_PRIM_SCAL_FUN_GRAD_INC_BETA_HPP
3 
10 #include <cmath>
11 
12 namespace stan {
13 namespace math {
14 
15 // Gradient of the incomplete beta function beta(a, b, z)
16 // with respect to the first two arguments, using the
17 // equivalence to a hypergeometric function.
18 // See http://dlmf.nist.gov/8.17#ii
19 inline void grad_inc_beta(double& g1, double& g2, double a, double b,
20  double z) {
21  using std::exp;
22  using std::log;
23 
24  double c1 = log(z);
25  double c2 = log1m(z);
26  double c3 = beta(a, b) * inc_beta(a, b, z);
27  double C = exp(a * c1 + b * c2) / a;
28  double dF1 = 0;
29  double dF2 = 0;
30  if (C)
31  grad_2F1(dF1, dF2, a + b, 1.0, a + 1, z);
32  g1 = fma((c1 - inv(a)), c3, C * (dF1 + dF2));
33  g2 = fma(c2, c3, C * dF1);
34 }
35 
36 } // namespace math
37 } // namespace stan
38 #endif
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
void grad_2F1(T &g_a1, T &g_b1, const T &a1, const T &a2, const T &b1, const T &z, const T &precision=1e-10, int max_steps=1e5)
Gradients of the hypergeometric function, 2F1.
Definition: grad_2F1.hpp:36
void grad_inc_beta(fvar< T > &g1, fvar< T > &g2, fvar< T > a, fvar< T > b, fvar< T > z)
Gradient of the incomplete beta function beta(a, b, z) with respect to the first two arguments...
fvar< T > inc_beta(const fvar< T > &a, const fvar< T > &b, const fvar< T > &x)
Definition: inc_beta.hpp:18
fvar< T > beta(const fvar< T > &x1, const fvar< T > &x2)
Return fvar with the beta function applied to the specified arguments and its gradient.
Definition: beta.hpp:51
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:11
fvar< typename stan::return_type< T1, T2, T3 >::type > fma(const fvar< T1 > &x1, const fvar< T2 > &x2, const fvar< T3 > &x3)
The fused multiply-add operation (C99).
Definition: fma.hpp:59
fvar< T > log1m(const fvar< T > &x)
Definition: log1m.hpp:12
fvar< T > inv(const fvar< T > &x)
Definition: inv.hpp:12

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