13 CppAD::vector<Type> tx(2);
25 CppAD::vector<Type> tx(2);
34 inline Type lgamma_approx(
const Type &y)
38 LogRootTwoPi_ = 0.9189385332046727418,
40 0.99999999999980993227684700473478,
41 676.520368121885098567009190444019,
42 -1259.13921672240287047156078755283,
43 771.3234287776530788486528258894,
44 -176.61502916214059906584551354,
45 12.507343278686904814458936853,
46 -0.13857109526572011689554707,
47 9.984369578019570859563e-6,
48 1.50563273514931155834e-7
56 for(k=1; k<=8; k++) { Ag += lanczos_7_c[k]/(x+k); }
58 term1 = (x+Type(0.5))*log((x+Type(7.5))/Type(M_E));
59 term2 = LogRootTwoPi_ + log(Ag);
60 return term1 + (term2 - Type(7.0));
69 inline Type
dnbinom(
const Type &x,
const Type &size,
const Type &prob,
75 n*log(p)+x*log(Type(1)-p);
76 if (give_log)
return logres;
else return exp(logres);
86 inline Type
dnbinom2(
const Type &x,
const Type &mu,
const Type &var,
90 Type n=mu*p/(Type(1)-p);
105 const Type &log_var_minus_mu,
108 CppAD::vector<Type> tx(4);
111 tx[2] = log_var_minus_mu;
113 Type ans = atomic::log_dnbinom_robust(tx)[0];
114 return ( give_log ? ans : exp(ans) );
123 inline Type
dpois(
const Type &x,
const Type &lambda,
int give_log=0)
125 Type logres = -lambda + x*log(lambda) -
lgamma(x+Type(1));
126 if (give_log)
return logres;
else return exp(logres);
134 Type
dgamma(Type y, Type shape, Type scale,
int give_log=0)
136 Type logres=-
lgamma(shape)+(shape-Type(1.0))*log(y)-y/scale-shape*log(scale);
137 if(give_log)
return logres;
else return exp(logres);
145 inline Type
dlgamma(Type y, Type shape, Type scale,
int give_log=0)
147 Type logres=-
lgamma(shape)-shape*log(scale)-exp(y)/scale+shape*y;
148 if(give_log)
return logres;
else return exp(logres);
158 inline Type
dzipois(
const Type &x,
const Type &lambda,
const Type &zip,
int give_log=0)
161 if (x==Type(0)) logres=log(zip + (Type(1)-zip)*
dpois(x, lambda,
false));
162 else logres=log(Type(1)-zip) +
dpois(x, lambda,
true);
163 if (give_log)
return logres;
else return exp(logres);
175 inline Type
dzinbinom(
const Type &x,
const Type &size,
const Type &p,
const Type & zip,
179 if (x==Type(0)) logres=log(zip + (Type(1)-zip)*
dnbinom(x, size, p,
false));
180 else logres=log(Type(1)-zip) +
dnbinom(x, size, p,
true);
181 if (give_log)
return logres;
else return exp(logres);
192 inline Type
dzinbinom2(
const Type &x,
const Type &mu,
const Type &var,
const Type & zip,
196 Type n=mu*p/(Type(1)-p);
205 double Rf_rnbinom(
double n,
double p);
211 return Rf_rnbinom(asDouble(n), asDouble(p));
221 Type n = mu * p / (Type(1) - p);
222 return Rf_rnbinom(asDouble(n), asDouble(p));
#define VECTORIZE2_tt(FUN)
Vectorize 2-argument functions.
Type rnbinom(Type n, Type p)
Simulate from a negative binomial distribution.
#define VECTORIZE2_n(FUN)
Add the 'n' integer argument to a simulation method with two arguments.
Type dpois(const Type &x, const Type &lambda, int give_log=0)
Poisson probability function.
#define VECTORIZE1_t(FUN)
Vectorize 1-argument functions.
Type dlgamma(Type y, Type shape, Type scale, int give_log=0)
Density of log(X) where X~gamma distributed.
Type lfactorial(Type x)
Logarithm of factorial function (following R argument convention).
Type dgamma(Type y, Type shape, Type scale, int give_log=0)
Density of X where X~gamma distributed.
Type dzinbinom2(const Type &x, const Type &mu, const Type &var, const Type &zip, int give_log=0)
Zero-Inflated negative binomial probability function.
Type lgamma(Type x)
Logarithm of gamma function (following R argument convention).
#define VECTORIZE4_ttti(FUN)
Vectorize 4-argument functions.
Type dnbinom(const Type &x, const Type &size, const Type &prob, int give_log=0)
Negative binomial probability function.Parameterized through size and prob parameters, following R-convention.
Type dnbinom_robust(const Type &x, const Type &log_mu, const Type &log_var_minus_mu, int give_log=0)
Negative binomial probability function.
Type dnbinom2(const Type &x, const Type &mu, const Type &var, int give_log=0)
Negative binomial probability function.Alternative parameterization through mean and variance paramet...
Type dzipois(const Type &x, const Type &lambda, const Type &zip, int give_log=0)
Zero-Inflated Poisson probability function.
Type dzinbinom(const Type &x, const Type &size, const Type &p, const Type &zip, int give_log=0)
Zero-Inflated negative binomial probability function.
Type rnbinom2(Type mu, Type var)
Simulate from a negative binomial distribution.
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 ...
#define VECTORIZE3_tti(FUN)
Vectorize 3-argument functions.