exponential.lisp 2.99 KB
Newer Older
1 2
;; Exponential distribution
;; Liam Healy, Sat Sep  2 2006 - 19:04
3
;; Time-stamp: <2010-05-24 20:46:09EDT exponential.lisp>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
;;
;; Copyright 2006, 2007, 2008, 2009 Liam M. Healy
;; Distributed under the terms of the GNU General Public License
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
20

liam's avatar
liam committed
21 22
(in-package :gsl)

23 24 25
;;; /usr/include/gsl/gsl_randist.h
;;; /usr/include/gsl/gsl_cdf.h

26
(defmfun sample
27
    ((generator random-number-generator) (type (eql :exponential))
28
     &key mu)
29
  "gsl_ran_exponential"
30
  (((mpointer generator) :pointer) (mu :double))
31
  :definition :method
32
  :c-return :double
33
  :documentation			; FDL
34
  "A random variate from the exponential distribution
35
   with mean mu. The distribution is
36
   p(x) dx = {1 \over \mu} \exp(-x/\mu) dx
37
   x >= 0.")
38

liam's avatar
liam committed
39
(defmfun exponential-pdf (x mu)
40 41
  "gsl_ran_exponential_pdf" ((x :double) (mu :double))
  :c-return :double
42 43 44
  :documentation			; FDL
  "The probability density p(x) at x
  for an exponential distribution with mean mu, using the formula
45
  given for #'sample :exponential.")
46

liam's avatar
liam committed
47
(defmfun exponential-P (x mu)
48 49
  "gsl_cdf_exponential_P" ((x :double) (mu :double))
  :c-return :double
50 51 52
  :documentation			; FDL
  "The cumulative distribution function
   P(x) for the exponential distribution with mean mu.")
53

liam's avatar
liam committed
54
(defmfun exponential-Q (x mu)
55 56
  "gsl_cdf_exponential_Q" ((x :double) (mu :double))
  :c-return :double
57 58 59
  :documentation			; FDL
  "The cumulative distribution function
   Q(x) for the exponential distribution with mean mu.")
60

liam's avatar
liam committed
61
(defmfun exponential-Pinv (P mu)
62 63
  "gsl_cdf_exponential_Pinv" ((P :double) (mu :double))
  :c-return :double
64 65 66
  :documentation			; FDL
  "The inverse cumulative distribution function
   P(x) for the exponential distribution with mean mu.")
67

liam's avatar
liam committed
68
(defmfun exponential-Qinv (Q mu)
69 70
  "gsl_cdf_exponential_Qinv" ((Q :double) (mu :double))
  :c-return :double
71 72 73
  :documentation 			; FDL
  "The inverse cumulative distribution function
   Q(x) for the exponential distribution with mean mu.")
74 75

;;; Examples and unit test
Liam Healy's avatar
Liam Healy committed
76
(save-test exponential
Liam Healy's avatar
Liam Healy committed
77
  (let ((rng (make-random-number-generator +mt19937+ 0)))
78 79
      (loop for i from 0 to 10
	    collect
80
	    (sample rng :exponential :mu 10.0d0)))
81 82
  ;; From randist/test.c
  ;;(testpdf (lambda (r) (exponential-pdf r 2.0d0)) :exponential :mu 2.0d0)
liam's avatar
liam committed
83 84 85 86 87 88
  (exponential-pdf 0.0d0 10.0d0)
  (exponential-p 1.0d0 2.0d0)
  (exponential-q 1.0d0 2.0d0)
  (exponential-pinv 0.3934693402873666d0 2.0d0)
  (exponential-qinv 0.6065306597126334d0 2.0d0))