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