inc_beta_dda.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_SCAL_FUN_INC_BETA_DDA_HPP
#define STAN_MATH_PRIM_SCAL_FUN_INC_BETA_DDA_HPP
#include <stan/math/prim/scal/err/domain_error.hpp>
#include <stan/math/prim/scal/fun/inc_beta.hpp>
#include <stan/math/prim/scal/fun/inc_beta_ddb.hpp>
#include <cmath>
namespace stan {
namespace math {
template <typename T>
T inc_beta_ddb(T a, T b, T z,
T digamma_b, T digamma_ab);
template <typename T>
T inc_beta_dda(T a, T b, T z,
T digamma_a, T digamma_ab) {
using std::log;
if (b > a)
if ((0.1 < z && z <= 0.75 && b > 500)
|| (0.01 < z && z <= 0.1 && b > 2500)
|| (0.001 < z && z <= 0.01 && b > 1e5))
return -inc_beta_ddb(b, a, 1 - z, digamma_a, digamma_ab);
if (z > 0.75 && a < 500)
return -inc_beta_ddb(b, a, 1 - z, digamma_a, digamma_ab);
if (z > 0.9 && a < 2500)
return -inc_beta_ddb(b, a, 1 - z, digamma_a, digamma_ab);
if (z > 0.99 && a < 1e5)
return -inc_beta_ddb(b, a, 1 - z, digamma_a, digamma_ab);
if (z > 0.999)
return -inc_beta_ddb(b, a, 1 - z, digamma_a, digamma_ab);
double threshold = 1e-10;
digamma_a += 1.0 / a; // Need digamma(a + 1), not digamma(a);
// Common prefactor to regularize numerator and denomentator
T prefactor = (a + 1) / (a + b);
prefactor = prefactor * prefactor * prefactor;
T sum_numer = (digamma_ab - digamma_a) * prefactor;
T sum_denom = prefactor;
T summand = prefactor * z * (a + b) / (a + 1);
T k = 1;
digamma_ab += 1.0 / (a + b);
digamma_a += 1.0 / (a + 1);
while (fabs(summand) > threshold) {
sum_numer += (digamma_ab - digamma_a) * summand;
sum_denom += summand;
summand *= (1 + (a + b) / k) * (1 + k) / (1 + (a + 1) / k);
digamma_ab += 1.0 / (a + b + k);
digamma_a += 1.0 / (a + 1 + k);
++k;
summand *= z / k;
if (k > 1e5)
domain_error("inc_beta_dda",
"did not converge within 10000 iterations", "", "");
}
return inc_beta(a, b, z) * (log(z) + sum_numer / sum_denom);
}
}
}
#endif
stan::math::fabs
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
stan
Definition: log_sum_exp.hpp:8
stan::math::inc_beta_dda
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, b) with respect to a.
Definition: inc_beta_dda.hpp:39
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
stan::math::inc_beta_ddb
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, b) with respect to b.
Definition: inc_beta_ddb.hpp:39
inc_beta_ddb.hpp
stan::math::inc_beta
fvar< T > inc_beta(const fvar< T > &a, const fvar< T > &b, const fvar< T > &x)
Definition: inc_beta.hpp:19
stan::math::domain_error
void 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.
Definition: domain_error.hpp:30
stan::math::e
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:94
domain_error.hpp
inc_beta.hpp

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