comments on approach for CFFI with existing system.
[CommonLispStat.git] / dists.lsp
blob570194025035384c5a8200ca1a1d494beb1bda50
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 ;;;;
12 ;;;; Package Setup
13 ;;;;
15 (defpackage :lisp-stat-probability
16 (:use :common-lisp
17 :lisp-stat-ffi-int
18 :lisp-stat-macros)
19 (:export
20 log-gamma
21 uniform-rand
22 normal-cdf normal-quant normal-dens normal-rand bivnorm-cdf
23 cauchy-cdf cauchy-quant cauchy-dens cauchy-rand
24 gamma-cdf gamma-quant gamma-dens gamma-rand
25 chisq-cdf chisq-quant chisq-dens chisq-rand
26 beta-cdf beta-quant beta-dens beta-rand
27 t-cdf t-quant t-dens t-rand
28 f-cdf f-quant f-dens f-rand
29 poisson-cdf poisson-quant poisson-pmf poisson-rand
30 binomial-cdf binomial-quant binomial-pmf binomial-rand))
32 (in-package :lisp-stat-probability)
34 (defmacro defbaserand (name onefun &rest args)
35 `(defun ,name (n ,@args)
36 (let ((result nil))
37 (dotimes (i n result)
38 (declare (fixnum i) (inline ,onefun))
39 (setf result (cons (,onefun ,@args) result))))))
41 (defbaserand base-uniform-rand one-uniform-rand)
42 (defbaserand base-normal-rand one-normal-rand)
43 (defbaserand base-cauchy-rand one-cauchy-rand)
44 (defbaserand base-gamma-rand one-gamma-rand a)
45 (defbaserand base-chisq-rand one-chisq-rand df)
46 (defbaserand base-beta-rand one-beta-rand a b)
47 (defbaserand base-t-rand one-t-rand df)
48 (defbaserand base-f-rand one-f-rand ndf ddf)
49 (defbaserand base-poisson-rand one-poisson-rand a)
50 (defbaserand base-binomial-rand one-binomial-rand a b)
52 (make-rv-function log-gamma base-log-gamma x)
54 (make-rv-function uniform-rand base-uniform-rand n)
56 (make-rv-function normal-cdf base-normal-cdf x)
57 (make-rv-function normal-quant base-normal-quant p)
58 (make-rv-function normal-dens base-normal-dens x)
59 (make-rv-function normal-rand base-normal-rand n)
60 (make-rv-function bivnorm-cdf base-bivnorm-cdf x y r)
62 (make-rv-function cauchy-cdf base-cauchy-cdf x)
63 (make-rv-function cauchy-quant base-cauchy-quant p)
64 (make-rv-function cauchy-dens base-cauchy-dens x)
65 (make-rv-function cauchy-rand base-cauchy-rand n)
67 (make-rv-function gamma-cdf base-gamma-cdf x a)
68 (make-rv-function gamma-quant base-gamma-quant p a)
69 (make-rv-function gamma-dens base-gamma-dens x a)
70 (make-rv-function gamma-rand base-gamma-rand n a)
72 (make-rv-function chisq-cdf base-chisq-cdf x df)
73 (make-rv-function chisq-quant base-chisq-quant p df)
74 (make-rv-function chisq-dens base-chisq-dens x df)
75 (make-rv-function chisq-rand base-chisq-rand n df)
77 (make-rv-function beta-cdf base-beta-cdf x a b)
78 (make-rv-function beta-quant base-beta-quant p a b)
79 (make-rv-function beta-dens base-beta-dens x a b)
80 (make-rv-function beta-rand base-beta-rand n a b)
82 (make-rv-function t-cdf base-t-cdf x df)
83 (make-rv-function t-quant base-t-quant p df)
84 (make-rv-function t-dens base-t-dens x df)
85 (make-rv-function t-rand base-t-rand n df)
87 (make-rv-function f-cdf base-f-cdf x ndf ddf)
88 (make-rv-function f-quant base-f-quant p ndf ddf)
89 (make-rv-function f-dens base-f-dens x ndf ddf)
90 (make-rv-function f-rand base-f-rand n ndf ddf)
92 (make-rv-function poisson-cdf base-poisson-cdf x a)
93 (make-rv-function poisson-quant base-poisson-quant p a)
94 (make-rv-function poisson-pmf base-poisson-pmf x a)
95 (make-rv-function poisson-rand base-poisson-rand n a)
97 (make-rv-function binomial-cdf base-binomial-cdf x a b)
98 (make-rv-function binomial-quant base-binomial-quant p a b)
99 (make-rv-function binomial-pmf base-binomial-pmf x a b)
100 (make-rv-function binomial-rand base-binomial-rand n a b)
102 ;;;;
103 ;;;; Documentation
104 ;;;;
106 (setf (documentation 'bivnorm-cdf 'function)
107 "Args: (x y r)
108 Returns the value of the standard bivariate normal distribution function
109 with correlation R at (X, Y). Vectorized.")
111 (setf (documentation 'normal-cdf 'function)
112 "Args: (x)
113 Returns the value of the standard normal distribution function at X.
114 Vectorized.")
116 (setf (documentation 'beta-cdf 'function)
117 "Args: (x alpha beta)
118 Returns the value of the Beta(ALPHA, BETA) distribution function at X.
119 Vectorized.")
121 (setf (documentation 'gamma-cdf 'function)
122 "Args: (x alpha)
123 Returns the value of the Gamma(alpha, 1) distribution function at X.
124 Vectorized.")
126 (setf (documentation 'chisq-cdf 'function)
127 "Args: (x df)
128 Returns the value of the Chi-Square(DF) distribution function at X. Vectorized.")
130 (setf (documentation 't-cdf 'function)
131 "Args: (x df)
132 Returns the value of the T(DF) distribution function at X. Vectorized.")
134 (setf (documentation 'f-cdf 'function)
135 "Args: (x ndf ddf)
136 Returns the value of the F(NDF, DDF) distribution function at X. Vectorized.")
138 (setf (documentation 'cauchy-cdf 'function)
139 "Args: (x)
140 Returns the value of the standard Cauchy distribution function at X.
141 Vectorized.")
143 (setf (documentation 'log-gamma 'function)
144 "Args: (x)
145 Returns the log gamma function of X. Vectorized.")
147 (setf (documentation 'normal-quant 'function)
148 "Args (p)
149 Returns the P-th quantile of the standard normal distribution. Vectorized.")
151 (setf (documentation 'cauchy-quant 'function)
152 "Args (p)
153 Returns the P-th quantile(s) of the standard Cauchy distribution. Vectorized.")
155 (setf (documentation 'beta-quant 'function)
156 "Args: (p alpha beta)
157 Returns the P-th quantile of the Beta(ALPHA, BETA) distribution. Vectorized.")
159 (setf (documentation 'gamma-quant 'function)
160 "Args: (p alpha)
161 Returns the P-th quantile of the Gamma(ALPHA, 1) distribution. Vectorized.")
163 (setf (documentation 'chisq-quant 'function)
164 "Args: (p df)
165 Returns the P-th quantile of the Chi-Square(DF) distribution. Vectorized.")
167 (setf (documentation 't-quant 'function)
168 "Args: (p df)
169 Returns the P-th quantile of the T(DF) distribution. Vectorized.")
171 (setf (documentation 'f-quant 'function)
172 "Args: (p ndf ddf)
173 Returns the P-th quantile of the F(NDF, DDF) distribution. Vectorized.")
175 (setf (documentation 'normal-dens 'function)
176 "Args: (x)
177 Returns the density at X of the standard normal distribution. Vectorized.")
179 (setf (documentation 'cauchy-dens 'function)
180 "Args: (x)
181 Returns the density at X of the standard Cauchy distribution. Vectorized.")
183 (setf (documentation 'beta-dens 'function)
184 "Args: (x alpha beta)
185 Returns the density at X of the Beta(ALPHA, BETA) distribution. Vectorized.")
187 (setf (documentation 'gamma-dens 'function)
188 "Args: (x alpha)
189 Returns the density at X of the Gamma(ALPHA, 1) distribution. Vectorized.")
191 (setf (documentation 'chisq-dens 'function)
192 "Args: (x alpha)
193 Returns the density at X of the Chi-Square(DF) distribution. Vectorized.")
195 (setf (documentation 't-dens 'function)
196 "Args: (x alpha)
197 Returns the density at X of the T(DF) distribution. Vectorized.")
199 (setf (documentation 'f-dens 'function)
200 "Args: (x ndf ddf)
201 Returns the density at X of the F(NDF, DDF) distribution. Vectorized.")
203 (setf (documentation 'uniform-rand 'function)
204 "Args: (n)
205 Returns a list of N uniform random variables from the range (0, 1).
206 Vectorized.")
208 (setf (documentation 'normal-rand 'function)
209 "Args: (n)
210 Returns a list of N standard normal random numbers. Vectorized.")
212 (setf (documentation 'cauchy-rand 'function)
213 "Args: (n)
214 Returns a list of N standard Cauchy random numbers. Vectorized.")
216 (setf (documentation 't-rand 'function)
217 "Args: (n df)
218 Returns a list of N T(DF) random variables. Vectorized.")
220 (setf (documentation 'f-rand 'function)
221 "Args: (n ndf ddf)
222 Returns a list of N F(NDF, DDF) random variables. Vectorized.")
224 (setf (documentation 'gamma-rand 'function)
225 "Args: (n a)
226 Returns a list of N Gamma(A, 1) random variables. Vectorized.")
228 (setf (documentation 'chisq-rand 'function)
229 "Args: (n df)
230 Returns a list of N Chi-Square(DF) random variables. Vectorized.")
232 (setf (documentation 'beta-rand 'function)
233 "Args: (n a b)
234 Returns a list of N beta(A, B) random variables. Vectorized.")
236 (setf (documentation 'binomial-cdf 'function)
237 "Args (x n p)
238 Returns value of the Binomial(N, P) distribution function at X. Vectorized.")
240 (setf (documentation 'poisson-cdf 'function)
241 "Args (x mu)
242 Returns value of the Poisson(MU) distribution function at X. Vectorized.")
244 (setf (documentation 'binomial-pmf 'function)
245 "Args (k n p)
246 Returns value of the Binomial(N, P) pmf function at integer K. Vectorized.")
248 (setf (documentation 'poisson-pmf 'function)
249 "Args (k mu)
250 Returns value of the Poisson(MU) pmf function at integer K. Vectorized.")
252 (setf (documentation 'binomial-quant 'function)
253 "Args: (x n p)
254 Returns x-th quantile (left continuous inverse) of Binomial(N, P) cdf.
255 Vectorized.")
257 (setf (documentation 'poisson-quant 'function)
258 "Args: (x mu)
259 Returns x-th quantile (left continuous inverse) of Poisson(MU) cdf.
260 Vectorized.")
262 (setf (documentation 'binomial-rand 'function)
263 "Args: (k n p)
264 Returns list of K draws from the Binomial(N, P) distribution. Vectorized.")
266 (setf (documentation 'poisson-rand 'function)
267 "Args: (k mu)
268 Returns list of K draws from the Poisson(MU) distribution. Vectorized.")