vectorized math is important
[CommonLispStat.git] / dists.lsp
blobf25352b820abfe5545dcdbc8538fd6e11e48d215
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 ;;;;
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)
28 (let ((result nil))
29 (dotimes (i n result)
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)
94 ;;;;
95 ;;;; Documentation
96 ;;;;
98 (setf (documentation 'bivnorm-cdf 'function)
99 "Args: (x y r)
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)
104 "Args: (x)
105 Returns the value of the standard normal distribution function at X.
106 Vectorized.")
108 (setf (documentation 'beta-cdf 'function)
109 "Args: (x alpha beta)
110 Returns the value of the Beta(ALPHA, BETA) distribution function at X.
111 Vectorized.")
113 (setf (documentation 'gamma-cdf 'function)
114 "Args: (x alpha)
115 Returns the value of the Gamma(alpha, 1) distribution function at X.
116 Vectorized.")
118 (setf (documentation 'chisq-cdf 'function)
119 "Args: (x df)
120 Returns the value of the Chi-Square(DF) distribution function at X. Vectorized.")
122 (setf (documentation 't-cdf 'function)
123 "Args: (x df)
124 Returns the value of the T(DF) distribution function at X. Vectorized.")
126 (setf (documentation 'f-cdf 'function)
127 "Args: (x ndf ddf)
128 Returns the value of the F(NDF, DDF) distribution function at X. Vectorized.")
130 (setf (documentation 'cauchy-cdf 'function)
131 "Args: (x)
132 Returns the value of the standard Cauchy distribution function at X.
133 Vectorized.")
135 (setf (documentation 'log-gamma 'function)
136 "Args: (x)
137 Returns the log gamma function of X. Vectorized.")
139 (setf (documentation 'normal-quant 'function)
140 "Args (p)
141 Returns the P-th quantile of the standard normal distribution. Vectorized.")
143 (setf (documentation 'cauchy-quant 'function)
144 "Args (p)
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)
152 "Args: (p alpha)
153 Returns the P-th quantile of the Gamma(ALPHA, 1) distribution. Vectorized.")
155 (setf (documentation 'chisq-quant 'function)
156 "Args: (p df)
157 Returns the P-th quantile of the Chi-Square(DF) distribution. Vectorized.")
159 (setf (documentation 't-quant 'function)
160 "Args: (p df)
161 Returns the P-th quantile of the T(DF) distribution. Vectorized.")
163 (setf (documentation 'f-quant 'function)
164 "Args: (p ndf ddf)
165 Returns the P-th quantile of the F(NDF, DDF) distribution. Vectorized.")
167 (setf (documentation 'normal-dens 'function)
168 "Args: (x)
169 Returns the density at X of the standard normal distribution. Vectorized.")
171 (setf (documentation 'cauchy-dens 'function)
172 "Args: (x)
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)
180 "Args: (x alpha)
181 Returns the density at X of the Gamma(ALPHA, 1) distribution. Vectorized.")
183 (setf (documentation 'chisq-dens 'function)
184 "Args: (x alpha)
185 Returns the density at X of the Chi-Square(DF) distribution. Vectorized.")
187 (setf (documentation 't-dens 'function)
188 "Args: (x alpha)
189 Returns the density at X of the T(DF) distribution. Vectorized.")
191 (setf (documentation 'f-dens 'function)
192 "Args: (x ndf ddf)
193 Returns the density at X of the F(NDF, DDF) distribution. Vectorized.")
195 (setf (documentation 'uniform-rand 'function)
196 "Args: (n)
197 Returns a list of N uniform random variables from the range (0, 1).
198 Vectorized.")
200 (setf (documentation 'normal-rand 'function)
201 "Args: (n)
202 Returns a list of N standard normal random numbers. Vectorized.")
204 (setf (documentation 'cauchy-rand 'function)
205 "Args: (n)
206 Returns a list of N standard Cauchy random numbers. Vectorized.")
208 (setf (documentation 't-rand 'function)
209 "Args: (n df)
210 Returns a list of N T(DF) random variables. Vectorized.")
212 (setf (documentation 'f-rand 'function)
213 "Args: (n ndf ddf)
214 Returns a list of N F(NDF, DDF) random variables. Vectorized.")
216 (setf (documentation 'gamma-rand 'function)
217 "Args: (n a)
218 Returns a list of N Gamma(A, 1) random variables. Vectorized.")
220 (setf (documentation 'chisq-rand 'function)
221 "Args: (n df)
222 Returns a list of N Chi-Square(DF) random variables. Vectorized.")
224 (setf (documentation 'beta-rand 'function)
225 "Args: (n a b)
226 Returns a list of N beta(A, B) random variables. Vectorized.")
228 (setf (documentation 'binomial-cdf 'function)
229 "Args (x n p)
230 Returns value of the Binomial(N, P) distribution function at X. Vectorized.")
232 (setf (documentation 'poisson-cdf 'function)
233 "Args (x mu)
234 Returns value of the Poisson(MU) distribution function at X. Vectorized.")
236 (setf (documentation 'binomial-pmf 'function)
237 "Args (k n p)
238 Returns value of the Binomial(N, P) pmf function at integer K. Vectorized.")
240 (setf (documentation 'poisson-pmf 'function)
241 "Args (k mu)
242 Returns value of the Poisson(MU) pmf function at integer K. Vectorized.")
244 (setf (documentation 'binomial-quant 'function)
245 "Args: (x n p)
246 Returns x-th quantile (left continuous inverse) of Binomial(N, P) cdf.
247 Vectorized.")
249 (setf (documentation 'poisson-quant 'function)
250 "Args: (x mu)
251 Returns x-th quantile (left continuous inverse) of Poisson(MU) cdf.
252 Vectorized.")
254 (setf (documentation 'binomial-rand 'function)
255 "Args: (k n p)
256 Returns list of K draws from the Binomial(N, P) distribution. Vectorized.")
258 (setf (documentation 'poisson-rand 'function)
259 "Args: (k mu)
260 Returns list of K draws from the Poisson(MU) distribution. Vectorized.")