4 #define TWEEDIE_DROP 37.0 6 #define TWEEDIE_INCRE 5 8 #define TWEEDIE_NTERM 20000 19 Float tweedie_logW(Float y, Float phi, Float p){
20 bool ok = (0 < y) && (0 < phi) && (1 < p) && (p < 2);
23 Float p1 = p - 1.0, p2 = 2.0 - p;
24 Float a = - p2 / p1, a1 = 1.0 / p1;
25 Float cc, w, sum_ww = 0.0;
26 double ww_max = -INFINITY ;
35 cc = a * log(p1) - log(p2);
36 jmax = asDouble( fmax2(1.0, pow(y, p2) / (phi * p2)) );
37 logz = - a * log(y) - a1 * log(phi) + cc;
41 cc = logz + a1 + a * log(-a);
46 if (j * (cc - a1 * log(j)) < (w - TWEEDIE_DROP))
54 if (j < 1 || j * (cc - a1 * log(j)) < w - TWEEDIE_DROP)
57 jl = std::fmax(1., floor(j)) ;
61 int nterms = (int) std::fmin(jd, TWEEDIE_NTERM) ;
62 std::vector<Float> ww(nterms);
66 int iterm = (int) std::fmin(jd, nterms) ;
67 for (
int k = 0; k < iterm; k++) {
70 ww_max = std::fmax( ww_max, asDouble(ww[k]) );
72 for (
int k = 0; k < iterm; k++)
73 sum_ww += exp(ww[k] - ww_max);
74 Float ans = log(sum_ww) + ww_max ;
Type lgamma(Type x)
Logarithm of gamma function (following R argument convention).