Stan Math Library: stan/math/prim/scal/fun/grad_reg_inc_gamma.hpp Source File

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

grad_reg_inc_gamma.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_FUN_GRAD_REG_INC_GAMMA_HPP
2 #define STAN_MATH_PRIM_SCAL_FUN_GRAD_REG_INC_GAMMA_HPP
3
4 #include <stan/math/prim/scal/fun/gamma_p.hpp>
5 #include <cmath>
6 #include <stdexcept>
7
8 namespace stan {
9 namespace math {
10
11 // Gradient of the regularized incomplete gamma functions igamma(a, g)
12 // Precomputed values
13 // g = boost::math::tgamma(a)
14 // dig = boost::math::digamma(a)
15 template<typename T>
16 T grad_reg_inc_gamma(T a, T z, T g, T dig, T precision = 1e-6) {
17 using boost::math::isinf;
18 using stan::math::gamma_p;
19 using std::domain_error;
20 using std::exp;
21 using std::fabs;
22 using std::log;
23
24 T S = 0;
25 T s = 1;
26 T l = log(z);
27 int k = 0;
28 T delta = s / (a * a);
29 while (fabs(delta) > precision) {
30 S += delta;
31 ++k;
32 s *= - z / k;
33 delta = s / ((k + a) * (k + a));
34 if (isinf(delta))
35 throw domain_error("stan::math::gradRegIncGamma not converging");
36 }
37 return gamma_p(a, z) * ( dig - l ) + exp( a * l ) * S / g;
38 }
39
40 }
41 }
42 #endif
std::isinf
int isinf(const stan::math::var &a)
Checks if the given number is infinite.
Definition: std_isinf.hpp:18
stan::math::fabs
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:14
stan
Definition: log_sum_exp.hpp:8
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
stan::math::grad_reg_inc_gamma
T grad_reg_inc_gamma(T a, T z, T g, T dig, T precision=1e-6)
Definition: grad_reg_inc_gamma.hpp:16
boost::math::isinf
bool isinf(const stan::math::var &v)
Checks if the given number is infinite.
Definition: boost_isinf.hpp:22
stan::math::exp
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
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:32
stan::math::gamma_p
fvar< T > gamma_p(const fvar< T > &x1, const fvar< T > &x2)
Definition: gamma_p.hpp:15
gamma_p.hpp
stan::math::e
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:95

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