Automatic Differentiation
 
Loading...
Searching...
No Matches
grad_inc_beta.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_GRAD_INC_BETA_HPP
2#define STAN_MATH_PRIM_FUN_GRAD_INC_BETA_HPP
3
13#include <cmath>
14
15namespace stan {
16namespace math {
17
18// Gradient of the incomplete beta function beta(a, b, z)
19// with respect to the first two arguments, using the
20// equivalence to a hypergeometric function.
21// See http://dlmf.nist.gov/8.17#ii
22inline void grad_inc_beta(double& g1, double& g2, double a, double b,
23 double z) {
24 using std::exp;
25 using std::log;
26
27 double c1 = log(z);
28 double c2 = log1m(z);
29 double c3 = beta(a, b) * inc_beta(a, b, z);
30 double C = exp(a * c1 + b * c2) / a;
31 double dF1 = 0;
32 double dF2 = 0;
33 double dF3 = 0;
34 double dFz = 0;
35 if (C) {
36 std::forward_as_tuple(dF1, dF2, dF3, dFz)
37 = grad_2F1<true>(a + b, 1.0, a + 1, z);
38 }
39 g1 = fma((c1 - inv(a)), c3, C * (dF1 + dF3));
40 g2 = fma(c2, c3, C * dF1);
41}
42
43} // namespace math
44} // namespace stan
45#endif
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
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 > log1m(const fvar< T > &x)
Definition log1m.hpp:12
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 > inv(const fvar< T > &x)
Definition inv.hpp:13
fvar< return_type_t< T1, T2, T3 > > fma(const fvar< T1 > &x1, const fvar< T2 > &x2, const fvar< T3 > &x3)
The fused multiply-add operation (C99).
Definition fma.hpp:60
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:15
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...