12 Type
pnorm(Type q, Type mean = 0., Type sd = 1.){
13 CppAD::vector<Type> tx(1);
14 tx[0] = (q - mean) / sd;
24 Type
qnorm(Type p, Type mean = 0., Type sd = 1.){
25 CppAD::vector<Type> tx(1);
36 Type
pgamma(Type q, Type shape, Type scale = 1.){
37 CppAD::vector<Type> tx(4);
50 Type
qgamma(Type q, Type shape, Type scale = 1.){
51 CppAD::vector<Type> tx(3);
63 Type
ppois(Type q, Type lambda){
64 CppAD::vector<Type> tx(2);
80 Type
pexp(Type x, Type rate)
82 return CppAD::CondExpGe(x,Type(0),1-exp(-rate*x),Type(0));
94 Type
dexp(Type x, Type rate,
int give_log=0)
97 return CppAD::CondExpGe(x,Type(0),rate*exp(-rate*x),Type(0));
99 return CppAD::CondExpGe(x,Type(0),log(rate)-rate*x,Type(-INFINITY));
109 template <
class Type>
112 return -log(1-p)/rate;
132 return CppAD::CondExpGe(x,Type(0),1-exp(-pow(x/scale,shape)),Type(0));
145 Type
dweibull(Type x, Type shape, Type scale,
int give_log=0)
148 return CppAD::CondExpGe(x,Type(0),shape/scale * pow(x/scale,shape-1) * exp(-pow(x/scale,shape)),Type(0));
150 return CppAD::CondExpGe(x,Type(0),log(shape) - log(scale) + (shape-1)*(log(x)-log(scale)) - pow(x/scale,shape),Type(-INFINITY));
165 Type res = scale * pow( (-log(1-p)) , 1/shape );
166 res = CppAD::CondExpLt(p,Type(0),Type(0),res);
167 res = CppAD::CondExpGt(p,Type(1),Type(0),res);
183 Type
dbinom(Type k, Type size, Type prob,
int give_log=0)
187 logres += CppAD::CondExpGt(k, Type(0), k * log(prob), Type(0) );
189 logres += CppAD::CondExpGt(size, k, (size - k) * log(1 - prob), Type(0) );
190 if (!give_log)
return exp(logres);
207 CppAD::vector<Type> tx(4);
212 Type ans = atomic::log_dbinom_robust(tx)[0];
216 return ( give_log ? ans : exp(ans) );
226 template <
class Type>
227 Type
dbeta(Type x, Type shape1, Type shape2,
int give_log)
229 Type res = exp(
lgamma(shape1+shape2) -
lgamma(shape1) -
lgamma(shape2)) * pow(x,shape1-1) * pow(1-x,shape2-1);
233 return CppAD::CondExpEq(x,Type(0),log(res),
lgamma(shape1+shape2) -
lgamma(shape1) -
lgamma(shape2) + (shape1-1)*log(x) + (shape2-1)*log(1-x));
245 template <
class Type>
246 Type
df(Type x, Type df1, Type df2,
int give_log)
248 Type logres =
lgamma((df1+df2)/2.) -
lgamma(df1/2.) -
lgamma(df2/2.) + df1/2.*log(Type(df1)/df2) + (df1/2.-1)*log(x) - (df1+df2)/2.*log(1+Type(df1)/df2*x);
249 if(!give_log)
return exp(logres);
262 template <
class Type>
263 Type
dlogis(Type x, Type location, Type scale,
int give_log)
265 Type logres = -(x-location)/scale - log(scale) - 2*log(1+exp(-(x-location)/scale));
266 if(!give_log)
return exp(logres);
278 template <
class Type>
279 Type
dsn(Type x, Type alpha,
int give_log=0)
282 if(!give_log)
return 2 *
dnorm(x,Type(0),Type(1),0) *
pnorm(alpha*x);
283 else return log(2.0) + log(
dnorm(x,Type(0),Type(1),0)) + log(
pnorm(alpha*x));
294 template <
class Type>
295 Type
dt(Type x, Type
df,
int give_log)
297 Type logres =
lgamma((df+1)/2) - Type(1)/2*log(df*M_PI) -
lgamma(df/2) - (df+1)/2*log(1+x*x/df);
298 if(!give_log)
return exp(logres);
311 template <
class Type>
315 Type logres =
lgamma(x.sum() + Type(1)) -
lgamma(xp1).sum() + (x*log(p)).
sum();
316 if(give_log)
return logres;
317 else return exp(logres);
338 template <
class Type>
339 Type
dSHASHo(Type x, Type mu, Type sigma, Type nu, Type tau,
int give_log = 0)
343 Type z = (x-mu)/sigma;
344 Type c = cosh(tau*log(z+sqrt(z*z+1))-nu);
345 Type r = sinh(tau*log(z+sqrt(z*z+1))-nu);
346 Type logres = -log(sigma) + log(tau) -0.5*log(2*M_PI) -0.5*log(1+(z*z)) +log(c) -0.5*(r*r);
348 if(!give_log)
return exp(logres);
367 template <
class Type>
368 Type
pSHASHo(Type q,Type mu,Type sigma,Type nu,Type tau,
int give_log=0)
372 Type z = (q-mu)/sigma;
373 Type r = sinh(tau * log(z+sqrt(z*z+1)) - nu);
376 if (!give_log)
return p;
395 template <
class Type>
396 Type
qSHASHo(Type p, Type mu, Type sigma, Type nu, Type tau,
int log_p = 0)
400 if(!log_p)
return mu + sigma*sinh((1/tau)* log(
qnorm(p)+sqrt(
qnorm(p)*
qnorm(p)+1)) + (nu/tau));
401 else return mu + sigma*sinh((1/tau)*log(
qnorm(exp(p))+sqrt(
qnorm(exp(p))*
qnorm(exp(p))+1))+(nu/tau));
416 template <
class Type>
417 Type
norm2SHASHo(Type x, Type mu, Type sigma, Type nu, Type tau,
int log_p = 0)
433 Type
pbeta(Type q, Type shape1, Type shape2){
434 CppAD::vector<Type> tx(4);
450 Type
qbeta(Type p, Type shape1, Type shape2){
451 CppAD::vector<Type> tx(3);
467 if(CppAD::Variable(nu)) {
468 CppAD::vector<Type> tx(3);
472 ans = atomic::bessel_k(tx)[0];
474 CppAD::vector<Type> tx(2);
490 if(CppAD::Variable(nu)) {
491 CppAD::vector<Type> tx(3);
495 ans = atomic::bessel_i(tx)[0];
497 CppAD::vector<Type> tx(2);
512 CppAD::vector<Type> tx(3);
516 Type ans = atomic::bessel_j(tx)[0];
527 CppAD::vector<Type> tx(3);
531 Type ans = atomic::bessel_y(tx)[0];
554 Type
dtweedie(Type y, Type mu, Type phi, Type p,
int give_log = 0) {
555 Type p1 = p - 1.0, p2 = 2.0 - p;
556 Type ans = -pow(mu, p2) / (phi * p2);
557 if (y > 0 || CppAD::Variable(y)) {
558 CppAD::vector<Type> tx(4);
563 Type ans2 = atomic::tweedie_logW(tx)[0];
564 ans2 += -y / (phi * p1 * pow(mu, p1)) - log(y);
565 if (CppAD::Variable(y)) {
566 ans += CppAD::CondExpGt(y, Type(0), ans2, Type(0));
571 return ( give_log ? ans : exp(ans) );
587 CppAD::vector<Type> tx(3);
605 CppAD::vector<Type> tx(3);
626 template<
class T1,
class T2,
class T3>
627 T1
dcompois(T1 x, T2 mode, T3 nu,
int give_log = 0) {
628 T2 loglambda = nu * log(mode);
631 return ( give_log ? ans : exp(ans) );
646 template<
class T1,
class T2,
class T3>
648 T2 logmean = log(mean);
652 return ( give_log ? ans : exp(ans) );
660 double Rf_rnorm(
double mu,
double sigma);
666 return Rf_rnorm(asDouble(mu), asDouble(sigma));
672 double Rf_rpois(
double mu);
678 return Rf_rpois(asDouble(mu));
684 double Rf_runif(
double a,
double b);
690 return Rf_runif(asDouble(a), asDouble(b));
696 double Rf_rbinom(
double size,
double prob);
702 return Rf_rbinom(asDouble(size), asDouble(prob));
708 double Rf_rgamma(
double shape,
double scale);
714 return Rf_rgamma(asDouble(shape), asDouble(scale));
720 double Rf_rexp(
double rate);
726 return Rf_rexp(asDouble(rate));
733 double Rf_rbeta(
double shape1,
double shape2);
737 Type
rbeta(Type shape1, Type shape2)
739 return Rf_rbeta(asDouble(shape1), asDouble(shape2));
746 double Rf_rf(
double df1,
double df2);
750 Type
rf(Type df1, Type df2)
752 return Rf_rf(asDouble(df1), asDouble(df2));
759 double Rf_rlogis(
double location,
double scale);
765 return Rf_rlogis(asDouble(location), asDouble(scale));
772 double Rf_rt(
double df);
778 return Rf_rt(asDouble(df));
785 double Rf_rweibull(
double shape,
double scale);
791 return Rf_rweibull(asDouble(shape), asDouble(scale));
801 Type loglambda = nu * log(mode);
802 return atomic::compois_utils::simulate(asDouble(loglambda), asDouble(nu));
811 Type logmean = log(mean);
813 return atomic::compois_utils::simulate(asDouble(loglambda), asDouble(nu));
822 Type logmean = log(mean);
824 Type mode = exp(loglambda / nu);
832 Type lambda = pow(mu, 2. - p) / (phi * (2. - p));
833 Type alpha = (2. - p) / (1. - p);
834 Type gam = phi * (p - 1.) * pow(mu, p - 1.);
835 Type N =
rpois(lambda);
836 Type ans =
rgamma( -alpha * N , gam );
#define VECTORIZE2_tt(FUN)
Vectorize 2-argument functions.
Type rexp(Type rate)
Simulate from an exponential distribution.
Type rcompois2(Type mean, Type nu)
Simulate from a Conway-Maxwell-Poisson distribution.
Type qbeta(Type p, Type shape1, Type shape2)
Quantile function of the beta distribution (following R argument convention).
Vector class used by TMB.
Type pbeta(Type q, Type shape1, Type shape2)
Distribution function of the beta distribution (following R argument convention). ...
CppAD::vector< Type > pnorm1(CppAD::vector< Type > x)
Atomic version of standard normal distribution function. Derivative is known to be 'dnorm1'...
Type ppois(Type q, Type lambda)
Distribution function of the poisson distribution (following R argument convention).
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...
Type pSHASHo(Type q, Type mu, Type sigma, Type nu, Type tau, int give_log=0)
Cumulative distribution function of the sinh-asinh distribution.
Type rcompois(Type mode, Type nu)
Simulate from a Conway-Maxwell-Poisson distribution.
#define VECTORIZE2_n(FUN)
Add the 'n' integer argument to a simulation method with two arguments.
#define VECTORIZE3_n(FUN)
Add the 'n' integer argument to a simulation method with three arguments.
Type dSHASHo(Type x, Type mu, Type sigma, Type nu, Type tau, int give_log=0)
Probability density function of the sinh-asinh distribution.
Type qnorm(Type p, Type mean=0., Type sd=1.)
Quantile function of the normal distribution (following R argument convention).
#define VECTORIZE1_t(FUN)
Vectorize 1-argument functions.
Type rtweedie(Type mu, Type phi, Type p)
Simulate from tweedie distribution.
Type qgamma(Type q, Type shape, Type scale=1.)
Quantile function of the gamma distribution (following R argument convention).
Type dbeta(Type x, Type shape1, Type shape2, int give_log)
Probability density function of the beta distribution.
CppAD::vector< Type > bessel_k_10(CppAD::vector< Type > x)
Atomic version of . Valid parameter range: .
Type df(Type x, Type df1, Type df2, int give_log)
Probability density function of the Fisher distribution.
Type dbinom(Type k, Type size, Type prob, int give_log=0)
Probability mass function of the binomial distribution.
Type dlogis(Type x, Type location, Type scale, int give_log)
Probability density function of the logistic distribution.
Type rlogis(Type location, Type scale)
Simulate from a logistic distribution.
Type dtweedie(Type y, Type mu, Type phi, Type p, int give_log=0)
dtweedie function (same as dtweedie.series from R package 'tweedie').
Type rf(Type df1, Type df2)
Simulate from an F distribution.
Type dnorm(Type x, Type mean, Type sd, int give_log=0)
Probability density function of the normal distribution.
Type lfactorial(Type x)
Logarithm of factorial function (following R argument convention).
Type besselY(Type x, Type nu)
besselY function (same as besselY from R).
Type dmultinom(vector< Type > x, vector< Type > p, int give_log=0)
Probability mass function of the multinomial distribution.
Type besselI(Type x, Type nu)
besselI function (same as besselI from R).
Type dexp(Type x, Type rate, int give_log=0)
Probability density function of the exponential distribution.
Type lgamma(Type x)
Logarithm of gamma function (following R argument convention).
#define VECTORIZE6_ttttti(FUN)
Vectorize 6-argument functions.
#define VECTORIZE4_ttti(FUN)
Vectorize 4-argument functions.
Type dbinom_robust(Type k, Type size, Type logit_p, int give_log=0)
Density of binomial distribution parameterized via logit(prob)
Type rweibull(Type shape, Type scale)
Simulate from a Weibull distribution.
#define VECTORIZE5_tttti(FUN)
Vectorize 5-argument functions.
Type norm2SHASHo(Type x, Type mu, Type sigma, Type nu, Type tau, int log_p=0)
Transforms a normal variable into a sinh-asinh variable.
Type pweibull(Type x, Type shape, Type scale)
Cumulative distribution function of the Weibull distribution.
Type qexp(Type p, Type rate)
Inverse cumulative distribution function of the exponential distribution.
Type dt(Type x, Type df, int give_log)
Probability density function of the Student t-distribution.
Type pexp(Type x, Type rate)
Cumulative distribution function of the exponential distribution.
CppAD::vector< Type > bessel_i_10(CppAD::vector< Type > x)
Atomic version of . Valid parameter range: .
Type besselK(Type x, Type nu)
besselK function (same as besselK from R).
T1 dcompois(T1 x, T2 mode, T3 nu, int give_log=0)
Conway-Maxwell-Poisson. Calculate density.
Type compois_calc_loglambda(Type logmean, Type nu)
Conway-Maxwell-Poisson. Calculate log(lambda) from log(mean).
#define VECTORIZE1_n(FUN)
Add the 'n' integer argument to a simulation method with one argument.
Type rpois(Type mu)
Simulate from a Poisson distribution.
Type rt(Type df)
Simulate from a Student's t distribution.
CppAD::vector< Type > qnorm1(CppAD::vector< Type > x)
Atomic version of standard normal quantile function. Derivative is expressed through 'dnorm1'...
Type dsn(Type x, Type alpha, int give_log=0)
Probability density function of the skew-normal distribution.
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: .
Type rgamma(Type shape, Type scale)
Simulate from a gamma distribution.
CppAD::vector< Type > ppois(CppAD::vector< Type > x)
Atomic version of poisson cdf . Valid parameter range: .
Type dweibull(Type x, Type shape, Type scale, int give_log=0)
Probability density function of the Weibull distribution.
Type sum(Vector< Type > x)
Type rbinom(Type size, Type prob)
Simulate from a binomial distribution.
Type rbeta(Type shape1, Type shape2)
Simulate from a beta distribution.
Type rnorm(Type mu, Type sigma)
Simulate from a normal distribution.
Type pnorm(Type q, Type mean=0., Type sd=1.)
Distribution function of the normal distribution (following R argument convention).
Type qweibull(Type p, Type shape, Type scale)
Inverse cumulative distribution function of the Weibull distribution.
Type besselJ(Type x, Type nu)
besselJ function (same as besselJ from R).
Type runif(Type a, Type b)
Simulate from a uniform distribution.
#define VECTORIZE3_tti(FUN)
Vectorize 3-argument functions.
Type compois_calc_logZ(Type loglambda, Type nu)
Conway-Maxwell-Poisson log normalizing constant.
T1 dcompois2(T1 x, T2 mean, T3 nu, int give_log=0)
Conway-Maxwell-Poisson. Calculate density parameterized via the mean.
#define VECTORIZE3_ttt(FUN)
Vectorize 3-argument functions.
Type qSHASHo(Type p, Type mu, Type sigma, Type nu, Type tau, int log_p=0)
Quantile function of the sinh-asinh distribution.
Type pgamma(Type q, Type shape, Type scale=1.)
Distribution function of the gamma distribution (following R argument convention).