Fix some issues with make dist-gzip
[maxima.git] / share / distrib / numdistrib.lisp
blob80e5b8298c67d8b268958e0a9c7a23d83d1298c5
1 ;; COPYRIGHT NOTICE
2 ;;
3 ;; Copyright (C) 2005-2009 Mario Rodriguez Riotorto
4 ;;
5 ;; This program is free software; you can redistribute
6 ;; it and/or modify it under the terms of the
7 ;; GNU General Public License as published by
8 ;; the Free Software Foundation; either version 2
9 ;; of the License, or (at your option) any later version.
10 ;;
11 ;; This program is distributed in the hope that it
12 ;; will be useful, but WITHOUT ANY WARRANTY;
13 ;; without even the implied warranty of MERCHANTABILITY
14 ;; or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details at
16 ;; http://www.gnu.org/copyleft/gpl.html
18 ;; This is a set of numerical routines used by package distrib.mac
20 ;; For questions, suggestions, bugs and the like, feel free
21 ;; to contact me at
22 ;; mario @@@ edu DOT xunta DOT es
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;; ;;
28 ;; Incomplete gamma and beta ;;
29 ;; and related functions ;;
30 ;; ;;
31 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
34 ;; Natural logarithm of the gamma function. This function is called from
35 ;; some numerical routines below.
36 (defun lngamma (p)
37 (simplify (list '(%log_gamma) p)))
40 ;; Natural logarithm of the beta function
41 ;; Conditions: 0<p,q<1.0e302
42 (defun lnbeta (p q)
43 (+ (lngamma p) (lngamma q) (- (lngamma (+ p q)))) )
46 ;; Lower regularized gamma incomplete. This function is called from
47 ;; some numerical routines below.
48 (defun igamma (x p)
49 (- 1.0 (simplify (list '(%gamma_incomplete_regularized) p x))) )
52 ;; Returns incomplete beta regularized in float format.
53 ;; Conditions: 0<=x<=1; a, b>0
54 (defun ibeta (x a b &aux ($numer t))
55 (declare (type flonum x a b))
56 (simplify (list '(%beta_incomplete_regularized) a b x)))
59 ;; Inverse of the incomplete beta function.
60 ;; Reference:
61 ;; algorithm as 109 appl. statist. (1977), vol.26, no.1
62 ;; Comments: Translated from Fortran.
63 ;; Conditions: 0<x<1; p,q>0
64 (defun iibeta (x p q)
65 (declare (type flonum x p q))
66 (let (fpu a pp qq indx r y s tt h w beta
67 yprev sq tx prev iex acu xin g adj xiibeta
68 (sae -300.0) (zero 0.0) (one 1.0) (two 2.0) (three 3.0)
69 (four 4.0) (five 5.0) (six 6.0))
70 (setf beta (lnbeta p q)
71 fpu (expt 10.0 sae)
72 xiibeta x)
74 ;; change tail if necessary
75 (if (<= x 0.5)
76 (setf a x pp p qq q indx nil)
77 (setf a (- one x) pp q qq p indx t))
79 ;; calculate the initial approximation
80 (setf r (sqrt (- (log (* a a))))
81 y (+ r
83 (/ (+ 2.30753 (* 0.27061 r))
84 (+ one (* (+ 0.99229 (* 0.04481 r)) r))))))
86 (cond ((and (> pp one) (> qq one))
87 (setf r (/ (+ (* y y) (- three)) six))
88 (setf s (/ one (+ pp pp (- one))))
89 (setf tt (/ one (+ qq qq (- one))))
90 (setf h (/ two (+ s tt)))
91 (setf w (+ (/ (* y (sqrt (+ h r))) h)
93 (* (- tt s)
94 (+ r (/ five six) (/ (- two) (* three h)))))))
95 (setf xiibeta (/ pp (+ pp (* qq (exp (+ w w)))))))
96 (t (setf r (+ qq qq))
97 (setf tt (/ one (* 9.0 qq)))
98 (setf tt (* r (expt (+ one (- tt) (* y (sqrt tt))) 3)) )
99 (cond ((<= tt zero)
100 (setf xiibeta (- one (exp (/ (+ (log (* (- one a) qq)) beta) qq)))))
101 (t (setf tt (/ (+ (* four pp) r (- two)) tt))
102 (cond ((<= tt one)
103 (setf xiibeta (exp (/ (+ (log (* a pp)) beta) pp))))
104 (t (setf xiibeta (+ one (- (/ two (+ tt one)))))))))))
106 ;; solve for x by a modified newton-raphson method,
107 ;; using the function ibeta
108 (setf r (- one pp))
109 (setf tt (- one qq))
110 (setf yprev zero)
111 (setf sq one)
112 (setf prev one)
113 (if (< xiibeta 1.0e-4) (setf xiibeta 1.0e-4))
114 (if (> xiibeta 0.9999) (setf xiibeta 0.9999))
115 (setf iex (max
116 (+ (/ -5.0 (expt pp 2)) (- (/ one (expt a 0.2))) -13.0)
117 sae))
118 (setf acu (expt 10.0 iex))
119 (tagbody
120 loop7
121 (setf y (ibeta xiibeta pp qq))
122 (setf xin xiibeta)
123 (setf y (* (- y a)
124 (exp
125 (+ beta
126 (* r (log xin))
127 (* tt (log (- one xin)))))))
128 (if (<= (* y yprev) zero) (setf prev (max sq fpu)))
129 (setf g one)
130 loop9
131 (setf adj (* g y))
132 (setf sq (* adj adj))
133 (if (>= sq prev) (go loop10))
134 (setf tx (- xiibeta adj))
135 (if (and (>= tx zero) (<= tx one)) (go loop11))
136 loop10
137 (setf g (/ g three))
138 (go loop9)
139 loop11
140 (if (<= prev acu) (go loop12))
141 (if (<= (* y y) acu) (go loop12))
142 (if (or (= tx zero) (= tx one)) (go loop10))
143 (if (= tx xiibeta) (go loop12))
144 (setf xiibeta tx)
145 (setf yprev y)
146 (go loop7)
147 loop12)
148 (if indx (- one xiibeta) xiibeta)) )
151 ;; Inverse of the incomplete gamma function.
152 ;; Comments: solves by the partition method the
153 ;; equation g(x)=0, where g(x)=igamma(x,p)-y
154 ;; This procedure is accurate about 14 significant
155 ;; digits except for small regions for x is in the vicinity of 0 and 1.
156 ;; Conditions: 0<x<1; p>0
157 (defun iigamma (x p)
158 (declare (type flonum x p))
159 (let (a b m (ga 100.0) (gb 100.0) (gm 100.0) (err 1.0e-16))
160 (declare (type flonum ga gb gm err))
161 (do ((i 0 (1+ i)))
162 ((<= (* ga gb) 0.0) '$done)
163 (setf a (- (expt 1.5 i) 1.0))
164 (setf b (- (expt 1.5 (+ i 1)) 1.0))
165 (setf ga (- (igamma a p) x))
166 (setf gb (- (igamma b p) x)))
167 ;; now we know the solution is in [a, b]
168 (cond ((< (abs ga) err) a)
169 ((< (abs gb) err) b)
170 (t (loop (setf m (/ (+ a b) 2))
171 (if (or (= m a) (= m b)) (return m))
172 (setf gm (- (igamma m p) x))
173 (if (< (abs gm) err) (return m))
174 (if (<= (* ga gm) 0.0)
175 (setf b m gb gm)
176 (setf a m ga gm))))) ))
179 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
180 ;; ;;
181 ;; Numerical routines for the noncentral chi2 ;;
182 ;; ;;
183 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
186 ;; Cumulative probability of the noncentral chi-square distribution.
187 ;; Reference:
188 ;; Ding, C. G. (1992)
189 ;; Algorithm AS275: Computing the non-central chi-squared
190 ;; distribution function. Appl.Statist., 41, 478-482.
191 ;; Comments: This is a translation of the C code in the R program.
192 ;; Conditions: x>0; f>0 (degrees of freedom) ; theta>0 (non centrality param.)
193 (defun cdfnchi2 (x f theta errmax reltol itrmax)
194 (declare (type flonum x f theta errmax reltol itrmax))
195 (cond
196 ((< theta 80)
197 (let ((sum 0.0)
198 (lamda (* 0.5 theta)))
199 (declare (type flonum sum lamda))
200 (do* ((i 0 (1+ i))
201 (pr (exp (- lamda)) (* pr (/ lamda i))))
202 ((= i 100) 'done)
203 (setf sum (+ sum (* pr (igamma (* 0.5 x) (+ (* 0.5 f) i))))) )
204 sum))
206 (let* ((dbl_epsilon 1.77635683940025e-15)
207 (dbl_min_exp (* (log 2.0) -1021.0)) ; ln(2) * DBL_MIN_EXP
208 (lam (* 0.5 theta))
209 (lamsml (< (- lam) dbl_min_exp))
210 (lu -1.0)
211 (l_lam -1.0)
212 (l_x -1.0)
213 (u 1.0) (v 1.0) (x2 1.0) (f2 1.0) (f_x_2n 1.0) (tt 1.0)
214 (lt 1.0) tsml (ans 1.0) (term 1.0) is_b is_r is_it n (f_2n 1.0) (bound 1.0) )
215 (declare (type flonum dbl_epsilon dbl_min_exp lam lu l_lam l_x u
216 v x2 f2 f_x_2n tt lt l_x ans term f_2n bound)
217 (type boolean lamsml tsml is_b is_r is_it) )
218 (cond
219 (lamsml
220 ; non centrality parameter too large
221 (setf u 0.0
222 lu (- lam)
223 l_lam (log lam)))
225 (setf u (exp (- lam)))))
227 ; evaluate the first term
228 (setf v u
229 x2 (* 0.5 x)
230 f2 (* 0.5 f)
231 f_x_2n (- f x) )
232 (setf tt (- x2 f2))
233 (cond
234 ((and (> (* f2 dbl_epsilon) 0.125) ; very large f and x ~= f: probably needs
235 (< (abs tt) ; other algorithm
236 (* (sqrt dbl_epsilon) f2)))
237 ; evade cancellation error
238 (setf lt (- (* (- 1 tt) (- 2 (/ tt (+ f2 1))))
239 (* 0.5 (log (* 2 ($float '$%pi) (+ f2 1.0)))))))
241 ; Usual case 2: careful not to overflow
242 (setf lt (- (* f2 (log x2))
244 (lngamma (+ f2 1.0))))))
245 (setf tsml (< lt dbl_min_exp))
246 (cond
247 ((and tsml
248 (> x (+ f theta (* 5 (sqrt (* 2 (+ f (* 2 theta))))))))
249 ; x > E[X] + 5* sigma(X)
250 (setf ans 1.0))
252 (cond
253 (tsml
254 (setf l_x (log x)
255 ans 0.0
256 term 0.0
257 tt 0.0))
259 (setf tt (exp lt))
260 (setf ans (* v tt))
261 (setf term ans)))
262 (setf is_b nil
263 is_r nil
264 is_it nil
266 f_2n (+ f 2.0)
267 f_x_2n (+ f_x_2n 2.0))
268 (loop
269 ;f_2n = f + 2*n
270 ;f_x_2n = f - x + 2*n > 0 <=> (f+2n) > x
271 (when (> f_x_2n 0)
272 ; find the error bound and check for convergence
273 (setf bound (/ (* tt x) f_x_2n))
274 (setf is_b (<= bound errmax))
275 (setf is_r (<= term (* reltol ans)))
276 (setf is_it (> n itrmax))
277 ; convergence only if BOTH absolute and relative error < 'bnd'
278 (when (or (and is_b is_r) is_it)
279 (return) ))
280 ; evaluate the next term of the
281 ; expansion and then the partial sum
282 (cond
283 (lamsml
284 (setf lu (+ lu l_lam (- (log n))))
285 (when (>= lu dbl_min_exp)
286 ; no underflow anymore => change regime
287 (setf v (exp lu)) ; the first non-0 'u'
288 (setf u v)
289 (setf lamsml nil)))
291 (setf u (* u (/ lam n)))
292 (setf v (+ v u))))
293 (cond
294 (tsml
295 (setf lt (+ lt l_x (- (log f_2n))))
296 (when (>= lt dbl_min_exp)
297 (setf tt (exp lt)) ; the first non-0 'tt'
298 (setf tsml nil)))
300 (setf tt (* tt (/ x f_2n)))))
301 (when (and (not lamsml) (not tsml))
302 (setf term (* v tt))
303 (setf ans (+ ans term)))
304 (incf n)
305 (setf f_2n (+ f_2n 2.0))
306 (setf f_x_2n (+ f_x_2n 2.0)) ) ; loop
307 (when is_it
308 ($print (format nil "Warning: cdf_noncentral_chi2 not converged in ~a iterations" itrmax)))
309 ans)) ))) )
312 ;; Quantiles of the noncentral chi-square distribution.
313 ;; Method: First calculate suboptimal Pearson's approximation, then
314 ;; estimate an interval and apply bipartite method.
315 ;; Comments: This is a translation of the C code in the R program.
316 ;; Conditions: 0<p<1; f>0 (degrees of freedom) ; theta>0 (non centrality param.)
317 (defun qnchi2 (p df lambda)
318 (let ((accu 1e-13)
319 (racc (* 8 +flonum-epsilon+))
320 (dbl_epsilon (* 8 +flonum-epsilon+))
321 (dbl_min +least-positive-normalized-flonum+)
322 (dbl_max +most-positive-flonum+)
323 (eps 1e-11) ; must be > accu
324 (reps 1e-10) ; relative tolerance
325 (ux 1.0) (lx 1.0) (nx 1.0) (pp 1.0))
326 (declare (type flonum accu eps reps ux lx nx pp dbl_epsilon dbl_min dbl_max))
328 ; This is Pearson's (1959) approximation,
329 ; which is usually good to 4 figs or so.
330 (let (b c ff)
331 (setf b (/ (* lambda lambda)
332 (+ df (* 3.0 lambda)) ))
333 (setf c (/ (+ df (* 3.0 lambda))
334 (+ df (* 2.0 lambda))))
335 (setf ff (/ (+ df (* 2.0 lambda))
336 (* c c)))
337 (setf ux (+ b (* c 2.0 (iigamma p (* 0.5 ff)))))
338 (when (< ux 0.0) (setf ux 1.0)))
340 ; finding an upper and lower bound
341 (cond
342 ((> p (- 1 dbl_epsilon)) ; probability near 1
343 '$inf)
345 (setf pp (min (- 1 dbl_epsilon)
346 (* p (+ 1 eps))))
347 (loop
348 (when (or (>= ux dbl_max)
349 (>= (cdfnchi2 ux df lambda eps reps 10000.0) pp))
350 (return))
351 (setf ux (* 2.0 ux)))
352 (setf pp (* p (- 1 eps)))
353 (setf lx (min ux dbl_max))
354 (loop
355 (when (or (<= lx dbl_min)
356 (<= (cdfnchi2 lx df lambda eps reps 10000.0) pp))
357 (return))
358 (setf lx (* 0.5 lx)))
360 ; interval (lx,ux) halving
361 (loop
362 (setf nx (* 0.5 (+ lx ux)))
363 (if (> (cdfnchi2 nx df lambda accu racc 100000.0) p)
364 (setf ux nx)
365 (setf lx nx))
366 (when (<= (/ (- ux lx) nx) accu)
367 (return)))
368 (* 0.5 (+ ux lx)) )) ))
372 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
373 ;; ;;
374 ;; Numerical routines for the noncentral t ;;
375 ;; ;;
376 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
379 ;; Cumulative probability of the noncentral Student's t distribution.
380 ;; Reference:
381 ;; Algorithm AS243: Appl.Statist., Lenth,R.V. (1989), vol. 38, no. 1
382 ;; Conditions: df>0 (degrees of freedom)
383 (defun cdfnt (x df delta)
384 (declare (type flonum x df delta))
385 (let ((alnrpi 0.57236494292470008707) ; log(sqrt(%pi))
386 (r2pi 0.79788456080286535588) ; sqrt(2/%pi)
387 (r2 1.41421356237309504880) ; sqrt(2)
388 (errmax 1.e-12)
389 (itrmax 1000)
390 (negdel nil)
391 (it 1)
392 (tnc 0.0)
393 a albeta b del errbd geven godd lambda p q rxb s tt xx xeven xodd)
394 (setf tt x
395 del delta)
396 (when (< x 0.0)
397 (setf negdel t
398 tt (- tt)
399 del (- del)))
400 ; initialize twin series
401 ; Guenther, J. (1978). Statist. Computn. Simuln. vol.6, 199.
402 (setf xx (/ (* x x) (+ (* x x) df)))
403 (when (> xx 0.0)
404 (setf lambda (* del del))
405 (setf p (* 0.5 (exp (* lambda -0.5))))
406 (setf q (* r2pi p del))
407 (setf s (- 0.5 p)
408 a 0.5
409 b (* 0.5 df))
410 (setf rxb (expt (- 1.0 xx) b)
411 albeta (+ alnrpi
412 (lngamma b)
413 (- (lngamma (+ a b)))))
414 (setf xodd (ibeta xx a b)
415 godd (* 2.0 rxb (exp (- (* a (log xx)) albeta)))
416 xeven (- 1.0 rxb)
417 geven (* b xx rxb))
418 (setf tnc (+ (* p xodd) (* q xeven)))
419 ; repeat until convergence or iteration limit
420 (loop
421 (setf a (+ a 1.0))
422 (setf xodd (- xodd godd)
423 xeven (- xeven geven)
424 godd (/ (* godd xx (+ a b -1.0)) a)
425 geven (/ (* geven xx (+ a b -0.5)) (+ a 0.5))
426 p (/ (* p lambda) (* 2.0 it))
427 q (/ (* q lambda) (+ (* 2.0 it) 1.0)))
428 (setf s (- s p)
429 tnc (+ tnc (* p xodd) (* q xeven))
430 it (+ it 1))
431 (setf errbd (* 2.0 s (- xodd godd)))
432 (when (or (< errbd errmax) (> it itrmax))
433 (return)) ))
434 (cond
435 ((> it itrmax)
436 ($print "Warning: cdf_noncentral_student_t didn't converge")
437 ($funmake 'cdf_noncentral_student_t '((mlist) x df delta)) )
439 (setf tnc (+ tnc ($float (+ 0.5 (* 0.5 ($erf (/ (- del) r2)))))))
440 (if negdel
441 (- 1.0 tnc)
442 tnc)))))
446 ;; Quantiles for the noncentral Student's t distribution
447 ;; by the inverse method. Translation of file qnt.c from R.
448 ;; Conditions: 0<=p<=1, df>0 (degrees of freedom)
449 (defun qnct (p df ncp)
450 (declare (type flonum p df ncp))
451 (let ((accu 1.0e-13)
452 (eps 1.0e-11)
453 (dbl_max most-positive-double-float) ; DBL_MAX
454 (dbl_epsilon double-float-epsilon) ; DBL_EPSILON
455 ux lx nx pp)
457 ; 1. finding an upper and lower bound
458 (setf pp (min (- 1.0 dbl_epsilon)
459 (* p (+ 1.0 eps))))
460 (setf ux (max 1.0 ncp))
461 (loop
462 (when (or (>= ux dbl_max)
463 (>= (cdfnt ux df ncp) pp))
464 (return 'done))
465 (setf ux (* ux 2)))
466 (setf pp (* p (- 1.0 eps)))
467 (setf lx (min -1.0 (- ncp)))
468 (loop
469 (when (or (<= lx (- dbl_max))
470 (<= (cdfnt lx df ncp) pp))
471 (return 'done))
472 (setf lx (* lx 2)))
474 ; 2. interval (lx,ux) halving
475 (loop
476 (setf nx (* 0.5 (+ lx ux)))
477 (if (> (cdfnt nx df ncp) p)
478 (setf ux nx)
479 (setf lx nx))
480 (when (<= (- ux lx) accu)
481 (return 'done)))
482 (* 0.5 (+ lx ux))))
486 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
487 ;; ;;
488 ;; Normal random simulation ;;
489 ;; ;;
490 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
493 ;; Generates random normal variates, with mean=0 and var=1,
494 ;; using the Box-Mueller method.
495 ;; Reference:
496 ;; Knuth, D.E. (1981) Seminumerical Algorithms.
497 ;; The Art of Computer Programming. Addison-Wesley.
498 (defvar *rnormal-iset* 0) ;; this flag indicates whether there is a second random variate
499 (defvar *rnormal-gset*) ;; stores the second random variate, if any
500 (defun rndnormal-box ()
501 (let (v1 v2 rsq fac)
502 (cond ((= *rnormal-iset* 0)
503 (loop (setf v1 (- (* 2.0 ($random 1.0)) 1.0))
504 (setf v2 (- (* 2.0 ($random 1.0)) 1.0))
505 (setf rsq (+ (* v1 v1) (* v2 v2)))
506 (if (and (< rsq 1.0) (> rsq 0.0)) (return 'done)))
507 (setf fac (sqrt (* (- 2.0) (log rsq) (/ 1.0 rsq))))
508 (setf *rnormal-gset* (* v1 fac))
509 (setf *rnormal-iset* 1)
510 (* v2 fac))
511 (t (setf *rnormal-iset* 0)
512 *rnormal-gset*))))
515 ;; The sample size ss must be a non negative integer.
516 ;; If ss=0, returns a number, otherwise a maxima list
517 ;; of length ss
518 (defun rndnormal (ss &aux sample)
519 (cond ((= ss 0)
520 (rndnormal-box))
521 (t (setf sample nil)
522 (dotimes (i ss (cons '(mlist simp) sample))
523 (setf sample (cons (rndnormal 0) sample))))))
526 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
527 ;; ;;
528 ;; Exponential random simulation ;;
529 ;; ;;
530 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
533 ;; Generates random exponential variates (m),
534 ;; using the inverse method.
535 (defun rndexp-inverse (m)
536 (declare (type flonum m))
537 (let (u)
538 (loop (setf u ($random 1.0))
539 (if (/= u 0.0) (return 'done)))
540 (/ (- (log (- 1.0 ($random 1.0)))) m)))
543 ;; The sample size ss must be a non negative integer.
544 ;; If ss=0, returns a number, otherwise a maxima list
545 ;; of length ss
546 (defun rndexp (m ss &aux sample)
547 (cond ((= ss 0)
548 (rndexp-inverse m))
549 (t (setf sample nil)
550 (dotimes (i ss (cons '(mlist simp) sample))
551 (setf sample (cons (rndexp m 0) sample))))))
554 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
555 ;; ;;
556 ;; Gamma random simulation ;;
557 ;; ;;
558 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
561 ;; Generates random gamma variates (a, b), combining
562 ;; Ahrens-Dieter and Cheng-Feast methods.
563 ;; References:
564 ;; [1] a >= 1
565 ;; Cheng, R.C.H. and Feast, G.M. (1979).
566 ;; Some simple gamma variate generators.
567 ;; Appl. Stat., 28, 3, 290-295.
568 ;; [2] 0 < a < 1.
569 ;; Ahrens, J.H. and Dieter, U. (1974).
570 ;; Computer methods for sampling from gamma, beta,
571 ;; poisson and binomial distributions.
572 ;; Computing, 12, 223-246.
573 (defun rndgamma-ahrens-cheng (aa bb)
574 (declare (type flonum aa bb))
575 (let (b c d f e1 r q x tt u1 u2 w (a (- aa 1.0)))
576 (cond ((<= aa 1.0) ;; Ahrens-Dieter algorithm
577 (setf e1 3.678794411714423216e-1)
578 (setf r (* aa e1))
579 (setf q (* ($random 1.0) (+ r 1.0)))
580 (cond ((< q 1.0)
581 (setf x (expt q (/ 1.0 aa)))
582 (setf tt (exp (- x))))
583 (t (setf x (- 1.0 (log (+ 1.0 (/ (- 1.0 q) r)))))
584 (setf tt (expt x a))))
585 (loop (if (< ($random 1.0) tt) (return 'done))
586 (setf q (* ($random 1.0) (+ r 1.0)))
587 (cond ((< q 1.0)
588 (setf x (expt q (/ 1.0 aa)))
589 (setf tt (exp (- x))))
590 (t (setf x (- 1.0 (log (+ 1.0 (/ (- 1.0 q) r)))))
591 (setf tt (expt x a)))))
592 (* bb x))
594 (t ;; Cheng-Feast algorithm GKM3
595 (loop (setf b (/ (- aa (/ 1.0 (* 6.0 aa))) a))
596 (setf c (/ 2.0 a))
597 (setf d (+ 2.0 c))
598 (cond ((< aa 2.5) ;; GKM1
599 (setf u1 ($random 1.0))
600 (setf u2 ($random 1.0)))
601 (t (setf f (/ 1.0 (sqrt aa))) ;; GKM2
602 (loop (setf u1 ($random 1.0))
603 (setf u2 (+ u1 (* (- 1.0 (* 1.86 ($random 1.0))) f)))
604 (if (and (< 0.0 u2) (< u2 1.0)) (return 'done)))))
605 (setf w (* b (/ u1 u2)))
606 (if (or (<= (+ (* c u2) (- d) w (/ 1.0 w)) 0.0)
607 (< (+ (* c (log u2)) (- (log w)) w -1.0) 0.0))
608 (return 'done)))
609 (* bb a w)))))
612 ;; The sample size ss must be a non negative integer.
613 ;; If ss=0, returns a number, otherwise a maxima list
614 ;; of length ss
615 (defun rndgamma (a b ss &aux sample)
616 (cond ((= ss 0)
617 (rndgamma-ahrens-cheng a b) )
618 (t (setf sample nil)
619 (dotimes (i ss (cons '(mlist simp) sample))
620 (setf sample (cons (rndgamma a b 0) sample))) )) )
623 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
624 ;; ;;
625 ;; Chi^2 random simulation ;;
626 ;; ;;
627 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
630 ;; The sample size ss must be a non negative integer.
631 ;; If ss=0, returns a number, otherwise a maxima list
632 ;; of length ss
633 (defun rndchi2 (n ss &aux sample)
634 (cond ((= ss 0)
635 (rndgamma-ahrens-cheng (* n 0.5) 2.0) )
636 (t (setf sample nil)
637 (dotimes (i ss (cons '(mlist simp) sample))
638 (setf sample (cons (rndchi2 n 0) sample))) )) )
641 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
642 ;; ;;
643 ;; Noncentral Chi^2 random simulation ;;
644 ;; ;;
645 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
648 ;; Generates random noncentral chi^2 variates (df, lambda)
649 ;; based on the fact that the noncentral chi^2 can be decomposed
650 ;; as the sum of a central chisquare with df degrees of freedom plus a
651 ;; noncentral chisquare with zero degrees of freedom (which is a Poisson
652 ;; mixture of central chisquares with integer degrees of freedom),
653 ;; see Formula (29.5b-c) in Johnson, Kotz, Balakrishnan (1995).
654 (defun rndnchi2-chi2-poisson (df lambda)
655 (declare (type flonum df lambda))
656 (cond
657 ((= lambda 0.0)
658 (rndgamma (* 0.5 df) 2.0 0))
660 (let ((r (coerce (rndpoisson (/ lambda 2.0) 0) 'flonum)))
661 (declare (type flonum r))
662 (when (> r 0.0)
663 (setf r (rndchi2 (* 2.0 r) 0)))
664 (setf r (+ r (rndgamma (* 0.5 df) 2.0 0)))
665 r) )))
669 ;; The sample size ss must be a non negative integer.
670 ;; If ss=0, returns a number, otherwise a maxima list
671 ;; of length ss
672 (defun rndnchi2 (df lambda ss &aux sample)
673 (cond ((= ss 0)
674 (rndnchi2-chi2-poisson df lambda))
675 (t (setf sample nil)
676 (dotimes (i ss (cons '(mlist simp) sample))
677 (setf sample (cons (rndnchi2 df lambda 0) sample))))))
680 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
681 ;; ;;
682 ;; Student's t random simulation ;;
683 ;; ;;
684 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
687 ;; Generates random Student's t variates (n), based
688 ;; on the fact that Z/sqrt(Y/n) is a Student's t random
689 ;; variable with n degrees of freedom, if Z~N(0,1)
690 ;; and Y~chi^2(n).
691 (defun rndstudent-ratio (n)
692 (declare (type flonum n))
693 (let (z y)
694 (setf z (rndnormal 0)
695 y (rndchi2 n 0))
696 (/ z (sqrt (/ y n)))))
699 ;; The sample size ss must be a non negative integer.
700 ;; If ss=0, returns a number, otherwise a maxima list
701 ;; of length ss
702 (defun rndstudent (n ss &aux sample)
703 (cond ((= ss 0)
704 (rndstudent-ratio n) )
705 (t (setf sample nil)
706 (dotimes (i ss (cons '(mlist simp) sample))
707 (setf sample (cons (rndstudent n 0) sample))) )) )
711 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
712 ;; ;;
713 ;; Noncentral Student's t random simulation ;;
714 ;; ;;
715 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
718 ;; Generates random Noncentral Student variates (n), based
719 ;; on the fact that X/sqrt(Y/n) is a Noncentral Student random
720 ;; variable with n degrees of freedom and noncentrality parameter k,
721 ;; if X~N(k,1) and Y~chi^2(n).
722 (defun rndncstudent-ratio (n k)
723 (declare (type flonum n k))
724 (let (x y)
725 (setf x (+ k (rndnormal 0))
726 y (rndchi2 n 0))
727 (/ x (sqrt (/ y n)))))
730 ;; The sample size ss must be a non negative integer.
731 ;; If ss=0, returns a number, otherwise a maxima list
732 ;; of length ss
733 (defun rndncstudent (n k ss &aux sample)
734 (cond ((= ss 0)
735 (rndncstudent-ratio n k) )
736 (t (setf sample nil)
737 (dotimes (i ss (cons '(mlist simp) sample))
738 (setf sample (cons (rndncstudent-ratio n k) sample))) )) )
742 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
743 ;; ;;
744 ;; Snedecor's F random simulation ;;
745 ;; ;;
746 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
749 ;; Generates random Snedecor's F variates (m,n), based
750 ;; on the fact that (X/m)/(Y/n) is a Snedecor's F random
751 ;; variable with m and n degrees of freedom, if X~chi^2(m)
752 ;; and Y~chi^2(n).
753 (defun rndf-ratio (m n)
754 (declare (type flonum m n))
755 (let (x y)
756 (setf x (rndchi2 m 0)
757 y (rndchi2 n 0))
758 (/ (* n x) (* m y))))
761 ;; The sample size ss must be a non negative integer.
762 ;; If ss=0, returns a number, otherwise a maxima list
763 ;; of length ss
764 (defun rndf (m n ss &aux sample)
765 (cond ((= ss 0)
766 (rndf-ratio m n))
767 (t (setf sample nil)
768 (dotimes (i ss (cons '(mlist simp) sample))
769 (setf sample (cons (rndf m n 0) sample))))))
772 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
773 ;; ;;
774 ;; Beta random simulation ;;
775 ;; ;;
776 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
779 ;; Generates random beta variates (a, b)
780 ;; Reference:
781 ;; [1] Cheng, R.C.H. (1978). Generating Beta Variates
782 ;; with Nonintegral Shape Parameters.
783 ;; Communications of the ACM, 21:317-322
784 ;; Comments:
785 ;; [1] Algorithms BB and BC
786 ;; [2] This is a translation from C of the
787 ;; ranlib.c library. R statistical package uses this same
788 ;; algorithm but in a more structured style.
789 ;; [3] Some global variables are defined in order to
790 ;; save time when many random variables are generated.
791 (defvar *rbeta-olda* -1.0)
792 (defvar *rbeta-oldb* -1.0)
793 (defvar *rbeta-alpha*)
794 (defvar *rbeta-beta*)
795 (defvar *rbeta-gamma*)
796 (defvar *rbeta-delta*)
797 (defvar *rbeta-k1*)
798 (defvar *rbeta-k2*)
799 (defvar *rbeta-a*)
800 (defvar *rbeta-b*)
802 (defun rndbeta-cheng0 (aa bb )
803 (declare (type flonum aa bb))
804 (let (qsame u1 u2 v w z tt r s y genbet
805 (expmax 7.0) (infnty 1.0e304))
806 (declare (type flonum expmax infnty))
807 (if (and (= *rbeta-olda* aa) (= *rbeta-oldb* bb))
808 (setf qsame t)
809 (setf qsame nil
810 *rbeta-olda* aa
811 *rbeta-oldb* bb))
813 (tagbody
814 (if (<= (min aa bb) 1.0) (go s100))
816 ;; Algorithm BB - Initialize
817 (if qsame (go s40))
818 (setf *rbeta-a* (min aa bb))
819 (setf *rbeta-b* (max aa bb))
820 (setf *rbeta-alpha* (+ *rbeta-a* *rbeta-b*))
821 (setf *rbeta-beta* (sqrt (/ (- *rbeta-alpha* 2.0)
822 (- (* 2.0 *rbeta-a* *rbeta-b*) *rbeta-alpha*))))
823 (setf *rbeta-gamma* (+ *rbeta-a* (/ 1.0 *rbeta-beta*)))
826 (setf u1 ($random 1.0))
828 ;; Step 1
829 (setf u2 ($random 1.0))
830 (setf v (* *rbeta-beta* (log (/ u1 (- 1.0 u1)))))
831 (if (<= v expmax)
832 (setf w (* *rbeta-a* (exp v)))
833 (setf w infnty))
834 (setf z (* u2 (expt u1 2)))
835 (setf r (- (* *rbeta-gamma* v) 1.3862944))
836 (setf s (+ *rbeta-a* r (- w)))
838 ;; Step 2
839 (if (>= (+ s 2.609438) (* 5.0 z)) (go s70))
841 ;; Step 3
842 (setf tt (log z))
843 (if (> s tt) (go s70))
845 ;; Step 4
846 (if (< (+ r (* *rbeta-alpha* (log (/ *rbeta-alpha* (+ *rbeta-b* w))))) tt)
847 (go s40))
850 ;; Step 5
851 (if (= w infnty)
852 (setf genbet 'rndbeta-encountered-infnty)
853 (if (/= aa *rbeta-a*)
854 (setf genbet (/ *rbeta-b* (+ *rbeta-b* w)))
855 (setf genbet (/ w (+ *rbeta-b* w)))))
856 (go s230)
858 s100
859 ;; Algorithm BC - Initialize
860 (if qsame (go s120))
861 (setf *rbeta-a* (max aa bb))
862 (setf *rbeta-b* (min aa bb))
863 (setf *rbeta-alpha* (+ *rbeta-a* *rbeta-b*))
864 (setf *rbeta-beta* (/ 1.0 *rbeta-b*))
865 (setf *rbeta-delta* (+ 1.0 *rbeta-a* (- *rbeta-b*)))
866 (setf *rbeta-k1* (/ (* *rbeta-delta* (+ 1.38889e-2 (* 4.16667e-2 *rbeta-b*)))
867 (- (* *rbeta-a* *rbeta-beta*) 0.777778)))
868 (setf *rbeta-k2* (+ 0.25 (* (+ 0.5 (/ 0.25 *rbeta-delta*)) *rbeta-b*)))
870 s120
871 (setf u1 ($random 1.0))
873 ;; Step 1
874 (setf u2 ($random 1.0))
875 (if (>= u1 0.5) (go s130))
877 ;; Step 2
878 (setf y (* u1 u2))
879 (setf z (* u1 y))
880 (if (>= (+ (* 0.25 u2) z (- y)) *rbeta-k1*) (go s120))
881 (go s170)
883 s130
884 ;; Step 3
885 (setf z (* (expt u1 2) u2))
886 (if (> z 0.25) (go s160))
887 (setf v (* *rbeta-beta* (log (/ u1 (- 1.0 u1)))))
888 (if (<= v expmax)
889 (setf w (* *rbeta-a* (exp v)))
890 (setf w infnty))
891 (go s200)
893 s160
894 (if (>= z *rbeta-k2*) (go s120))
896 s170
897 ;; Step 4 Step 5
898 (setf v (* *rbeta-beta* (log (/ u1 (- 1.0 u1)))))
899 (if (<= v expmax)
900 (setf w (* *rbeta-a* (exp v)))
901 (setf w infnty))
903 s190
904 (if (< (- (* *rbeta-alpha* (+ (log (/ *rbeta-alpha* (+ *rbeta-b* w))) v)) 1.3862944) (log z))
905 (go s120))
907 s200
908 ;; Step 6
909 (if (= w infnty)
910 (setf genbet 'rndbeta-encountered-infnty)
911 (if (/= aa *rbeta-a*)
912 (setf genbet (/ *rbeta-b* (+ *rbeta-b* w)))
913 (setf genbet (/ w (+ *rbeta-b* w)))))
915 s230)
916 genbet))
918 (defun rndbeta-cheng (aa bb )
919 (let (genbet)
920 (loop while (eq (setq genbet (rndbeta-cheng0 aa bb)) 'rndbeta-encountered-infnty))
921 genbet))
924 ;; The sample size ss must be a non negative integer.
925 ;; If ss=0, returns a number, otherwise a maxima list
926 ;; of length ss
927 (defun rndbeta (a b ss &aux sample)
928 (cond ((= ss 0)
929 (rndbeta-cheng a b) )
930 (t (setf sample nil)
931 (dotimes (i ss (cons '(mlist simp) sample))
932 (setf sample (cons (rndbeta a b 0) sample))) )) )
935 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
936 ;; ;;
937 ;; Binomial random simulation ;;
938 ;; ;;
939 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
942 ;; Generates random binomial variates (n p)
943 ;; Reference:
944 ;; [1] Kachitvichyanukul, V., Schmeiser, B.W.
945 ;; Binomial Random Variate Generation.
946 ;; Communications of the ACM, 31, 2
947 ;; (February, 1988) 216
948 ;; Comments:
949 ;; [1] Algorithm BTPE
950 ;; [2] This is a translation from C of the
951 ;; ranlib.c library. R statistical package uses this same
952 ;; algorithm but in a more structured style.
953 ;; [3] Some global variables are defined in order to
954 ;; save time when many random variables are generated.
955 (defvar *rbin-psave* -1.0)
956 (defvar *rbin-nsave* -1) ;; integer
957 (defvar *rbin-m*) ;; integer
958 (defvar *rbin-xnp*)
959 (defvar *rbin-p*)
960 (defvar *rbin-q*)
961 (defvar *rbin-ffm*)
962 (defvar *rbin-xnpq*)
963 (defvar *rbin-fm*)
964 (defvar *rbin-xm*)
965 (defvar *rbin-xl*)
966 (defvar *rbin-xr*)
967 (defvar *rbin-c*)
968 (defvar *rbin-al*)
969 (defvar *rbin-xll*)
970 (defvar *rbin-xlr*)
971 (defvar *rbin-p1*)
972 (defvar *rbin-p2*)
973 (defvar *rbin-p3*)
974 (defvar *rbin-p4*)
975 (defvar *rbin-qn*)
976 (defvar *rbin-r*)
977 (defvar *rbin-g*)
978 (defun rndbinomial-kachit (n pp)
979 (declare (type integer n)
980 (type flonum pp))
981 (let (u v x f amaxp ynorm alv x1 f1 z w z2 x2 f2 w2 ix k t1 mp ix1)
982 (tagbody
983 (if (/= pp *rbin-psave*) (go s10))
984 (if (/= n *rbin-nsave*) (go s20))
985 (if (< *rbin-xnp* 30.0) (go s150))
986 (go s30)
989 ;; setup, perform only when parameters change
990 (setf *rbin-psave* pp)
991 (setf *rbin-p* (min *rbin-psave* (- 1.0 *rbin-psave*)))
992 (setf *rbin-q* (- 1.0 *rbin-p*))
995 (setf *rbin-xnp* (* n *rbin-p*))
996 (setf *rbin-nsave* n)
997 (if (< *rbin-xnp* 30.0) (go s140))
998 (setf *rbin-ffm* (+ *rbin-xnp* *rbin-p*))
999 (setf *rbin-m* (round *rbin-ffm*))
1000 (setf *rbin-fm* *rbin-m*)
1001 (setf *rbin-xnpq* (* *rbin-xnp* *rbin-q*))
1002 (setf *rbin-p1* (+ 0.5 (round (- (* 2.195 (sqrt *rbin-xnpq*)) (* 4.6 *rbin-q*)))))
1003 (setf *rbin-xm* (+ 0.5 *rbin-fm*))
1004 (setf *rbin-xl* (- *rbin-xm* *rbin-p1*))
1005 (setf *rbin-xr* (+ *rbin-xm* *rbin-p1*))
1006 (setf *rbin-c* (+ 0.134 (/ 20.5 (+ 15.3 *rbin-fm*))))
1007 (setf *rbin-al* (/ (- *rbin-ffm* *rbin-xl*) (- *rbin-ffm* (* *rbin-xl* *rbin-p*))))
1008 (setf *rbin-xll* (* *rbin-al* (+ 1.0 (* 0.5 *rbin-al*))))
1009 (setf *rbin-al* (/ (- *rbin-xr* *rbin-ffm*) (* *rbin-xr* *rbin-q*)))
1010 (setf *rbin-xlr* (* *rbin-al* (+ 1.0 (* 0.5 *rbin-al*))))
1011 (setf *rbin-p2* (* *rbin-p1* (+ 1.0 *rbin-c* *rbin-c*)))
1012 (setf *rbin-p3* (+ *rbin-p2* (/ *rbin-c* *rbin-xll*)))
1013 (setf *rbin-p4* (+ *rbin-p3* (/ *rbin-c* *rbin-xlr*)))
1016 ;; generate variate
1017 (setf u (* *rbin-p4* ($random 1.0)))
1018 (setf v ($random 1.0))
1020 ;; triangular region
1021 (if (> u *rbin-p1*) (go s40))
1022 (setf ix (round (+ *rbin-xm* (- (* *rbin-p1* v)) u)))
1023 (go s170)
1026 ;; parallelogram region
1027 (if (> u *rbin-p2*) (go s50))
1028 (setf x (+ *rbin-xl* (/ (- u *rbin-p1*) *rbin-c*)))
1029 (setf v (+ (* v *rbin-c*) 1.0 (- (/ (abs (- *rbin-xm* x)) *rbin-p1*))))
1030 (if (or (> v 1.0) (<= v 0.0)) (go s30))
1031 (setf ix (round x))
1032 (go s70)
1035 ;; left tail
1036 (if (> u *rbin-p3*) (go s60))
1037 (setf ix (round (+ *rbin-xl* (/ (log v) *rbin-xll*))))
1038 (if (< ix 0) (go s30))
1039 (setf v (* v (- u *rbin-p2*) *rbin-xll*))
1040 (go s70)
1043 ;; right tail
1044 (setf ix (round (- *rbin-xr* (/ (log v) *rbin-xlr*))))
1045 (if (> ix n) (go s30))
1046 (setf v (* v (- u *rbin-p3*) *rbin-xlr*))
1049 ;; determine appropriate way to perform accept/reject test
1050 (setf k (abs (- ix *rbin-m*)))
1051 (if (and (> k 20) (< k (- (/ *rbin-xnpq* 2.0) 1.0))) (go s130))
1053 ;; explicit evaluation
1054 (setf f 1.0)
1055 (setf *rbin-r* (/ *rbin-p* *rbin-q*))
1056 (setf *rbin-g* (* *rbin-r* (+ n 1.0)))
1057 (setf t1 (- *rbin-m* ix))
1058 (if (< t1 0)
1059 (go s80)
1060 (if (= t1 0)
1061 (go s120)
1062 (go s100)))
1065 (setf mp (+ *rbin-m* 1))
1066 (do ((i mp (1+ i)))
1067 ((> i ix) 'done)
1068 (setf f (* f (- (/ *rbin-g* i) *rbin-r*))))
1069 (go s120)
1071 s100
1072 (setf ix1 (+ ix 1))
1073 (do ((i ix1 (1+ i)))
1074 ((> i *rbin-m*) 'done)
1075 (setf f (/ f (- (/ *rbin-g* i) *rbin-r*))))
1077 s120
1078 (if (<= v f) (go s170))
1079 (go s30)
1081 s130
1082 ;; squeezing using upper and lower bounds on alog(f(x))
1083 (setf amaxp (* (/ k *rbin-xnpq*)
1084 (+ (/ (+ (* k (+ (/ k 3.0) 0.625)) 0.1666666666666) *rbin-xnpq*)
1085 0.5)))
1086 (setf ynorm (- (/ (* k k) (* 2.0 *rbin-xnpq*))))
1087 (setf alv (log v))
1088 (if (< alv (- ynorm amaxp)) (go s170))
1089 (if (> alv (+ ynorm amaxp)) (go s30))
1091 ;; Stirling's formula to machine accuracy for
1092 ;; the final acceptance / rejection test
1093 (setf x1 (+ ix 1.0))
1094 (setf f1 (+ *rbin-fm* 1.0))
1095 (setf z (+ n 1.0 (- *rbin-fm*)))
1096 (setf w (+ n 1.0 (- ix)))
1097 (setf z2 (* z z))
1098 (setf x2 (* x1 x1))
1099 (setf f2 (* f1 f1))
1100 (setf w2 (* w w))
1101 (if (<= alv (+ (* *rbin-xm* (log (/ f1 x1)))
1102 (* (+ n (- *rbin-m*) 0.5) (log (/ z w)))
1103 (* (+ ix (- *rbin-m*)) (log (/ (* w *rbin-p*) (* x1 *rbin-q*))))
1104 (/ (/ (+ 13860.0 (- (/ (+ 462.0 (- (/ (+ 132.0
1105 (- (/ (+ 99.0 (- (/ 140.0 f2))) f2))) f2))) f2))) f1) 166320.0)
1106 (/ (/ (+ 13860.0 (- (/ (+ 462.0 (- (/ (+ 132.0
1107 (- (/ (+ 99.0 (- (/ 140.0 z2))) z2))) z2))) z2))) z) 166320.0)
1108 (/ (/ (+ 13860.0 (- (/ (+ 462.0 (- (/ (+ 132.0
1109 (- (/ (+ 99.0 (- (/ 140.0 x2))) x2))) x2))) x2))) x1) 166320.0)
1110 (/ (/ (+ 13860.0 (- (/ (+ 462.0 (- (/ (+ 132.0
1111 (- (/ (+ 99.0 (- (/ 140.0 w2))) w2))) w2))) w2))) w) 166320.0)))
1112 (go s170))
1113 (go s30)
1115 s140
1116 ;; inverse cdf logic for mean less than 30
1117 (setf *rbin-qn* (expt *rbin-q* n))
1118 (setf *rbin-r* (/ *rbin-p* *rbin-q*))
1119 (setf *rbin-g* (* *rbin-r* (+ n 1.0)))
1121 s150
1122 (setf ix 0)
1123 (setf f *rbin-qn*)
1124 (setf u ($random 1.0))
1126 s160
1127 (if (< u f) (go s170))
1128 (if (> ix 110) (go s150))
1129 (setf u (- u f))
1130 (setf ix (+ ix 1))
1131 (setf f (* f (- (/ *rbin-g* ix) *rbin-r*)))
1132 (go s160)
1134 s170)
1135 (if (> *rbin-psave* 0.5)
1136 (- n ix)
1137 ix)))
1140 ;; The sample size ss must be a non negative integer.
1141 ;; If ss=0, returns a number, otherwise a maxima list
1142 ;; of length ss
1143 (defun rndbinomial (n p ss &aux sample)
1144 (cond ((= ss 0)
1145 (rndbinomial-kachit n p))
1146 (t (setf sample nil)
1147 (dotimes (i ss (cons '(mlist simp) sample))
1148 (setf sample (cons (rndbinomial n p 0) sample))))))
1151 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1152 ;; ;;
1153 ;; Poisson random simulation ;;
1154 ;; ;;
1155 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1158 ;; Generates random Poisson variates (m)
1159 ;; Reference:
1160 ;; [1] Ahrens, J.H. and Dieter, U.
1161 ;; Computer Generation of Poisson Deviates
1162 ;; From Modified Normal Distributions.
1163 ;; ACM Trans. Math. Software, 8, 2
1164 ;; (June 1982),163-179
1165 ;; Comments:
1166 ;; [1] Slightly modified version of the program in the
1167 ;; above article
1168 ;; [2] This is a translation from C of the
1169 ;; ranlib.c library. R statistical package uses this same
1170 ;; algorithm but in a more structured style.
1171 ;; [3] Some global variables are defined in order to
1172 ;; save time when many random variables are generated.
1173 (defvar *rpos-muold* 0.0)
1174 (defvar *rpos-muprev* 0.0)
1175 (defvar *rpos-s*)
1176 (defvar *rpos-l*)
1177 (defvar *rpos-d*)
1178 (defvar *rpos-p0*)
1179 (defvar *rpos-omega*)
1180 (defvar *rpos-b1*)
1181 (defvar *rpos-b2*)
1182 (defvar *rpos-c*)
1183 (defvar *rpos-c0*)
1184 (defvar *rpos-c1*)
1185 (defvar *rpos-c2*)
1186 (defvar *rpos-c3*)
1187 (defvar *rpos-m*)
1188 (defvar *rpos-p*)
1189 (defvar *rpos-q*)
1190 (defvar *rpos-pp* (make-array 35 :initial-element 0.0 :element-type 'flonum))
1191 (defun rndpoisson-ahrens (mu)
1192 (declare (type flonum mu))
1194 ;; This algorithm doesn't handle MU = 0 correctly; *RPOS-S* and friends aren't initialized.
1195 ;; So just handle the special case here and bail out.
1196 (if (= mu 0.0)
1197 (return-from rndpoisson-ahrens 0))
1199 (let ( ignpoi j kflag del difmuk e fk fx fy g px py tt u v x xx
1200 (a0 -0.5) (a1 0.3333333) (a2 -0.2500068) (a3 0.2000118)
1201 (a4 -0.1661269) (a5 0.1421878) (a6 -0.1384794) (a7 0.125006)
1202 (fact (make-array 10
1203 :element-type 'flonum
1204 :initial-contents '(1.0 1.0 2.0 6.0 24.0 120.0
1205 720.0 5040.0 40320.0 362880.0))))
1206 (declare (type flonum a0 a1 a2 a3 a4 a5 a6 a7))
1207 (declare (type (simple-array flonum (10)) fact))
1208 (tagbody
1209 (if (= mu *rpos-muprev*) (go s10))
1210 (if (< mu 10.0) (go s120))
1212 ;; Case A. (Recalculation of, *rpos-s*, *rpos-d*, *rpos-l* if mu has changed)
1213 (setf *rpos-muprev* mu)
1214 (setf *rpos-s* (sqrt mu))
1215 (setf *rpos-d* (* 6.0 mu mu))
1216 (setf *rpos-l* (floor (- mu 1.1484)))
1219 ;; Step n. Normal sample
1220 (setf g (+ mu (* *rpos-s* (rndnormal 0))))
1221 (if (< g 0.0) (go s20))
1222 (setf ignpoi (floor g))
1224 ;; Step I. Immediate acceptance if ignpoi is large enough
1225 (if (>= ignpoi *rpos-l*) (go s200))
1227 ;; Step S. Squeez acceptance
1228 (setf fk (coerce ignpoi 'flonum))
1229 (setf difmuk (- mu fk))
1230 (setf u ($random 1.0))
1231 (if (>= (* *rpos-d* u) (* difmuk difmuk difmuk)) (go s200))
1234 ;; Step P. Preparations for steps Q and H.
1235 ;; (Recalculations of parameters if necessary)
1236 ;; .3989423...=(2*%pi)^(-.5) .416667...E-1=1./24. .1428571...=1./7.
1237 ;; The quantities *rpos-b1*, *rpos-b2*, *rpos-c3*, *rpos-c2*, *rpos-c1*, *rpos-c0* are for thr Hermite
1238 ;; approximations to the discrete normal probabilities fk.
1239 ;; c=0.1069/mu guarantees majorization by the 'hat'-function.
1240 (if (= mu *rpos-muold*) (go s30))
1241 (setf *rpos-muold* mu)
1242 (setf *rpos-omega* (/ 0.39894228040143 *rpos-s*))
1243 (setf *rpos-b1* (/ 0.41666666666667e-1 mu))
1244 (setf *rpos-b2* (* 0.3 *rpos-b1* *rpos-b1*))
1245 (setf *rpos-c3* (* 0.14285714285714 *rpos-b1* *rpos-b2*))
1246 (setf *rpos-c2* (- *rpos-b2* (* 15.0 *rpos-c3*)))
1247 (setf *rpos-c1* (+ *rpos-b1* (* -6.0 *rpos-b2*) (* 45.0 *rpos-c3*)))
1248 (setf *rpos-c0* (+ 1.0 (- *rpos-b1*) (* 3.0 *rpos-b2*) (* -15.0 *rpos-c3*)))
1249 (setf *rpos-c* (/ 0.1069 mu))
1252 (if (< g 0.0) (go s50))
1254 ;; subroutine F is called
1255 (setf kflag 0)
1256 (go s70)
1259 ;; Step Q. Quotient acceptance (rare case)
1260 (if (<= (- fy (* u fy)) (* py (exp (- px fx)))) (go s200))
1263 ;; Step E. Exponential sample - rndexp is called for
1264 ;; standard exponential variate e and sample tt from the Laplace 'hat'
1265 ;; (if tt <= -.6744 then pk < fk for all mu >= 10.)
1266 (setf e (rndexp 1.0 0))
1267 (setf u ($random 1.0))
1268 (setf u (+ u (- u 1.0)))
1269 (setf tt (+ 1.8 (if (or (and (> u 0.0) (< e 0.0))
1270 (and (< u 0.0) (> e 0.0)))
1271 (- e)
1272 e)))
1273 (if (<= tt -0.6744) (go s50))
1274 (setf ignpoi (floor (+ mu (* *rpos-s* tt))))
1275 (setf fk (coerce ignpoi 'flonum))
1276 (setf difmuk (- mu fk))
1278 ;; subroutine F is called
1279 (setf kflag 1)
1280 (go s70)
1283 ;; Step F. Hat acceptance (E is repeated on rejection)
1284 (if (> (* *rpos-c* (abs u)) (- (* py (exp (+ px e))) (* fy (exp (+ fx e))))) (go s50))
1285 (go s200)
1288 ;; Step F. Subroutine F. Calculation of px, py, fx, fy.
1289 ;; Case ignpoi < 10 uses factorials from table fact
1290 (if (>= ignpoi 10) (go s80))
1291 (setf px (- mu))
1292 (setf py (/ (expt mu ignpoi) (aref fact ignpoi)))
1293 (go s110)
1296 ;; Case ignpoi >= 10 uses polynomial approximation
1297 ;; a0-a7 for accuracy when advisable
1298 ;; .8333333E-1=1./12. .3989423=(2*%pi)^(-.5)
1299 (setf del (/ 8.333333333333333e-2 fk))
1300 (setf del (- del (* 4.8 del del del)))
1301 (setf v (/ difmuk fk))
1302 (if (<= (abs v) 0.25) (go s90))
1303 (setf px (+ (* fk (log (+ 1.0 v))) (- difmuk) (- del)))
1304 (go s100)
1307 (setf px (+ (* fk v v
1308 (+ (* (+ (* (+ (* (+ (* (+ (* (+ (* (+ (* a7 v)
1309 a6) v) a5) v) a4) v) a3) v) a2) v) a1) v) a0))
1310 (- del)))
1312 s100
1313 (setf py (/ 0.39894228040143 (sqrt fk)))
1315 s110
1316 (setf x (/ (- 0.5 difmuk) *rpos-s*))
1317 (setf xx (* x x))
1318 (setf fx (* -0.5 xx))
1319 (setf fy (* *rpos-omega* (+ (* (+ (* (+ (* *rpos-c3* xx)
1320 *rpos-c2*) xx) *rpos-c1*) xx) *rpos-c0*)))
1321 (if (<= kflag 0) (go s40))
1322 (go s60)
1324 s120
1325 ;; Case B. Start new table and calculate *rpos-p0* if necessary
1326 (setf *rpos-muprev* 0.0)
1327 (if (= mu *rpos-muold*) (go s130))
1328 (setf *rpos-muold* mu)
1329 (setf *rpos-m* (max 1 (floor mu)))
1330 (setf *rpos-l* 0)
1331 (setf *rpos-p* (exp (- mu)))
1332 (setf *rpos-p0* *rpos-p*)
1333 (setf *rpos-q* *rpos-p*)
1335 s130
1336 ;; Step U. Uniform sample for inversion method
1337 (setf u ($random 1.0))
1338 (setf ignpoi 0)
1339 (if (<= u *rpos-p0*) (go s200))
1341 ;; Step T.
1342 (if (= *rpos-l* 0) (go s150))
1343 (setf j 1)
1344 (if (> u 0.458) (setf j (min *rpos-l* *rpos-m*)))
1345 (do ((k j (1+ k)))
1346 ((> k *rpos-l*) 'done)
1347 (cond ((<= u (aref *rpos-pp* (- k 1)))
1348 (setf ignpoi k)
1349 (go s200))))
1350 (if (= *rpos-l* 35) (go s130))
1352 s150
1353 ;; Step C. Creation of new Poisson probabilities p
1354 ;; and their cumulatives *rpos-q*=*rpos-pp*(k)
1355 (setf *rpos-l* (1+ *rpos-l*))
1356 (do ((k *rpos-l* (1+ k)))
1357 ((> k 35) 'done)
1358 (setf *rpos-p* (/ (* *rpos-p* mu) (float k)))
1359 (setf *rpos-q* (+ *rpos-q* *rpos-p*))
1360 (setf (aref *rpos-pp* (- k 1)) *rpos-q*)
1361 (cond ((<= u *rpos-q*)
1362 (setf *rpos-l* k)
1363 (setf ignpoi k)
1364 (go s200)) ) )
1365 (setf *rpos-l* 35)
1366 (go s130)
1368 s200 )
1369 ignpoi))
1372 ;; The sample size ss must be a non negative integer.
1373 ;; If ss=0, returns a number, otherwise a maxima list
1374 ;; of length ss
1375 (defun rndpoisson (m ss &aux sample)
1376 (declare (type flonum m))
1377 (declare (type integer ss))
1378 (cond ((= ss 0)
1379 (rndpoisson-ahrens m))
1380 (t (setf sample nil)
1381 (dotimes (i ss (cons '(mlist simp) sample))
1382 (setf sample (cons (rndpoisson m 0) sample))))))
1385 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1386 ;; ;;
1387 ;; Geometric random simulation ;;
1388 ;; ;;
1389 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1392 ;; Generates random geometric variates (p)
1393 ;; by simulation of Bernoulli trials.
1394 (defun rndgeo-trials (p)
1395 (declare (type flonum p))
1396 (let ((sum 0))
1397 (declare (type integer sum))
1398 (loop (if (<= ($random 1.0) p) (return sum))
1399 (setf sum (1+ sum)))))
1402 ;; The sample size ss must be a non negative integer.
1403 ;; If ss=0, returns a number, otherwise a maxima list
1404 ;; of length ss
1405 (defun rndgeo (p ss &aux sample)
1406 (declare (type flonum p))
1407 (declare (type integer ss))
1408 (cond ((= ss 0)
1409 (rndgeo-trials p))
1410 (t (setf sample nil)
1411 (dotimes (i ss (cons '(mlist simp) sample))
1412 (setf sample (cons (rndgeo p 0) sample))))))
1415 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1416 ;; ;;
1417 ;; Hypergeometric random simulation ;;
1418 ;; ;;
1419 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1423 ;; Computes the logarithm of the factorial log(n!)
1424 ;; This function is called from rndhypergeo-kachit
1425 (defun afc (i)
1426 (declare (type integer i))
1427 (let ((di (float i))
1428 (al (make-array 9
1429 :element-type 'flonum
1430 :initial-contents '( 0.0
1431 0.0 ;; ln(0!)=ln(1)
1432 0.0 ;; ln(1!)=ln(1)
1433 0.69314718055994530941723212145817 ;; ln(2)
1434 1.79175946922805500081247735838070 ;; ln(6)
1435 3.17805383034794561964694160129705 ;; ln(24)
1436 4.78749174278204599424770093452324
1437 6.57925121201010099506017829290394
1438 8.52516136106541430016553103634712))))
1439 (declare (type flonum di))
1440 (declare (type (simple-array flonum (9)) al))
1441 (cond ((<= i 7)
1442 (aref al (1+ i)))
1443 (t (+ (* (+ di 0.5)
1444 (log di))
1445 (- di)
1446 (/ 0.83333333333333333333333333333333 di)
1447 (/ (- 0.00277777777777777777777777777778)
1448 di di di)
1449 0.91893853320467274178032973640562))) ) )
1452 ;; Generates random hypergeometric variates (n1, n2, n),
1453 ;; Reference:
1454 ;; [1] Kachitvichyanukul, V., Schmeiser, B.W. (1985)
1455 ;; Computer generation of hypergeometric random variates.
1456 ;; Journal of Statistical Computation and Simulation 22, 127-145.
1457 ;; Comments:
1458 ;; [1] This is a translation from C of the
1459 ;; rhyper.c file in the R statistical package.
1460 ;; [2] Some global variables are defined in order to
1461 ;; save time when many random variables are generated.
1462 ;; integer globals
1463 (defvar *rhyp-n1s* -1)
1464 (defvar *rhyp-n2s* -1)
1465 (defvar *rhyp-ks* -1)
1466 (defvar *rhyp-k*)
1467 (defvar *rhyp-n1*)
1468 (defvar *rhyp-n2*)
1469 (defvar *rhyp-m*)
1470 (defvar *rhyp-minjx*)
1471 (defvar *rhyp-maxjx*)
1472 ;; long globals
1473 (defvar *rhyp-tn*)
1474 (defvar *rhyp-a*)
1475 (defvar *rhyp-d*)
1476 (defvar *rhyp-s*)
1477 (defvar *rhyp-w*)
1478 (defvar *rhyp-xl*)
1479 (defvar *rhyp-xr*)
1480 (defvar *rhyp-kl*)
1481 (defvar *rhyp-kr*)
1482 (defvar *rhyp-lamdl*)
1483 (defvar *rhyp-lamdr*)
1484 (defvar *rhyp-p1*)
1485 (defvar *rhyp-p2*)
1486 (defvar *rhyp-p3*)
1487 (defun rndhypergeo-kachit (nn1 nn2 kk)
1488 (declare (type integer nn1 nn2 kk))
1489 (let (ix reject setup1 setup2 e f g p r tt u v y de dg dr ds dt gl
1490 gu nk nm ub xk xm xn y1 ym yn yk alv
1491 (con 57.56462733) (deltal 0.0078) (deltau 0.0034) (scale 1.e25))
1492 (declare (type flonum con deltal deltau scale))
1493 ;; if new parameter values, initialize
1494 (setf reject t)
1495 (cond ((or (/= nn1 *rhyp-n1s*) (/= nn2 *rhyp-n2s*))
1496 (setf setup1 t
1497 setup2 t))
1498 (t (cond ((/= kk *rhyp-ks*)
1499 (setf setup1 nil
1500 setup2 t))
1501 (t (setf setup1 nil
1502 setup2 nil)))))
1503 (cond (setup1
1504 (setf *rhyp-n1s* nn1
1505 *rhyp-n2s* nn2
1506 *rhyp-tn* (+ nn1 nn2))
1507 (cond ((<= nn1 nn2)
1508 (setf *rhyp-n1* nn1
1509 *rhyp-n2* nn2))
1510 (t (setf *rhyp-n1* nn2
1511 *rhyp-n2* nn1)))))
1512 (cond (setup2
1513 (setf *rhyp-ks* kk)
1514 (cond ((>= (+ kk kk) *rhyp-tn*)
1515 (setf *rhyp-k* (- *rhyp-tn* kk)))
1516 (t (setf *rhyp-k* kk)))))
1517 (cond ((or setup1 setup2)
1518 (setf *rhyp-m* (/ (* (+ *rhyp-k* 1.0) (+ *rhyp-n1* 1.0)) (+ *rhyp-tn* 2.0))
1519 *rhyp-minjx* (max 0 (- *rhyp-k* *rhyp-n2*))
1520 *rhyp-maxjx* (min *rhyp-n1* *rhyp-k*))))
1522 ;; generate random variate --- Three basic cases
1523 (cond ((= *rhyp-minjx* *rhyp-maxjx*) ;; I: degenerate distribution ------------
1524 (setf ix *rhyp-maxjx*)
1525 (cond ((>= (+ kk kk) *rhyp-tn*)
1526 (cond ((> nn1 nn2)
1527 (+ kk (- nn2) ix))
1528 (t (- nn1 ix))))
1529 (t (cond ((> nn1 nn2)
1530 (- kk ix))
1531 (t ix)))))
1532 ((< (- *rhyp-m* *rhyp-minjx*) 10) ;; II: inverse transformation ------------
1533 (cond ((or setup1 setup2)
1534 (cond ((< *rhyp-k* *rhyp-n2*)
1535 (setf *rhyp-w* (exp (+ con
1536 (afc *rhyp-n2*)
1537 (afc (+ *rhyp-n1* *rhyp-n2* (- *rhyp-k*)))
1538 (- (afc (- *rhyp-n2* *rhyp-k*)))
1539 (- (afc (+ *rhyp-n1* *rhyp-n2*)))))))
1540 (t (setf *rhyp-w* (exp (+ con
1541 (afc *rhyp-n1*)
1542 (afc *rhyp-k*)
1543 (- (afc (- *rhyp-k* *rhyp-n2*)))
1544 (- (afc (+ *rhyp-n1* *rhyp-n2*))))))))))
1545 (tagbody
1547 (setf p *rhyp-w*
1548 ix *rhyp-minjx*
1549 u (* ($random 1.0) scale))
1551 (cond ((> u p)
1552 (setf u (- u p))
1553 (setf p (* p (- *rhyp-n1* ix) (- *rhyp-k* ix)))
1554 (setf ix (1+ ix))
1555 (setf p (/ p ix (+ *rhyp-n2* (- *rhyp-k*) ix)))
1556 (if (> ix *rhyp-maxjx*) (go l10) (go l20))))))
1557 (t (cond ((or setup1 setup2) ;; III : h2pe ----------------------------
1558 (setf *rhyp-s* (sqrt (/ (* (- *rhyp-tn* *rhyp-k*) *rhyp-k* *rhyp-n1* *rhyp-n2*)
1559 (- *rhyp-tn* 1) *rhyp-tn* *rhyp-tn*)))
1560 (setf *rhyp-d* (+ (round (* 1.5 *rhyp-s*)) 0.5))
1561 (setf *rhyp-xl* (+ *rhyp-m* (- *rhyp-d*) 0.5)
1562 *rhyp-xr* (+ *rhyp-m* *rhyp-d* 0.5))
1563 (setf *rhyp-a* (+ (afc *rhyp-m*)
1564 (afc (- *rhyp-n1* *rhyp-m*))
1565 (afc (- *rhyp-k* *rhyp-m*))
1566 (afc (+ *rhyp-n2* (- *rhyp-k*) *rhyp-m*))))
1567 (setf *rhyp-kl* (exp (- *rhyp-a*
1568 (afc (round *rhyp-xl*))
1569 (afc (round (- *rhyp-n1* *rhyp-xl*)))
1570 (afc (round (- *rhyp-k* *rhyp-xl*)))
1571 (afc (round (+ *rhyp-n2* (- *rhyp-k*) *rhyp-xl*))))))
1572 (setf *rhyp-kr* (exp (- *rhyp-a*
1573 (afc (round (- *rhyp-xr* 1)))
1574 (afc (round (+ *rhyp-n1* (- *rhyp-xr*) 1)))
1575 (afc (round (+ *rhyp-k* (- *rhyp-xr*) 1)))
1576 (afc (round (+ *rhyp-n2* (- *rhyp-k*) *rhyp-xr* (- 1)))))))
1577 (setf *rhyp-lamdl* (- (log (/ (* *rhyp-xl* (+ *rhyp-n2* (- *rhyp-k*) *rhyp-xl*))
1578 (+ *rhyp-n1* (- *rhyp-xl*) 1.0)
1579 (+ *rhyp-k* (- *rhyp-xl*) 1.0))))
1580 *rhyp-lamdr* (- (log (/ (* (+ *rhyp-n1* (- *rhyp-xr*) 1.0)
1581 (+ *rhyp-k* (- *rhyp-xr*) 1.0))
1582 *rhyp-xr*
1583 (+ *rhyp-n2* (- *rhyp-k*) *rhyp-xr*)))))
1584 (setf *rhyp-p1* (+ *rhyp-d* *rhyp-d*)
1585 *rhyp-p2* (+ *rhyp-p1* (/ *rhyp-kl* *rhyp-lamdl*))
1586 *rhyp-p3* (+ *rhyp-p2* (/ *rhyp-kr* *rhyp-lamdr*)))))
1587 (tagbody
1589 (setf u (* ($random 1.0) *rhyp-p3*))
1590 (setf v ($random 1.0))
1591 (cond ((< u *rhyp-p1*)
1592 ;; rectangular region
1593 (setf ix (round (+ *rhyp-xl* u))))
1594 ((<= u *rhyp-p2*)
1595 ;; left tail
1596 (setf ix (round (+ *rhyp-xl* (/ (log v) *rhyp-lamdl*))))
1597 (if (< ix *rhyp-minjx*) (go l30))
1598 (setf v (* v (- u *rhyp-p1*) *rhyp-lamdl*)))
1599 (t ;; right tail
1600 (setf ix (round (- *rhyp-xr* (/ (log v) *rhyp-lamdr*))))
1601 (if (> ix *rhyp-maxjx*) (go l30))
1602 (setf v (* v (- u *rhyp-p2*) *rhyp-lamdr*))))
1603 ;; acceptance/rejection test
1604 (cond ((or (< *rhyp-m* 100) (<= ix 50))
1605 ;; explicit evaluation
1606 (setf f 1.0)
1607 (cond ((< *rhyp-m* ix)
1608 (do ((i (+ *rhyp-m* 1) (1+ i)))
1609 ((> i ix) 'done)
1610 (setf f (/ (* f (+ *rhyp-n1* (- i) 1) (+ *rhyp-k* (- i) 1))
1611 (+ *rhyp-n2* (- *rhyp-k*) i)
1612 i))))
1613 ((> *rhyp-m* ix)
1614 (do ((i (+ ix 1) (1+ i)))
1615 ((> i *rhyp-m*) 'done)
1616 (setf f (/ (* f i (+ *rhyp-n2* (- *rhyp-k*) i))
1617 (- *rhyp-n1* i)
1618 (- *rhyp-k* i))))))
1619 (cond ((<= v f) (setf reject nil))))
1620 (t ;; squeeze using upper and lower bounds
1621 (setf y ix
1622 y1 (+ y 1.0)
1623 ym (- y *rhyp-m*)
1624 yn (+ *rhyp-n1* (- *rhyp-k*) 1.0)
1625 yk (+ *rhyp-k* (- y) 1.0)
1626 nk (+ *rhyp-n2* (- *rhyp-k*) y1)
1627 r (/ (- ym) y1)
1628 *rhyp-s* (/ ym yn)
1629 tt (/ ym yk)
1630 e (/ (- ym) nk)
1631 g (- (/ (* yn yk) (* y1 nk)) 1.0)
1632 dg 1.0)
1633 (if (< g 1.0) (setf dg (+ 1.0 g)))
1634 (setf gu (* g (+ 1.0 (* g (+ -0.5 (/ g 3.0)))))
1635 gl (- gu (/ (* g g g g 0.25) dg))
1636 xm (+ *rhyp-m* 0.5)
1637 xn (+ *rhyp-n1* (- *rhyp-m*) 0.5)
1638 xk (+ *rhyp-k* (- *rhyp-m*) 0.5)
1639 nm (+ *rhyp-n2* (- *rhyp-k*) xm)
1640 ub (+ (* y gu)
1641 (* (- *rhyp-m*) gl)
1642 deltau
1643 (* xm r (+ 1.0 (* r (+ -0.5 (/ r 3.0)))))
1644 (* xn *rhyp-s* (+ 1.0 (* *rhyp-s* (+ -0.5 (/ *rhyp-s* 3.0)))))
1645 (* xk tt (+ 1.0 (* tt (+ -0.5 (/ tt 3.0)))))
1646 (* nm e (+ 1.0 (* e (+ -0.5 (/ e 3.0)))))))
1647 ;; test against upper bound
1648 (setf alv (log v))
1649 (cond ((> alv ub)
1650 (setf reject t))
1651 (t (setf dr (* r r r r xm))
1652 (if (< r 0.0) (setf dr (/ dr (+ 1.0 r))))
1653 (setf ds (* *rhyp-s* *rhyp-s* *rhyp-s* *rhyp-s* xn))
1654 (if (< *rhyp-s* 0.0) (setf ds (/ ds (+ 1.0 *rhyp-s*))))
1655 (setf dt (* tt tt tt tt xk))
1656 (if (< tt 0.0) (setf dt (/ dt (+ 1.0 tt))))
1657 (setf de (* e e e e nm))
1658 (if (< e 0.0) (setf de (/ de (+ 1.0 e))))
1659 (cond ((< alv (- ub
1660 (* 0.25 (+ dr ds dt de))
1661 (* (+ y *rhyp-m*) (- gl gu))
1662 deltal))
1663 (setf reject nil))
1664 ((<= alv (- *rhyp-a*
1665 (afc ix)
1666 (afc (- *rhyp-n1* ix))
1667 (afc (- *rhyp-k* ix))
1668 (afc (+ *rhyp-n2* (- *rhyp-k*) ix))))
1669 (setf reject nil))
1670 (t (setf reject t)))))))
1671 (if reject (go l30)))))
1672 ;; return appropriate variate
1673 (cond ((>= (+ kk kk) *rhyp-tn*)
1674 (cond ((> nn1 nn2)
1675 (+ kk (- nn2) ix))
1676 (t (- nn1 ix))))
1677 (t (cond ((> nn1 nn2)
1678 (- kk ix))
1679 (t ix))))))
1682 ;; The sample size ss must be a non negative integer.
1683 ;; If ss=0, returns a number, otherwise a maxima list
1684 ;; of length ss
1685 (defun rndhypergeo (n1 n2 n ss &aux sample)
1686 (declare (type integer n1 n2 n ss))
1687 (cond ((= ss 0)
1688 (rndhypergeo-kachit n1 n2 n) )
1689 (t (setf sample nil)
1690 (dotimes (i ss (cons '(mlist simp) sample))
1691 (setf sample (cons (rndhypergeo n1 n2 n 0) sample))) )) )
1695 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1696 ;; ;;
1697 ;; Negative binomial random simulation ;;
1698 ;; ;;
1699 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1702 ;; Generates random negative binomial variates (n p)
1703 ;; Reference:
1704 ;; [1] Devroye, L. (1986)
1705 ;; Non-Uniform Random Variate Generation.
1706 ;; Springer Verlag, p. 480
1707 ;; Comments:
1708 ;; [1] This is a translation from C of the
1709 ;; rnbinom.c file in the R statistical package.
1710 (defun rndnegbinom-devroye (n p)
1711 (declare (type flonum p))
1712 (rndpoisson (rndgamma n (/ (- 1.0 p) p) 0) 0) )
1715 ;; The sample size ss must be a non negative integer.
1716 ;; If ss=0, returns a number, otherwise a maxima list
1717 ;; of length ss
1718 (defun rndnegbinom (n p ss &aux sample)
1719 (cond ((= ss 0)
1720 (rndnegbinom-devroye n p) )
1721 (t (setf sample nil)
1722 (dotimes (i ss (cons '(mlist simp) sample))
1723 (setf sample (cons (rndnegbinom n p 0) sample))) )) )