1#ifndef STAN_MATH_PRIM_FUN_LOG_GAMMA_Q_DGAMMA_HPP
2#define STAN_MATH_PRIM_FUN_LOG_GAMMA_Q_DGAMMA_HPP
40template <
typename T_a,
typename T_z>
44 int max_steps = 250) {
46 const T_return log_prefactor = a *
log(z) - z -
lgamma(a);
48 T_return b_init = z + 1.0 - a;
51 : std::decay_t<decltype(b_init)>(
EPSILON);
54 for (
int i = 1; i <= max_steps; ++i) {
55 T_a an = -i * (i - a);
56 const T_return b = b_init + 2.0 * i;
59 : std::decay_t<decltype(D)>(
EPSILON);
62 : std::decay_t<decltype(C)>(
EPSILON);
64 const T_return delta = C * D;
67 if (delta_m1 < precision) {
71 return log_prefactor -
log(f);
93template <
typename T_a,
typename T_z>
97 int max_steps = 250) {
102 if (z_val > a_val + 1.0) {
103 std::pair<T_return, T_return> result{
107 const T_return Q_val =
exp(result.first);
110 result.second = dQ_da / Q_val;
114 const double P_val =
gamma_p(a_val, z_val);
115 std::pair<T_return, T_return> result{
log1m(P_val), 0.0};
118 const T_return Q_val =
exp(result.first);
122 result.second = dQ_da / Q_val;
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
constexpr double LOG_Q_GAMMA_CF_PRECISION
return_type_t< T_a, T_z > log_q_gamma_cf(const T_a &a, const T_z &z, double precision=LOG_Q_GAMMA_CF_PRECISION, int max_steps=250)
Compute log(Q(a,z)) using continued fraction expansion for upper incomplete gamma function.
double value_of_rec(const fvar< T > &v)
Return the value of the specified variable.
std::pair< return_type_t< T_a, T_z >, return_type_t< T_a, T_z > > log_gamma_q_dgamma(const T_a &a, const T_z &z, double precision=internal::LOG_Q_GAMMA_CF_PRECISION, int max_steps=250)
Compute log(Q(a,z)) and its gradient with respect to a using continued fraction expansion,...
static constexpr double EPSILON
Smallest positive value.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
fvar< T > log(const fvar< T > &x)
return_type_t< T1, T2 > grad_reg_inc_gamma(T1 a, T2 z, T1 g, T1 dig, double precision=1e-6, int max_steps=1e5)
Gradient of the regularized incomplete gamma functions igamma(a, z)
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
fvar< T > gamma_p(const fvar< T > &x1, const fvar< T > &x2)
fvar< T > tgamma(const fvar< T > &x)
Return the result of applying the gamma function to the specified argument.
fvar< T > log1m(const fvar< T > &x)
fvar< T > inv(const fvar< T > &x)
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
fvar< T > fabs(const fvar< T > &x)
fvar< T > exp(const fvar< T > &x)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...