2 ;;; Copyright (c) 2005--2007, by A.J. Rossini <blindglobe@gmail.com>
3 ;;; See COPYRIGHT file for any additional restrictions (BSD license).
4 ;;; Since 1991, ANSI was finally finished. Edited for ANSI Common Lisp.
6 ;;;; dists -- Lisp-Stat interface to basic probability distribution routines
8 ;;;; Copyright (c) 1991, by Luke Tierney. Permission is granted for
16 (in-package :lisp-stat-basics
)
18 (export '(log-gamma uniform-rand normal-cdf normal-quant normal-dens
19 normal-rand bivnorm-cdf cauchy-cdf cauchy-quant cauchy-dens
20 cauchy-rand gamma-cdf gamma-quant gamma-dens gamma-rand
21 chisq-cdf chisq-quant chisq-dens chisq-rand beta-cdf beta-quant
22 beta-dens beta-rand t-cdf t-quant t-dens t-rand f-cdf f-quant
23 f-dens f-rand poisson-cdf poisson-quant poisson-pmf poisson-rand
24 binomial-cdf binomial-quant binomial-pmf binomial-rand
))
26 (defmacro defbaserand
(name onefun
&rest args
)
27 `(defun ,name
(n ,@args
)
30 (declare (fixnum i
) (inline ,onefun
))
31 (setf result
(cons (,onefun
,@args
) result
))))))
33 (defbaserand base-uniform-rand one-uniform-rand
)
34 (defbaserand base-normal-rand one-normal-rand
)
35 (defbaserand base-cauchy-rand one-cauchy-rand
)
36 (defbaserand base-gamma-rand one-gamma-rand a
)
37 (defbaserand base-chisq-rand one-chisq-rand df
)
38 (defbaserand base-beta-rand one-beta-rand a b
)
39 (defbaserand base-t-rand one-t-rand df
)
40 (defbaserand base-f-rand one-f-rand ndf ddf
)
41 (defbaserand base-poisson-rand one-poisson-rand a
)
42 (defbaserand base-binomial-rand one-binomial-rand a b
)
44 (make-rv-function log-gamma base-log-gamma x
)
46 (make-rv-function uniform-rand base-uniform-rand n
)
48 (make-rv-function normal-cdf base-normal-cdf x
)
49 (make-rv-function normal-quant base-normal-quant p
)
50 (make-rv-function normal-dens base-normal-dens x
)
51 (make-rv-function normal-rand base-normal-rand n
)
52 (make-rv-function bivnorm-cdf base-bivnorm-cdf x y r
)
54 (make-rv-function cauchy-cdf base-cauchy-cdf x
)
55 (make-rv-function cauchy-quant base-cauchy-quant p
)
56 (make-rv-function cauchy-dens base-cauchy-dens x
)
57 (make-rv-function cauchy-rand base-cauchy-rand n
)
59 (make-rv-function gamma-cdf base-gamma-cdf x a
)
60 (make-rv-function gamma-quant base-gamma-quant p a
)
61 (make-rv-function gamma-dens base-gamma-dens x a
)
62 (make-rv-function gamma-rand base-gamma-rand n a
)
64 (make-rv-function chisq-cdf base-chisq-cdf x df
)
65 (make-rv-function chisq-quant base-chisq-quant p df
)
66 (make-rv-function chisq-dens base-chisq-dens x df
)
67 (make-rv-function chisq-rand base-chisq-rand n df
)
69 (make-rv-function beta-cdf base-beta-cdf x a b
)
70 (make-rv-function beta-quant base-beta-quant p a b
)
71 (make-rv-function beta-dens base-beta-dens x a b
)
72 (make-rv-function beta-rand base-beta-rand n a b
)
74 (make-rv-function t-cdf base-t-cdf x df
)
75 (make-rv-function t-quant base-t-quant p df
)
76 (make-rv-function t-dens base-t-dens x df
)
77 (make-rv-function t-rand base-t-rand n df
)
79 (make-rv-function f-cdf base-f-cdf x ndf ddf
)
80 (make-rv-function f-quant base-f-quant p ndf ddf
)
81 (make-rv-function f-dens base-f-dens x ndf ddf
)
82 (make-rv-function f-rand base-f-rand n ndf ddf
)
84 (make-rv-function poisson-cdf base-poisson-cdf x a
)
85 (make-rv-function poisson-quant base-poisson-quant p a
)
86 (make-rv-function poisson-pmf base-poisson-pmf x a
)
87 (make-rv-function poisson-rand base-poisson-rand n a
)
89 (make-rv-function binomial-cdf base-binomial-cdf x a b
)
90 (make-rv-function binomial-quant base-binomial-quant p a b
)
91 (make-rv-function binomial-pmf base-binomial-pmf x a b
)
92 (make-rv-function binomial-rand base-binomial-rand n a b
)
98 (setf (documentation 'bivnorm-cdf
'function
)
100 Returns the value of the standard bivariate normal distribution function
101 with correlation R at (X, Y). Vectorized.")
103 (setf (documentation 'normal-cdf
'function
)
105 Returns the value of the standard normal distribution function at X.
108 (setf (documentation 'beta-cdf
'function
)
109 "Args: (x alpha beta)
110 Returns the value of the Beta(ALPHA, BETA) distribution function at X.
113 (setf (documentation 'gamma-cdf
'function
)
115 Returns the value of the Gamma(alpha, 1) distribution function at X.
118 (setf (documentation 'chisq-cdf
'function
)
120 Returns the value of the Chi-Square(DF) distribution function at X. Vectorized.")
122 (setf (documentation 't-cdf
'function
)
124 Returns the value of the T(DF) distribution function at X. Vectorized.")
126 (setf (documentation 'f-cdf
'function
)
128 Returns the value of the F(NDF, DDF) distribution function at X. Vectorized.")
130 (setf (documentation 'cauchy-cdf
'function
)
132 Returns the value of the standard Cauchy distribution function at X.
135 (setf (documentation 'log-gamma
'function
)
137 Returns the log gamma function of X. Vectorized.")
139 (setf (documentation 'normal-quant
'function
)
141 Returns the P-th quantile of the standard normal distribution. Vectorized.")
143 (setf (documentation 'cauchy-quant
'function
)
145 Returns the P-th quantile(s) of the standard Cauchy distribution. Vectorized.")
147 (setf (documentation 'beta-quant
'function
)
148 "Args: (p alpha beta)
149 Returns the P-th quantile of the Beta(ALPHA, BETA) distribution. Vectorized.")
151 (setf (documentation 'gamma-quant
'function
)
153 Returns the P-th quantile of the Gamma(ALPHA, 1) distribution. Vectorized.")
155 (setf (documentation 'chisq-quant
'function
)
157 Returns the P-th quantile of the Chi-Square(DF) distribution. Vectorized.")
159 (setf (documentation 't-quant
'function
)
161 Returns the P-th quantile of the T(DF) distribution. Vectorized.")
163 (setf (documentation 'f-quant
'function
)
165 Returns the P-th quantile of the F(NDF, DDF) distribution. Vectorized.")
167 (setf (documentation 'normal-dens
'function
)
169 Returns the density at X of the standard normal distribution. Vectorized.")
171 (setf (documentation 'cauchy-dens
'function
)
173 Returns the density at X of the standard Cauchy distribution. Vectorized.")
175 (setf (documentation 'beta-dens
'function
)
176 "Args: (x alpha beta)
177 Returns the density at X of the Beta(ALPHA, BETA) distribution. Vectorized.")
179 (setf (documentation 'gamma-dens
'function
)
181 Returns the density at X of the Gamma(ALPHA, 1) distribution. Vectorized.")
183 (setf (documentation 'chisq-dens
'function
)
185 Returns the density at X of the Chi-Square(DF) distribution. Vectorized.")
187 (setf (documentation 't-dens
'function
)
189 Returns the density at X of the T(DF) distribution. Vectorized.")
191 (setf (documentation 'f-dens
'function
)
193 Returns the density at X of the F(NDF, DDF) distribution. Vectorized.")
195 (setf (documentation 'uniform-rand
'function
)
197 Returns a list of N uniform random variables from the range (0, 1).
200 (setf (documentation 'normal-rand
'function
)
202 Returns a list of N standard normal random numbers. Vectorized.")
204 (setf (documentation 'cauchy-rand
'function
)
206 Returns a list of N standard Cauchy random numbers. Vectorized.")
208 (setf (documentation 't-rand
'function
)
210 Returns a list of N T(DF) random variables. Vectorized.")
212 (setf (documentation 'f-rand
'function
)
214 Returns a list of N F(NDF, DDF) random variables. Vectorized.")
216 (setf (documentation 'gamma-rand
'function
)
218 Returns a list of N Gamma(A, 1) random variables. Vectorized.")
220 (setf (documentation 'chisq-rand
'function
)
222 Returns a list of N Chi-Square(DF) random variables. Vectorized.")
224 (setf (documentation 'beta-rand
'function
)
226 Returns a list of N beta(A, B) random variables. Vectorized.")
228 (setf (documentation 'binomial-cdf
'function
)
230 Returns value of the Binomial(N, P) distribution function at X. Vectorized.")
232 (setf (documentation 'poisson-cdf
'function
)
234 Returns value of the Poisson(MU) distribution function at X. Vectorized.")
236 (setf (documentation 'binomial-pmf
'function
)
238 Returns value of the Binomial(N, P) pmf function at integer K. Vectorized.")
240 (setf (documentation 'poisson-pmf
'function
)
242 Returns value of the Poisson(MU) pmf function at integer K. Vectorized.")
244 (setf (documentation 'binomial-quant
'function
)
246 Returns x-th quantile (left continuous inverse) of Binomial(N, P) cdf.
249 (setf (documentation 'poisson-quant
'function
)
251 Returns x-th quantile (left continuous inverse) of Poisson(MU) cdf.
254 (setf (documentation 'binomial-rand
'function
)
256 Returns list of K draws from the Binomial(N, P) distribution. Vectorized.")
258 (setf (documentation 'poisson-rand
'function
)
260 Returns list of K draws from the Poisson(MU) distribution. Vectorized.")