12 return ( logx < logy ?
13 logy + log1p (exp (logx - logy)) :
14 logx + log1p (exp (logy - logx)) );
18 T R_Log1_Exp (
const T &x) {
19 return ((x) > -M_LN2 ? log(-expm1(x)) : log1p(-exp(x))) ;
23 return logx + R_Log1_Exp(logy - logx);
46 template <
class Float>
49 const Float &log_var_minus_mu,
53 Float log_var =
logspace_add( log_mu, log_var_minus_mu );
54 Float log_p = log_mu - log_var;
55 Float log_n = 2 * log_mu - log_var_minus_mu;
57 Float logres = n * log_p;
59 Float log_1mp = log_var_minus_mu - log_var;
62 return ( give_log ? logres : exp(logres) );
81 Float
dbinom_robust(Float k, Float size, Float logit_p,
int give_log=0)
86 Float logres = k * log_p + (size-k) * log_1mp;
87 return ( give_log ? logres : exp(logres) );
Type lgamma(Type x)
Logarithm of gamma function (following R argument convention).
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.
Type logspace_add(Type logx, Type logy)
Addition in log-space.
Type logspace_sub(Type logx, Type logy)
Subtraction in log-space.