| Stan Math Library 2.12.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/err/domain_error.hpp>
5 #include <stan/math/prim/scal/fun/gamma_p.hpp>
6 #include <stan/math/prim/scal/fun/gamma_q.hpp>
7 #include <stan/math/prim/scal/fun/is_inf.hpp>
8 #include <stan/math/prim/scal/fun/square.hpp>
9 #include <cmath>
10
11 namespace stan {
12 namespace math {
13
39 template<typename T>
40 T grad_reg_inc_gamma(T a, T z, T g, T dig, double precision = 1e-6) {
41 using std::domain_error;
42 using std::exp;
43 using std::fabs;
44 using std::log;
45
46 T l = log(z);
47 if (z >= a && z >= 8) {
48 // asymptotic expansion http://dlmf.nist.gov/8.11#E2
49 T S = 0;
50 T a_minus_one_minus_k = a - 1;
51 T fac = a_minus_one_minus_k; // falling_factorial(a-1, k)
52 T dfac = 1; // d/da[falling_factorial(a-1, k)]
53 T zpow = z; // z ** k
54 T delta = dfac / zpow;
55
56 for (int k = 1; k < 10; ++k) {
57 a_minus_one_minus_k -= 1;
58
59 S += delta;
60
61 zpow *= z;
62 dfac = a_minus_one_minus_k * dfac + fac;
63 fac *= a_minus_one_minus_k;
64 delta = dfac / zpow;
65
66 if (is_inf(delta))
67 stan::math::domain_error("grad_reg_inc_gamma",
68 "is not converging", "", "");
69 }
70
71 return gamma_q(a, z) * (l - dig) + exp(-z + (a - 1) * l) * S / g;
72 } else {
73 // gradient of series expansion http://dlmf.nist.gov/8.7#E3
74
75 T S = 0;
76 T s = 1;
77 int k = 0;
78 T delta = s / square(a);
79 while (fabs(delta) > precision) {
80 S += delta;
81 ++k;
82 s *= - z / k;
83 delta = s / square(k + a);
84 if (is_inf(delta))
85 stan::math::domain_error("grad_reg_inc_gamma",
86 "is not converging", "", "");
87 }
88 return gamma_p(a, z) * ( dig - l ) + exp( a * l ) * S / g;
89 }
90 }
91
92 }
93 }
94 #endif
stan::math::fabs
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
stan
Definition: log_sum_exp.hpp:8
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
square.hpp
is_inf.hpp
gamma_q.hpp
stan::math::square
fvar< T > square(const fvar< T > &x)
Definition: square.hpp:14
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:30
stan::math::is_inf
int is_inf(const fvar< T > &x)
Returns 1 if the input's value is infinite and 0 otherwise.
Definition: is_inf.hpp:21
stan::math::gamma_p
fvar< T > gamma_p(const fvar< T > &x1, const fvar< T > &x2)
Definition: gamma_p.hpp:14
gamma_p.hpp
stan::math::e
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:94
domain_error.hpp
stan::math::grad_reg_inc_gamma
T grad_reg_inc_gamma(T a, T z, T g, T dig, double precision=1e-6)
Gradient of the regularized incomplete gamma functions igamma(a, z)
Definition: grad_reg_inc_gamma.hpp:40
stan::math::gamma_q
fvar< T > gamma_q(const fvar< T > &x1, const fvar< T > &x2)
Definition: gamma_q.hpp:14

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