In code for index display properties, protect property getting from non-symbol arguments.
[maxima.git] / share / distrib / numdistrib.lisp
blob30b439a67ba427a65cba656fdf7ac9f77f7f5798
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*)
801 (defun rndbeta-cheng (aa bb )
802 (declare (type flonum aa bb))
803 (let (qsame u1 u2 v w z tt r s y genbet
804 (expmax 7.0) (infnty 1.0e304))
805 (declare (type flonum expmax infnty))
806 (if (and (= *rbeta-olda* aa) (= *rbeta-oldb* bb))
807 (setf qsame t)
808 (setf qsame nil
809 *rbeta-olda* aa
810 *rbeta-oldb* bb))
812 (tagbody
813 (if (<= (min aa bb) 1.0) (go s100))
815 ;; Algorithm BB - Initialize
816 (if qsame (go s40))
817 (setf *rbeta-a* (min aa bb))
818 (setf *rbeta-b* (max aa bb))
819 (setf *rbeta-alpha* (+ *rbeta-a* *rbeta-b*))
820 (setf *rbeta-beta* (sqrt (/ (- *rbeta-alpha* 2.0)
821 (- (* 2.0 *rbeta-a* *rbeta-b*) *rbeta-alpha*))))
822 (setf *rbeta-gamma* (+ *rbeta-a* (/ 1.0 *rbeta-beta*)))
825 (setf u1 ($random 1.0))
827 ;; Step 1
828 (setf u2 ($random 1.0))
829 (setf v (* *rbeta-beta* (log (/ u1 (- 1.0 u1)))))
830 (if (<= v expmax)
831 (setf w (* *rbeta-a* (exp v)))
832 (setf w infnty))
833 (setf z (* u2 (expt u1 2)))
834 (setf r (- (* *rbeta-gamma* v) 1.3862944))
835 (setf s (+ *rbeta-a* r (- w)))
837 ;; Step 2
838 (if (>= (+ s 2.609438) (* 5.0 z)) (go s70))
840 ;; Step 3
841 (setf tt (log z))
842 (if (> s tt) (go s70))
844 ;; Step 4
845 (if (< (+ r (* *rbeta-alpha* (log (/ *rbeta-alpha* (+ *rbeta-b* w))))) tt)
846 (go s40))
849 ;; Step 5
850 (if (/= aa *rbeta-a*)
851 (setf genbet (/ *rbeta-b* (+ *rbeta-b* w)))
852 (setf genbet (/ w (+ *rbeta-b* w))))
853 (go s230)
855 s100
856 ;; Algorithm BC - Initialize
857 (if qsame (go s120))
858 (setf *rbeta-a* (max aa bb))
859 (setf *rbeta-b* (min aa bb))
860 (setf *rbeta-alpha* (+ *rbeta-a* *rbeta-b*))
861 (setf *rbeta-beta* (/ 1.0 *rbeta-b*))
862 (setf *rbeta-delta* (+ 1.0 *rbeta-a* (- *rbeta-b*)))
863 (setf *rbeta-k1* (/ (* *rbeta-delta* (+ 1.38889e-2 (* 4.16667e-2 *rbeta-b*)))
864 (- (* *rbeta-a* *rbeta-beta*) 0.777778)))
865 (setf *rbeta-k2* (+ 0.25 (* (+ 0.5 (/ 0.25 *rbeta-delta*)) *rbeta-b*)))
867 s120
868 (setf u1 ($random 1.0))
870 ;; Step 1
871 (setf u2 ($random 1.0))
872 (if (>= u1 0.5) (go s130))
874 ;; Step 2
875 (setf y (* u1 u2))
876 (setf z (* u1 y))
877 (if (>= (+ (* 0.25 u2) z (- y)) *rbeta-k1*) (go s120))
878 (go s170)
880 s130
881 ;; Step 3
882 (setf z (* (expt u1 2) u2))
883 (if (> z 0.25) (go s160))
884 (setf v (* *rbeta-beta* (log (/ u1 (- 1.0 u1)))))
885 (if (<= v expmax)
886 (setf w (* *rbeta-a* (exp v)))
887 (setf w infnty))
888 (go s200)
890 s160
891 (if (>= z *rbeta-k2*) (go s120))
893 s170
894 ;; Step 4 Step 5
895 (setf v (* *rbeta-beta* (log (/ u1 (- 1.0 u1)))))
896 (if (<= v expmax)
897 (setf w (* *rbeta-a* (exp v)))
898 (setf w infnty))
900 s190
901 (if (< (- (* *rbeta-alpha* (+ (log (/ *rbeta-alpha* (+ *rbeta-b* w))) v)) 1.3862944) (log z))
902 (go s120))
904 s200
905 ;; Step 6
906 (if (/= aa *rbeta-a*)
907 (setf genbet (/ *rbeta-b* (+ *rbeta-b* w)))
908 (setf genbet (/ w (+ *rbeta-b* w))))
910 s230)
911 genbet))
914 ;; The sample size ss must be a non negative integer.
915 ;; If ss=0, returns a number, otherwise a maxima list
916 ;; of length ss
917 (defun rndbeta (a b ss &aux sample)
918 (cond ((= ss 0)
919 (rndbeta-cheng a b) )
920 (t (setf sample nil)
921 (dotimes (i ss (cons '(mlist simp) sample))
922 (setf sample (cons (rndbeta a b 0) sample))) )) )
925 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
926 ;; ;;
927 ;; Binomial random simulation ;;
928 ;; ;;
929 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
932 ;; Generates random binomial variates (n p)
933 ;; Reference:
934 ;; [1] Kachitvichyanukul, V., Schmeiser, B.W.
935 ;; Binomial Random Variate Generation.
936 ;; Communications of the ACM, 31, 2
937 ;; (February, 1988) 216
938 ;; Comments:
939 ;; [1] Algorithm BTPE
940 ;; [2] This is a translation from C of the
941 ;; ranlib.c library. R statistical package uses this same
942 ;; algorithm but in a more structured style.
943 ;; [3] Some global variables are defined in order to
944 ;; save time when many random variables are generated.
945 (defvar *rbin-psave* -1.0)
946 (defvar *rbin-nsave* -1) ;; integer
947 (defvar *rbin-m*) ;; integer
948 (defvar *rbin-xnp*)
949 (defvar *rbin-p*)
950 (defvar *rbin-q*)
951 (defvar *rbin-ffm*)
952 (defvar *rbin-xnpq*)
953 (defvar *rbin-fm*)
954 (defvar *rbin-xm*)
955 (defvar *rbin-xl*)
956 (defvar *rbin-xr*)
957 (defvar *rbin-c*)
958 (defvar *rbin-al*)
959 (defvar *rbin-xll*)
960 (defvar *rbin-xlr*)
961 (defvar *rbin-p1*)
962 (defvar *rbin-p2*)
963 (defvar *rbin-p3*)
964 (defvar *rbin-p4*)
965 (defvar *rbin-qn*)
966 (defvar *rbin-r*)
967 (defvar *rbin-g*)
968 (defun rndbinomial-kachit (n pp)
969 (declare (type integer n)
970 (type flonum pp))
971 (let (u v x f amaxp ynorm alv x1 f1 z w z2 x2 f2 w2 ix k t1 mp ix1)
972 (tagbody
973 (if (/= pp *rbin-psave*) (go s10))
974 (if (/= n *rbin-nsave*) (go s20))
975 (if (< *rbin-xnp* 30.0) (go s150))
976 (go s30)
979 ;; setup, perform only when parameters change
980 (setf *rbin-psave* pp)
981 (setf *rbin-p* (min *rbin-psave* (- 1.0 *rbin-psave*)))
982 (setf *rbin-q* (- 1.0 *rbin-p*))
985 (setf *rbin-xnp* (* n *rbin-p*))
986 (setf *rbin-nsave* n)
987 (if (< *rbin-xnp* 30.0) (go s140))
988 (setf *rbin-ffm* (+ *rbin-xnp* *rbin-p*))
989 (setf *rbin-m* (round *rbin-ffm*))
990 (setf *rbin-fm* *rbin-m*)
991 (setf *rbin-xnpq* (* *rbin-xnp* *rbin-q*))
992 (setf *rbin-p1* (+ 0.5 (round (- (* 2.195 (sqrt *rbin-xnpq*)) (* 4.6 *rbin-q*)))))
993 (setf *rbin-xm* (+ 0.5 *rbin-fm*))
994 (setf *rbin-xl* (- *rbin-xm* *rbin-p1*))
995 (setf *rbin-xr* (+ *rbin-xm* *rbin-p1*))
996 (setf *rbin-c* (+ 0.134 (/ 20.5 (+ 15.3 *rbin-fm*))))
997 (setf *rbin-al* (/ (- *rbin-ffm* *rbin-xl*) (- *rbin-ffm* (* *rbin-xl* *rbin-p*))))
998 (setf *rbin-xll* (* *rbin-al* (+ 1.0 (* 0.5 *rbin-al*))))
999 (setf *rbin-al* (/ (- *rbin-xr* *rbin-ffm*) (* *rbin-xr* *rbin-q*)))
1000 (setf *rbin-xlr* (* *rbin-al* (+ 1.0 (* 0.5 *rbin-al*))))
1001 (setf *rbin-p2* (* *rbin-p1* (+ 1.0 *rbin-c* *rbin-c*)))
1002 (setf *rbin-p3* (+ *rbin-p2* (/ *rbin-c* *rbin-xll*)))
1003 (setf *rbin-p4* (+ *rbin-p3* (/ *rbin-c* *rbin-xlr*)))
1006 ;; generate variate
1007 (setf u (* *rbin-p4* ($random 1.0)))
1008 (setf v ($random 1.0))
1010 ;; triangular region
1011 (if (> u *rbin-p1*) (go s40))
1012 (setf ix (round (+ *rbin-xm* (- (* *rbin-p1* v)) u)))
1013 (go s170)
1016 ;; parallelogram region
1017 (if (> u *rbin-p2*) (go s50))
1018 (setf x (+ *rbin-xl* (/ (- u *rbin-p1*) *rbin-c*)))
1019 (setf v (+ (* v *rbin-c*) 1.0 (- (/ (abs (- *rbin-xm* x)) *rbin-p1*))))
1020 (if (or (> v 1.0) (<= v 0.0)) (go s30))
1021 (setf ix (round x))
1022 (go s70)
1025 ;; left tail
1026 (if (> u *rbin-p3*) (go s60))
1027 (setf ix (round (+ *rbin-xl* (/ (log v) *rbin-xll*))))
1028 (if (< ix 0) (go s30))
1029 (setf v (* v (- u *rbin-p2*) *rbin-xll*))
1030 (go s70)
1033 ;; right tail
1034 (setf ix (round (- *rbin-xr* (/ (log v) *rbin-xlr*))))
1035 (if (> ix n) (go s30))
1036 (setf v (* v (- u *rbin-p3*) *rbin-xlr*))
1039 ;; determine appropriate way to perform accept/reject test
1040 (setf k (abs (- ix *rbin-m*)))
1041 (if (and (> k 20) (< k (- (/ *rbin-xnpq* 2.0) 1.0))) (go s130))
1043 ;; explicit evaluation
1044 (setf f 1.0)
1045 (setf *rbin-r* (/ *rbin-p* *rbin-q*))
1046 (setf *rbin-g* (* *rbin-r* (+ n 1.0)))
1047 (setf t1 (- *rbin-m* ix))
1048 (if (< t1 0)
1049 (go s80)
1050 (if (= t1 0)
1051 (go s120)
1052 (go s100)))
1055 (setf mp (+ *rbin-m* 1))
1056 (do ((i mp (1+ i)))
1057 ((> i ix) 'done)
1058 (setf f (* f (- (/ *rbin-g* i) *rbin-r*))))
1059 (go s120)
1061 s100
1062 (setf ix1 (+ ix 1))
1063 (do ((i ix1 (1+ i)))
1064 ((> i *rbin-m*) 'done)
1065 (setf f (/ f (- (/ *rbin-g* i) *rbin-r*))))
1067 s120
1068 (if (<= v f) (go s170))
1069 (go s30)
1071 s130
1072 ;; squeezing using upper and lower bounds on alog(f(x))
1073 (setf amaxp (* (/ k *rbin-xnpq*)
1074 (+ (/ (+ (* k (+ (/ k 3.0) 0.625)) 0.1666666666666) *rbin-xnpq*)
1075 0.5)))
1076 (setf ynorm (- (/ (* k k) (* 2.0 *rbin-xnpq*))))
1077 (setf alv (log v))
1078 (if (< alv (- ynorm amaxp)) (go s170))
1079 (if (> alv (+ ynorm amaxp)) (go s30))
1081 ;; Stirling's formula to machine accuracy for
1082 ;; the final acceptance / rejection test
1083 (setf x1 (+ ix 1.0))
1084 (setf f1 (+ *rbin-fm* 1.0))
1085 (setf z (+ n 1.0 (- *rbin-fm*)))
1086 (setf w (+ n 1.0 (- ix)))
1087 (setf z2 (* z z))
1088 (setf x2 (* x1 x1))
1089 (setf f2 (* f1 f1))
1090 (setf w2 (* w w))
1091 (if (<= alv (+ (* *rbin-xm* (log (/ f1 x1)))
1092 (* (+ n (- *rbin-m*) 0.5) (log (/ z w)))
1093 (* (+ ix (- *rbin-m*)) (log (/ (* w *rbin-p*) (* x1 *rbin-q*))))
1094 (/ (/ (+ 13860.0 (- (/ (+ 462.0 (- (/ (+ 132.0
1095 (- (/ (+ 99.0 (- (/ 140.0 f2))) f2))) f2))) f2))) f1) 166320.0)
1096 (/ (/ (+ 13860.0 (- (/ (+ 462.0 (- (/ (+ 132.0
1097 (- (/ (+ 99.0 (- (/ 140.0 z2))) z2))) z2))) z2))) z) 166320.0)
1098 (/ (/ (+ 13860.0 (- (/ (+ 462.0 (- (/ (+ 132.0
1099 (- (/ (+ 99.0 (- (/ 140.0 x2))) x2))) x2))) x2))) x1) 166320.0)
1100 (/ (/ (+ 13860.0 (- (/ (+ 462.0 (- (/ (+ 132.0
1101 (- (/ (+ 99.0 (- (/ 140.0 w2))) w2))) w2))) w2))) w) 166320.0)))
1102 (go s170))
1103 (go s30)
1105 s140
1106 ;; inverse cdf logic for mean less than 30
1107 (setf *rbin-qn* (expt *rbin-q* n))
1108 (setf *rbin-r* (/ *rbin-p* *rbin-q*))
1109 (setf *rbin-g* (* *rbin-r* (+ n 1.0)))
1111 s150
1112 (setf ix 0)
1113 (setf f *rbin-qn*)
1114 (setf u ($random 1.0))
1116 s160
1117 (if (< u f) (go s170))
1118 (if (> ix 110) (go s150))
1119 (setf u (- u f))
1120 (setf ix (+ ix 1))
1121 (setf f (* f (- (/ *rbin-g* ix) *rbin-r*)))
1122 (go s160)
1124 s170)
1125 (if (> *rbin-psave* 0.5)
1126 (- n ix)
1127 ix)))
1130 ;; The sample size ss must be a non negative integer.
1131 ;; If ss=0, returns a number, otherwise a maxima list
1132 ;; of length ss
1133 (defun rndbinomial (n p ss &aux sample)
1134 (cond ((= ss 0)
1135 (rndbinomial-kachit n p))
1136 (t (setf sample nil)
1137 (dotimes (i ss (cons '(mlist simp) sample))
1138 (setf sample (cons (rndbinomial n p 0) sample))))))
1141 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1142 ;; ;;
1143 ;; Poisson random simulation ;;
1144 ;; ;;
1145 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1148 ;; Generates random Poisson variates (m)
1149 ;; Reference:
1150 ;; [1] Ahrens, J.H. and Dieter, U.
1151 ;; Computer Generation of Poisson Deviates
1152 ;; From Modified Normal Distributions.
1153 ;; ACM Trans. Math. Software, 8, 2
1154 ;; (June 1982),163-179
1155 ;; Comments:
1156 ;; [1] Slightly modified version of the program in the
1157 ;; above article
1158 ;; [2] This is a translation from C of the
1159 ;; ranlib.c library. R statistical package uses this same
1160 ;; algorithm but in a more structured style.
1161 ;; [3] Some global variables are defined in order to
1162 ;; save time when many random variables are generated.
1163 (defvar *rpos-muold* 0.0)
1164 (defvar *rpos-muprev* 0.0)
1165 (defvar *rpos-s*)
1166 (defvar *rpos-l*)
1167 (defvar *rpos-d*)
1168 (defvar *rpos-p0*)
1169 (defvar *rpos-omega*)
1170 (defvar *rpos-b1*)
1171 (defvar *rpos-b2*)
1172 (defvar *rpos-c*)
1173 (defvar *rpos-c0*)
1174 (defvar *rpos-c1*)
1175 (defvar *rpos-c2*)
1176 (defvar *rpos-c3*)
1177 (defvar *rpos-m*)
1178 (defvar *rpos-p*)
1179 (defvar *rpos-q*)
1180 (defvar *rpos-pp* (make-array 35 :initial-element 0.0 :element-type 'flonum))
1181 (defun rndpoisson-ahrens (mu)
1182 (declare (type flonum mu))
1184 ;; This algorithm doesn't handle MU = 0 correctly; *RPOS-S* and friends aren't initialized.
1185 ;; So just handle the special case here and bail out.
1186 (if (= mu 0.0)
1187 (return-from rndpoisson-ahrens 0))
1189 (let ( ignpoi j kflag del difmuk e fk fx fy g px py tt u v x xx
1190 (a0 -0.5) (a1 0.3333333) (a2 -0.2500068) (a3 0.2000118)
1191 (a4 -0.1661269) (a5 0.1421878) (a6 -0.1384794) (a7 0.125006)
1192 (fact (make-array 10
1193 :element-type 'flonum
1194 :initial-contents '(1.0 1.0 2.0 6.0 24.0 120.0
1195 720.0 5040.0 40320.0 362880.0))))
1196 (declare (type flonum a0 a1 a2 a3 a4 a5 a6 a7))
1197 (declare (type (simple-array flonum (10)) fact))
1198 (tagbody
1199 (if (= mu *rpos-muprev*) (go s10))
1200 (if (< mu 10.0) (go s120))
1202 ;; Case A. (Recalculation of, *rpos-s*, *rpos-d*, *rpos-l* if mu has changed)
1203 (setf *rpos-muprev* mu)
1204 (setf *rpos-s* (sqrt mu))
1205 (setf *rpos-d* (* 6.0 mu mu))
1206 (setf *rpos-l* (floor (- mu 1.1484)))
1209 ;; Step n. Normal sample
1210 (setf g (+ mu (* *rpos-s* (rndnormal 0))))
1211 (if (< g 0.0) (go s20))
1212 (setf ignpoi (floor g))
1214 ;; Step I. Immediate acceptance if ignpoi is large enough
1215 (if (>= ignpoi *rpos-l*) (go s200))
1217 ;; Step S. Squeez acceptance
1218 (setf fk (coerce ignpoi 'flonum))
1219 (setf difmuk (- mu fk))
1220 (setf u ($random 1.0))
1221 (if (>= (* *rpos-d* u) (* difmuk difmuk difmuk)) (go s200))
1224 ;; Step P. Preparations for steps Q and H.
1225 ;; (Recalculations of parameters if necessary)
1226 ;; .3989423...=(2*%pi)^(-.5) .416667...E-1=1./24. .1428571...=1./7.
1227 ;; The quantities *rpos-b1*, *rpos-b2*, *rpos-c3*, *rpos-c2*, *rpos-c1*, *rpos-c0* are for thr Hermite
1228 ;; approximations to the discrete normal probabilities fk.
1229 ;; c=0.1069/mu guarantees majorization by the 'hat'-function.
1230 (if (= mu *rpos-muold*) (go s30))
1231 (setf *rpos-muold* mu)
1232 (setf *rpos-omega* (/ 0.39894228040143 *rpos-s*))
1233 (setf *rpos-b1* (/ 0.41666666666667e-1 mu))
1234 (setf *rpos-b2* (* 0.3 *rpos-b1* *rpos-b1*))
1235 (setf *rpos-c3* (* 0.14285714285714 *rpos-b1* *rpos-b2*))
1236 (setf *rpos-c2* (- *rpos-b2* (* 15.0 *rpos-c3*)))
1237 (setf *rpos-c1* (+ *rpos-b1* (* -6.0 *rpos-b2*) (* 45.0 *rpos-c3*)))
1238 (setf *rpos-c0* (+ 1.0 (- *rpos-b1*) (* 3.0 *rpos-b2*) (* -15.0 *rpos-c3*)))
1239 (setf *rpos-c* (/ 0.1069 mu))
1242 (if (< g 0.0) (go s50))
1244 ;; subroutine F is called
1245 (setf kflag 0)
1246 (go s70)
1249 ;; Step Q. Quotient acceptance (rare case)
1250 (if (<= (- fy (* u fy)) (* py (exp (- px fx)))) (go s200))
1253 ;; Step E. Exponential sample - rndexp is called for
1254 ;; standard exponential variate e and sample tt from the Laplace 'hat'
1255 ;; (if tt <= -.6744 then pk < fk for all mu >= 10.)
1256 (setf e (rndexp 1.0 0))
1257 (setf u ($random 1.0))
1258 (setf u (+ u (- u 1.0)))
1259 (setf tt (+ 1.8 (if (or (and (> u 0.0) (< e 0.0))
1260 (and (< u 0.0) (> e 0.0)))
1261 (- e)
1262 e)))
1263 (if (<= tt -0.6744) (go s50))
1264 (setf ignpoi (floor (+ mu (* *rpos-s* tt))))
1265 (setf fk (coerce ignpoi 'flonum))
1266 (setf difmuk (- mu fk))
1268 ;; subroutine F is called
1269 (setf kflag 1)
1270 (go s70)
1273 ;; Step F. Hat acceptance (E is repeated on rejection)
1274 (if (> (* *rpos-c* (abs u)) (- (* py (exp (+ px e))) (* fy (exp (+ fx e))))) (go s50))
1275 (go s200)
1278 ;; Step F. Subroutine F. Calculation of px, py, fx, fy.
1279 ;; Case ignpoi < 10 uses factorials from table fact
1280 (if (>= ignpoi 10) (go s80))
1281 (setf px (- mu))
1282 (setf py (/ (expt mu ignpoi) (aref fact ignpoi)))
1283 (go s110)
1286 ;; Case ignpoi >= 10 uses polynomial approximation
1287 ;; a0-a7 for accuracy when advisable
1288 ;; .8333333E-1=1./12. .3989423=(2*%pi)^(-.5)
1289 (setf del (/ 8.333333333333333e-2 fk))
1290 (setf del (- del (* 4.8 del del del)))
1291 (setf v (/ difmuk fk))
1292 (if (<= (abs v) 0.25) (go s90))
1293 (setf px (+ (* fk (log (+ 1.0 v))) (- difmuk) (- del)))
1294 (go s100)
1297 (setf px (+ (* fk v v
1298 (+ (* (+ (* (+ (* (+ (* (+ (* (+ (* (+ (* a7 v)
1299 a6) v) a5) v) a4) v) a3) v) a2) v) a1) v) a0))
1300 (- del)))
1302 s100
1303 (setf py (/ 0.39894228040143 (sqrt fk)))
1305 s110
1306 (setf x (/ (- 0.5 difmuk) *rpos-s*))
1307 (setf xx (* x x))
1308 (setf fx (* -0.5 xx))
1309 (setf fy (* *rpos-omega* (+ (* (+ (* (+ (* *rpos-c3* xx)
1310 *rpos-c2*) xx) *rpos-c1*) xx) *rpos-c0*)))
1311 (if (<= kflag 0) (go s40))
1312 (go s60)
1314 s120
1315 ;; Case B. Start new table and calculate *rpos-p0* if necessary
1316 (setf *rpos-muprev* 0.0)
1317 (if (= mu *rpos-muold*) (go s130))
1318 (setf *rpos-muold* mu)
1319 (setf *rpos-m* (max 1 (floor mu)))
1320 (setf *rpos-l* 0)
1321 (setf *rpos-p* (exp (- mu)))
1322 (setf *rpos-p0* *rpos-p*)
1323 (setf *rpos-q* *rpos-p*)
1325 s130
1326 ;; Step U. Uniform sample for inversion method
1327 (setf u ($random 1.0))
1328 (setf ignpoi 0)
1329 (if (<= u *rpos-p0*) (go s200))
1331 ;; Step T.
1332 (if (= *rpos-l* 0) (go s150))
1333 (setf j 1)
1334 (if (> u 0.458) (setf j (min *rpos-l* *rpos-m*)))
1335 (do ((k j (1+ k)))
1336 ((> k *rpos-l*) 'done)
1337 (cond ((<= u (aref *rpos-pp* (- k 1)))
1338 (setf ignpoi k)
1339 (go s200))))
1340 (if (= *rpos-l* 35) (go s130))
1342 s150
1343 ;; Step C. Creation of new Poisson probabilities p
1344 ;; and their cumulatives *rpos-q*=*rpos-pp*(k)
1345 (setf *rpos-l* (1+ *rpos-l*))
1346 (do ((k *rpos-l* (1+ k)))
1347 ((> k 35) 'done)
1348 (setf *rpos-p* (/ (* *rpos-p* mu) (float k)))
1349 (setf *rpos-q* (+ *rpos-q* *rpos-p*))
1350 (setf (aref *rpos-pp* (- k 1)) *rpos-q*)
1351 (cond ((<= u *rpos-q*)
1352 (setf *rpos-l* k)
1353 (setf ignpoi k)
1354 (go s200)) ) )
1355 (setf *rpos-l* 35)
1356 (go s130)
1358 s200 )
1359 ignpoi))
1362 ;; The sample size ss must be a non negative integer.
1363 ;; If ss=0, returns a number, otherwise a maxima list
1364 ;; of length ss
1365 (defun rndpoisson (m ss &aux sample)
1366 (declare (type flonum m))
1367 (declare (type integer ss))
1368 (cond ((= ss 0)
1369 (rndpoisson-ahrens m))
1370 (t (setf sample nil)
1371 (dotimes (i ss (cons '(mlist simp) sample))
1372 (setf sample (cons (rndpoisson m 0) sample))))))
1375 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1376 ;; ;;
1377 ;; Geometric random simulation ;;
1378 ;; ;;
1379 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1382 ;; Generates random geometric variates (p)
1383 ;; by simulation of Bernoulli trials.
1384 (defun rndgeo-trials (p)
1385 (declare (type flonum p))
1386 (let ((sum 0))
1387 (declare (type integer sum))
1388 (loop (if (<= ($random 1.0) p) (return sum))
1389 (setf sum (1+ sum)))))
1392 ;; The sample size ss must be a non negative integer.
1393 ;; If ss=0, returns a number, otherwise a maxima list
1394 ;; of length ss
1395 (defun rndgeo (p ss &aux sample)
1396 (declare (type flonum p))
1397 (declare (type integer ss))
1398 (cond ((= ss 0)
1399 (rndgeo-trials p))
1400 (t (setf sample nil)
1401 (dotimes (i ss (cons '(mlist simp) sample))
1402 (setf sample (cons (rndgeo p 0) sample))))))
1405 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1406 ;; ;;
1407 ;; Hypergeometric random simulation ;;
1408 ;; ;;
1409 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1413 ;; Computes the logarithm of the factorial log(n!)
1414 ;; This function is called from rndhypergeo-kachit
1415 (defun afc (i)
1416 (declare (type integer i))
1417 (let ((di (float i))
1418 (al (make-array 9
1419 :element-type 'flonum
1420 :initial-contents '( 0.0
1421 0.0 ;; ln(0!)=ln(1)
1422 0.0 ;; ln(1!)=ln(1)
1423 0.69314718055994530941723212145817 ;; ln(2)
1424 1.79175946922805500081247735838070 ;; ln(6)
1425 3.17805383034794561964694160129705 ;; ln(24)
1426 4.78749174278204599424770093452324
1427 6.57925121201010099506017829290394
1428 8.52516136106541430016553103634712))))
1429 (declare (type flonum di))
1430 (declare (type (simple-array flonum (9)) al))
1431 (cond ((<= i 7)
1432 (aref al (1+ i)))
1433 (t (+ (* (+ di 0.5)
1434 (log di))
1435 (- di)
1436 (/ 0.83333333333333333333333333333333 di)
1437 (/ (- 0.00277777777777777777777777777778)
1438 di di di)
1439 0.91893853320467274178032973640562))) ) )
1442 ;; Generates random hypergeometric variates (n1, n2, n),
1443 ;; Reference:
1444 ;; [1] Kachitvichyanukul, V., Schmeiser, B.W. (1985)
1445 ;; Computer generation of hypergeometric random variates.
1446 ;; Journal of Statistical Computation and Simulation 22, 127-145.
1447 ;; Comments:
1448 ;; [1] This is a translation from C of the
1449 ;; rhyper.c file in the R statistical package.
1450 ;; [2] Some global variables are defined in order to
1451 ;; save time when many random variables are generated.
1452 ;; integer globals
1453 (defvar *rhyp-n1s* -1)
1454 (defvar *rhyp-n2s* -1)
1455 (defvar *rhyp-ks* -1)
1456 (defvar *rhyp-k*)
1457 (defvar *rhyp-n1*)
1458 (defvar *rhyp-n2*)
1459 (defvar *rhyp-m*)
1460 (defvar *rhyp-minjx*)
1461 (defvar *rhyp-maxjx*)
1462 ;; long globals
1463 (defvar *rhyp-tn*)
1464 (defvar *rhyp-a*)
1465 (defvar *rhyp-d*)
1466 (defvar *rhyp-s*)
1467 (defvar *rhyp-w*)
1468 (defvar *rhyp-xl*)
1469 (defvar *rhyp-xr*)
1470 (defvar *rhyp-kl*)
1471 (defvar *rhyp-kr*)
1472 (defvar *rhyp-lamdl*)
1473 (defvar *rhyp-lamdr*)
1474 (defvar *rhyp-p1*)
1475 (defvar *rhyp-p2*)
1476 (defvar *rhyp-p3*)
1477 (defun rndhypergeo-kachit (nn1 nn2 kk)
1478 (declare (type integer nn1 nn2 kk))
1479 (let (ix reject setup1 setup2 e f g p r tt u v y de dg dr ds dt gl
1480 gu nk nm ub xk xm xn y1 ym yn yk alv
1481 (con 57.56462733) (deltal 0.0078) (deltau 0.0034) (scale 1.e25))
1482 (declare (type flonum con deltal deltau scale))
1483 ;; if new parameter values, initialize
1484 (setf reject t)
1485 (cond ((or (/= nn1 *rhyp-n1s*) (/= nn2 *rhyp-n2s*))
1486 (setf setup1 t
1487 setup2 t))
1488 (t (cond ((/= kk *rhyp-ks*)
1489 (setf setup1 nil
1490 setup2 t))
1491 (t (setf setup1 nil
1492 setup2 nil)))))
1493 (cond (setup1
1494 (setf *rhyp-n1s* nn1
1495 *rhyp-n2s* nn2
1496 *rhyp-tn* (+ nn1 nn2))
1497 (cond ((<= nn1 nn2)
1498 (setf *rhyp-n1* nn1
1499 *rhyp-n2* nn2))
1500 (t (setf *rhyp-n1* nn2
1501 *rhyp-n2* nn1)))))
1502 (cond (setup2
1503 (setf *rhyp-ks* kk)
1504 (cond ((>= (+ kk kk) *rhyp-tn*)
1505 (setf *rhyp-k* (- *rhyp-tn* kk)))
1506 (t (setf *rhyp-k* kk)))))
1507 (cond ((or setup1 setup2)
1508 (setf *rhyp-m* (/ (* (+ *rhyp-k* 1.0) (+ *rhyp-n1* 1.0)) (+ *rhyp-tn* 2.0))
1509 *rhyp-minjx* (max 0 (- *rhyp-k* *rhyp-n2*))
1510 *rhyp-maxjx* (min *rhyp-n1* *rhyp-k*))))
1512 ;; generate random variate --- Three basic cases
1513 (cond ((= *rhyp-minjx* *rhyp-maxjx*) ;; I: degenerate distribution ------------
1514 (setf ix *rhyp-maxjx*)
1515 (cond ((>= (+ kk kk) *rhyp-tn*)
1516 (cond ((> nn1 nn2)
1517 (+ kk (- nn2) ix))
1518 (t (- nn1 ix))))
1519 (t (cond ((> nn1 nn2)
1520 (- kk ix))
1521 (t ix)))))
1522 ((< (- *rhyp-m* *rhyp-minjx*) 10) ;; II: inverse transformation ------------
1523 (cond ((or setup1 setup2)
1524 (cond ((< *rhyp-k* *rhyp-n2*)
1525 (setf *rhyp-w* (exp (+ con
1526 (afc *rhyp-n2*)
1527 (afc (+ *rhyp-n1* *rhyp-n2* (- *rhyp-k*)))
1528 (- (afc (- *rhyp-n2* *rhyp-k*)))
1529 (- (afc (+ *rhyp-n1* *rhyp-n2*)))))))
1530 (t (setf *rhyp-w* (exp (+ con
1531 (afc *rhyp-n1*)
1532 (afc *rhyp-k*)
1533 (- (afc (- *rhyp-k* *rhyp-n2*)))
1534 (- (afc (+ *rhyp-n1* *rhyp-n2*))))))))))
1535 (tagbody
1537 (setf p *rhyp-w*
1538 ix *rhyp-minjx*
1539 u (* ($random 1.0) scale))
1541 (cond ((> u p)
1542 (setf u (- u p))
1543 (setf p (* p (- *rhyp-n1* ix) (- *rhyp-k* ix)))
1544 (setf ix (1+ ix))
1545 (setf p (/ p ix (+ *rhyp-n2* (- *rhyp-k*) ix)))
1546 (if (> ix *rhyp-maxjx*) (go l10) (go l20))))))
1547 (t (cond ((or setup1 setup2) ;; III : h2pe ----------------------------
1548 (setf *rhyp-s* (sqrt (/ (* (- *rhyp-tn* *rhyp-k*) *rhyp-k* *rhyp-n1* *rhyp-n2*)
1549 (- *rhyp-tn* 1) *rhyp-tn* *rhyp-tn*)))
1550 (setf *rhyp-d* (+ (round (* 1.5 *rhyp-s*)) 0.5))
1551 (setf *rhyp-xl* (+ *rhyp-m* (- *rhyp-d*) 0.5)
1552 *rhyp-xr* (+ *rhyp-m* *rhyp-d* 0.5))
1553 (setf *rhyp-a* (+ (afc *rhyp-m*)
1554 (afc (- *rhyp-n1* *rhyp-m*))
1555 (afc (- *rhyp-k* *rhyp-m*))
1556 (afc (+ *rhyp-n2* (- *rhyp-k*) *rhyp-m*))))
1557 (setf *rhyp-kl* (exp (- *rhyp-a*
1558 (afc (round *rhyp-xl*))
1559 (afc (round (- *rhyp-n1* *rhyp-xl*)))
1560 (afc (round (- *rhyp-k* *rhyp-xl*)))
1561 (afc (round (+ *rhyp-n2* (- *rhyp-k*) *rhyp-xl*))))))
1562 (setf *rhyp-kr* (exp (- *rhyp-a*
1563 (afc (round (- *rhyp-xr* 1)))
1564 (afc (round (+ *rhyp-n1* (- *rhyp-xr*) 1)))
1565 (afc (round (+ *rhyp-k* (- *rhyp-xr*) 1)))
1566 (afc (round (+ *rhyp-n2* (- *rhyp-k*) *rhyp-xr* (- 1)))))))
1567 (setf *rhyp-lamdl* (- (log (/ (* *rhyp-xl* (+ *rhyp-n2* (- *rhyp-k*) *rhyp-xl*))
1568 (+ *rhyp-n1* (- *rhyp-xl*) 1.0)
1569 (+ *rhyp-k* (- *rhyp-xl*) 1.0))))
1570 *rhyp-lamdr* (- (log (/ (* (+ *rhyp-n1* (- *rhyp-xr*) 1.0)
1571 (+ *rhyp-k* (- *rhyp-xr*) 1.0))
1572 *rhyp-xr*
1573 (+ *rhyp-n2* (- *rhyp-k*) *rhyp-xr*)))))
1574 (setf *rhyp-p1* (+ *rhyp-d* *rhyp-d*)
1575 *rhyp-p2* (+ *rhyp-p1* (/ *rhyp-kl* *rhyp-lamdl*))
1576 *rhyp-p3* (+ *rhyp-p2* (/ *rhyp-kr* *rhyp-lamdr*)))))
1577 (tagbody
1579 (setf u (* ($random 1.0) *rhyp-p3*))
1580 (setf v ($random 1.0))
1581 (cond ((< u *rhyp-p1*)
1582 ;; rectangular region
1583 (setf ix (round (+ *rhyp-xl* u))))
1584 ((<= u *rhyp-p2*)
1585 ;; left tail
1586 (setf ix (round (+ *rhyp-xl* (/ (log v) *rhyp-lamdl*))))
1587 (if (< ix *rhyp-minjx*) (go l30))
1588 (setf v (* v (- u *rhyp-p1*) *rhyp-lamdl*)))
1589 (t ;; right tail
1590 (setf ix (round (- *rhyp-xr* (/ (log v) *rhyp-lamdr*))))
1591 (if (> ix *rhyp-maxjx*) (go l30))
1592 (setf v (* v (- u *rhyp-p2*) *rhyp-lamdr*))))
1593 ;; acceptance/rejection test
1594 (cond ((or (< *rhyp-m* 100) (<= ix 50))
1595 ;; explicit evaluation
1596 (setf f 1.0)
1597 (cond ((< *rhyp-m* ix)
1598 (do ((i (+ *rhyp-m* 1) (1+ i)))
1599 ((> i ix) 'done)
1600 (setf f (/ (* f (+ *rhyp-n1* (- i) 1) (+ *rhyp-k* (- i) 1))
1601 (+ *rhyp-n2* (- *rhyp-k*) i)
1602 i))))
1603 ((> *rhyp-m* ix)
1604 (do ((i (+ ix 1) (1+ i)))
1605 ((> i *rhyp-m*) 'done)
1606 (setf f (/ (* f i (+ *rhyp-n2* (- *rhyp-k*) i))
1607 (- *rhyp-n1* i)
1608 (- *rhyp-k* i))))))
1609 (cond ((<= v f) (setf reject nil))))
1610 (t ;; squeeze using upper and lower bounds
1611 (setf y ix
1612 y1 (+ y 1.0)
1613 ym (- y *rhyp-m*)
1614 yn (+ *rhyp-n1* (- *rhyp-k*) 1.0)
1615 yk (+ *rhyp-k* (- y) 1.0)
1616 nk (+ *rhyp-n2* (- *rhyp-k*) y1)
1617 r (/ (- ym) y1)
1618 *rhyp-s* (/ ym yn)
1619 tt (/ ym yk)
1620 e (/ (- ym) nk)
1621 g (- (/ (* yn yk) (* y1 nk)) 1.0)
1622 dg 1.0)
1623 (if (< g 1.0) (setf dg (+ 1.0 g)))
1624 (setf gu (* g (+ 1.0 (* g (+ -0.5 (/ g 3.0)))))
1625 gl (- gu (/ (* g g g g 0.25) dg))
1626 xm (+ *rhyp-m* 0.5)
1627 xn (+ *rhyp-n1* (- *rhyp-m*) 0.5)
1628 xk (+ *rhyp-k* (- *rhyp-m*) 0.5)
1629 nm (+ *rhyp-n2* (- *rhyp-k*) xm)
1630 ub (+ (* y gu)
1631 (* (- *rhyp-m*) gl)
1632 deltau
1633 (* xm r (+ 1.0 (* r (+ -0.5 (/ r 3.0)))))
1634 (* xn *rhyp-s* (+ 1.0 (* *rhyp-s* (+ -0.5 (/ *rhyp-s* 3.0)))))
1635 (* xk tt (+ 1.0 (* tt (+ -0.5 (/ tt 3.0)))))
1636 (* nm e (+ 1.0 (* e (+ -0.5 (/ e 3.0)))))))
1637 ;; test against upper bound
1638 (setf alv (log v))
1639 (cond ((> alv ub)
1640 (setf reject t))
1641 (t (setf dr (* r r r r xm))
1642 (if (< r 0.0) (setf dr (/ dr (+ 1.0 r))))
1643 (setf ds (* *rhyp-s* *rhyp-s* *rhyp-s* *rhyp-s* xn))
1644 (if (< *rhyp-s* 0.0) (setf ds (/ ds (+ 1.0 *rhyp-s*))))
1645 (setf dt (* tt tt tt tt xk))
1646 (if (< tt 0.0) (setf dt (/ dt (+ 1.0 tt))))
1647 (setf de (* e e e e nm))
1648 (if (< e 0.0) (setf de (/ de (+ 1.0 e))))
1649 (cond ((< alv (- ub
1650 (* 0.25 (+ dr ds dt de))
1651 (* (+ y *rhyp-m*) (- gl gu))
1652 deltal))
1653 (setf reject nil))
1654 ((<= alv (- *rhyp-a*
1655 (afc ix)
1656 (afc (- *rhyp-n1* ix))
1657 (afc (- *rhyp-k* ix))
1658 (afc (+ *rhyp-n2* (- *rhyp-k*) ix))))
1659 (setf reject nil))
1660 (t (setf reject t)))))))
1661 (if reject (go l30)))))
1662 ;; return appropriate variate
1663 (cond ((>= (+ kk kk) *rhyp-tn*)
1664 (cond ((> nn1 nn2)
1665 (+ kk (- nn2) ix))
1666 (t (- nn1 ix))))
1667 (t (cond ((> nn1 nn2)
1668 (- kk ix))
1669 (t ix))))))
1672 ;; The sample size ss must be a non negative integer.
1673 ;; If ss=0, returns a number, otherwise a maxima list
1674 ;; of length ss
1675 (defun rndhypergeo (n1 n2 n ss &aux sample)
1676 (declare (type integer n1 n2 n ss))
1677 (cond ((= ss 0)
1678 (rndhypergeo-kachit n1 n2 n) )
1679 (t (setf sample nil)
1680 (dotimes (i ss (cons '(mlist simp) sample))
1681 (setf sample (cons (rndhypergeo n1 n2 n 0) sample))) )) )
1685 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1686 ;; ;;
1687 ;; Negative binomial random simulation ;;
1688 ;; ;;
1689 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1692 ;; Generates random negative binomial variates (n p)
1693 ;; Reference:
1694 ;; [1] Devroye, L. (1986)
1695 ;; Non-Uniform Random Variate Generation.
1696 ;; Springer Verlag, p. 480
1697 ;; Comments:
1698 ;; [1] This is a translation from C of the
1699 ;; rnbinom.c file in the R statistical package.
1700 (defun rndnegbinom-devroye (n p)
1701 (declare (type flonum p))
1702 (rndpoisson (rndgamma n (/ (- 1.0 p) p) 0) 0) )
1705 ;; The sample size ss must be a non negative integer.
1706 ;; If ss=0, returns a number, otherwise a maxima list
1707 ;; of length ss
1708 (defun rndnegbinom (n p ss &aux sample)
1709 (cond ((= ss 0)
1710 (rndnegbinom-devroye n p) )
1711 (t (setf sample nil)
1712 (dotimes (i ss (cons '(mlist simp) sample))
1713 (setf sample (cons (rndnegbinom n p 0) sample))) )) )