Pristine Start using Luke's original CLS 1.0 alpha 1
[CommonLispStat.git] / dists.lsp
blobac44907df21f40bf730cf8e442e19d23bec4c8c7
1 ;;;; dists -- Lisp-Stat interface to basic probability distribution routines
2 ;;;;
3 ;;;; Copyright (c) 1991, by Luke Tierney. Permission is granted for
4 ;;;; unrestricted use.
6 ;;;;
7 ;;;; Package Setup
8 ;;;;
10 #+:CLtL2
11 (in-package lisp-stat-basics)
12 #-:CLtL2
13 (in-package 'lisp-stat-basics)
15 (export '(log-gamma uniform-rand normal-cdf normal-quant normal-dens
16 normal-rand bivnorm-cdf cauchy-cdf cauchy-quant cauchy-dens
17 cauchy-rand gamma-cdf gamma-quant gamma-dens gamma-rand
18 chisq-cdf chisq-quant chisq-dens chisq-rand beta-cdf beta-quant
19 beta-dens beta-rand t-cdf t-quant t-dens t-rand f-cdf f-quant
20 f-dens f-rand poisson-cdf poisson-quant poisson-pmf poisson-rand
21 binomial-cdf binomial-quant binomial-pmf binomial-rand))
23 (defmacro defbaserand (name onefun &rest args)
24 `(defun ,name (n ,@args)
25 (let ((result nil))
26 (dotimes (i n result)
27 (declare (fixnum i) (inline ,onefun))
28 (setf result (cons (,onefun ,@args) result))))))
30 (defbaserand base-uniform-rand one-uniform-rand)
31 (defbaserand base-normal-rand one-normal-rand)
32 (defbaserand base-cauchy-rand one-cauchy-rand)
33 (defbaserand base-gamma-rand one-gamma-rand a)
34 (defbaserand base-chisq-rand one-chisq-rand df)
35 (defbaserand base-beta-rand one-beta-rand a b)
36 (defbaserand base-t-rand one-t-rand df)
37 (defbaserand base-f-rand one-f-rand ndf ddf)
38 (defbaserand base-poisson-rand one-poisson-rand a)
39 (defbaserand base-binomial-rand one-binomial-rand a b)
41 (make-rv-function log-gamma base-log-gamma x)
43 (make-rv-function uniform-rand base-uniform-rand n)
45 (make-rv-function normal-cdf base-normal-cdf x)
46 (make-rv-function normal-quant base-normal-quant p)
47 (make-rv-function normal-dens base-normal-dens x)
48 (make-rv-function normal-rand base-normal-rand n)
49 (make-rv-function bivnorm-cdf base-bivnorm-cdf x y r)
51 (make-rv-function cauchy-cdf base-cauchy-cdf x)
52 (make-rv-function cauchy-quant base-cauchy-quant p)
53 (make-rv-function cauchy-dens base-cauchy-dens x)
54 (make-rv-function cauchy-rand base-cauchy-rand n)
56 (make-rv-function gamma-cdf base-gamma-cdf x a)
57 (make-rv-function gamma-quant base-gamma-quant p a)
58 (make-rv-function gamma-dens base-gamma-dens x a)
59 (make-rv-function gamma-rand base-gamma-rand n a)
61 (make-rv-function chisq-cdf base-chisq-cdf x df)
62 (make-rv-function chisq-quant base-chisq-quant p df)
63 (make-rv-function chisq-dens base-chisq-dens x df)
64 (make-rv-function chisq-rand base-chisq-rand n df)
66 (make-rv-function beta-cdf base-beta-cdf x a b)
67 (make-rv-function beta-quant base-beta-quant p a b)
68 (make-rv-function beta-dens base-beta-dens x a b)
69 (make-rv-function beta-rand base-beta-rand n a b)
71 (make-rv-function t-cdf base-t-cdf x df)
72 (make-rv-function t-quant base-t-quant p df)
73 (make-rv-function t-dens base-t-dens x df)
74 (make-rv-function t-rand base-t-rand n df)
76 (make-rv-function f-cdf base-f-cdf x ndf ddf)
77 (make-rv-function f-quant base-f-quant p ndf ddf)
78 (make-rv-function f-dens base-f-dens x ndf ddf)
79 (make-rv-function f-rand base-f-rand n ndf ddf)
81 (make-rv-function poisson-cdf base-poisson-cdf x a)
82 (make-rv-function poisson-quant base-poisson-quant p a)
83 (make-rv-function poisson-pmf base-poisson-pmf x a)
84 (make-rv-function poisson-rand base-poisson-rand n a)
86 (make-rv-function binomial-cdf base-binomial-cdf x a b)
87 (make-rv-function binomial-quant base-binomial-quant p a b)
88 (make-rv-function binomial-pmf base-binomial-pmf x a b)
89 (make-rv-function binomial-rand base-binomial-rand n a b)
91 ;;;;
92 ;;;; Documentation
93 ;;;;
95 (setf (documentation 'bivnorm-cdf 'function)
96 "Args: (x y r)
97 Returns the value of the standard bivariate normal distribution function
98 with correlation R at (X, Y). Vectorized.")
100 (setf (documentation 'normal-cdf 'function)
101 "Args: (x)
102 Returns the value of the standard normal distribution function at X.
103 Vectorized.")
105 (setf (documentation 'beta-cdf 'function)
106 "Args: (x alpha beta)
107 Returns the value of the Beta(ALPHA, BETA) distribution function at X.
108 Vectorized.")
110 (setf (documentation 'gamma-cdf 'function)
111 "Args: (x alpha)
112 Returns the value of the Gamma(alpha, 1) distribution function at X.
113 Vectorized.")
115 (setf (documentation 'chisq-cdf 'function)
116 "Args: (x df)
117 Returns the value of the Chi-Square(DF) distribution function at X. Vectorized.")
119 (setf (documentation 't-cdf 'function)
120 "Args: (x df)
121 Returns the value of the T(DF) distribution function at X. Vectorized.")
123 (setf (documentation 'f-cdf 'function)
124 "Args: (x ndf ddf)
125 Returns the value of the F(NDF, DDF) distribution function at X. Vectorized.")
127 (setf (documentation 'cauchy-cdf 'function)
128 "Args: (x)
129 Returns the value of the standard Cauchy distribution function at X.
130 Vectorized.")
132 (setf (documentation 'log-gamma 'function)
133 "Args: (x)
134 Returns the log gamma function of X. Vectorized.")
136 (setf (documentation 'normal-quant 'function)
137 "Args (p)
138 Returns the P-th quantile of the standard normal distribution. Vectorized.")
140 (setf (documentation 'cauchy-quant 'function)
141 "Args (p)
142 Returns the P-th quantile(s) of the standard Cauchy distribution. Vectorized.")
144 (setf (documentation 'beta-quant 'function)
145 "Args: (p alpha beta)
146 Returns the P-th quantile of the Beta(ALPHA, BETA) distribution. Vectorized.")
148 (setf (documentation 'gamma-quant 'function)
149 "Args: (p alpha)
150 Returns the P-th quantile of the Gamma(ALPHA, 1) distribution. Vectorized.")
152 (setf (documentation 'chisq-quant 'function)
153 "Args: (p df)
154 Returns the P-th quantile of the Chi-Square(DF) distribution. Vectorized.")
156 (setf (documentation 't-quant 'function)
157 "Args: (p df)
158 Returns the P-th quantile of the T(DF) distribution. Vectorized.")
160 (setf (documentation 'f-quant 'function)
161 "Args: (p ndf ddf)
162 Returns the P-th quantile of the F(NDF, DDF) distribution. Vectorized.")
164 (setf (documentation 'normal-dens 'function)
165 "Args: (x)
166 Returns the density at X of the standard normal distribution. Vectorized.")
168 (setf (documentation 'cauchy-dens 'function)
169 "Args: (x)
170 Returns the density at X of the standard Cauchy distribution. Vectorized.")
172 (setf (documentation 'beta-dens 'function)
173 "Args: (x alpha beta)
174 Returns the density at X of the Beta(ALPHA, BETA) distribution. Vectorized.")
176 (setf (documentation 'gamma-dens 'function)
177 "Args: (x alpha)
178 Returns the density at X of the Gamma(ALPHA, 1) distribution. Vectorized.")
180 (setf (documentation 'chisq-dens 'function)
181 "Args: (x alpha)
182 Returns the density at X of the Chi-Square(DF) distribution. Vectorized.")
184 (setf (documentation 't-dens 'function)
185 "Args: (x alpha)
186 Returns the density at X of the T(DF) distribution. Vectorized.")
188 (setf (documentation 'f-dens 'function)
189 "Args: (x ndf ddf)
190 Returns the density at X of the F(NDF, DDF) distribution. Vectorized.")
192 (setf (documentation 'uniform-rand 'function)
193 "Args: (n)
194 Returns a list of N uniform random variables from the range (0, 1).
195 Vectorized.")
197 (setf (documentation 'normal-rand 'function)
198 "Args: (n)
199 Returns a list of N standard normal random numbers. Vectorized.")
201 (setf (documentation 'cauchy-rand 'function)
202 "Args: (n)
203 Returns a list of N standard Cauchy random numbers. Vectorized.")
205 (setf (documentation 't-rand 'function)
206 "Args: (n df)
207 Returns a list of N T(DF) random variables. Vectorized.")
209 (setf (documentation 'f-rand 'function)
210 "Args: (n ndf ddf)
211 Returns a list of N F(NDF, DDF) random variables. Vectorized.")
213 (setf (documentation 'gamma-rand 'function)
214 "Args: (n a)
215 Returns a list of N Gamma(A, 1) random variables. Vectorized.")
217 (setf (documentation 'chisq-rand 'function)
218 "Args: (n df)
219 Returns a list of N Chi-Square(DF) random variables. Vectorized.")
221 (setf (documentation 'beta-rand 'function)
222 "Args: (n a b)
223 Returns a list of N beta(A, B) random variables. Vectorized.")
225 (setf (documentation 'binomial-cdf 'function)
226 "Args (x n p)
227 Returns value of the Binomial(N, P) distribution function at X. Vectorized.")
229 (setf (documentation 'poisson-cdf 'function)
230 "Args (x mu)
231 Returns value of the Poisson(MU) distribution function at X. Vectorized.")
233 (setf (documentation 'binomial-pmf 'function)
234 "Args (k n p)
235 Returns value of the Binomial(N, P) pmf function at integer K. Vectorized.")
237 (setf (documentation 'poisson-pmf 'function)
238 "Args (k mu)
239 Returns value of the Poisson(MU) pmf function at integer K. Vectorized.")
241 (setf (documentation 'binomial-quant 'function)
242 "Args: (x n p)
243 Returns x-th quantile (left continuous inverse) of Binomial(N, P) cdf.
244 Vectorized.")
246 (setf (documentation 'poisson-quant 'function)
247 "Args: (x mu)
248 Returns x-th quantile (left continuous inverse) of Poisson(MU) cdf.
249 Vectorized.")
251 (setf (documentation 'binomial-rand 'function)
252 "Args: (k n p)
253 Returns list of K draws from the Binomial(N, P) distribution. Vectorized.")
255 (setf (documentation 'poisson-rand 'function)
256 "Args: (k mu)
257 Returns list of K draws from the Poisson(MU) distribution. Vectorized.")