Automatic Differentiation
 
Loading...
Searching...
No Matches
inc_beta_dda.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_INC_BETA_DDA_HPP
2#define STAN_MATH_PRIM_FUN_INC_BETA_DDA_HPP
3
11#include <cmath>
12
13namespace stan {
14namespace math {
15
38template <typename T>
39T inc_beta_dda(T a, T b, T z, T digamma_a, T digamma_ab) {
40 using std::fabs;
41 using std::log;
42 using std::pow;
43
44 if (b > a) {
45 if ((0.1 < z && z <= 0.75 && b > 500) || (0.01 < z && z <= 0.1 && b > 2500)
46 || (0.001 < z && z <= 0.01 && b > 1e5)) {
47 return -inc_beta_ddb(b, a, 1 - z, digamma_a, digamma_ab);
48 }
49 }
50
51 if (z > 0.75 && a < 500) {
52 return -inc_beta_ddb(b, a, 1 - z, digamma_a, digamma_ab);
53 }
54 if (z > 0.9 && a < 2500) {
55 return -inc_beta_ddb(b, a, 1 - z, digamma_a, digamma_ab);
56 }
57 if (z > 0.99 && a < 1e5) {
58 return -inc_beta_ddb(b, a, 1 - z, digamma_a, digamma_ab);
59 }
60 if (z > 0.999) {
61 return -inc_beta_ddb(b, a, 1 - z, digamma_a, digamma_ab);
62 }
63
64 double threshold = 1e-10;
65
66 const T a_plus_b = a + b;
67 const T a_plus_1 = a + 1;
68
69 digamma_a += inv(a); // Need digamma(a + 1), not digamma(a);
70
71 // Common prefactor to regularize numerator and denominator
72 T prefactor = pow(a_plus_1 / a_plus_b, 3);
73
74 T sum_numer = (digamma_ab - digamma_a) * prefactor;
75 T sum_denom = prefactor;
76
77 T summand = prefactor * z * a_plus_b / a_plus_1;
78
79 T k = 1;
80 digamma_ab += inv(a_plus_b);
81 digamma_a += inv(a_plus_1);
82
83 while (fabs(summand) > threshold) {
84 sum_numer += (digamma_ab - digamma_a) * summand;
85 sum_denom += summand;
86
87 summand *= (1 + (a_plus_b) / k) * (1 + k) / (1 + a_plus_1 / k);
88 digamma_ab += inv(a_plus_b + k);
89 digamma_a += inv(a_plus_1 + k);
90 ++k;
91 summand *= z / k;
92
93 if (k > 1e5) {
94 throw_domain_error("inc_beta_dda",
95 "did not converge within 10000 iterations", "", "");
96 }
97 }
98 return inc_beta(a, b, z) * (log(z) + sum_numer / sum_denom);
99}
100
101} // namespace math
102} // namespace stan
103#endif
static constexpr double e()
Return the base of the natural logarithm.
Definition constants.hpp:20
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
void throw_domain_error(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message.
T inc_beta_dda(T a, T b, T z, T digamma_a, T digamma_ab)
Returns the partial derivative of the regularized incomplete beta function, I_{z}(a,...
fvar< T > inc_beta(const fvar< T > &a, const fvar< T > &b, const fvar< T > &x)
Definition inc_beta.hpp:19
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition pow.hpp:19
fvar< T > inv(const fvar< T > &x)
Definition inv.hpp:12
fvar< T > fabs(const fvar< T > &x)
Definition fabs.hpp:15
T inc_beta_ddb(T a, T b, T z, T digamma_b, T digamma_ab)
Returns the partial derivative of the regularized incomplete beta function, I_{z}(a,...
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9