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
*)
802 (defun rndbeta-cheng0 (aa bb
)
803 (declare (type flonum aa bb
))
804 (let (qsame u1 u2 v w z tt r s y genbet
805 (expmax 7.0) (infnty 1.0e304
))
806 (declare (type flonum expmax infnty
))
807 (if (and (= *rbeta-olda
* aa
) (= *rbeta-oldb
* bb
))
814 (if (<= (min aa bb
) 1.0) (go s100
))
816 ;; Algorithm BB - Initialize
818 (setf *rbeta-a
* (min aa bb
))
819 (setf *rbeta-b
* (max aa bb
))
820 (setf *rbeta-alpha
* (+ *rbeta-a
* *rbeta-b
*))
821 (setf *rbeta-beta
* (sqrt (/ (- *rbeta-alpha
* 2.0)
822 (- (* 2.0 *rbeta-a
* *rbeta-b
*) *rbeta-alpha
*))))
823 (setf *rbeta-gamma
* (+ *rbeta-a
* (/ 1.0 *rbeta-beta
*)))
826 (setf u1
($random
1.0))
829 (setf u2
($random
1.0))
830 (setf v
(* *rbeta-beta
* (log (/ u1
(- 1.0 u1
)))))
832 (setf w
(* *rbeta-a
* (exp v
)))
834 (setf z
(* u2
(expt u1
2)))
835 (setf r
(- (* *rbeta-gamma
* v
) 1.3862944))
836 (setf s
(+ *rbeta-a
* r
(- w
)))
839 (if (>= (+ s
2.609438) (* 5.0 z
)) (go s70
))
843 (if (> s tt
) (go s70
))
846 (if (< (+ r
(* *rbeta-alpha
* (log (/ *rbeta-alpha
* (+ *rbeta-b
* w
))))) tt
)
852 (setf genbet
'rndbeta-encountered-infnty
)
853 (if (/= aa
*rbeta-a
*)
854 (setf genbet
(/ *rbeta-b
* (+ *rbeta-b
* w
)))
855 (setf genbet
(/ w
(+ *rbeta-b
* w
)))))
859 ;; Algorithm BC - Initialize
861 (setf *rbeta-a
* (max aa bb
))
862 (setf *rbeta-b
* (min aa bb
))
863 (setf *rbeta-alpha
* (+ *rbeta-a
* *rbeta-b
*))
864 (setf *rbeta-beta
* (/ 1.0 *rbeta-b
*))
865 (setf *rbeta-delta
* (+ 1.0 *rbeta-a
* (- *rbeta-b
*)))
866 (setf *rbeta-k1
* (/ (* *rbeta-delta
* (+ 1.38889e-2 (* 4.16667e-2 *rbeta-b
*)))
867 (- (* *rbeta-a
* *rbeta-beta
*) 0.777778)))
868 (setf *rbeta-k2
* (+ 0.25 (* (+ 0.5 (/ 0.25 *rbeta-delta
*)) *rbeta-b
*)))
871 (setf u1
($random
1.0))
874 (setf u2
($random
1.0))
875 (if (>= u1
0.5) (go s130
))
880 (if (>= (+ (* 0.25 u2
) z
(- y
)) *rbeta-k1
*) (go s120
))
885 (setf z
(* (expt u1
2) u2
))
886 (if (> z
0.25) (go s160
))
887 (setf v
(* *rbeta-beta
* (log (/ u1
(- 1.0 u1
)))))
889 (setf w
(* *rbeta-a
* (exp v
)))
894 (if (>= z
*rbeta-k2
*) (go s120
))
898 (setf v
(* *rbeta-beta
* (log (/ u1
(- 1.0 u1
)))))
900 (setf w
(* *rbeta-a
* (exp v
)))
904 (if (< (- (* *rbeta-alpha
* (+ (log (/ *rbeta-alpha
* (+ *rbeta-b
* w
))) v
)) 1.3862944) (log z
))
910 (setf genbet
'rndbeta-encountered-infnty
)
911 (if (/= aa
*rbeta-a
*)
912 (setf genbet
(/ *rbeta-b
* (+ *rbeta-b
* w
)))
913 (setf genbet
(/ w
(+ *rbeta-b
* w
)))))
918 (defun rndbeta-cheng (aa bb
)
920 (loop while
(eq (setq genbet
(rndbeta-cheng0 aa bb
)) 'rndbeta-encountered-infnty
))
924 ;; The sample size ss must be a non negative integer.
925 ;; If ss=0, returns a number, otherwise a maxima list
927 (defun rndbeta (a b ss
&aux sample
)
929 (rndbeta-cheng a b
) )
931 (dotimes (i ss
(cons '(mlist simp
) sample
))
932 (setf sample
(cons (rndbeta a b
0) sample
))) )) )
935 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
937 ;; Binomial random simulation ;;
939 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
942 ;; Generates random binomial variates (n p)
944 ;; [1] Kachitvichyanukul, V., Schmeiser, B.W.
945 ;; Binomial Random Variate Generation.
946 ;; Communications of the ACM, 31, 2
947 ;; (February, 1988) 216
949 ;; [1] Algorithm BTPE
950 ;; [2] This is a translation from C of the
951 ;; ranlib.c library. R statistical package uses this same
952 ;; algorithm but in a more structured style.
953 ;; [3] Some global variables are defined in order to
954 ;; save time when many random variables are generated.
955 (defvar *rbin-psave
* -
1.0)
956 (defvar *rbin-nsave
* -
1) ;; integer
957 (defvar *rbin-m
*) ;; integer
978 (defun rndbinomial-kachit (n pp
)
979 (declare (type integer n
)
981 (let (u v x f amaxp ynorm alv x1 f1 z w z2 x2 f2 w2 ix k t1 mp ix1
)
983 (if (/= pp
*rbin-psave
*) (go s10
))
984 (if (/= n
*rbin-nsave
*) (go s20
))
985 (if (< *rbin-xnp
* 30.0) (go s150
))
989 ;; setup, perform only when parameters change
990 (setf *rbin-psave
* pp
)
991 (setf *rbin-p
* (min *rbin-psave
* (- 1.0 *rbin-psave
*)))
992 (setf *rbin-q
* (- 1.0 *rbin-p
*))
995 (setf *rbin-xnp
* (* n
*rbin-p
*))
996 (setf *rbin-nsave
* n
)
997 (if (< *rbin-xnp
* 30.0) (go s140
))
998 (setf *rbin-ffm
* (+ *rbin-xnp
* *rbin-p
*))
999 (setf *rbin-m
* (round *rbin-ffm
*))
1000 (setf *rbin-fm
* *rbin-m
*)
1001 (setf *rbin-xnpq
* (* *rbin-xnp
* *rbin-q
*))
1002 (setf *rbin-p1
* (+ 0.5 (round (- (* 2.195 (sqrt *rbin-xnpq
*)) (* 4.6 *rbin-q
*)))))
1003 (setf *rbin-xm
* (+ 0.5 *rbin-fm
*))
1004 (setf *rbin-xl
* (- *rbin-xm
* *rbin-p1
*))
1005 (setf *rbin-xr
* (+ *rbin-xm
* *rbin-p1
*))
1006 (setf *rbin-c
* (+ 0.134 (/ 20.5 (+ 15.3 *rbin-fm
*))))
1007 (setf *rbin-al
* (/ (- *rbin-ffm
* *rbin-xl
*) (- *rbin-ffm
* (* *rbin-xl
* *rbin-p
*))))
1008 (setf *rbin-xll
* (* *rbin-al
* (+ 1.0 (* 0.5 *rbin-al
*))))
1009 (setf *rbin-al
* (/ (- *rbin-xr
* *rbin-ffm
*) (* *rbin-xr
* *rbin-q
*)))
1010 (setf *rbin-xlr
* (* *rbin-al
* (+ 1.0 (* 0.5 *rbin-al
*))))
1011 (setf *rbin-p2
* (* *rbin-p1
* (+ 1.0 *rbin-c
* *rbin-c
*)))
1012 (setf *rbin-p3
* (+ *rbin-p2
* (/ *rbin-c
* *rbin-xll
*)))
1013 (setf *rbin-p4
* (+ *rbin-p3
* (/ *rbin-c
* *rbin-xlr
*)))
1017 (setf u
(* *rbin-p4
* ($random
1.0)))
1018 (setf v
($random
1.0))
1020 ;; triangular region
1021 (if (> u
*rbin-p1
*) (go s40
))
1022 (setf ix
(round (+ *rbin-xm
* (- (* *rbin-p1
* v
)) u
)))
1026 ;; parallelogram region
1027 (if (> u
*rbin-p2
*) (go s50
))
1028 (setf x
(+ *rbin-xl
* (/ (- u
*rbin-p1
*) *rbin-c
*)))
1029 (setf v
(+ (* v
*rbin-c
*) 1.0 (- (/ (abs (- *rbin-xm
* x
)) *rbin-p1
*))))
1030 (if (or (> v
1.0) (<= v
0.0)) (go s30
))
1036 (if (> u
*rbin-p3
*) (go s60
))
1037 (setf ix
(round (+ *rbin-xl
* (/ (log v
) *rbin-xll
*))))
1038 (if (< ix
0) (go s30
))
1039 (setf v
(* v
(- u
*rbin-p2
*) *rbin-xll
*))
1044 (setf ix
(round (- *rbin-xr
* (/ (log v
) *rbin-xlr
*))))
1045 (if (> ix n
) (go s30
))
1046 (setf v
(* v
(- u
*rbin-p3
*) *rbin-xlr
*))
1049 ;; determine appropriate way to perform accept/reject test
1050 (setf k
(abs (- ix
*rbin-m
*)))
1051 (if (and (> k
20) (< k
(- (/ *rbin-xnpq
* 2.0) 1.0))) (go s130
))
1053 ;; explicit evaluation
1055 (setf *rbin-r
* (/ *rbin-p
* *rbin-q
*))
1056 (setf *rbin-g
* (* *rbin-r
* (+ n
1.0)))
1057 (setf t1
(- *rbin-m
* ix
))
1065 (setf mp
(+ *rbin-m
* 1))
1068 (setf f
(* f
(- (/ *rbin-g
* i
) *rbin-r
*))))
1073 (do ((i ix1
(1+ i
)))
1074 ((> i
*rbin-m
*) 'done
)
1075 (setf f
(/ f
(- (/ *rbin-g
* i
) *rbin-r
*))))
1078 (if (<= v f
) (go s170
))
1082 ;; squeezing using upper and lower bounds on alog(f(x))
1083 (setf amaxp
(* (/ k
*rbin-xnpq
*)
1084 (+ (/ (+ (* k
(+ (/ k
3.0) 0.625)) 0.1666666666666) *rbin-xnpq
*)
1086 (setf ynorm
(- (/ (* k k
) (* 2.0 *rbin-xnpq
*))))
1088 (if (< alv
(- ynorm amaxp
)) (go s170
))
1089 (if (> alv
(+ ynorm amaxp
)) (go s30
))
1091 ;; Stirling's formula to machine accuracy for
1092 ;; the final acceptance / rejection test
1093 (setf x1
(+ ix
1.0))
1094 (setf f1
(+ *rbin-fm
* 1.0))
1095 (setf z
(+ n
1.0 (- *rbin-fm
*)))
1096 (setf w
(+ n
1.0 (- ix
)))
1101 (if (<= alv
(+ (* *rbin-xm
* (log (/ f1 x1
)))
1102 (* (+ n
(- *rbin-m
*) 0.5) (log (/ z w
)))
1103 (* (+ ix
(- *rbin-m
*)) (log (/ (* w
*rbin-p
*) (* x1
*rbin-q
*))))
1104 (/ (/ (+ 13860.0 (- (/ (+ 462.0 (- (/ (+ 132.0
1105 (- (/ (+ 99.0 (- (/ 140.0 f2
))) f2
))) f2
))) f2
))) f1
) 166320.0)
1106 (/ (/ (+ 13860.0 (- (/ (+ 462.0 (- (/ (+ 132.0
1107 (- (/ (+ 99.0 (- (/ 140.0 z2
))) z2
))) z2
))) z2
))) z
) 166320.0)
1108 (/ (/ (+ 13860.0 (- (/ (+ 462.0 (- (/ (+ 132.0
1109 (- (/ (+ 99.0 (- (/ 140.0 x2
))) x2
))) x2
))) x2
))) x1
) 166320.0)
1110 (/ (/ (+ 13860.0 (- (/ (+ 462.0 (- (/ (+ 132.0
1111 (- (/ (+ 99.0 (- (/ 140.0 w2
))) w2
))) w2
))) w2
))) w
) 166320.0)))
1116 ;; inverse cdf logic for mean less than 30
1117 (setf *rbin-qn
* (expt *rbin-q
* n
))
1118 (setf *rbin-r
* (/ *rbin-p
* *rbin-q
*))
1119 (setf *rbin-g
* (* *rbin-r
* (+ n
1.0)))
1124 (setf u
($random
1.0))
1127 (if (< u f
) (go s170
))
1128 (if (> ix
110) (go s150
))
1131 (setf f
(* f
(- (/ *rbin-g
* ix
) *rbin-r
*)))
1135 (if (> *rbin-psave
* 0.5)
1140 ;; The sample size ss must be a non negative integer.
1141 ;; If ss=0, returns a number, otherwise a maxima list
1143 (defun rndbinomial (n p ss
&aux sample
)
1145 (rndbinomial-kachit n p
))
1146 (t (setf sample nil
)
1147 (dotimes (i ss
(cons '(mlist simp
) sample
))
1148 (setf sample
(cons (rndbinomial n p
0) sample
))))))
1151 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1153 ;; Poisson random simulation ;;
1155 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1158 ;; Generates random Poisson variates (m)
1160 ;; [1] Ahrens, J.H. and Dieter, U.
1161 ;; Computer Generation of Poisson Deviates
1162 ;; From Modified Normal Distributions.
1163 ;; ACM Trans. Math. Software, 8, 2
1164 ;; (June 1982),163-179
1166 ;; [1] Slightly modified version of the program in the
1168 ;; [2] This is a translation from C of the
1169 ;; ranlib.c library. R statistical package uses this same
1170 ;; algorithm but in a more structured style.
1171 ;; [3] Some global variables are defined in order to
1172 ;; save time when many random variables are generated.
1173 (defvar *rpos-muold
* 0.0)
1174 (defvar *rpos-muprev
* 0.0)
1179 (defvar *rpos-omega
*)
1190 (defvar *rpos-pp
* (make-array 35 :initial-element
0.0 :element-type
'flonum
))
1191 (defun rndpoisson-ahrens (mu)
1192 (declare (type flonum mu
))
1194 ;; This algorithm doesn't handle MU = 0 correctly; *RPOS-S* and friends aren't initialized.
1195 ;; So just handle the special case here and bail out.
1197 (return-from rndpoisson-ahrens
0))
1199 (let ( ignpoi j kflag del difmuk e fk fx fy g px py tt u v x xx
1200 (a0 -
0.5) (a1 0.3333333) (a2 -
0.2500068) (a3 0.2000118)
1201 (a4 -
0.1661269) (a5 0.1421878) (a6 -
0.1384794) (a7 0.125006)
1202 (fact (make-array 10
1203 :element-type
'flonum
1204 :initial-contents
'(1.0
1.0 2.0 6.0 24.0 120.0
1205 720.0 5040.0 40320.0 362880.0))))
1206 (declare (type flonum a0 a1 a2 a3 a4 a5 a6 a7
))
1207 (declare (type (simple-array flonum
(10)) fact
))
1209 (if (= mu
*rpos-muprev
*) (go s10
))
1210 (if (< mu
10.0) (go s120
))
1212 ;; Case A. (Recalculation of, *rpos-s*, *rpos-d*, *rpos-l* if mu has changed)
1213 (setf *rpos-muprev
* mu
)
1214 (setf *rpos-s
* (sqrt mu
))
1215 (setf *rpos-d
* (* 6.0 mu mu
))
1216 (setf *rpos-l
* (floor (- mu
1.1484)))
1219 ;; Step n. Normal sample
1220 (setf g
(+ mu
(* *rpos-s
* (rndnormal 0))))
1221 (if (< g
0.0) (go s20
))
1222 (setf ignpoi
(floor g
))
1224 ;; Step I. Immediate acceptance if ignpoi is large enough
1225 (if (>= ignpoi
*rpos-l
*) (go s200
))
1227 ;; Step S. Squeez acceptance
1228 (setf fk
(coerce ignpoi
'flonum
))
1229 (setf difmuk
(- mu fk
))
1230 (setf u
($random
1.0))
1231 (if (>= (* *rpos-d
* u
) (* difmuk difmuk difmuk
)) (go s200
))
1234 ;; Step P. Preparations for steps Q and H.
1235 ;; (Recalculations of parameters if necessary)
1236 ;; .3989423...=(2*%pi)^(-.5) .416667...E-1=1./24. .1428571...=1./7.
1237 ;; The quantities *rpos-b1*, *rpos-b2*, *rpos-c3*, *rpos-c2*, *rpos-c1*, *rpos-c0* are for thr Hermite
1238 ;; approximations to the discrete normal probabilities fk.
1239 ;; c=0.1069/mu guarantees majorization by the 'hat'-function.
1240 (if (= mu
*rpos-muold
*) (go s30
))
1241 (setf *rpos-muold
* mu
)
1242 (setf *rpos-omega
* (/ 0.39894228040143 *rpos-s
*))
1243 (setf *rpos-b1
* (/ 0.41666666666667e-1 mu
))
1244 (setf *rpos-b2
* (* 0.3 *rpos-b1
* *rpos-b1
*))
1245 (setf *rpos-c3
* (* 0.14285714285714 *rpos-b1
* *rpos-b2
*))
1246 (setf *rpos-c2
* (- *rpos-b2
* (* 15.0 *rpos-c3
*)))
1247 (setf *rpos-c1
* (+ *rpos-b1
* (* -
6.0 *rpos-b2
*) (* 45.0 *rpos-c3
*)))
1248 (setf *rpos-c0
* (+ 1.0 (- *rpos-b1
*) (* 3.0 *rpos-b2
*) (* -
15.0 *rpos-c3
*)))
1249 (setf *rpos-c
* (/ 0.1069 mu
))
1252 (if (< g
0.0) (go s50
))
1254 ;; subroutine F is called
1259 ;; Step Q. Quotient acceptance (rare case)
1260 (if (<= (- fy
(* u fy
)) (* py
(exp (- px fx
)))) (go s200
))
1263 ;; Step E. Exponential sample - rndexp is called for
1264 ;; standard exponential variate e and sample tt from the Laplace 'hat'
1265 ;; (if tt <= -.6744 then pk < fk for all mu >= 10.)
1266 (setf e
(rndexp 1.0 0))
1267 (setf u
($random
1.0))
1268 (setf u
(+ u
(- u
1.0)))
1269 (setf tt
(+ 1.8 (if (or (and (> u
0.0) (< e
0.0))
1270 (and (< u
0.0) (> e
0.0)))
1273 (if (<= tt -
0.6744) (go s50
))
1274 (setf ignpoi
(floor (+ mu
(* *rpos-s
* tt
))))
1275 (setf fk
(coerce ignpoi
'flonum
))
1276 (setf difmuk
(- mu fk
))
1278 ;; subroutine F is called
1283 ;; Step F. Hat acceptance (E is repeated on rejection)
1284 (if (> (* *rpos-c
* (abs u
)) (- (* py
(exp (+ px e
))) (* fy
(exp (+ fx e
))))) (go s50
))
1288 ;; Step F. Subroutine F. Calculation of px, py, fx, fy.
1289 ;; Case ignpoi < 10 uses factorials from table fact
1290 (if (>= ignpoi
10) (go s80
))
1292 (setf py
(/ (expt mu ignpoi
) (aref fact ignpoi
)))
1296 ;; Case ignpoi >= 10 uses polynomial approximation
1297 ;; a0-a7 for accuracy when advisable
1298 ;; .8333333E-1=1./12. .3989423=(2*%pi)^(-.5)
1299 (setf del
(/ 8.333333333333333e-2 fk
))
1300 (setf del
(- del
(* 4.8 del del del
)))
1301 (setf v
(/ difmuk fk
))
1302 (if (<= (abs v
) 0.25) (go s90
))
1303 (setf px
(+ (* fk
(log (+ 1.0 v
))) (- difmuk
) (- del
)))
1307 (setf px
(+ (* fk v v
1308 (+ (* (+ (* (+ (* (+ (* (+ (* (+ (* (+ (* a7 v
)
1309 a6
) v
) a5
) v
) a4
) v
) a3
) v
) a2
) v
) a1
) v
) a0
))
1313 (setf py
(/ 0.39894228040143 (sqrt fk
)))
1316 (setf x
(/ (- 0.5 difmuk
) *rpos-s
*))
1318 (setf fx
(* -
0.5 xx
))
1319 (setf fy
(* *rpos-omega
* (+ (* (+ (* (+ (* *rpos-c3
* xx
)
1320 *rpos-c2
*) xx
) *rpos-c1
*) xx
) *rpos-c0
*)))
1321 (if (<= kflag
0) (go s40
))
1325 ;; Case B. Start new table and calculate *rpos-p0* if necessary
1326 (setf *rpos-muprev
* 0.0)
1327 (if (= mu
*rpos-muold
*) (go s130
))
1328 (setf *rpos-muold
* mu
)
1329 (setf *rpos-m
* (max 1 (floor mu
)))
1331 (setf *rpos-p
* (exp (- mu
)))
1332 (setf *rpos-p0
* *rpos-p
*)
1333 (setf *rpos-q
* *rpos-p
*)
1336 ;; Step U. Uniform sample for inversion method
1337 (setf u
($random
1.0))
1339 (if (<= u
*rpos-p0
*) (go s200
))
1342 (if (= *rpos-l
* 0) (go s150
))
1344 (if (> u
0.458) (setf j
(min *rpos-l
* *rpos-m
*)))
1346 ((> k
*rpos-l
*) 'done
)
1347 (cond ((<= u
(aref *rpos-pp
* (- k
1)))
1350 (if (= *rpos-l
* 35) (go s130
))
1353 ;; Step C. Creation of new Poisson probabilities p
1354 ;; and their cumulatives *rpos-q*=*rpos-pp*(k)
1355 (setf *rpos-l
* (1+ *rpos-l
*))
1356 (do ((k *rpos-l
* (1+ k
)))
1358 (setf *rpos-p
* (/ (* *rpos-p
* mu
) (float k
)))
1359 (setf *rpos-q
* (+ *rpos-q
* *rpos-p
*))
1360 (setf (aref *rpos-pp
* (- k
1)) *rpos-q
*)
1361 (cond ((<= u
*rpos-q
*)
1372 ;; The sample size ss must be a non negative integer.
1373 ;; If ss=0, returns a number, otherwise a maxima list
1375 (defun rndpoisson (m ss
&aux sample
)
1376 (declare (type flonum m
))
1377 (declare (type integer ss
))
1379 (rndpoisson-ahrens m
))
1380 (t (setf sample nil
)
1381 (dotimes (i ss
(cons '(mlist simp
) sample
))
1382 (setf sample
(cons (rndpoisson m
0) sample
))))))
1385 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1387 ;; Geometric random simulation ;;
1389 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1392 ;; Generates random geometric variates (p)
1393 ;; by simulation of Bernoulli trials.
1394 (defun rndgeo-trials (p)
1395 (declare (type flonum p
))
1397 (declare (type integer sum
))
1398 (loop (if (<= ($random
1.0) p
) (return sum
))
1399 (setf sum
(1+ sum
)))))
1402 ;; The sample size ss must be a non negative integer.
1403 ;; If ss=0, returns a number, otherwise a maxima list
1405 (defun rndgeo (p ss
&aux sample
)
1406 (declare (type flonum p
))
1407 (declare (type integer ss
))
1410 (t (setf sample nil
)
1411 (dotimes (i ss
(cons '(mlist simp
) sample
))
1412 (setf sample
(cons (rndgeo p
0) sample
))))))
1415 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1417 ;; Hypergeometric random simulation ;;
1419 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1423 ;; Computes the logarithm of the factorial log(n!)
1424 ;; This function is called from rndhypergeo-kachit
1426 (declare (type integer i
))
1427 (let ((di (float i
))
1429 :element-type
'flonum
1430 :initial-contents
'( 0.0
1433 0.69314718055994530941723212145817 ;; ln(2)
1434 1.79175946922805500081247735838070 ;; ln(6)
1435 3.17805383034794561964694160129705 ;; ln(24)
1436 4.78749174278204599424770093452324
1437 6.57925121201010099506017829290394
1438 8.52516136106541430016553103634712))))
1439 (declare (type flonum di
))
1440 (declare (type (simple-array flonum
(9)) al
))
1446 (/ 0.83333333333333333333333333333333 di
)
1447 (/ (- 0.00277777777777777777777777777778)
1449 0.91893853320467274178032973640562))) ) )
1452 ;; Generates random hypergeometric variates (n1, n2, n),
1454 ;; [1] Kachitvichyanukul, V., Schmeiser, B.W. (1985)
1455 ;; Computer generation of hypergeometric random variates.
1456 ;; Journal of Statistical Computation and Simulation 22, 127-145.
1458 ;; [1] This is a translation from C of the
1459 ;; rhyper.c file in the R statistical package.
1460 ;; [2] Some global variables are defined in order to
1461 ;; save time when many random variables are generated.
1463 (defvar *rhyp-n1s
* -
1)
1464 (defvar *rhyp-n2s
* -
1)
1465 (defvar *rhyp-ks
* -
1)
1470 (defvar *rhyp-minjx
*)
1471 (defvar *rhyp-maxjx
*)
1482 (defvar *rhyp-lamdl
*)
1483 (defvar *rhyp-lamdr
*)
1487 (defun rndhypergeo-kachit (nn1 nn2 kk
)
1488 (declare (type integer nn1 nn2 kk
))
1489 (let (ix reject setup1 setup2 e f g p r tt u v y de dg dr ds dt gl
1490 gu nk nm ub xk xm xn y1 ym yn yk alv
1491 (con 57.56462733) (deltal 0.0078) (deltau 0.0034) (scale 1.e25
))
1492 (declare (type flonum con deltal deltau scale
))
1493 ;; if new parameter values, initialize
1495 (cond ((or (/= nn1
*rhyp-n1s
*) (/= nn2
*rhyp-n2s
*))
1498 (t (cond ((/= kk
*rhyp-ks
*)
1504 (setf *rhyp-n1s
* nn1
1506 *rhyp-tn
* (+ nn1 nn2
))
1510 (t (setf *rhyp-n1
* nn2
1514 (cond ((>= (+ kk kk
) *rhyp-tn
*)
1515 (setf *rhyp-k
* (- *rhyp-tn
* kk
)))
1516 (t (setf *rhyp-k
* kk
)))))
1517 (cond ((or setup1 setup2
)
1518 (setf *rhyp-m
* (/ (* (+ *rhyp-k
* 1.0) (+ *rhyp-n1
* 1.0)) (+ *rhyp-tn
* 2.0))
1519 *rhyp-minjx
* (max 0 (- *rhyp-k
* *rhyp-n2
*))
1520 *rhyp-maxjx
* (min *rhyp-n1
* *rhyp-k
*))))
1522 ;; generate random variate --- Three basic cases
1523 (cond ((= *rhyp-minjx
* *rhyp-maxjx
*) ;; I: degenerate distribution ------------
1524 (setf ix
*rhyp-maxjx
*)
1525 (cond ((>= (+ kk kk
) *rhyp-tn
*)
1529 (t (cond ((> nn1 nn2
)
1532 ((< (- *rhyp-m
* *rhyp-minjx
*) 10) ;; II: inverse transformation ------------
1533 (cond ((or setup1 setup2
)
1534 (cond ((< *rhyp-k
* *rhyp-n2
*)
1535 (setf *rhyp-w
* (exp (+ con
1537 (afc (+ *rhyp-n1
* *rhyp-n2
* (- *rhyp-k
*)))
1538 (- (afc (- *rhyp-n2
* *rhyp-k
*)))
1539 (- (afc (+ *rhyp-n1
* *rhyp-n2
*)))))))
1540 (t (setf *rhyp-w
* (exp (+ con
1543 (- (afc (- *rhyp-k
* *rhyp-n2
*)))
1544 (- (afc (+ *rhyp-n1
* *rhyp-n2
*))))))))))
1549 u
(* ($random
1.0) scale
))
1553 (setf p
(* p
(- *rhyp-n1
* ix
) (- *rhyp-k
* ix
)))
1555 (setf p
(/ p ix
(+ *rhyp-n2
* (- *rhyp-k
*) ix
)))
1556 (if (> ix
*rhyp-maxjx
*) (go l10
) (go l20
))))))
1557 (t (cond ((or setup1 setup2
) ;; III : h2pe ----------------------------
1558 (setf *rhyp-s
* (sqrt (/ (* (- *rhyp-tn
* *rhyp-k
*) *rhyp-k
* *rhyp-n1
* *rhyp-n2
*)
1559 (- *rhyp-tn
* 1) *rhyp-tn
* *rhyp-tn
*)))
1560 (setf *rhyp-d
* (+ (round (* 1.5 *rhyp-s
*)) 0.5))
1561 (setf *rhyp-xl
* (+ *rhyp-m
* (- *rhyp-d
*) 0.5)
1562 *rhyp-xr
* (+ *rhyp-m
* *rhyp-d
* 0.5))
1563 (setf *rhyp-a
* (+ (afc *rhyp-m
*)
1564 (afc (- *rhyp-n1
* *rhyp-m
*))
1565 (afc (- *rhyp-k
* *rhyp-m
*))
1566 (afc (+ *rhyp-n2
* (- *rhyp-k
*) *rhyp-m
*))))
1567 (setf *rhyp-kl
* (exp (- *rhyp-a
*
1568 (afc (round *rhyp-xl
*))
1569 (afc (round (- *rhyp-n1
* *rhyp-xl
*)))
1570 (afc (round (- *rhyp-k
* *rhyp-xl
*)))
1571 (afc (round (+ *rhyp-n2
* (- *rhyp-k
*) *rhyp-xl
*))))))
1572 (setf *rhyp-kr
* (exp (- *rhyp-a
*
1573 (afc (round (- *rhyp-xr
* 1)))
1574 (afc (round (+ *rhyp-n1
* (- *rhyp-xr
*) 1)))
1575 (afc (round (+ *rhyp-k
* (- *rhyp-xr
*) 1)))
1576 (afc (round (+ *rhyp-n2
* (- *rhyp-k
*) *rhyp-xr
* (- 1)))))))
1577 (setf *rhyp-lamdl
* (- (log (/ (* *rhyp-xl
* (+ *rhyp-n2
* (- *rhyp-k
*) *rhyp-xl
*))
1578 (+ *rhyp-n1
* (- *rhyp-xl
*) 1.0)
1579 (+ *rhyp-k
* (- *rhyp-xl
*) 1.0))))
1580 *rhyp-lamdr
* (- (log (/ (* (+ *rhyp-n1
* (- *rhyp-xr
*) 1.0)
1581 (+ *rhyp-k
* (- *rhyp-xr
*) 1.0))
1583 (+ *rhyp-n2
* (- *rhyp-k
*) *rhyp-xr
*)))))
1584 (setf *rhyp-p1
* (+ *rhyp-d
* *rhyp-d
*)
1585 *rhyp-p2
* (+ *rhyp-p1
* (/ *rhyp-kl
* *rhyp-lamdl
*))
1586 *rhyp-p3
* (+ *rhyp-p2
* (/ *rhyp-kr
* *rhyp-lamdr
*)))))
1589 (setf u
(* ($random
1.0) *rhyp-p3
*))
1590 (setf v
($random
1.0))
1591 (cond ((< u
*rhyp-p1
*)
1592 ;; rectangular region
1593 (setf ix
(round (+ *rhyp-xl
* u
))))
1596 (setf ix
(round (+ *rhyp-xl
* (/ (log v
) *rhyp-lamdl
*))))
1597 (if (< ix
*rhyp-minjx
*) (go l30
))
1598 (setf v
(* v
(- u
*rhyp-p1
*) *rhyp-lamdl
*)))
1600 (setf ix
(round (- *rhyp-xr
* (/ (log v
) *rhyp-lamdr
*))))
1601 (if (> ix
*rhyp-maxjx
*) (go l30
))
1602 (setf v
(* v
(- u
*rhyp-p2
*) *rhyp-lamdr
*))))
1603 ;; acceptance/rejection test
1604 (cond ((or (< *rhyp-m
* 100) (<= ix
50))
1605 ;; explicit evaluation
1607 (cond ((< *rhyp-m
* ix
)
1608 (do ((i (+ *rhyp-m
* 1) (1+ i
)))
1610 (setf f
(/ (* f
(+ *rhyp-n1
* (- i
) 1) (+ *rhyp-k
* (- i
) 1))
1611 (+ *rhyp-n2
* (- *rhyp-k
*) i
)
1614 (do ((i (+ ix
1) (1+ i
)))
1615 ((> i
*rhyp-m
*) 'done
)
1616 (setf f
(/ (* f i
(+ *rhyp-n2
* (- *rhyp-k
*) i
))
1619 (cond ((<= v f
) (setf reject nil
))))
1620 (t ;; squeeze using upper and lower bounds
1624 yn
(+ *rhyp-n1
* (- *rhyp-k
*) 1.0)
1625 yk
(+ *rhyp-k
* (- y
) 1.0)
1626 nk
(+ *rhyp-n2
* (- *rhyp-k
*) y1
)
1631 g
(- (/ (* yn yk
) (* y1 nk
)) 1.0)
1633 (if (< g
1.0) (setf dg
(+ 1.0 g
)))
1634 (setf gu
(* g
(+ 1.0 (* g
(+ -
0.5 (/ g
3.0)))))
1635 gl
(- gu
(/ (* g g g g
0.25) dg
))
1637 xn
(+ *rhyp-n1
* (- *rhyp-m
*) 0.5)
1638 xk
(+ *rhyp-k
* (- *rhyp-m
*) 0.5)
1639 nm
(+ *rhyp-n2
* (- *rhyp-k
*) xm
)
1643 (* xm r
(+ 1.0 (* r
(+ -
0.5 (/ r
3.0)))))
1644 (* xn
*rhyp-s
* (+ 1.0 (* *rhyp-s
* (+ -
0.5 (/ *rhyp-s
* 3.0)))))
1645 (* xk tt
(+ 1.0 (* tt
(+ -
0.5 (/ tt
3.0)))))
1646 (* nm e
(+ 1.0 (* e
(+ -
0.5 (/ e
3.0)))))))
1647 ;; test against upper bound
1651 (t (setf dr
(* r r r r xm
))
1652 (if (< r
0.0) (setf dr
(/ dr
(+ 1.0 r
))))
1653 (setf ds
(* *rhyp-s
* *rhyp-s
* *rhyp-s
* *rhyp-s
* xn
))
1654 (if (< *rhyp-s
* 0.0) (setf ds
(/ ds
(+ 1.0 *rhyp-s
*))))
1655 (setf dt
(* tt tt tt tt xk
))
1656 (if (< tt
0.0) (setf dt
(/ dt
(+ 1.0 tt
))))
1657 (setf de
(* e e e e nm
))
1658 (if (< e
0.0) (setf de
(/ de
(+ 1.0 e
))))
1660 (* 0.25 (+ dr ds dt de
))
1661 (* (+ y
*rhyp-m
*) (- gl gu
))
1664 ((<= alv
(- *rhyp-a
*
1666 (afc (- *rhyp-n1
* ix
))
1667 (afc (- *rhyp-k
* ix
))
1668 (afc (+ *rhyp-n2
* (- *rhyp-k
*) ix
))))
1670 (t (setf reject t
)))))))
1671 (if reject
(go l30
)))))
1672 ;; return appropriate variate
1673 (cond ((>= (+ kk kk
) *rhyp-tn
*)
1677 (t (cond ((> nn1 nn2
)
1682 ;; The sample size ss must be a non negative integer.
1683 ;; If ss=0, returns a number, otherwise a maxima list
1685 (defun rndhypergeo (n1 n2 n ss
&aux sample
)
1686 (declare (type integer n1 n2 n ss
))
1688 (rndhypergeo-kachit n1 n2 n
) )
1689 (t (setf sample nil
)
1690 (dotimes (i ss
(cons '(mlist simp
) sample
))
1691 (setf sample
(cons (rndhypergeo n1 n2 n
0) sample
))) )) )
1695 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1697 ;; Negative binomial random simulation ;;
1699 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1702 ;; Generates random negative binomial variates (n p)
1704 ;; [1] Devroye, L. (1986)
1705 ;; Non-Uniform Random Variate Generation.
1706 ;; Springer Verlag, p. 480
1708 ;; [1] This is a translation from C of the
1709 ;; rnbinom.c file in the R statistical package.
1710 (defun rndnegbinom-devroye (n p
)
1711 (declare (type flonum p
))
1712 (rndpoisson (rndgamma n
(/ (- 1.0 p
) p
) 0) 0) )
1715 ;; The sample size ss must be a non negative integer.
1716 ;; If ss=0, returns a number, otherwise a maxima list
1718 (defun rndnegbinom (n p ss
&aux sample
)
1720 (rndnegbinom-devroye n p
) )
1721 (t (setf sample nil
)
1722 (dotimes (i ss
(cons '(mlist simp
) sample
))
1723 (setf sample
(cons (rndnegbinom n p
0) sample
))) )) )