doc cleanup.
[CommonLispStat.git] / dists.lsp
blobedbc6cd5813ab04caaf3e13e141cf155d216aab0
1 ;;; -*- mode: lisp -*-
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
7 ;;;
8 ;;; Copyright (c) 1991, by Luke Tierney. Permission is granted for
9 ;;; unrestricted use.
11 ;;; This stuff needs to be improved. We really could use something
12 ;;; like the R libraries, but of course in a better packaged manner.
14 ;;; Currently, there is a function for everything. Probably better to
15 ;;; simplify by thinking about a more generic approach, distribution
16 ;;; being specified by keyword.
18 ;;;
19 ;;; Package Setup
20 ;;;
22 (in-package :cl-user)
24 (defpackage :lisp-stat-probability
25 (:use :common-lisp
26 :cffi
27 :lisp-stat-ffi-int
28 :lisp-stat-macros)
29 (:export log-gamma
30 uniform-rand
31 normal-cdf normal-quant normal-dens normal-rand
32 bivnorm-cdf
33 cauchy-cdf cauchy-quant cauchy-dens cauchy-rand
34 gamma-cdf gamma-quant gamma-dens gamma-rand
35 chisq-cdf chisq-quant chisq-dens chisq-rand
36 beta-cdf beta-quant beta-dens beta-rand
37 t-cdf t-quant t-dens t-rand
38 f-cdf f-quant f-dens f-rand
39 poisson-cdf poisson-quant poisson-pmf poisson-rand
40 binomial-cdf binomial-quant binomial-pmf binomial-rand))
42 (in-package :lisp-stat-probability)
44 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45 ;;;
46 ;;; CFFI support for Probability Distributions
47 ;;;
48 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
50 ;;;
51 ;;; C-callable uniform generator
52 ;;;
54 (defcfun ("register_uni" register-uni)
55 :void (f :pointer))
56 (defcallback ccl-uni :int () (ccl-store-double (random 1.0)) 0)
57 (register-uni (callback ccl-uni))
59 (defun one-uniform-rand () (random 1.0))
61 ;;;
62 ;;; Log-gamma function
63 ;;;
65 (defcfun ("ccl_gamma" ccl-base-log-gamma)
66 :double (x :double))
67 (defun base-log-gamma (x)
68 (ccl-base-log-gamma (float x 1d0)))
70 ;;;
71 ;;; Normal distribution
72 ;;;
74 (defcfun ("ccl_normalcdf" ccl-base-normal-cdf)
75 :double (x :double))
76 (defun base-normal-cdf (x)
77 (ccl-base-normal-cdf (float x 1d0)))
79 (defcfun ("ccl_normalquant" ccl-base-normal-quant)
80 :double (x :double))
81 (defun base-normal-quant (x)
82 (ccl-base-normal-quant (float x 1d0)))
84 (defcfun ("ccl_normaldens" ccl-base-normal-dens)
85 :double (x :double))
86 (defun base-normal-dens (x)
87 (ccl-base-normal-dens (float x 1d0)))
89 (defcfun ("ccl_normalrand" one-normal-rand)
90 :float)
92 (defcfun ("ccl_bnormcdf" ccl-base-bivnorm-cdf)
93 :double (x :double) (y :double) (z :double))
94 (defun base-bivnorm-cdf (x y z)
95 (ccl-base-bivnorm-cdf (float x 1d0) (float y 1d0) (float z 1d0)))
97 ;;;
98 ;;; Cauchy distribution
99 ;;;
101 (defcfun ("ccl_cauchycdf" ccl-base-cauchy-cdf)
102 :double (x :double))
103 (defun base-cauchy-cdf (x)
104 (ccl-base-cauchy-cdf (float x 1d0)))
106 (defcfun ("ccl_cauchyquant" ccl-base-cauchy-quant)
107 :double (x :double))
108 (defun base-cauchy-quant (x)
109 (ccl-base-cauchy-quant (float x 1d0)))
111 (defcfun ("ccl_cauchydens" ccl-base-cauchy-dens)
112 :double (x :double))
113 (defun base-cauchy-dens (x)
114 (ccl-base-cauchy-dens (float x 1d0)))
116 (defcfun ("ccl_cauchyrand" one-cauchy-rand)
117 :double)
119 ;;;;
120 ;;;; Gamma distribution
121 ;;;;
123 (defcfun ("ccl_gammacdf" ccl-base-gamma-cdf)
124 :double (x :double) (y :double))
125 (defun base-gamma-cdf (x y)
126 (ccl-base-gamma-cdf (float x 1d0) (float y 1d0)))
128 (defcfun ("ccl_gammaquant" ccl-base-gamma-quant)
129 :double (x :double) (y :double))
130 (defun base-gamma-quant (x y)
131 (ccl-base-gamma-quant (float x 1d0) (float y 1d0)))
133 (defcfun ("ccl_gammadens" ccl-base-gamma-dens)
134 :double (x :double) (y :double))
135 (defun base-gamma-dens (x y)
136 (ccl-base-gamma-dens (float x 1d0) (float y 1d0)))
138 (defcfun ("ccl_gammarand" ccl-gamma-rand)
139 :double (x :double))
140 (defun one-gamma-rand (x)
141 (ccl-gamma-rand (float x 1d0)))
143 ;;;;
144 ;;;; Chi-square distribution
145 ;;;;
147 (defcfun ("ccl_chisqcdf" ccl-base-chisq-cdf)
148 :double (x :double) (y :double))
149 (defun base-chisq-cdf (x y)
150 (ccl-base-chisq-cdf (float x 1d0) (float y 1d0)))
152 (defcfun ("ccl_chisqquant" ccl-base-chisq-quant)
153 :double (x :double) (y :double))
154 (defun base-chisq-quant (x y)
155 (ccl-base-chisq-quant (float x 1d0) (float y 1d0)))
157 (defcfun ("ccl_chisqdens" ccl-base-chisq-dens)
158 :double (x :double) (y :double))
159 (defun base-chisq-dens (x y)
160 (ccl-base-chisq-dens (float x 1d0) (float y 1d0)))
162 (defcfun ("ccl_chisqrand" ccl-chisq-rand)
163 :double (x :double))
164 (defun one-chisq-rand (x)
165 (ccl-chisq-rand (float x 1d0)))
167 ;;;;
168 ;;;; Beta distribution
169 ;;;;
171 (defcfun ("ccl_betacdf" ccl-base-beta-cdf)
172 :double (x :double) (y :double) (z :double))
173 (defun base-beta-cdf (x y z)
174 (ccl-base-beta-cdf (float x 1d0) (float y 1d0) (float z 1d0)))
176 (defcfun ("ccl_betaquant" ccl-base-beta-quant)
177 :double (x :double) (y :double) (z :double))
178 (defun base-beta-quant (x y z)
179 (ccl-base-beta-quant (float x 1d0) (float y 1d0) (float z 1d0)))
181 (defcfun ("ccl_betadens" ccl-base-beta-dens)
182 :double (x :double) (y :double) (z :double))
183 (defun base-beta-dens (x y z)
184 (ccl-base-beta-dens (float x 1d0) (float y 1d0) (float z 1d0)))
186 (defcfun ("ccl_betarand" ccl-beta-rand)
187 :double (x :double) (y :double))
188 (defun one-beta-rand (x y)
189 (ccl-beta-rand (float x 1d0) (float y 1d0)))
191 ;;;;
192 ;;;; t distribution
193 ;;;;
195 (defcfun ("ccl_tcdf" ccl-base-t-cdf)
196 :double (x :double) (y :double))
197 (defun base-t-cdf (x y)
198 (ccl-base-t-cdf (float x 1d0) (float y 1d0)))
200 (defcfun ("ccl_tquant" ccl-base-t-quant)
201 :double (x :double) (y :double))
202 (defun base-t-quant (x y)
203 (ccl-base-t-quant (float x 1d0) (float y 1d0)))
205 (defcfun ("ccl_tdens" ccl-base-t-dens)
206 :double (x :double) (y :double))
207 (defun base-t-dens (x y)
208 (ccl-base-t-dens (float x 1d0) (float y 1d0)))
210 (defcfun ("ccl_trand" ccl-t-rand)
211 :double (x :double))
212 (defun one-t-rand (x)
213 (ccl-t-rand (float x 1d0)))
215 ;;;;
216 ;;;; F distribution
217 ;;;;
219 (defcfun ("ccl_fcdf" ccl-base-f-cdf)
220 :double (x :double) (y :double) (z :double))
221 (defun base-f-cdf (x y z)
222 (ccl-base-f-cdf (float x 1d0) (float y 1d0) (float z 1d0)))
224 (defcfun ("ccl_fquant" ccl-base-f-quant)
225 :double (x :double) (y :double) (z :double))
226 (defun base-f-quant (x y z)
227 (ccl-base-f-quant (float x 1d0) (float y 1d0) (float z 1d0)))
229 (defcfun ("ccl_fdens" ccl-base-f-dens)
230 :double (x :double) (y :double) (z :double))
231 (defun base-f-dens (x y z)
232 (ccl-base-f-dens (float x 1d0) (float y 1d0) (float z 1d0)))
234 (defcfun ("ccl_frand" ccl-f-rand)
235 :double (x :double) (y :double))
236 (defun one-f-rand (x y) (ccl-f-rand (float x 1d0) (float y 1d0)))
238 ;;;;
239 ;;;; Poisson distribution
240 ;;;;
242 (defcfun ("ccl_poissoncdf" ccl-base-poisson-cdf)
243 :double (x :double) (y :double))
244 (defun base-poisson-cdf (x y)
245 (ccl-base-poisson-cdf (float x 1d0) (float y 1d0)))
247 (defcfun ("ccl_poissonquant" ccl-base-poisson-quant)
248 :int (x :double) (y :double))
249 (defun base-poisson-quant (x y)
250 (ccl-base-poisson-quant (float x 1d0) (float y 1d0)))
252 (defcfun ("ccl_poissonpmf" ccl-base-poisson-pmf)
253 :double (x :int) (y :double))
254 (defun base-poisson-pmf (x y)
255 (ccl-base-poisson-pmf x (float y 1d0)))
257 (defcfun ("ccl_poissonrand" ccl-poisson-rand)
258 :int (x :double))
259 (defun one-poisson-rand (x)
260 (ccl-poisson-rand (float x 1d0)))
262 ;;;;
263 ;;;; Binomial distribution
264 ;;;;
266 (defcfun ("ccl_binomialcdf" ccl-base-binomial-cdf)
267 :double (x :double) (y :int) (z :double))
268 (defun base-binomial-cdf (x y z)
269 (ccl-base-binomial-cdf (float x 1d0) y (float z 1d0)))
271 (defcfun ("ccl_binomialquant" ccl-base-binomial-quant)
272 :int (x :double) (y :int) (z :double))
273 (defun base-binomial-quant (x y z)
274 (ccl-base-binomial-quant (float x 1d0) y (float z 1d0)))
276 (defcfun ("ccl_binomialpmf" ccl-base-binomial-pmf)
277 :double (x :int) (y :int) (z :double))
278 (defun base-binomial-pmf (x y z)
279 (ccl-base-binomial-pmf x y (float z 1d0)))
281 (defcfun ("ccl_binomialrand" ccl-binomial-rand)
282 :int (x :int) (y :double))
283 (defun one-binomial-rand (x y)
284 (ccl-binomial-rand x (float y 1d0)))
290 ;;; definitions though macros
292 (defmacro defbaserand (name onefun &rest args)
293 `(defun ,name (n ,@args)
294 (let ((result nil))
295 (dotimes (i n result)
296 (declare (fixnum i) (inline ,onefun))
297 (setf result (cons (,onefun ,@args) result))))))
299 (defbaserand base-uniform-rand one-uniform-rand)
300 (defbaserand base-normal-rand one-normal-rand)
301 (defbaserand base-cauchy-rand one-cauchy-rand)
302 (defbaserand base-gamma-rand one-gamma-rand a)
303 (defbaserand base-chisq-rand one-chisq-rand df)
304 (defbaserand base-beta-rand one-beta-rand a b)
305 (defbaserand base-t-rand one-t-rand df)
306 (defbaserand base-f-rand one-f-rand ndf ddf)
307 (defbaserand base-poisson-rand one-poisson-rand a)
308 (defbaserand base-binomial-rand one-binomial-rand a b)
310 (make-rv-function log-gamma base-log-gamma x)
312 (make-rv-function uniform-rand base-uniform-rand n)
314 (make-rv-function normal-cdf base-normal-cdf x)
315 (make-rv-function normal-quant base-normal-quant p)
316 (make-rv-function normal-dens base-normal-dens x)
317 (make-rv-function normal-rand base-normal-rand n)
318 (make-rv-function bivnorm-cdf base-bivnorm-cdf x y r)
320 (make-rv-function cauchy-cdf base-cauchy-cdf x)
321 (make-rv-function cauchy-quant base-cauchy-quant p)
322 (make-rv-function cauchy-dens base-cauchy-dens x)
323 (make-rv-function cauchy-rand base-cauchy-rand n)
325 (make-rv-function gamma-cdf base-gamma-cdf x a)
326 (make-rv-function gamma-quant base-gamma-quant p a)
327 (make-rv-function gamma-dens base-gamma-dens x a)
328 (make-rv-function gamma-rand base-gamma-rand n a)
330 (make-rv-function chisq-cdf base-chisq-cdf x df)
331 (make-rv-function chisq-quant base-chisq-quant p df)
332 (make-rv-function chisq-dens base-chisq-dens x df)
333 (make-rv-function chisq-rand base-chisq-rand n df)
335 (make-rv-function beta-cdf base-beta-cdf x a b)
336 (make-rv-function beta-quant base-beta-quant p a b)
337 (make-rv-function beta-dens base-beta-dens x a b)
338 (make-rv-function beta-rand base-beta-rand n a b)
340 (make-rv-function t-cdf base-t-cdf x df)
341 (make-rv-function t-quant base-t-quant p df)
342 (make-rv-function t-dens base-t-dens x df)
343 (make-rv-function t-rand base-t-rand n df)
345 (make-rv-function f-cdf base-f-cdf x ndf ddf)
346 (make-rv-function f-quant base-f-quant p ndf ddf)
347 (make-rv-function f-dens base-f-dens x ndf ddf)
348 (make-rv-function f-rand base-f-rand n ndf ddf)
350 (make-rv-function poisson-cdf base-poisson-cdf x a)
351 (make-rv-function poisson-quant base-poisson-quant p a)
352 (make-rv-function poisson-pmf base-poisson-pmf x a)
353 (make-rv-function poisson-rand base-poisson-rand n a)
355 (make-rv-function binomial-cdf base-binomial-cdf x a b)
356 (make-rv-function binomial-quant base-binomial-quant p a b)
357 (make-rv-function binomial-pmf base-binomial-pmf x a b)
358 (make-rv-function binomial-rand base-binomial-rand n a b)
360 ;;;;
361 ;;;; Documentation
362 ;;;;
364 (setf (documentation 'bivnorm-cdf 'function)
365 "Args: (x y r)
366 Returns the value of the standard bivariate normal distribution function
367 with correlation R at (X, Y). Vectorized.")
369 (setf (documentation 'normal-cdf 'function)
370 "Args: (x)
371 Returns the value of the standard normal distribution function at X.
372 Vectorized.")
374 (setf (documentation 'beta-cdf 'function)
375 "Args: (x alpha beta)
376 Returns the value of the Beta(ALPHA, BETA) distribution function at X.
377 Vectorized.")
379 (setf (documentation 'gamma-cdf 'function)
380 "Args: (x alpha)
381 Returns the value of the Gamma(alpha, 1) distribution function at X.
382 Vectorized.")
384 (setf (documentation 'chisq-cdf 'function)
385 "Args: (x df)
386 Returns the value of the Chi-Square(DF) distribution function at X. Vectorized.")
388 (setf (documentation 't-cdf 'function)
389 "Args: (x df)
390 Returns the value of the T(DF) distribution function at X. Vectorized.")
392 (setf (documentation 'f-cdf 'function)
393 "Args: (x ndf ddf)
394 Returns the value of the F(NDF, DDF) distribution function at X. Vectorized.")
396 (setf (documentation 'cauchy-cdf 'function)
397 "Args: (x)
398 Returns the value of the standard Cauchy distribution function at X.
399 Vectorized.")
401 (setf (documentation 'log-gamma 'function)
402 "Args: (x)
403 Returns the log gamma function of X. Vectorized.")
405 (setf (documentation 'normal-quant 'function)
406 "Args (p)
407 Returns the P-th quantile of the standard normal distribution. Vectorized.")
409 (setf (documentation 'cauchy-quant 'function)
410 "Args (p)
411 Returns the P-th quantile(s) of the standard Cauchy distribution. Vectorized.")
413 (setf (documentation 'beta-quant 'function)
414 "Args: (p alpha beta)
415 Returns the P-th quantile of the Beta(ALPHA, BETA) distribution. Vectorized.")
417 (setf (documentation 'gamma-quant 'function)
418 "Args: (p alpha)
419 Returns the P-th quantile of the Gamma(ALPHA, 1) distribution. Vectorized.")
421 (setf (documentation 'chisq-quant 'function)
422 "Args: (p df)
423 Returns the P-th quantile of the Chi-Square(DF) distribution. Vectorized.")
425 (setf (documentation 't-quant 'function)
426 "Args: (p df)
427 Returns the P-th quantile of the T(DF) distribution. Vectorized.")
429 (setf (documentation 'f-quant 'function)
430 "Args: (p ndf ddf)
431 Returns the P-th quantile of the F(NDF, DDF) distribution. Vectorized.")
433 (setf (documentation 'normal-dens 'function)
434 "Args: (x)
435 Returns the density at X of the standard normal distribution. Vectorized.")
437 (setf (documentation 'cauchy-dens 'function)
438 "Args: (x)
439 Returns the density at X of the standard Cauchy distribution. Vectorized.")
441 (setf (documentation 'beta-dens 'function)
442 "Args: (x alpha beta)
443 Returns the density at X of the Beta(ALPHA, BETA) distribution. Vectorized.")
445 (setf (documentation 'gamma-dens 'function)
446 "Args: (x alpha)
447 Returns the density at X of the Gamma(ALPHA, 1) distribution. Vectorized.")
449 (setf (documentation 'chisq-dens 'function)
450 "Args: (x alpha)
451 Returns the density at X of the Chi-Square(DF) distribution. Vectorized.")
453 (setf (documentation 't-dens 'function)
454 "Args: (x alpha)
455 Returns the density at X of the T(DF) distribution. Vectorized.")
457 (setf (documentation 'f-dens 'function)
458 "Args: (x ndf ddf)
459 Returns the density at X of the F(NDF, DDF) distribution. Vectorized.")
461 (setf (documentation 'uniform-rand 'function)
462 "Args: (n)
463 Returns a list of N uniform random variables from the range (0, 1).
464 Vectorized.")
466 (setf (documentation 'normal-rand 'function)
467 "Args: (n)
468 Returns a list of N standard normal random numbers. Vectorized.")
470 (setf (documentation 'cauchy-rand 'function)
471 "Args: (n)
472 Returns a list of N standard Cauchy random numbers. Vectorized.")
474 (setf (documentation 't-rand 'function)
475 "Args: (n df)
476 Returns a list of N T(DF) random variables. Vectorized.")
478 (setf (documentation 'f-rand 'function)
479 "Args: (n ndf ddf)
480 Returns a list of N F(NDF, DDF) random variables. Vectorized.")
482 (setf (documentation 'gamma-rand 'function)
483 "Args: (n a)
484 Returns a list of N Gamma(A, 1) random variables. Vectorized.")
486 (setf (documentation 'chisq-rand 'function)
487 "Args: (n df)
488 Returns a list of N Chi-Square(DF) random variables. Vectorized.")
490 (setf (documentation 'beta-rand 'function)
491 "Args: (n a b)
492 Returns a list of N beta(A, B) random variables. Vectorized.")
494 (setf (documentation 'binomial-cdf 'function)
495 "Args (x n p)
496 Returns value of the Binomial(N, P) distribution function at X. Vectorized.")
498 (setf (documentation 'poisson-cdf 'function)
499 "Args (x mu)
500 Returns value of the Poisson(MU) distribution function at X. Vectorized.")
502 (setf (documentation 'binomial-pmf 'function)
503 "Args (k n p)
504 Returns value of the Binomial(N, P) pmf function at integer K. Vectorized.")
506 (setf (documentation 'poisson-pmf 'function)
507 "Args (k mu)
508 Returns value of the Poisson(MU) pmf function at integer K. Vectorized.")
510 (setf (documentation 'binomial-quant 'function)
511 "Args: (x n p)
512 Returns x-th quantile (left continuous inverse) of Binomial(N, P) cdf.
513 Vectorized.")
515 (setf (documentation 'poisson-quant 'function)
516 "Args: (x mu)
517 Returns x-th quantile (left continuous inverse) of Poisson(MU) cdf.
518 Vectorized.")
520 (setf (documentation 'binomial-rand 'function)
521 "Args: (k n p)
522 Returns list of K draws from the Binomial(N, P) distribution. Vectorized.")
524 (setf (documentation 'poisson-rand 'function)
525 "Args: (k mu)
526 Returns list of K draws from the Poisson(MU) distribution. Vectorized.")