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
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.
24 (defpackage :lisp-stat-probability
29 (:export log-gamma set-seed
31 normal-cdf normal-quant normal-dens normal-rand
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
)
46 "stupid dummy function, need to implement rng seeding tool."
49 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
51 ;;; CFFI support for Probability Distributions
53 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
56 ;;; C-callable uniform generator
59 (defcfun ("register_uni" register-uni
)
61 (defcallback ccl-uni
:int
() (ccl-store-double (random 1.0)) 0)
62 (register-uni (callback ccl-uni
))
64 (defun one-uniform-rand () (random 1.0))
67 ;;; Log-gamma function
70 (defcfun ("ccl_gamma" ccl-base-log-gamma
)
72 (defun base-log-gamma (x)
73 (ccl-base-log-gamma (float x
1d0
)))
76 ;;; Normal distribution
79 (defcfun ("ccl_normalcdf" ccl-base-normal-cdf
)
81 (defun base-normal-cdf (x)
82 (ccl-base-normal-cdf (float x
1d0
)))
84 (defcfun ("ccl_normalquant" ccl-base-normal-quant
)
86 (defun base-normal-quant (x)
87 (ccl-base-normal-quant (float x
1d0
)))
89 (defcfun ("ccl_normaldens" ccl-base-normal-dens
)
91 (defun base-normal-dens (x)
92 (ccl-base-normal-dens (float x
1d0
)))
94 (defcfun ("ccl_normalrand" one-normal-rand
)
97 (defcfun ("ccl_bnormcdf" ccl-base-bivnorm-cdf
)
98 :double
(x :double
) (y :double
) (z :double
))
99 (defun base-bivnorm-cdf (x y z
)
100 (ccl-base-bivnorm-cdf (float x
1d0
) (float y
1d0
) (float z
1d0
)))
103 ;;; Cauchy distribution
106 (defcfun ("ccl_cauchycdf" ccl-base-cauchy-cdf
)
108 (defun base-cauchy-cdf (x)
109 (ccl-base-cauchy-cdf (float x
1d0
)))
111 (defcfun ("ccl_cauchyquant" ccl-base-cauchy-quant
)
113 (defun base-cauchy-quant (x)
114 (ccl-base-cauchy-quant (float x
1d0
)))
116 (defcfun ("ccl_cauchydens" ccl-base-cauchy-dens
)
118 (defun base-cauchy-dens (x)
119 (ccl-base-cauchy-dens (float x
1d0
)))
121 (defcfun ("ccl_cauchyrand" one-cauchy-rand
)
125 ;;;; Gamma distribution
128 (defcfun ("ccl_gammacdf" ccl-base-gamma-cdf
)
129 :double
(x :double
) (y :double
))
130 (defun base-gamma-cdf (x y
)
131 (ccl-base-gamma-cdf (float x
1d0
) (float y
1d0
)))
133 (defcfun ("ccl_gammaquant" ccl-base-gamma-quant
)
134 :double
(x :double
) (y :double
))
135 (defun base-gamma-quant (x y
)
136 (ccl-base-gamma-quant (float x
1d0
) (float y
1d0
)))
138 (defcfun ("ccl_gammadens" ccl-base-gamma-dens
)
139 :double
(x :double
) (y :double
))
140 (defun base-gamma-dens (x y
)
141 (ccl-base-gamma-dens (float x
1d0
) (float y
1d0
)))
143 (defcfun ("ccl_gammarand" ccl-gamma-rand
)
145 (defun one-gamma-rand (x)
146 (ccl-gamma-rand (float x
1d0
)))
149 ;;;; Chi-square distribution
152 (defcfun ("ccl_chisqcdf" ccl-base-chisq-cdf
)
153 :double
(x :double
) (y :double
))
154 (defun base-chisq-cdf (x y
)
155 (ccl-base-chisq-cdf (float x
1d0
) (float y
1d0
)))
157 (defcfun ("ccl_chisqquant" ccl-base-chisq-quant
)
158 :double
(x :double
) (y :double
))
159 (defun base-chisq-quant (x y
)
160 (ccl-base-chisq-quant (float x
1d0
) (float y
1d0
)))
162 (defcfun ("ccl_chisqdens" ccl-base-chisq-dens
)
163 :double
(x :double
) (y :double
))
164 (defun base-chisq-dens (x y
)
165 (ccl-base-chisq-dens (float x
1d0
) (float y
1d0
)))
167 (defcfun ("ccl_chisqrand" ccl-chisq-rand
)
169 (defun one-chisq-rand (x)
170 (ccl-chisq-rand (float x
1d0
)))
173 ;;;; Beta distribution
176 (defcfun ("ccl_betacdf" ccl-base-beta-cdf
)
177 :double
(x :double
) (y :double
) (z :double
))
178 (defun base-beta-cdf (x y z
)
179 (ccl-base-beta-cdf (float x
1d0
) (float y
1d0
) (float z
1d0
)))
181 (defcfun ("ccl_betaquant" ccl-base-beta-quant
)
182 :double
(x :double
) (y :double
) (z :double
))
183 (defun base-beta-quant (x y z
)
184 (ccl-base-beta-quant (float x
1d0
) (float y
1d0
) (float z
1d0
)))
186 (defcfun ("ccl_betadens" ccl-base-beta-dens
)
187 :double
(x :double
) (y :double
) (z :double
))
188 (defun base-beta-dens (x y z
)
189 (ccl-base-beta-dens (float x
1d0
) (float y
1d0
) (float z
1d0
)))
191 (defcfun ("ccl_betarand" ccl-beta-rand
)
192 :double
(x :double
) (y :double
))
193 (defun one-beta-rand (x y
)
194 (ccl-beta-rand (float x
1d0
) (float y
1d0
)))
200 (defcfun ("ccl_tcdf" ccl-base-t-cdf
)
201 :double
(x :double
) (y :double
))
202 (defun base-t-cdf (x y
)
203 (ccl-base-t-cdf (float x
1d0
) (float y
1d0
)))
205 (defcfun ("ccl_tquant" ccl-base-t-quant
)
206 :double
(x :double
) (y :double
))
207 (defun base-t-quant (x y
)
208 (ccl-base-t-quant (float x
1d0
) (float y
1d0
)))
210 (defcfun ("ccl_tdens" ccl-base-t-dens
)
211 :double
(x :double
) (y :double
))
212 (defun base-t-dens (x y
)
213 (ccl-base-t-dens (float x
1d0
) (float y
1d0
)))
215 (defcfun ("ccl_trand" ccl-t-rand
)
217 (defun one-t-rand (x)
218 (ccl-t-rand (float x
1d0
)))
224 (defcfun ("ccl_fcdf" ccl-base-f-cdf
)
225 :double
(x :double
) (y :double
) (z :double
))
226 (defun base-f-cdf (x y z
)
227 (ccl-base-f-cdf (float x
1d0
) (float y
1d0
) (float z
1d0
)))
229 (defcfun ("ccl_fquant" ccl-base-f-quant
)
230 :double
(x :double
) (y :double
) (z :double
))
231 (defun base-f-quant (x y z
)
232 (ccl-base-f-quant (float x
1d0
) (float y
1d0
) (float z
1d0
)))
234 (defcfun ("ccl_fdens" ccl-base-f-dens
)
235 :double
(x :double
) (y :double
) (z :double
))
236 (defun base-f-dens (x y z
)
237 (ccl-base-f-dens (float x
1d0
) (float y
1d0
) (float z
1d0
)))
239 (defcfun ("ccl_frand" ccl-f-rand
)
240 :double
(x :double
) (y :double
))
241 (defun one-f-rand (x y
) (ccl-f-rand (float x
1d0
) (float y
1d0
)))
244 ;;;; Poisson distribution
247 (defcfun ("ccl_poissoncdf" ccl-base-poisson-cdf
)
248 :double
(x :double
) (y :double
))
249 (defun base-poisson-cdf (x y
)
250 (ccl-base-poisson-cdf (float x
1d0
) (float y
1d0
)))
252 (defcfun ("ccl_poissonquant" ccl-base-poisson-quant
)
253 :int
(x :double
) (y :double
))
254 (defun base-poisson-quant (x y
)
255 (ccl-base-poisson-quant (float x
1d0
) (float y
1d0
)))
257 (defcfun ("ccl_poissonpmf" ccl-base-poisson-pmf
)
258 :double
(x :int
) (y :double
))
259 (defun base-poisson-pmf (x y
)
260 (ccl-base-poisson-pmf x
(float y
1d0
)))
262 (defcfun ("ccl_poissonrand" ccl-poisson-rand
)
264 (defun one-poisson-rand (x)
265 (ccl-poisson-rand (float x
1d0
)))
268 ;;;; Binomial distribution
271 (defcfun ("ccl_binomialcdf" ccl-base-binomial-cdf
)
272 :double
(x :double
) (y :int
) (z :double
))
273 (defun base-binomial-cdf (x y z
)
274 (ccl-base-binomial-cdf (float x
1d0
) y
(float z
1d0
)))
276 (defcfun ("ccl_binomialquant" ccl-base-binomial-quant
)
277 :int
(x :double
) (y :int
) (z :double
))
278 (defun base-binomial-quant (x y z
)
279 (ccl-base-binomial-quant (float x
1d0
) y
(float z
1d0
)))
281 (defcfun ("ccl_binomialpmf" ccl-base-binomial-pmf
)
282 :double
(x :int
) (y :int
) (z :double
))
283 (defun base-binomial-pmf (x y z
)
284 (ccl-base-binomial-pmf x y
(float z
1d0
)))
286 (defcfun ("ccl_binomialrand" ccl-binomial-rand
)
287 :int
(x :int
) (y :double
))
288 (defun one-binomial-rand (x y
)
289 (ccl-binomial-rand x
(float y
1d0
)))
295 ;;; definitions though macros
297 (defmacro defbaserand
(name onefun
&rest args
)
298 `(defun ,name
(n ,@args
)
300 (dotimes (i n result
)
301 (declare (fixnum i
) (inline ,onefun
))
302 (setf result
(cons (,onefun
,@args
) result
))))))
304 (defbaserand base-uniform-rand one-uniform-rand
)
305 (defbaserand base-normal-rand one-normal-rand
)
306 (defbaserand base-cauchy-rand one-cauchy-rand
)
307 (defbaserand base-gamma-rand one-gamma-rand a
)
308 (defbaserand base-chisq-rand one-chisq-rand df
)
309 (defbaserand base-beta-rand one-beta-rand a b
)
310 (defbaserand base-t-rand one-t-rand df
)
311 (defbaserand base-f-rand one-f-rand ndf ddf
)
312 (defbaserand base-poisson-rand one-poisson-rand a
)
313 (defbaserand base-binomial-rand one-binomial-rand a b
)
315 (make-rv-function log-gamma base-log-gamma x
)
317 (make-rv-function uniform-rand base-uniform-rand n
)
319 (make-rv-function normal-cdf base-normal-cdf x
)
320 (make-rv-function normal-quant base-normal-quant p
)
321 (make-rv-function normal-dens base-normal-dens x
)
322 (make-rv-function normal-rand base-normal-rand n
)
323 (make-rv-function bivnorm-cdf base-bivnorm-cdf x y r
)
325 (make-rv-function cauchy-cdf base-cauchy-cdf x
)
326 (make-rv-function cauchy-quant base-cauchy-quant p
)
327 (make-rv-function cauchy-dens base-cauchy-dens x
)
328 (make-rv-function cauchy-rand base-cauchy-rand n
)
330 (make-rv-function gamma-cdf base-gamma-cdf x a
)
331 (make-rv-function gamma-quant base-gamma-quant p a
)
332 (make-rv-function gamma-dens base-gamma-dens x a
)
333 (make-rv-function gamma-rand base-gamma-rand n a
)
335 (make-rv-function chisq-cdf base-chisq-cdf x df
)
336 (make-rv-function chisq-quant base-chisq-quant p df
)
337 (make-rv-function chisq-dens base-chisq-dens x df
)
338 (make-rv-function chisq-rand base-chisq-rand n df
)
340 (make-rv-function beta-cdf base-beta-cdf x a b
)
341 (make-rv-function beta-quant base-beta-quant p a b
)
342 (make-rv-function beta-dens base-beta-dens x a b
)
343 (make-rv-function beta-rand base-beta-rand n a b
)
345 (make-rv-function t-cdf base-t-cdf x df
)
346 (make-rv-function t-quant base-t-quant p df
)
347 (make-rv-function t-dens base-t-dens x df
)
348 (make-rv-function t-rand base-t-rand n df
)
350 (make-rv-function f-cdf base-f-cdf x ndf ddf
)
351 (make-rv-function f-quant base-f-quant p ndf ddf
)
352 (make-rv-function f-dens base-f-dens x ndf ddf
)
353 (make-rv-function f-rand base-f-rand n ndf ddf
)
355 (make-rv-function poisson-cdf base-poisson-cdf x a
)
356 (make-rv-function poisson-quant base-poisson-quant p a
)
357 (make-rv-function poisson-pmf base-poisson-pmf x a
)
358 (make-rv-function poisson-rand base-poisson-rand n a
)
360 (make-rv-function binomial-cdf base-binomial-cdf x a b
)
361 (make-rv-function binomial-quant base-binomial-quant p a b
)
362 (make-rv-function binomial-pmf base-binomial-pmf x a b
)
363 (make-rv-function binomial-rand base-binomial-rand n a b
)
369 (setf (documentation 'bivnorm-cdf
'function
)
371 Returns the value of the standard bivariate normal distribution function
372 with correlation R at (X, Y). Vectorized.")
374 (setf (documentation 'normal-cdf
'function
)
376 Returns the value of the standard normal distribution function at X.
379 (setf (documentation 'beta-cdf
'function
)
380 "Args: (x alpha beta)
381 Returns the value of the Beta(ALPHA, BETA) distribution function at X.
384 (setf (documentation 'gamma-cdf
'function
)
386 Returns the value of the Gamma(alpha, 1) distribution function at X.
389 (setf (documentation 'chisq-cdf
'function
)
391 Returns the value of the Chi-Square(DF) distribution function at X. Vectorized.")
393 (setf (documentation 't-cdf
'function
)
395 Returns the value of the T(DF) distribution function at X. Vectorized.")
397 (setf (documentation 'f-cdf
'function
)
399 Returns the value of the F(NDF, DDF) distribution function at X. Vectorized.")
401 (setf (documentation 'cauchy-cdf
'function
)
403 Returns the value of the standard Cauchy distribution function at X.
406 (setf (documentation 'log-gamma
'function
)
408 Returns the log gamma function of X. Vectorized.")
410 (setf (documentation 'normal-quant
'function
)
412 Returns the P-th quantile of the standard normal distribution. Vectorized.")
414 (setf (documentation 'cauchy-quant
'function
)
416 Returns the P-th quantile(s) of the standard Cauchy distribution. Vectorized.")
418 (setf (documentation 'beta-quant
'function
)
419 "Args: (p alpha beta)
420 Returns the P-th quantile of the Beta(ALPHA, BETA) distribution. Vectorized.")
422 (setf (documentation 'gamma-quant
'function
)
424 Returns the P-th quantile of the Gamma(ALPHA, 1) distribution. Vectorized.")
426 (setf (documentation 'chisq-quant
'function
)
428 Returns the P-th quantile of the Chi-Square(DF) distribution. Vectorized.")
430 (setf (documentation 't-quant
'function
)
432 Returns the P-th quantile of the T(DF) distribution. Vectorized.")
434 (setf (documentation 'f-quant
'function
)
436 Returns the P-th quantile of the F(NDF, DDF) distribution. Vectorized.")
438 (setf (documentation 'normal-dens
'function
)
440 Returns the density at X of the standard normal distribution. Vectorized.")
442 (setf (documentation 'cauchy-dens
'function
)
444 Returns the density at X of the standard Cauchy distribution. Vectorized.")
446 (setf (documentation 'beta-dens
'function
)
447 "Args: (x alpha beta)
448 Returns the density at X of the Beta(ALPHA, BETA) distribution. Vectorized.")
450 (setf (documentation 'gamma-dens
'function
)
452 Returns the density at X of the Gamma(ALPHA, 1) distribution. Vectorized.")
454 (setf (documentation 'chisq-dens
'function
)
456 Returns the density at X of the Chi-Square(DF) distribution. Vectorized.")
458 (setf (documentation 't-dens
'function
)
460 Returns the density at X of the T(DF) distribution. Vectorized.")
462 (setf (documentation 'f-dens
'function
)
464 Returns the density at X of the F(NDF, DDF) distribution. Vectorized.")
466 (setf (documentation 'uniform-rand
'function
)
468 Returns a list of N uniform random variables from the range (0, 1).
471 (setf (documentation 'normal-rand
'function
)
473 Returns a list of N standard normal random numbers. Vectorized.")
475 (setf (documentation 'cauchy-rand
'function
)
477 Returns a list of N standard Cauchy random numbers. Vectorized.")
479 (setf (documentation 't-rand
'function
)
481 Returns a list of N T(DF) random variables. Vectorized.")
483 (setf (documentation 'f-rand
'function
)
485 Returns a list of N F(NDF, DDF) random variables. Vectorized.")
487 (setf (documentation 'gamma-rand
'function
)
489 Returns a list of N Gamma(A, 1) random variables. Vectorized.")
491 (setf (documentation 'chisq-rand
'function
)
493 Returns a list of N Chi-Square(DF) random variables. Vectorized.")
495 (setf (documentation 'beta-rand
'function
)
497 Returns a list of N beta(A, B) random variables. Vectorized.")
499 (setf (documentation 'binomial-cdf
'function
)
501 Returns value of the Binomial(N, P) distribution function at X. Vectorized.")
503 (setf (documentation 'poisson-cdf
'function
)
505 Returns value of the Poisson(MU) distribution function at X. Vectorized.")
507 (setf (documentation 'binomial-pmf
'function
)
509 Returns value of the Binomial(N, P) pmf function at integer K. Vectorized.")
511 (setf (documentation 'poisson-pmf
'function
)
513 Returns value of the Poisson(MU) pmf function at integer K. Vectorized.")
515 (setf (documentation 'binomial-quant
'function
)
517 Returns x-th quantile (left continuous inverse) of Binomial(N, P) cdf.
520 (setf (documentation 'poisson-quant
'function
)
522 Returns x-th quantile (left continuous inverse) of Poisson(MU) cdf.
525 (setf (documentation 'binomial-rand
'function
)
527 Returns list of K draws from the Binomial(N, P) distribution. Vectorized.")
529 (setf (documentation 'poisson-rand
'function
)
531 Returns list of K draws from the Poisson(MU) distribution. Vectorized.")