5742
|
1 /* This code is in the public domain */ |
|
2 |
|
3 /* Needs the following defines: |
|
4 * NAN: value to return for Not-A-Number |
|
5 * RUNI: uniform generator on (0,1) |
|
6 * RNOR: normal generator |
|
7 * LGAMMA: log gamma function |
|
8 * INFINITE: function to test whether a value is infinite |
|
9 */ |
|
10 |
|
11 #if defined (HAVE_CONFIG_H) |
|
12 #include <config.h> |
|
13 #endif |
|
14 |
|
15 #include <math.h> |
|
16 #include <stdio.h> |
|
17 |
|
18 #include "f77-fcn.h" |
|
19 #include "lo-ieee.h" |
|
20 #include "lo-error.h" |
|
21 #include "randmtzig.h" |
|
22 #include "randpoisson.h" |
|
23 |
|
24 #undef NAN |
|
25 #define NAN octave_NaN |
6096
|
26 #undef INFINITE |
5742
|
27 #define INFINITE lo_ieee_isinf |
|
28 #define RUNI oct_randu() |
|
29 #define RNOR oct_randn() |
|
30 #define LGAMMA xlgamma |
|
31 |
|
32 F77_RET_T |
|
33 F77_FUNC (dlgams, DLGAMS) (const double *, double *, double *); |
|
34 |
|
35 static double |
|
36 xlgamma (double x) |
|
37 { |
|
38 double result; |
|
39 double sgngam; |
|
40 |
|
41 if (lo_ieee_isnan (x)) |
|
42 result = x; |
|
43 else if (x <= 0 || lo_ieee_isinf (x)) |
|
44 result = octave_Inf; |
|
45 else |
|
46 F77_XFCN (dlgams, DLGAMS, (&x, &result, &sgngam)); |
|
47 |
|
48 return result; |
|
49 } |
|
50 |
|
51 /* ---- pprsc.c from Stadloeber's winrand --- */ |
|
52 |
|
53 #include <math.h> |
|
54 |
|
55 /* flogfak(k) = ln(k!) */ |
|
56 static double |
|
57 flogfak (double k) |
|
58 { |
|
59 #define C0 9.18938533204672742e-01 |
|
60 #define C1 8.33333333333333333e-02 |
|
61 #define C3 -2.77777777777777778e-03 |
|
62 #define C5 7.93650793650793651e-04 |
|
63 #define C7 -5.95238095238095238e-04 |
|
64 |
|
65 static double logfak[30L] = { |
|
66 0.00000000000000000, 0.00000000000000000, 0.69314718055994531, |
|
67 1.79175946922805500, 3.17805383034794562, 4.78749174278204599, |
|
68 6.57925121201010100, 8.52516136106541430, 10.60460290274525023, |
|
69 12.80182748008146961, 15.10441257307551530, 17.50230784587388584, |
|
70 19.98721449566188615, 22.55216385312342289, 25.19122118273868150, |
|
71 27.89927138384089157, 30.67186010608067280, 33.50507345013688888, |
|
72 36.39544520803305358, 39.33988418719949404, 42.33561646075348503, |
|
73 45.38013889847690803, 48.47118135183522388, 51.60667556776437357, |
|
74 54.78472939811231919, 58.00360522298051994, 61.26170176100200198, |
|
75 64.55753862700633106, 67.88974313718153498, 71.25703896716800901 |
|
76 }; |
|
77 |
|
78 double r, rr; |
|
79 |
|
80 if (k >= 30.0) |
|
81 { |
|
82 r = 1.0 / k; |
|
83 rr = r * r; |
|
84 return ((k + 0.5)*log(k) - k + C0 + r*(C1 + rr*(C3 + rr*(C5 + rr*C7)))); |
|
85 } |
|
86 else |
|
87 return (logfak[(int)k]); |
|
88 } |
|
89 |
|
90 |
|
91 /****************************************************************** |
|
92 * * |
|
93 * Poisson Distribution - Patchwork Rejection/Inversion * |
|
94 * * |
|
95 ****************************************************************** |
|
96 * * |
|
97 * For parameter my < 10 Tabulated Inversion is applied. * |
|
98 * For my >= 10 Patchwork Rejection is employed: * |
|
99 * The area below the histogram function f(x) is rearranged in * |
|
100 * its body by certain point reflections. Within a large center * |
|
101 * interval variates are sampled efficiently by rejection from * |
|
102 * uniform hats. Rectangular immediate acceptance regions speed * |
|
103 * up the generation. The remaining tails are covered by * |
|
104 * exponential functions. * |
|
105 * * |
|
106 ****************************************************************** |
|
107 * * |
|
108 * FUNCTION : - pprsc samples a random number from the Poisson * |
|
109 * distribution with parameter my > 0. * |
|
110 * REFERENCE : - H. Zechner (1994): Efficient sampling from * |
|
111 * continuous and discrete unimodal distributions, * |
|
112 * Doctoral Dissertation, 156 pp., Technical * |
|
113 * University Graz, Austria. * |
|
114 * SUBPROGRAM : - drand(seed) ... (0,1)-Uniform generator with * |
|
115 * unsigned long integer *seed. * |
|
116 * * |
|
117 * Implemented by H. Zechner, January 1994 * |
|
118 * Revised by F. Niederl, July 1994 * |
|
119 * * |
|
120 ******************************************************************/ |
|
121 |
|
122 static double |
|
123 f (double k, double l_nu, double c_pm) |
|
124 { |
|
125 return exp(k * l_nu - flogfak(k) - c_pm); |
|
126 } |
|
127 |
|
128 static double |
|
129 pprsc (double my) |
|
130 { |
|
131 static double my_last = -1.0; |
|
132 static double m, k2, k4, k1, k5; |
|
133 static double dl, dr, r1, r2, r4, r5, ll, lr, l_my, c_pm, |
|
134 f1, f2, f4, f5, p1, p2, p3, p4, p5, p6; |
|
135 double Dk, X, Y; |
|
136 double Ds, U, V, W; |
|
137 |
|
138 if (my != my_last) |
|
139 { /* set-up */ |
|
140 my_last = my; |
|
141 /* approximate deviation of reflection points k2, k4 from my - 1/2 */ |
|
142 Ds = sqrt(my + 0.25); |
|
143 |
|
144 /* mode m, reflection points k2 and k4, and points k1 and k5, */ |
|
145 /* which delimit the centre region of h(x) */ |
|
146 m = floor(my); |
|
147 k2 = ceil(my - 0.5 - Ds); |
|
148 k4 = floor(my - 0.5 + Ds); |
|
149 k1 = k2 + k2 - m + 1L; |
|
150 k5 = k4 + k4 - m; |
|
151 |
|
152 /* range width of the critical left and right centre region */ |
|
153 dl = (k2 - k1); |
|
154 dr = (k5 - k4); |
|
155 |
|
156 /* recurrence constants r(k)=p(k)/p(k-1) at k = k1, k2, k4+1, k5+1 */ |
|
157 r1 = my / k1; |
|
158 r2 = my / k2; |
|
159 r4 = my / (k4 + 1.0); |
|
160 r5 = my / (k5 + 1.0); |
|
161 |
|
162 /* reciprocal values of the scale parameters of exp. tail envelope */ |
|
163 ll = log(r1); /* expon. tail left */ |
|
164 lr = -log(r5); /* expon. tail right*/ |
|
165 |
|
166 /* Poisson constants, necessary for computing function values f(k) */ |
|
167 l_my = log(my); |
|
168 c_pm = m * l_my - flogfak(m); |
|
169 |
|
170 /* function values f(k) = p(k)/p(m) at k = k2, k4, k1, k5 */ |
|
171 f2 = f(k2, l_my, c_pm); |
|
172 f4 = f(k4, l_my, c_pm); |
|
173 f1 = f(k1, l_my, c_pm); |
|
174 f5 = f(k5, l_my, c_pm); |
|
175 |
|
176 /* area of the two centre and the two exponential tail regions */ |
|
177 /* area of the two immediate acceptance regions between k2, k4 */ |
|
178 p1 = f2 * (dl + 1.0); /* immed. left */ |
|
179 p2 = f2 * dl + p1; /* centre left */ |
|
180 p3 = f4 * (dr + 1.0) + p2; /* immed. right */ |
|
181 p4 = f4 * dr + p3; /* centre right */ |
|
182 p5 = f1 / ll + p4; /* exp. tail left */ |
|
183 p6 = f5 / lr + p5; /* exp. tail right*/ |
|
184 } |
|
185 |
|
186 for (;;) |
|
187 { |
|
188 /* generate uniform number U -- U(0, p6) */ |
|
189 /* case distinction corresponding to U */ |
|
190 if ((U = RUNI * p6) < p2) |
|
191 { /* centre left */ |
|
192 |
|
193 /* immediate acceptance region |
|
194 R2 = [k2, m) *[0, f2), X = k2, ... m -1 */ |
|
195 if ((V = U - p1) < 0.0) return(k2 + floor(U/f2)); |
|
196 /* immediate acceptance region |
|
197 R1 = [k1, k2)*[0, f1), X = k1, ... k2-1 */ |
|
198 if ((W = V / dl) < f1 ) return(k1 + floor(V/f1)); |
|
199 |
|
200 /* computation of candidate X < k2, and its counterpart Y > k2 */ |
|
201 /* either squeeze-acceptance of X or acceptance-rejection of Y */ |
|
202 Dk = floor(dl * RUNI) + 1.0; |
|
203 if (W <= f2 - Dk * (f2 - f2/r2)) |
|
204 { /* quick accept of */ |
|
205 return(k2 - Dk); /* X = k2 - Dk */ |
|
206 } |
|
207 if ((V = f2 + f2 - W) < 1.0) |
|
208 { /* quick reject of Y*/ |
|
209 Y = k2 + Dk; |
|
210 if (V <= f2 + Dk * (1.0 - f2)/(dl + 1.0)) |
|
211 { /* quick accept of */ |
|
212 return(Y); /* Y = k2 + Dk */ |
|
213 } |
|
214 if (V <= f(Y, l_my, c_pm)) return(Y); /* final accept of Y*/ |
|
215 } |
|
216 X = k2 - Dk; |
|
217 } |
|
218 else if (U < p4) |
|
219 { /* centre right */ |
|
220 /* immediate acceptance region |
|
221 R3 = [m, k4+1)*[0, f4), X = m, ... k4 */ |
|
222 if ((V = U - p3) < 0.0) return(k4 - floor((U - p2)/f4)); |
|
223 /* immediate acceptance region |
|
224 R4 = [k4+1, k5+1)*[0, f5) */ |
|
225 if ((W = V / dr) < f5 ) return(k5 - floor(V/f5)); |
|
226 |
|
227 /* computation of candidate X > k4, and its counterpart Y < k4 */ |
|
228 /* either squeeze-acceptance of X or acceptance-rejection of Y */ |
|
229 Dk = floor(dr * RUNI) + 1.0; |
|
230 if (W <= f4 - Dk * (f4 - f4*r4)) |
|
231 { /* quick accept of */ |
|
232 return(k4 + Dk); /* X = k4 + Dk */ |
|
233 } |
|
234 if ((V = f4 + f4 - W) < 1.0) |
|
235 { /* quick reject of Y*/ |
|
236 Y = k4 - Dk; |
|
237 if (V <= f4 + Dk * (1.0 - f4)/ dr) |
|
238 { /* quick accept of */ |
|
239 return(Y); /* Y = k4 - Dk */ |
|
240 } |
|
241 if (V <= f(Y, l_my, c_pm)) return(Y); /* final accept of Y*/ |
|
242 } |
|
243 X = k4 + Dk; |
|
244 } |
|
245 else |
|
246 { |
|
247 W = RUNI; |
|
248 if (U < p5) |
|
249 { /* expon. tail left */ |
|
250 Dk = floor(1.0 - log(W)/ll); |
|
251 if ((X = k1 - Dk) < 0L) continue; /* 0 <= X <= k1 - 1 */ |
|
252 W *= (U - p4) * ll; /* W -- U(0, h(x)) */ |
|
253 if (W <= f1 - Dk * (f1 - f1/r1)) |
|
254 return(X); /* quick accept of X*/ |
|
255 } |
|
256 else |
|
257 { /* expon. tail right*/ |
|
258 Dk = floor(1.0 - log(W)/lr); |
|
259 X = k5 + Dk; /* X >= k5 + 1 */ |
|
260 W *= (U - p5) * lr; /* W -- U(0, h(x)) */ |
|
261 if (W <= f5 - Dk * (f5 - f5*r5)) |
|
262 return(X); /* quick accept of X*/ |
|
263 } |
|
264 } |
|
265 |
|
266 /* acceptance-rejection test of candidate X from the original area */ |
|
267 /* test, whether W <= f(k), with W = U*h(x) and U -- U(0, 1)*/ |
|
268 /* log f(X) = (X - m)*log(my) - log X! + log m! */ |
|
269 if (log(W) <= X * l_my - flogfak(X) - c_pm) return(X); |
|
270 } |
|
271 } |
|
272 /* ---- pprsc.c end ------ */ |
|
273 |
|
274 |
|
275 /* The remainder of the file is by Paul Kienzle */ |
|
276 |
|
277 /* Given uniform u, find x such that CDF(L,x)==u. Return x. */ |
|
278 static void |
|
279 poisson_cdf_lookup(double lambda, double *p, size_t n) |
|
280 { |
|
281 /* Table size is predicated on the maximum value of lambda |
|
282 * we want to store in the table, and the maximum value of |
|
283 * returned by the uniform random number generator on [0,1). |
|
284 * With lambda==10 and u_max = 1 - 1/(2^32+1), we |
|
285 * have poisson_pdf(lambda,36) < 1-u_max. If instead our |
|
286 * generator uses more bits of mantissa or returns a value |
|
287 * in the range [0,1], then for lambda==10 we need a table |
|
288 * size of 46 instead. For long doubles, the table size |
|
289 * will need to be longer still. */ |
|
290 #define TABLESIZE 46 |
|
291 double t[TABLESIZE]; |
|
292 |
|
293 /* Precompute the table for the u up to and including 0.458. |
|
294 * We will almost certainly need it. */ |
|
295 int intlambda = (int)floor(lambda); |
|
296 double P; |
|
297 int tableidx; |
|
298 size_t i = n; |
|
299 |
|
300 t[0] = P = exp(-lambda); |
|
301 for (tableidx = 1; tableidx <= intlambda; tableidx++) { |
|
302 P = P*lambda/(double)tableidx; |
|
303 t[tableidx] = t[tableidx-1] + P; |
|
304 } |
|
305 |
|
306 while (i-- > 0) { |
|
307 double u = RUNI; |
|
308 |
|
309 /* If u > 0.458 we know we can jump to floor(lambda) before |
|
310 * comparing (this observation is based on Stadlober's winrand |
|
311 * code). For lambda >= 1, this will be a win. Lambda < 1 |
|
312 * is already fast, so adding an extra comparison is not a |
|
313 * problem. */ |
|
314 int k = (u > 0.458 ? intlambda : 0); |
|
315 |
|
316 /* We aren't using a for loop here because when we find the |
|
317 * right k we want to jump to the next iteration of the |
|
318 * outer loop, and the continue statement will only work for |
|
319 * the inner loop. */ |
|
320 nextk: |
|
321 if ( u <= t[k] ) { |
|
322 p[i] = (double) k; |
|
323 continue; |
|
324 } |
|
325 if (++k < tableidx) goto nextk; |
|
326 |
|
327 /* We only need high values of the table very rarely so we |
|
328 * don't automatically compute the entire table. */ |
|
329 while (tableidx < TABLESIZE) { |
|
330 P = P*lambda/(double)tableidx; |
|
331 t[tableidx] = t[tableidx-1] + P; |
|
332 /* Make sure we converge to 1.0 just in case u is uniform |
|
333 * on [0,1] rather than [0,1). */ |
|
334 if (t[tableidx] == t[tableidx-1]) t[tableidx] = 1.0; |
|
335 tableidx++; |
|
336 if (u <= t[tableidx-1]) break; |
|
337 } |
|
338 |
|
339 /* We are assuming that the table size is big enough here. |
|
340 * This should be true even if RUNI is returning values in |
|
341 * the range [0,1] rather than [0,1). |
|
342 */ |
|
343 p[i] = (double)(tableidx-1); |
|
344 } |
|
345 } |
|
346 |
|
347 /* From Press, et al., Numerical Recipes */ |
|
348 static void |
|
349 poisson_rejection (double lambda, double *p, size_t n) |
|
350 { |
|
351 double sq = sqrt(2.0*lambda); |
|
352 double alxm = log(lambda); |
|
353 double g = lambda*alxm - LGAMMA(lambda+1.0); |
|
354 size_t i; |
|
355 |
|
356 for (i = 0; i < n; i++) |
|
357 { |
|
358 double y, em, t; |
|
359 do { |
|
360 do { |
|
361 y = tan(M_PI*RUNI); |
|
362 em = sq * y + lambda; |
|
363 } while (em < 0.0); |
|
364 em = floor(em); |
|
365 t = 0.9*(1.0+y*y)*exp(em*alxm-flogfak(em)-g); |
|
366 } while (RUNI > t); |
|
367 p[i] = em; |
|
368 } |
|
369 } |
|
370 |
|
371 /* The cutoff of L <= 1e8 in the following two functions before using |
|
372 * the normal approximation is based on: |
|
373 * > L=1e8; x=floor(linspace(0,2*L,1000)); |
|
374 * > max(abs(normal_pdf(x,L,L)-poisson_pdf(x,L))) |
|
375 * ans = 1.1376e-28 |
|
376 * For L=1e7, the max is around 1e-9, which is within the step size of RUNI. |
|
377 * For L>1e10 the pprsc function breaks down, as I saw from the histogram |
|
378 * of a large sample, so 1e8 is both small enough and large enough. */ |
|
379 |
|
380 /* Generate a set of poisson numbers with the same distribution */ |
|
381 void |
|
382 oct_fill_randp (double L, octave_idx_type n, double *p) |
|
383 { |
|
384 octave_idx_type i; |
|
385 if (L < 0.0 || INFINITE(L)) |
|
386 { |
|
387 for (i=0; i<n; i++) |
|
388 p[i] = NAN; |
|
389 } |
|
390 else if (L <= 10.0) |
|
391 { |
|
392 poisson_cdf_lookup(L, p, n); |
|
393 } |
|
394 else if (L <= 1e8) |
|
395 { |
|
396 for (i=0; i<n; i++) |
|
397 p[i] = pprsc(L); |
|
398 } |
|
399 else |
|
400 { |
|
401 /* normal approximation: from Phys. Rev. D (1994) v50 p1284 */ |
|
402 const double sqrtL = sqrt(L); |
|
403 for (i = 0; i < L; i++) |
|
404 { |
|
405 p[i] = floor(RNOR*sqrtL + L + 0.5); |
|
406 if (p[i] < 0.0) |
|
407 p[i] = 0.0; /* will probably never happen */ |
|
408 } |
|
409 } |
|
410 } |
|
411 |
|
412 /* Generate one poisson variate */ |
|
413 double |
|
414 oct_randp (double L) |
|
415 { |
|
416 double ret; |
|
417 if (L < 0.0) ret = NAN; |
|
418 else if (L <= 12.0) { |
|
419 /* From Press, et al. Numerical recipes */ |
|
420 double g = exp(-L); |
|
421 int em = -1; |
|
422 double t = 1.0; |
|
423 do { |
|
424 ++em; |
|
425 t *= RUNI; |
|
426 } while (t > g); |
|
427 ret = em; |
|
428 } else if (L <= 1e8) { |
|
429 /* numerical recipes */ |
|
430 poisson_rejection(L, &ret, 1); |
|
431 } else if (INFINITE(L)) { |
5775
|
432 /* FIXME R uses NaN, but the normal approx. suggests that as |
5742
|
433 * limit should be inf. Which is correct? */ |
|
434 ret = NAN; |
|
435 } else { |
|
436 /* normal approximation: from Phys. Rev. D (1994) v50 p1284 */ |
|
437 ret = floor(RNOR*sqrt(L) + L + 0.5); |
|
438 if (ret < 0.0) ret = 0.0; /* will probably never happen */ |
|
439 } |
|
440 return ret; |
|
441 } |
|
442 |
|
443 /* |
|
444 ;;; Local Variables: *** |
|
445 ;;; mode: C *** |
|
446 ;;; End: *** |
|
447 */ |