3 ;; Copyright (C) 2005-2009 Mario Rodriguez Riotorto
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.
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
22 ;; mario @@@ edu DOT xunta DOT es
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;; Incomplete gamma and beta ;;
29 ;; and related functions ;;
31 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
34 ;; Natural logarithm of the gamma function. This function is called from
35 ;; some numerical routines below.
37 (simplify (list '(%log_gamma
) p
)))
40 ;; Natural logarithm of the beta function
41 ;; Conditions: 0<p,q<1.0e302
43 (+ (lngamma p
) (lngamma q
) (- (lngamma (+ p q
)))) )
46 ;; Lower regularized gamma incomplete. This function is called from
47 ;; some numerical routines below.
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.
61 ;; algorithm as 109 appl. statist. (1977), vol.26, no.1
62 ;; Comments: Translated from Fortran.
63 ;; Conditions: 0<x<1; p,q>0
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
)
74 ;; change tail if necessary
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
))))
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
)
94 (+ r
(/ five six
) (/ (- two
) (* three h
)))))))
95 (setf xiibeta
(/ pp
(+ pp
(* qq
(exp (+ w w
)))))))
97 (setf tt
(/ one
(* 9.0 qq
)))
98 (setf tt
(* r
(expt (+ one
(- tt
) (* y
(sqrt tt
))) 3)) )
100 (setf xiibeta
(- one
(exp (/ (+ (log (* (- one a
) qq
)) beta
) qq
)))))
101 (t (setf tt
(/ (+ (* four pp
) r
(- two
)) tt
))
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
113 (if (< xiibeta
1.0e-4) (setf xiibeta
1.0e-4))
114 (if (> xiibeta
0.9999) (setf xiibeta
0.9999))
116 (+ (/ -
5.0 (expt pp
2)) (- (/ one
(expt a
0.2))) -
13.0)
118 (setf acu
(expt 10.0 iex
))
121 (setf y
(ibeta xiibeta pp qq
))
127 (* tt
(log (- one xin
)))))))
128 (if (<= (* y yprev
) zero
) (setf prev
(max sq fpu
)))
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
))
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
))
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
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
))
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
)
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)
176 (setf a m ga gm
))))) ))
179 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
181 ;; Numerical routines for the noncentral chi2 ;;
183 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
186 ;; Cumulative probability of the noncentral chi-square distribution.
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
))
198 (lamda (* 0.5 theta
)))
199 (declare (type flonum sum lamda
))
201 (pr (exp (- lamda
)) (* pr
(/ lamda i
))))
203 (setf sum
(+ sum
(* pr
(igamma (* 0.5 x
) (+ (* 0.5 f
) i
))))) )
206 (let* ((dbl_epsilon 1.77635683940025e-15)
207 (dbl_min_exp (* (log 2.0) -
1021.0)) ; ln(2) * DBL_MIN_EXP
209 (lamsml (< (- lam
) dbl_min_exp
))
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
) )
220 ; non centrality parameter too large
225 (setf u
(exp (- lam
)))))
227 ; evaluate the first term
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
))
248 (> x
(+ f theta
(* 5 (sqrt (* 2 (+ f
(* 2 theta
))))))))
249 ; x > E[X] + 5* sigma(X)
267 f_x_2n
(+ f_x_2n
2.0))
270 ;f_x_2n = f - x + 2*n > 0 <=> (f+2n) > x
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
)
280 ; evaluate the next term of the
281 ; expansion and then the partial sum
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'
291 (setf u
(* u
(/ lam n
)))
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'
300 (setf tt
(* tt
(/ x f_2n
)))))
301 (when (and (not lamsml
) (not tsml
))
303 (setf ans
(+ ans term
)))
305 (setf f_2n
(+ f_2n
2.0))
306 (setf f_x_2n
(+ f_x_2n
2.0)) ) ; loop
308 ($print
(format nil
"Warning: cdf_noncentral_chi2 not converged in ~a iterations" itrmax
)))
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
)
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.
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
))
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
342 ((> p
(- 1 dbl_epsilon
)) ; probability near 1
345 (setf pp
(min (- 1 dbl_epsilon
)
348 (when (or (>= ux dbl_max
)
349 (>= (cdfnchi2 ux df lambda eps reps
10000.0) pp
))
351 (setf ux
(* 2.0 ux
)))
352 (setf pp
(* p
(- 1 eps
)))
353 (setf lx
(min ux dbl_max
))
355 (when (or (<= lx dbl_min
)
356 (<= (cdfnchi2 lx df lambda eps reps
10000.0) pp
))
358 (setf lx
(* 0.5 lx
)))
360 ; interval (lx,ux) halving
362 (setf nx
(* 0.5 (+ lx ux
)))
363 (if (> (cdfnchi2 nx df lambda accu racc
100000.0) p
)
366 (when (<= (/ (- ux lx
) nx
) accu
)
368 (* 0.5 (+ ux lx
)) )) ))
372 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
374 ;; Numerical routines for the noncentral t ;;
376 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
379 ;; Cumulative probability of the noncentral Student's t distribution.
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)
393 a albeta b del errbd geven godd lambda p q rxb s tt xx xeven xodd
)
400 ; initialize twin series
401 ; Guenther, J. (1978). Statist. Computn. Simuln. vol.6, 199.
402 (setf xx
(/ (* x x
) (+ (* x x
) df
)))
404 (setf lambda
(* del del
))
405 (setf p
(* 0.5 (exp (* lambda -
0.5))))
406 (setf q
(* r2pi p del
))
410 (setf rxb
(expt (- 1.0 xx
) b
)
413 (- (lngamma (+ a b
)))))
414 (setf xodd
(ibeta xx a b
)
415 godd
(* 2.0 rxb
(exp (- (* a
(log xx
)) albeta
)))
418 (setf tnc
(+ (* p xodd
) (* q xeven
)))
419 ; repeat until convergence or iteration limit
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)))
429 tnc
(+ tnc
(* p xodd
) (* q xeven
))
431 (setf errbd
(* 2.0 s
(- xodd godd
)))
432 (when (or (< errbd errmax
) (> 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
)))))))
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
))
453 (dbl_max most-positive-double-float
) ; DBL_MAX
454 (dbl_epsilon double-float-epsilon
) ; DBL_EPSILON
457 ; 1. finding an upper and lower bound
458 (setf pp
(min (- 1.0 dbl_epsilon
)
460 (setf ux
(max 1.0 ncp
))
462 (when (or (>= ux dbl_max
)
463 (>= (cdfnt ux df ncp
) pp
))
466 (setf pp
(* p
(- 1.0 eps
)))
467 (setf lx
(min -
1.0 (- ncp
)))
469 (when (or (<= lx
(- dbl_max
))
470 (<= (cdfnt lx df ncp
) pp
))
474 ; 2. interval (lx,ux) halving
476 (setf nx
(* 0.5 (+ lx ux
)))
477 (if (> (cdfnt nx df ncp
) p
)
480 (when (<= (- ux lx
) accu
)
486 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
488 ;; Normal random simulation ;;
490 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
493 ;; Generates random normal variates, with mean=0 and var=1,
494 ;; using the Box-Mueller method.
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 ()
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)
511 (t (setf *rnormal-iset
* 0)
515 ;; The sample size ss must be a non negative integer.
516 ;; If ss=0, returns a number, otherwise a maxima list
518 (defun rndnormal (ss &aux sample
)
522 (dotimes (i ss
(cons '(mlist simp
) sample
))
523 (setf sample
(cons (rndnormal 0) sample
))))))
526 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
528 ;; Exponential random simulation ;;
530 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
533 ;; Generates random exponential variates (m),
534 ;; using the inverse method.
535 (defun rndexp-inverse (m)
536 (declare (type flonum m
))
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
546 (defun rndexp (m ss
&aux sample
)
550 (dotimes (i ss
(cons '(mlist simp
) sample
))
551 (setf sample
(cons (rndexp m
0) sample
))))))
554 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
556 ;; Gamma random simulation ;;
558 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
561 ;; Generates random gamma variates (a, b), combining
562 ;; Ahrens-Dieter and Cheng-Feast methods.
565 ;; Cheng, R.C.H. and Feast, G.M. (1979).
566 ;; Some simple gamma variate generators.
567 ;; Appl. Stat., 28, 3, 290-295.
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)
579 (setf q
(* ($random
1.0) (+ r
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)))
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
)))))
594 (t ;; Cheng-Feast algorithm GKM3
595 (loop (setf b
(/ (- aa
(/ 1.0 (* 6.0 aa
))) a
))
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))
612 ;; The sample size ss must be a non negative integer.
613 ;; If ss=0, returns a number, otherwise a maxima list
615 (defun rndgamma (a b ss
&aux sample
)
617 (rndgamma-ahrens-cheng a b
) )
619 (dotimes (i ss
(cons '(mlist simp
) sample
))
620 (setf sample
(cons (rndgamma a b
0) sample
))) )) )
623 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
625 ;; Chi^2 random simulation ;;
627 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
630 ;; The sample size ss must be a non negative integer.
631 ;; If ss=0, returns a number, otherwise a maxima list
633 (defun rndchi2 (n ss
&aux sample
)
635 (rndgamma-ahrens-cheng (* n
0.5) 2.0) )
637 (dotimes (i ss
(cons '(mlist simp
) sample
))
638 (setf sample
(cons (rndchi2 n
0) sample
))) )) )
641 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
643 ;; Noncentral Chi^2 random simulation ;;
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
))
658 (rndgamma (* 0.5 df
) 2.0 0))
660 (let ((r (coerce (rndpoisson (/ lambda
2.0) 0) 'flonum
)))
661 (declare (type flonum r
))
663 (setf r
(rndchi2 (* 2.0 r
) 0)))
664 (setf r
(+ r
(rndgamma (* 0.5 df
) 2.0 0)))
669 ;; The sample size ss must be a non negative integer.
670 ;; If ss=0, returns a number, otherwise a maxima list
672 (defun rndnchi2 (df lambda ss
&aux sample
)
674 (rndnchi2-chi2-poisson df lambda
))
676 (dotimes (i ss
(cons '(mlist simp
) sample
))
677 (setf sample
(cons (rndnchi2 df lambda
0) sample
))))))
680 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
682 ;; Student's t random simulation ;;
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)
691 (defun rndstudent-ratio (n)
692 (declare (type flonum n
))
694 (setf z
(rndnormal 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
702 (defun rndstudent (n ss
&aux sample
)
704 (rndstudent-ratio n
) )
706 (dotimes (i ss
(cons '(mlist simp
) sample
))
707 (setf sample
(cons (rndstudent n
0) sample
))) )) )
711 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
713 ;; Noncentral Student's t random simulation ;;
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
))
725 (setf x
(+ k
(rndnormal 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
733 (defun rndncstudent (n k ss
&aux sample
)
735 (rndncstudent-ratio n k
) )
737 (dotimes (i ss
(cons '(mlist simp
) sample
))
738 (setf sample
(cons (rndncstudent-ratio n k
) sample
))) )) )
742 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
744 ;; Snedecor's F random simulation ;;
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)
753 (defun rndf-ratio (m n
)
754 (declare (type flonum m n
))
756 (setf x
(rndchi2 m
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
764 (defun rndf (m n ss
&aux sample
)
768 (dotimes (i ss
(cons '(mlist simp
) sample
))
769 (setf sample
(cons (rndf m n
0) sample
))))))
772 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
774 ;; Beta random simulation ;;
776 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
779 ;; Generates random beta variates (a, b)
781 ;; [1] Cheng, R.C.H. (1978). Generating Beta Variates
782 ;; with Nonintegral Shape Parameters.
783 ;; Communications of the ACM, 21:317-322
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
*)
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
))
813 (if (<= (min aa bb
) 1.0) (go s100
))
815 ;; Algorithm BB - Initialize
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))
828 (setf u2
($random
1.0))
829 (setf v
(* *rbeta-beta
* (log (/ u1
(- 1.0 u1
)))))
831 (setf w
(* *rbeta-a
* (exp v
)))
833 (setf z
(* u2
(expt u1
2)))
834 (setf r
(- (* *rbeta-gamma
* v
) 1.3862944))
835 (setf s
(+ *rbeta-a
* r
(- w
)))
838 (if (>= (+ s
2.609438) (* 5.0 z
)) (go s70
))
842 (if (> s tt
) (go s70
))
845 (if (< (+ r
(* *rbeta-alpha
* (log (/ *rbeta-alpha
* (+ *rbeta-b
* w
))))) tt
)
850 (if (/= aa
*rbeta-a
*)
851 (setf genbet
(/ *rbeta-b
* (+ *rbeta-b
* w
)))
852 (setf genbet
(/ w
(+ *rbeta-b
* w
))))
856 ;; Algorithm BC - Initialize
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
*)))
868 (setf u1
($random
1.0))
871 (setf u2
($random
1.0))
872 (if (>= u1
0.5) (go s130
))
877 (if (>= (+ (* 0.25 u2
) z
(- y
)) *rbeta-k1
*) (go s120
))
882 (setf z
(* (expt u1
2) u2
))
883 (if (> z
0.25) (go s160
))
884 (setf v
(* *rbeta-beta
* (log (/ u1
(- 1.0 u1
)))))
886 (setf w
(* *rbeta-a
* (exp v
)))
891 (if (>= z
*rbeta-k2
*) (go s120
))
895 (setf v
(* *rbeta-beta
* (log (/ u1
(- 1.0 u1
)))))
897 (setf w
(* *rbeta-a
* (exp v
)))
901 (if (< (- (* *rbeta-alpha
* (+ (log (/ *rbeta-alpha
* (+ *rbeta-b
* w
))) v
)) 1.3862944) (log z
))
906 (if (/= aa
*rbeta-a
*)
907 (setf genbet
(/ *rbeta-b
* (+ *rbeta-b
* w
)))
908 (setf genbet
(/ w
(+ *rbeta-b
* w
))))
914 ;; The sample size ss must be a non negative integer.
915 ;; If ss=0, returns a number, otherwise a maxima list
917 (defun rndbeta (a b ss
&aux sample
)
919 (rndbeta-cheng a b
) )
921 (dotimes (i ss
(cons '(mlist simp
) sample
))
922 (setf sample
(cons (rndbeta a b
0) sample
))) )) )
925 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
927 ;; Binomial random simulation ;;
929 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
932 ;; Generates random binomial variates (n p)
934 ;; [1] Kachitvichyanukul, V., Schmeiser, B.W.
935 ;; Binomial Random Variate Generation.
936 ;; Communications of the ACM, 31, 2
937 ;; (February, 1988) 216
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
968 (defun rndbinomial-kachit (n pp
)
969 (declare (type integer n
)
971 (let (u v x f amaxp ynorm alv x1 f1 z w z2 x2 f2 w2 ix k t1 mp ix1
)
973 (if (/= pp
*rbin-psave
*) (go s10
))
974 (if (/= n
*rbin-nsave
*) (go s20
))
975 (if (< *rbin-xnp
* 30.0) (go s150
))
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
*)))
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
)))
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
))
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
*))
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
1045 (setf *rbin-r
* (/ *rbin-p
* *rbin-q
*))
1046 (setf *rbin-g
* (* *rbin-r
* (+ n
1.0)))
1047 (setf t1
(- *rbin-m
* ix
))
1055 (setf mp
(+ *rbin-m
* 1))
1058 (setf f
(* f
(- (/ *rbin-g
* i
) *rbin-r
*))))
1063 (do ((i ix1
(1+ i
)))
1064 ((> i
*rbin-m
*) 'done
)
1065 (setf f
(/ f
(- (/ *rbin-g
* i
) *rbin-r
*))))
1068 (if (<= v f
) (go s170
))
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
*)
1076 (setf ynorm
(- (/ (* k k
) (* 2.0 *rbin-xnpq
*))))
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
)))
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)))
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)))
1114 (setf u
($random
1.0))
1117 (if (< u f
) (go s170
))
1118 (if (> ix
110) (go s150
))
1121 (setf f
(* f
(- (/ *rbin-g
* ix
) *rbin-r
*)))
1125 (if (> *rbin-psave
* 0.5)
1130 ;; The sample size ss must be a non negative integer.
1131 ;; If ss=0, returns a number, otherwise a maxima list
1133 (defun rndbinomial (n p ss
&aux sample
)
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1143 ;; Poisson random simulation ;;
1145 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1148 ;; Generates random Poisson variates (m)
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
1156 ;; [1] Slightly modified version of the program in the
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)
1169 (defvar *rpos-omega
*)
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.
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
))
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
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)))
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
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
))
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
))
1282 (setf py
(/ (expt mu ignpoi
) (aref fact ignpoi
)))
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
)))
1297 (setf px
(+ (* fk v v
1298 (+ (* (+ (* (+ (* (+ (* (+ (* (+ (* (+ (* a7 v
)
1299 a6
) v
) a5
) v
) a4
) v
) a3
) v
) a2
) v
) a1
) v
) a0
))
1303 (setf py
(/ 0.39894228040143 (sqrt fk
)))
1306 (setf x
(/ (- 0.5 difmuk
) *rpos-s
*))
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
))
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
)))
1321 (setf *rpos-p
* (exp (- mu
)))
1322 (setf *rpos-p0
* *rpos-p
*)
1323 (setf *rpos-q
* *rpos-p
*)
1326 ;; Step U. Uniform sample for inversion method
1327 (setf u
($random
1.0))
1329 (if (<= u
*rpos-p0
*) (go s200
))
1332 (if (= *rpos-l
* 0) (go s150
))
1334 (if (> u
0.458) (setf j
(min *rpos-l
* *rpos-m
*)))
1336 ((> k
*rpos-l
*) 'done
)
1337 (cond ((<= u
(aref *rpos-pp
* (- k
1)))
1340 (if (= *rpos-l
* 35) (go s130
))
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
)))
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
*)
1362 ;; The sample size ss must be a non negative integer.
1363 ;; If ss=0, returns a number, otherwise a maxima list
1365 (defun rndpoisson (m ss
&aux sample
)
1366 (declare (type flonum m
))
1367 (declare (type integer ss
))
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1377 ;; Geometric random simulation ;;
1379 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1382 ;; Generates random geometric variates (p)
1383 ;; by simulation of Bernoulli trials.
1384 (defun rndgeo-trials (p)
1385 (declare (type flonum p
))
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
1395 (defun rndgeo (p ss
&aux sample
)
1396 (declare (type flonum p
))
1397 (declare (type integer ss
))
1400 (t (setf sample nil
)
1401 (dotimes (i ss
(cons '(mlist simp
) sample
))
1402 (setf sample
(cons (rndgeo p
0) sample
))))))
1405 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1407 ;; Hypergeometric random simulation ;;
1409 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1413 ;; Computes the logarithm of the factorial log(n!)
1414 ;; This function is called from rndhypergeo-kachit
1416 (declare (type integer i
))
1417 (let ((di (float i
))
1419 :element-type
'flonum
1420 :initial-contents
'( 0.0
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
))
1436 (/ 0.83333333333333333333333333333333 di
)
1437 (/ (- 0.00277777777777777777777777777778)
1439 0.91893853320467274178032973640562))) ) )
1442 ;; Generates random hypergeometric variates (n1, n2, n),
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.
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.
1453 (defvar *rhyp-n1s
* -
1)
1454 (defvar *rhyp-n2s
* -
1)
1455 (defvar *rhyp-ks
* -
1)
1460 (defvar *rhyp-minjx
*)
1461 (defvar *rhyp-maxjx
*)
1472 (defvar *rhyp-lamdl
*)
1473 (defvar *rhyp-lamdr
*)
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
1485 (cond ((or (/= nn1
*rhyp-n1s
*) (/= nn2
*rhyp-n2s
*))
1488 (t (cond ((/= kk
*rhyp-ks
*)
1494 (setf *rhyp-n1s
* nn1
1496 *rhyp-tn
* (+ nn1 nn2
))
1500 (t (setf *rhyp-n1
* nn2
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
*)
1519 (t (cond ((> nn1 nn2
)
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
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
1533 (- (afc (- *rhyp-k
* *rhyp-n2
*)))
1534 (- (afc (+ *rhyp-n1
* *rhyp-n2
*))))))))))
1539 u
(* ($random
1.0) scale
))
1543 (setf p
(* p
(- *rhyp-n1
* ix
) (- *rhyp-k
* 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))
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
*)))))
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
))))
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
*)))
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
1597 (cond ((< *rhyp-m
* ix
)
1598 (do ((i (+ *rhyp-m
* 1) (1+ i
)))
1600 (setf f
(/ (* f
(+ *rhyp-n1
* (- i
) 1) (+ *rhyp-k
* (- i
) 1))
1601 (+ *rhyp-n2
* (- *rhyp-k
*) i
)
1604 (do ((i (+ ix
1) (1+ i
)))
1605 ((> i
*rhyp-m
*) 'done
)
1606 (setf f
(/ (* f i
(+ *rhyp-n2
* (- *rhyp-k
*) i
))
1609 (cond ((<= v f
) (setf reject nil
))))
1610 (t ;; squeeze using upper and lower bounds
1614 yn
(+ *rhyp-n1
* (- *rhyp-k
*) 1.0)
1615 yk
(+ *rhyp-k
* (- y
) 1.0)
1616 nk
(+ *rhyp-n2
* (- *rhyp-k
*) y1
)
1621 g
(- (/ (* yn yk
) (* y1 nk
)) 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
))
1627 xn
(+ *rhyp-n1
* (- *rhyp-m
*) 0.5)
1628 xk
(+ *rhyp-k
* (- *rhyp-m
*) 0.5)
1629 nm
(+ *rhyp-n2
* (- *rhyp-k
*) xm
)
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
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
))))
1650 (* 0.25 (+ dr ds dt de
))
1651 (* (+ y
*rhyp-m
*) (- gl gu
))
1654 ((<= alv
(- *rhyp-a
*
1656 (afc (- *rhyp-n1
* ix
))
1657 (afc (- *rhyp-k
* ix
))
1658 (afc (+ *rhyp-n2
* (- *rhyp-k
*) ix
))))
1660 (t (setf reject t
)))))))
1661 (if reject
(go l30
)))))
1662 ;; return appropriate variate
1663 (cond ((>= (+ kk kk
) *rhyp-tn
*)
1667 (t (cond ((> nn1 nn2
)
1672 ;; The sample size ss must be a non negative integer.
1673 ;; If ss=0, returns a number, otherwise a maxima list
1675 (defun rndhypergeo (n1 n2 n ss
&aux sample
)
1676 (declare (type integer n1 n2 n ss
))
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1687 ;; Negative binomial random simulation ;;
1689 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1692 ;; Generates random negative binomial variates (n p)
1694 ;; [1] Devroye, L. (1986)
1695 ;; Non-Uniform Random Variate Generation.
1696 ;; Springer Verlag, p. 480
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
1708 (defun rndnegbinom (n p ss
&aux sample
)
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
))) )) )