1#ifndef STAN_MATH_PRIM_FUN_INC_BETA_DDB_HPP
2#define STAN_MATH_PRIM_FUN_INC_BETA_DDB_HPP
48 if ((0.1 < z && z <= 0.75 && b > 500) || (0.01 < z && z <= 0.1 && b > 2500)
49 || (0.001 < z && z <= 0.01 && b > 1e5)) {
50 return -
inc_beta_dda(b, a, 1 - z, digamma_b, digamma_ab);
54 if ((z > 0.75 && a < 500) || (z > 0.9 && a < 2500) || (z > 0.99 && a < 1e5)
56 return -
inc_beta_dda(b, a, 1 - z, digamma_b, digamma_ab);
59 double threshold = 1
e-10;
61 const T a_plus_b = a + b;
62 const T a_plus_1 = a + 1;
65 T prefactor =
pow(a_plus_1 / a_plus_b, 3);
67 T sum_numer = digamma_ab * prefactor;
68 T sum_denom = prefactor;
70 T summand = prefactor * z * a_plus_b / a_plus_1;
73 digamma_ab +=
inv(a_plus_b);
75 while (
fabs(summand) > threshold) {
76 sum_numer += digamma_ab * summand;
79 summand *= (1 + (a_plus_b) / k) * (1 + k) / (1 + a_plus_1 / k);
80 digamma_ab +=
inv(a_plus_b + k);
86 "did not converge within 100000 iterations",
"",
"");
90 return inc_beta(a, b, z) * (
log1m(z) - digamma_b + sum_numer / sum_denom);
static constexpr double e()
Return the base of the natural logarithm.
auto pow(const T1 &x1, const T2 &x2)
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)
fvar< T > log1m(const fvar< T > &x)
fvar< T > inv(const fvar< T > &x)
fvar< T > fabs(const fvar< T > &x)
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 ...