Stan Math Library  2.14.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 
8 #include <cmath>
9 
10 namespace stan {
11  namespace math {
12 
13  // Gradient of the incomplete beta function beta(a, b, z)
14  // with respect to the first two arguments, using the
15  // equivalence to a hypergeometric function.
16  // See http://dlmf.nist.gov/8.17#ii
17  inline void grad_inc_beta(double& g1, double& g2,
18  double a, double b, double z) {
19  using std::exp;
20  using std::log;
21 
22  double c1 = log(z);
23  double c2 = log1m(z);
24  double c3 = exp(lbeta(a, b)) * inc_beta(a, b, z);
25  double C = exp(a * c1 + b * c2) / a;
26  double dF1 = 0;
27  double dF2 = 0;
28  if (C) grad_2F1(dF1, dF2, a + b, 1.0, a + 1, z);
29  g1 = (c1 - 1.0 / a) * c3 + C * (dF1 + dF2);
30  g2 = c2 * c3 + C * dF1;
31  }
32 
33  }
34 }
35 #endif
fvar< T > lbeta(const fvar< T > &x1, const fvar< T > &x2)
Definition: lbeta.hpp:15
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
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:19
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
fvar< T > log1m(const fvar< T > &x)
Definition: log1m.hpp:13
void grad_2F1(T &gradA, T &gradC, T a, T b, T c, T z, T precision=1e-6)
Definition: grad_2F1.hpp:12

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