29 #if defined (HAVE_CONFIG_H)
54 #define C0 9.18938533204672742e-01
55 #define C1 8.33333333333333333e-02
56 #define C3 -2.77777777777777778e-03
57 #define C5 7.93650793650793651e-04
58 #define C7 -5.95238095238095238e-04
60 static double logfak[30L] =
62 0.00000000000000000, 0.00000000000000000, 0.69314718055994531,
63 1.79175946922805500, 3.17805383034794562, 4.78749174278204599,
64 6.57925121201010100, 8.52516136106541430, 10.60460290274525023,
65 12.80182748008146961, 15.10441257307551530, 17.50230784587388584,
66 19.98721449566188615, 22.55216385312342289, 25.19122118273868150,
67 27.89927138384089157, 30.67186010608067280, 33.50507345013688888,
68 36.39544520803305358, 39.33988418719949404, 42.33561646075348503,
69 45.38013889847690803, 48.47118135183522388, 51.60667556776437357,
70 54.78472939811231919, 58.00360522298051994, 61.26170176100200198,
71 64.55753862700633106, 67.88974313718153498, 71.25703896716800901
80 return ((k + 0.5)*std::log (k) - k +
C0
84 return (logfak[
static_cast<int> (k)]);
118 static double f (
double k,
double l_nu,
double c_pm)
120 return exp (k * l_nu -
flogfak (k) - c_pm);
125 static double my_last = -1.0;
126 static double m, k2, k4, k1, k5;
127 static double dl, dr, r1, r2, r4, r5, ll, lr, l_my, c_pm,
128 f1, f2, f4, f5, p1, p2, p3, p4, p5, p6;
136 Ds = std::sqrt (my + 0.25);
141 k2 =
ceil (my - 0.5 - Ds);
143 k1 = k2 + k2 -
m + 1L;
153 r4 = my / (k4 + 1.0);
154 r5 = my / (k5 + 1.0);
161 l_my = std::log (my);
165 f2 =
f (k2, l_my, c_pm);
166 f4 =
f (k4, l_my, c_pm);
167 f1 =
f (k1, l_my, c_pm);
168 f5 =
f (k5, l_my, c_pm);
172 p1 = f2 * (dl + 1.0);
174 p3 = f4 * (dr + 1.0) + p2;
189 if ((
V = U - p1) < 0.0)
return (k2 +
std::floor (U/f2));
192 if ((W =
V / dl) < f1 )
return (k1 +
std::floor (
V/f1));
197 if (W <= f2 - Dk * (f2 - f2/r2))
201 if ((
V = f2 + f2 - W) < 1.0)
204 if (
V <= f2 + Dk * (1.0 - f2)/(dl + 1.0))
208 if (
V <=
f (Y, l_my, c_pm))
return (Y);
216 if ((
V = U - p3) < 0.0)
return (k4 -
std::floor ((U - p2)/f4));
219 if ((W =
V / dr) < f5 )
return (k5 -
std::floor (
V/f5));
224 if (W <= f4 - Dk * (f4 - f4*r4))
228 if ((
V = f4 + f4 - W) < 1.0)
231 if (
V <= f4 + Dk * (1.0 - f4)/ dr)
235 if (
V <=
f (Y, l_my, c_pm))
return (Y);
245 if ((X = k1 - Dk) < 0L)
continue;
247 if (W <= f1 - Dk * (f1 - f1/r1))
255 if (W <= f5 - Dk * (f5 - f5*r5))
263 if (std::log (W) <= X * l_my -
flogfak (X) - c_pm)
return (X);
283 template <
typename T>
291 int intlambda =
static_cast<int> (
std::floor (lambda));
296 t[0] = P = exp (-lambda);
297 for (tableidx = 1; tableidx <= intlambda; tableidx++)
299 P = P*lambda/
static_cast<double> (tableidx);
300 t[tableidx] = t[tableidx-1] + P;
312 int k = (u > 0.458 ? intlambda : 0);
321 p[i] =
static_cast<T
> (k);
331 P = P*lambda/
static_cast<double> (tableidx);
332 t[tableidx] = t[tableidx-1] + P;
335 if (t[tableidx] == t[tableidx-1]) t[tableidx] = 1.0;
337 if (u <= t[tableidx-1])
break;
343 p[i] =
static_cast<T
> (tableidx-1);
348 template <
typename T>
352 double sq = std::sqrt (2.0*lambda);
353 double alxm = std::log (lambda);
354 double g = lambda*alxm -
xlgamma (lambda+1.0);
357 for (i = 0; i <
n; i++)
365 em = sq * y + lambda;
368 t = 0.9*(1.0+y*y)*exp (em*alxm-
flogfak (em)-g);
396 poisson_cdf_lookup<T> (L, p,
n);
406 const double sqrtL = std::sqrt (L);
407 for (i = 0; i <
n; i++)
409 p[i] =
std::floor (rand_normal<T> () * sqrtL + L + 0.5);
434 t *= rand_uniform<T> ();
441 poisson_rejection<T> (L, &ret, 1);
452 ret =
std::floor (rand_normal<T> () * std::sqrt (L) + L + 0.5);
453 if (ret < 0.0) ret = 0.0;
F77_RET_T const F77_INT const F77_INT const F77_INT const F77_DBLE const F77_DBLE F77_INT F77_DBLE * V
F77_RET_T const F77_DBLE * x
std::complex< T > ceil(const std::complex< T > &x)
std::complex< T > floor(const std::complex< T > &x)
static double pprsc(double my)
static double f(double k, double l_nu, double c_pm)
static void poisson_cdf_lookup(double lambda, T *p, size_t n)
template void rand_poisson< float >(float, octave_idx_type, float *)
template void rand_poisson< double >(double, octave_idx_type, double *)
static void poisson_rejection(double lambda, T *p, size_t n)
static double flogfak(double k)
static double xlgamma(double x)
void rand_poisson(T L_arg, octave_idx_type n, T *p)
double rand_uniform< double >(void)