1#ifndef STAN_MATH_PRIM_FUN_INC_BETA_DDA_HPP
2#define STAN_MATH_PRIM_FUN_INC_BETA_DDA_HPP
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);
51 if (z > 0.75 && a < 500) {
52 return -
inc_beta_ddb(b, a, 1 - z, digamma_a, digamma_ab);
54 if (z > 0.9 && a < 2500) {
55 return -
inc_beta_ddb(b, a, 1 - z, digamma_a, digamma_ab);
57 if (z > 0.99 && a < 1e5) {
58 return -
inc_beta_ddb(b, a, 1 - z, digamma_a, digamma_ab);
61 return -
inc_beta_ddb(b, a, 1 - z, digamma_a, digamma_ab);
64 double threshold = 1
e-10;
66 const T a_plus_b = a + b;
67 const T a_plus_1 = a + 1;
72 T prefactor =
pow(a_plus_1 / a_plus_b, 3);
74 T sum_numer = (digamma_ab - digamma_a) * prefactor;
75 T sum_denom = prefactor;
77 T summand = prefactor * z * a_plus_b / a_plus_1;
80 digamma_ab +=
inv(a_plus_b);
81 digamma_a +=
inv(a_plus_1);
83 while (
fabs(summand) > threshold) {
84 sum_numer += (digamma_ab - digamma_a) * summand;
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);
95 "did not converge within 10000 iterations",
"",
"");
98 return inc_beta(a, b, z) * (
log(z) + sum_numer / sum_denom);
static constexpr double e()
Return the base of the natural logarithm.
auto pow(const T1 &x1, const T2 &x2)
fvar< T > log(const fvar< T > &x)
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 > 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 ...