46 double Rf_pnorm5(
double,
double,
double,
int,
int);
47 double Rf_qnorm5(
double,
double,
double,
int,
int);
48 double Rf_ppois(
double,
double,
int,
int);
49 double Rf_bessel_k(
double,
double,
double);
50 double Rf_bessel_i(
double,
double,
double);
51 double Rf_pgamma(
double,
double,
double,
int,
int);
52 double Rf_qgamma(
double,
double,
double,
int,
int);
53 double Rf_lgammafn(
double);
54 double Rf_psigamma(
double,
double);
55 double Rf_fmin2(
double,
double);
57 typedef void integr_fn(
double *x,
int n,
void *ex);
58 void Rdqags(integr_fn f,
void *ex,
double *a,
double *b,
59 double *epsabs,
double *epsrel,
60 double *result,
double *abserr,
int *neval,
int *ier,
61 int *limit,
int *lenw,
int *last,
int *iwork,
double *work);
62 void Rdqagi(integr_fn f,
void *ex,
double *bound,
int *inf,
63 double *epsabs,
double *epsrel,
64 double *result,
double *abserr,
int *neval,
int *ier,
65 int *limit,
int *lenw,
int *last,
66 int *iwork,
double *work);
72 void integrand_D_incpl_gamma_shape(
double *x,
int nx,
void *ex);
77 void integrand_D_incpl_gamma_shape(
double *x,
int nx,
void *ex){
78 double* parms=(
double*)ex;
79 double shape = parms[0];
81 double logc = parms[2];
82 for(
int i=0; i<nx; i++){
83 x[i] = exp( -exp(x[i]) + shape * x[i] + logc ) * pow(x[i], n);
89 return exp(logc + Rf_lgammafn(shape)) * Rf_pgamma(x, shape, 1.0, 1, 0);
101 int* iwork = (
int*)malloc(limit *
sizeof(
int));
102 double* work = (
double*)malloc(lenw *
sizeof(
double));
109 bound = log(Rf_fmin2(x,shape));
111 Rdqagi(integrand_D_incpl_gamma_shape, ex, &bound, &inf,
113 &result1, &abserr, &neval, &ier,
114 &limit, &lenw, &last, iwork, work);
117 Rf_warning(
"incpl_gamma (indef) integrate unreliable: x=%f shape=%f n=%f ier=%i", x, shape, n, ier);
125 Rdqags(integrand_D_incpl_gamma_shape, ex, &a, &b,
127 &result2, &abserr, &neval, &ier,
128 &limit, &lenw, &last, iwork, work);
131 Rf_warning(
"incpl_gamma (def) integrate unreliable: x=%f shape=%f n=%f ier=%i", x, shape, n, ier);
137 return result1 + result2;
140 double logp = log(y) - Rf_lgammafn(shape) - logc;
142 return Rf_qgamma(exp(logp), shape, scale, 1, 0);
145 double D_lgamma(
double x,
double n){
146 if(n<.5)
return Rf_lgammafn(x);
147 else return Rf_psigamma(x,n-1.0);
149 #endif // #ifdef WITH_LIBTMB 153 #ifdef CPPAD_FRAMEWORK 154 #include "atomic_macro.hpp" 157 #ifdef TMBAD_FRAMEWORK 158 #include "tmbad_atomic_macro.hpp" 167 typedef Eigen::Map<Eigen::Matrix<Type,Eigen::Dynamic,Eigen::Dynamic> > MapMatrix;
168 typedef Eigen::Map<const Eigen::Matrix<Type,Eigen::Dynamic,Eigen::Dynamic> > ConstMapMatrix;
169 typedef Eigen::LDLT<Eigen::Matrix<Type, Eigen::Dynamic, Eigen::Dynamic> > LDLT;
179 typename TypeDefs<Type>::ConstMapMatrix vec2mat(
const CppAD::vector<Type> &x,
int m,
int n,
int offset=0){
180 typedef typename TypeDefs<Type>::ConstMapMatrix ConstMapMatrix_t;
181 ConstMapMatrix_t res(&x[offset], m, n);
191 CppAD::vector<Type> res(n);
192 for(
int i=0;i<n;i++)res[i]=x(i);
201 return Type(1.0/sqrt(2.0*M_PI)) * exp(-Type(.5)*x*x);
209 TMB_ATOMIC_STATIC_FUNCTION(
217 ty[0] = Rmath::Rf_pnorm5(tx[0],0,1,1,0);
220 px[0] = dnorm1(tx[0]) * py[0];
228 TMB_ATOMIC_STATIC_FUNCTION(
235 ty[0] = Rmath::Rf_qnorm5(tx[0],0,1,1,0);
238 px[0] = Type(1) / dnorm1(ty[0]) * py[0];
251 TMB_ATOMIC_STATIC_FUNCTION(
259 ty[0]=Rmath::D_incpl_gamma_shape(tx[0],tx[1],tx[2],tx[3]);
262 px[0] = exp( -tx[0] + (tx[1]-Type(1.0)) * log(tx[0]) + tx[3] ) * pow(log(tx[0]),tx[2]) * py[0];
266 tx_[2] = tx[2] + Type(1.0);
270 px[3] = ty[0] * py[0];
284 TMB_ATOMIC_STATIC_FUNCTION(
292 ty[0]=Rmath::inv_incpl_gamma(tx[0],tx[1],tx[2]);
298 Type tmp = exp(-value+logc)*pow(value,shape-Type(1));
299 px[0] = 1.0 / tmp * py[0];
317 TMB_ATOMIC_STATIC_FUNCTION(
325 ty[0]=Rmath::D_lgamma(tx[0],tx[1]);
330 tx_[1]=tx[1]+Type(1.0);
341 TMB_ATOMIC_STATIC_FUNCTION(
349 ty[0]=Rmath::Rf_ppois(tx[0],tx[1],1,0);
356 arg[0] = n - Type(1);
359 px[1] = (-value +
ppois(arg)) * py[0];
368 TMB_ATOMIC_STATIC_FUNCTION(
376 ty[0] = Rmath::Rf_bessel_k(tx[0], tx[1], 1.0 );
384 arg[1] = nu + Type(1);
385 px[0] = ( -
bessel_k_10(arg) + value * (nu / x) ) * py[0];
395 TMB_ATOMIC_STATIC_FUNCTION(
403 ty[0] = Rmath::Rf_bessel_i(tx[0], tx[1], 1.0 );
410 arg[1] = nu + Type(1);
412 arg[1] = nu - Type(1);
414 px[0] = Type(0.5) * ( B_left + B_right ) * py[0];
440 CppAD::Integer(tx[0]) * CppAD::Integer(tx[1])
443 typedef TypeDefs<double>::MapMatrix MapMatrix_t;
444 typedef TypeDefs<double>::ConstMapMatrix ConstMapMatrix_t;
445 int n1 = CppAD::Integer(tx[0]);
446 int n3 = CppAD::Integer(tx[1]);
447 int n2 = ( n1 + n3 > 0 ? (tx.size() - 2) / (n1 + n3) : 0 );
448 ConstMapMatrix_t X(&tx[2 ], n1, n2);
449 ConstMapMatrix_t Y(&tx[2+n1*n2], n2, n3);
450 MapMatrix_t Z(&ty[0 ], n1, n3);
454 typedef typename TypeDefs<Type>::MapMatrix MapMatrix_t;
455 int n1 = CppAD::Integer(tx[0]);
456 int n3 = CppAD::Integer(tx[1]);
457 int n2 = ( n1 + n3 > 0 ? (tx.size() - 2) / (n1 + n3) : 0 );
459 matrix<Type> Yt = vec2mat(tx, n2, n3, 2 + n1*n2).transpose();
461 MapMatrix_t res1(&px[2 ], n1, n2);
462 MapMatrix_t res2(&px[2+n1*n2], n2, n3);
465 px[0] = 0; px[1] = 0;
481 typedef TypeDefs<double>::MapMatrix MapMatrix_t;
482 typedef TypeDefs<double>::ConstMapMatrix ConstMapMatrix_t;
483 int n = sqrt((
double)tx.size());
484 ConstMapMatrix_t X(&tx[0], n, n);
485 MapMatrix_t Y(&ty[0], n, n);
489 typedef typename TypeDefs<Type>::MapMatrix MapMatrix_t;
490 int n = sqrt((
double)ty.size());
491 MapMatrix_t res(&px[0], n, n);
511 int n=sqrt((
double)tx.size());
515 double res=LUdiag.abs().log().sum();
519 CppAD::vector<Type> invX =
matinv(tx);
520 for(
size_t i=0; i<tx.size(); i++) px[i] = invX[i] * py[0];
536 typedef TypeDefs<double>::LDLT LDLT_t;
537 using namespace Eigen;
538 int n=sqrt((
double)tx.size());
544 vector<double> D = ldlt.vectorD();
545 double logdetX = D.log().sum();
547 for(
int i=0;i<n*n;i++)ty[i+1]=iX(i);
550 int n=sqrt((
double)tx.size());
584 CppAD::vector<Type> arg(2+x.size()+y.size());
585 arg[0] = x.rows(); arg[1] = y.cols();
586 for(
int i=0;i<x.size();i++){arg[2+i]=x(i);}
587 for(
int i=0;i<y.size();i++){arg[2+i+x.size()]=y(i);}
588 CppAD::vector<Type> res(x.rows()*y.cols());
590 return vec2mat(res,x.rows(),y.cols());
613 return vec2mat(
matinv(mat2vec(x)),n,n);
626 CppAD::vector<Type> res =
invpd(mat2vec(x));
628 return vec2mat(res,n,n,1);
636 return logdet(mat2vec(x))[0];
645 Type logdetQ = -
logdet(Sigma);
646 Type quadform = (x*(Q*x)).sum();
647 return -Type(.5)*logdetQ + Type(.5)*quadform + x.size()*Type(log(sqrt(2.0*M_PI)));
652 #include "checkpoint_macro.hpp" Vector class used by TMB.
CppAD::vector< Type > pnorm1(CppAD::vector< Type > x)
Atomic version of standard normal distribution function. Derivative is known to be 'dnorm1'...
CppAD::vector< Type > inv_incpl_gamma(CppAD::vector< Type > x)
Atomic version of inverse of scaled incomplete gamma function. Given find such that where the 3 in...
CppAD::vector< Type > bessel_k_10(CppAD::vector< Type > x)
Atomic version of . Valid parameter range: .
Namespace with special functions and derivatives.
Matrix class used by TMB.
CppAD::vector< Type > matinv(CppAD::vector< Type > x)
Atomic version of matrix inversion. Inverts n-by-n matrix by LU-decomposition.
CppAD::vector< Type > bessel_i_10(CppAD::vector< Type > x)
Atomic version of . Valid parameter range: .
CppAD::vector< Type > matmul(CppAD::vector< Type > x)
Atomic version of matrix multiply. Multiplies n1-by-n2 matrix with n2-by-n3 matrix.
CppAD::vector< Type > qnorm1(CppAD::vector< Type > x)
Atomic version of standard normal quantile function. Derivative is expressed through 'dnorm1'...
CppAD::vector< Type > invpd(CppAD::vector< Type > x)
Atomic version of log determinant and inverse of positive definite n-by-n matrix. Calculated by Chole...
CppAD::vector< Type > D_incpl_gamma_shape(CppAD::vector< Type > x)
Atomic version of scaled incomplete gamma function differentiated to any order wrt. shape parameter where the 4 input parameters are passed as a vector . Note that the normalized incomplete gamma function is obtained as the special case and . Valid parameter range: .
CppAD::vector< Type > ppois(CppAD::vector< Type > x)
Atomic version of poisson cdf . Valid parameter range: .
#define TMB_ATOMIC_VECTOR_FUNCTION( ATOMIC_NAME, OUTPUT_DIM, ATOMIC_DOUBLE, ATOMIC_REVERSE)
Construct atomic vector function based on known derivatives.
CppAD::vector< Type > D_lgamma(CppAD::vector< Type > x)
Atomic version of the n'th order derivative of the log gamma function. where the 2 input parameters ...
matrix< Type > matinvpd(matrix< Type > x, Type &logdet)
Matrix inverse and determinant.
CppAD::vector< Type > logdet(CppAD::vector< Type > x)
Atomic version of log determinant of positive definite n-by-n matrix.