TMB Documentation  v1.9.11
atomic.hpp
Go to the documentation of this file.
1 /********************************************************************
2  * Special math functions are available in TMB by including this file
3  * in the cpp file.
4  ********************************************************************/
5 
10 template<class Type> Type lgamma(Type x);
11 
12 namespace atomic {
13 
14 #define TINY_AD_USE_TINY_VEC 1
15 #include "tiny_ad/tiny_ad.hpp"
16 #include "mask.hpp"
17 
18 /********************************************************************
19  * Adding 'pbeta'
20  ********************************************************************/
21 #include "beta/pbeta.hpp" // Get namespace 'toms708'
22 TMB_BIND_ATOMIC(pbeta,
23  111,
24  toms708::pbeta(x[0], x[1], x[2], 1, 0) )
25 
26 /********************************************************************
27  * Adding 'bessel_k'
28  ********************************************************************/
29 #include "bessel/bessel.hpp" // Get namespace 'bessel_utils'
30 TMB_BIND_ATOMIC(bessel_k,
31  11,
32  bessel_utils::bessel_k(x[0], x[1], 1.) )
33 
34 /********************************************************************
35  * Adding 'bessel_i'
36  ********************************************************************/
37 #include "bessel/bessel.hpp" // Get namespace 'bessel_utils'
38 TMB_BIND_ATOMIC(bessel_i,
39  11,
40  bessel_utils::bessel_i(x[0], x[1], 1.) )
41 
42 /********************************************************************
43  * Adding 'bessel_j'
44  ********************************************************************/
45 #include "bessel/bessel.hpp" // Get namespace 'bessel_utils'
46 TMB_BIND_ATOMIC(bessel_j,
47  11,
48  bessel_utils::bessel_j(x[0], x[1]) )
49 
50 /********************************************************************
51  * Adding 'bessel_y'
52  ********************************************************************/
53 #include "bessel/bessel.hpp" // Get namespace 'bessel_utils'
54 TMB_BIND_ATOMIC(bessel_y,
55  11,
56  bessel_utils::bessel_y(x[0], x[1]) )
57 
58 /********************************************************************
59  * Adding 'dtweedie'
60  ********************************************************************/
61 #include "tweedie/tweedie.hpp"
62 TMB_BIND_ATOMIC(tweedie_logW,
63  011,
64  tweedie_utils::tweedie_logW(x[0], x[1], x[2]) )
65 
66 /********************************************************************
67  * Adding numerically robust utility functions
68  ********************************************************************/
69 #include "robust/distributions.hpp"
70 TMB_BIND_ATOMIC(log_dnbinom_robust,
71  011,
72  robust_utils::dnbinom_robust(x[0], x[1], x[2], true) )
73 
74 TMB_BIND_ATOMIC(log_dbinom_robust,
75  001,
76  robust_utils::dbinom_robust(x[0], x[1], x[2], true) )
77 
78 TMB_BIND_ATOMIC(logspace_add,
79  11,
80  robust_utils::logspace_add(x[0], x[1]) )
81 
82 TMB_BIND_ATOMIC(logspace_sub,
83  11,
84  robust_utils::logspace_sub(x[0], x[1]) )
85 
86 /********************************************************************
87  * Adding Conway-Maxwell_poisson distribution
88  ********************************************************************/
89 #include "compois/compois.hpp"
90 TMB_BIND_ATOMIC(compois_calc_logZ,
91  11,
92  compois_utils::calc_logZ(x[0], x[1]) )
93 
94 TMB_BIND_ATOMIC(compois_calc_loglambda,
95  11,
96  compois_utils::calc_loglambda(x[0], x[1]) )
97 
98 /********************************************************************
99  * Adding 'qbeta'
100  ********************************************************************/
101 extern "C" double Rf_qbeta(double, double, double, int, int);
102 template <class Type>
103 Type dbeta(Type x, Type shape1, Type shape2) {
104  Type logres =
105  lgamma(shape1 + shape2) - lgamma(shape1) - lgamma(shape2) +
106  (shape1 - 1.) * log(x) + (shape2 - 1.) * log(1. - x);
107  return exp(logres);
108 }
109 TMB_ATOMIC_STATIC_FUNCTION(
110  // ATOMIC_NAME
111  qbeta
112  ,
113  // INPUT_DIM
114  3
115  ,
116  // ATOMIC_DOUBLE
117  ty[0] = Rf_qbeta(tx[0], tx[1], tx[2], 1, 0);
118  ,
119  // ATOMIC_REVERSE
120  Type p = ty[0];
121  Type a = tx[1];
122  Type b = tx[2];
123  Type tmp = atomic::dbeta(p, a, b);
124  px[0] = 1.0 / tmp * py[0];
125  CppAD::vector<Type> arg(4);
126  arg[0] = p;
127  arg[1] = a;
128  arg[2] = b;
129  arg[3] = Type(1); // 1st order partials wrt. a and b
130  CppAD::vector<Type> D_shape = pbeta(arg);
131  px[1] = -D_shape[1] / tmp * py[0];
132  px[2] = -D_shape[2] / tmp * py[0];
133  )
134 
135 } // End namespace atomic
136 
137 
Type qbeta(Type p, Type shape1, Type shape2)
Quantile function of the beta distribution (following R argument convention).
Type pbeta(Type q, Type shape1, Type shape2)
Distribution function of the beta distribution (following R argument convention). ...
Type dbeta(Type x, Type shape1, Type shape2, int give_log)
Probability density function of the beta distribution.
Namespace with special functions and derivatives.
Type lgamma(Type x)
Logarithm of gamma function (following R argument convention).
Definition: lgamma.hpp:12
Type dbinom_robust(Type k, Type size, Type logit_p, int give_log=0)
Density of binomial distribution parameterized via logit(prob)
Type dnbinom_robust(const Type &x, const Type &log_mu, const Type &log_var_minus_mu, int give_log=0)
Negative binomial probability function.
Definition: lgamma.hpp:103
Type compois_calc_loglambda(Type logmean, Type nu)
Conway-Maxwell-Poisson. Calculate log(lambda) from log(mean).
Type logspace_add(Type logx, Type logy)
Addition in log-space.
Type logspace_sub(Type logx, Type logy)
Subtraction in log-space.
Type compois_calc_logZ(Type loglambda, Type nu)
Conway-Maxwell-Poisson log normalizing constant.
License: GPL v2