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