29 #if defined (HAVE_CONFIG_H)
44 static double xlgamma (
double x)
55 #define C0 9.18938533204672742e-01
56 #define C1 8.33333333333333333e-02
57 #define C3 -2.77777777777777778e-03
58 #define C5 7.93650793650793651e-04
59 #define C7 -5.95238095238095238e-04
61 static double logfak[30L] =
63 0.00000000000000000, 0.00000000000000000, 0.69314718055994531,
64 1.79175946922805500, 3.17805383034794562, 4.78749174278204599,
65 6.57925121201010100, 8.52516136106541430, 10.60460290274525023,
66 12.80182748008146961, 15.10441257307551530, 17.50230784587388584,
67 19.98721449566188615, 22.55216385312342289, 25.19122118273868150,
68 27.89927138384089157, 30.67186010608067280, 33.50507345013688888,
69 36.39544520803305358, 39.33988418719949404, 42.33561646075348503,
70 45.38013889847690803, 48.47118135183522388, 51.60667556776437357,
71 54.78472939811231919, 58.00360522298051994, 61.26170176100200198,
72 64.55753862700633106, 67.88974313718153498, 71.25703896716800901
81 return ((k + 0.5)*std::log (k) - k +
C0
85 return (logfak[
static_cast<int> (k)]);
120 f (
double k,
double l_nu,
double c_pm)
122 return exp (k * l_nu - flogfak (k) - c_pm);
128 static double my_last = -1.0;
129 static double m, k2, k4, k1, k5;
130 static double dl, dr, r1, r2, r4, r5, ll, lr, l_my, c_pm,
131 f1, f2, f4, f5, p1, p2, p3, p4, p5, p6;
140 Ds = std::sqrt (my + 0.25);
145 k2 =
ceil (my - 0.5 - Ds);
147 k1 = k2 + k2 -
m + 1L;
157 r4 = my / (k4 + 1.0);
158 r5 = my / (k5 + 1.0);
165 l_my = std::log (my);
166 c_pm =
m * l_my - flogfak (
m);
169 f2 =
f (k2, l_my, c_pm);
170 f4 =
f (k4, l_my, c_pm);
171 f1 =
f (k1, l_my, c_pm);
172 f5 =
f (k5, l_my, c_pm);
176 p1 = f2 * (dl + 1.0);
178 p3 = f4 * (dr + 1.0) + p2;
194 if ((
V = U - p1) < 0.0)
return (k2 +
std::floor (U/f2));
197 if ((W =
V / dl) < f1 )
return (k1 +
std::floor (
V/f1));
202 if (W <= f2 - Dk * (f2 - f2/r2))
207 if ((
V = f2 + f2 - W) < 1.0)
211 if (
V <= f2 + Dk * (1.0 - f2)/(dl + 1.0))
216 if (
V <=
f (Y, l_my, c_pm))
return (Y);
225 if ((
V = U - p3) < 0.0)
return (k4 -
std::floor ((U - p2)/f4));
228 if ((W =
V / dr) < f5 )
return (k5 -
std::floor (
V/f5));
233 if (W <= f4 - Dk * (f4 - f4*r4))
238 if ((
V = f4 + f4 - W) < 1.0)
242 if (
V <= f4 + Dk * (1.0 - f4)/ dr)
247 if (
V <=
f (Y, l_my, c_pm))
return (Y);
258 if ((X = k1 - Dk) < 0L)
continue;
260 if (W <= f1 - Dk * (f1 - f1/r1))
269 if (W <= f5 - Dk * (f5 - f5*r5))
277 if (std::log (W) <= X * l_my - flogfak (X) - c_pm)
return (X);
297 template <
typename T>
299 poisson_cdf_lookup (
double lambda, T *p, std::size_t
n)
305 int intlambda =
static_cast<int> (
std::floor (lambda));
310 t[0] = P = exp (-lambda);
311 for (tableidx = 1; tableidx <= intlambda; tableidx++)
313 P = P*lambda/
static_cast<double> (tableidx);
314 t[tableidx] = t[tableidx-1] + P;
326 int k = (u > 0.458 ? intlambda : 0);
335 p[i] =
static_cast<T
> (k);
345 P = P*lambda/
static_cast<double> (tableidx);
346 t[tableidx] = t[tableidx-1] + P;
349 if (t[tableidx] == t[tableidx-1]) t[tableidx] = 1.0;
351 if (u <= t[tableidx-1])
break;
357 p[i] =
static_cast<T
> (tableidx-1);
362 template <
typename T>
364 poisson_rejection (
double lambda, T *p, std::size_t
n)
366 double sq = std::sqrt (2.0*lambda);
367 double alxm = std::log (lambda);
368 double g = lambda*alxm - xlgamma (lambda+1.0);
371 for (i = 0; i <
n; i++)
379 em = sq * y + lambda;
383 t = 0.9*(1.0+y*y)* exp (em*alxm-flogfak (em)-g);
401 template <
typename T>
414 poisson_cdf_lookup<T> (L, p,
n);
424 const double sqrtL = std::sqrt (L);
425 for (i = 0; i <
n; i++)
427 p[i] =
std::floor (rand_normal<T> () * sqrtL + L + 0.5);
438 template <
typename T>
454 t *= rand_uniform<T> ();
462 poisson_rejection<T> (L, &ret, 1);
473 ret =
std::floor (rand_normal<T> () * std::sqrt (L) + L + 0.5);
474 if (ret < 0.0) ret = 0.0;
482 OCTAVE_END_NAMESPACE(
octave)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
F77_RET_T const F77_INT const F77_INT const F77_INT const F77_DBLE const F77_DBLE F77_INT F77_DBLE * V
std::complex< T > floor(const std::complex< T > &x)
std::complex< T > ceil(const std::complex< T > &x)
F77_RET_T const F77_DBLE * x
F77_RET_T const F77_DBLE const F77_DBLE * f
double rand_uniform< double >()
void rand_poisson(T L_arg, octave_idx_type n, T *p)
template void rand_poisson< double >(double, octave_idx_type, double *)
template void rand_poisson< float >(float, octave_idx_type, float *)