Stan Math Library  2.6.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
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 
5 #include <cmath>
6 
7 namespace stan {
8  namespace math {
9 
10  // Gradient of the regularized incomplete gamma functions igamma(a, g)
11  // Precomputed values
12  // g = boost::math::tgamma(a)
13  // dig = boost::math::digamma(a)
14  template<typename T>
15  T grad_reg_inc_gamma(T a, T z, T g, T dig, T precision = 1e-6) {
16  using boost::math::isinf;
17  using stan::math::gamma_p;
18  using std::domain_error;
19  using std::exp;
20  using std::fabs;
21  using std::log;
22 
23  T S = 0;
24  T s = 1;
25  T l = log(z);
26  int k = 0;
27  T delta = s / (a * a);
28  while (fabs(delta) > precision) {
29  S += delta;
30  ++k;
31  s *= - z / k;
32  delta = s / ((k + a) * (k + a));
33  if (isinf(delta))
34  throw domain_error("stan::math::gradRegIncGamma not converging");
35  }
36  return gamma_p(a, z) * ( dig - l ) + exp( a * l ) * S / g;
37  }
38 
39  }
40 }
41 #endif
int isinf(const stan::math::var &a)
Checks if the given number is infinite.
Definition: std_isinf.hpp:18
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:14
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
T grad_reg_inc_gamma(T a, T z, T g, T dig, T precision=1e-6)
bool isinf(const stan::math::var &v)
Checks if the given number is infinite.
Definition: boost_isinf.hpp:22
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
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.
fvar< T > gamma_p(const fvar< T > &x1, const fvar< T > &x2)
Definition: gamma_p.hpp:15
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:95

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