From 43ac6cef39cbbef2910ae25f24541f1d5bb8ca5b Mon Sep 17 00:00:00 2001 From: liam <liam@a3d8a0fb-c1db-0310-ace7-a616afeb9e30> Date: Mon, 18 Sep 2006 02:09:07 +0000 Subject: [PATCH] Add Laplace random number distribution. git-svn-id: svn+ssh://pop/opt/space/mathematics/gsl/trunk@3135 a3d8a0fb-c1db-0310-ace7-a616afeb9e30 --- gsll.asd | 5 +-- random/laplace.lisp | 81 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 random/laplace.lisp diff --git a/gsll.asd b/gsll.asd index 790e14ac..21711f43 100644 --- a/gsll.asd +++ b/gsll.asd @@ -3,7 +3,7 @@ ; description: Definition of GSLL system ; date: ; author: Liam Healy -; modified: Sat Sep 2 2006 - 19:18 +; modified: Sun Sep 17 2006 - 22:00 ;******************************************************** ;;; $Id: $ @@ -97,4 +97,5 @@ (:file "gaussian" :depends-on (rng-types)) (:file "gaussian-tail" :depends-on (rng-types)) (:file "gaussian-bivariate" :depends-on (rng-types)) - (:file "exponential" :depends-on (rng-types)))))) + (:file "exponential" :depends-on (rng-types)) + (:file "laplace" :depends-on (rng-types)))))) diff --git a/random/laplace.lisp b/random/laplace.lisp new file mode 100644 index 00000000..9277564d --- /dev/null +++ b/random/laplace.lisp @@ -0,0 +1,81 @@ +;******************************************************** +; file: exponential.lisp +; description: Exponential distribution +; date: Sun Sep 17 2006 +; author: Liam M. Healy +; modified: Sun Sep 17 2006 - 22:06 +;******************************************************** +;;; $Id: $ + +(in-package :gsl) + +(defun-gsl laplace (generator a) + "gsl_ran_laplace" + (((generator generator) :pointer) (a :double)) + :c-return :double + :documentation + "A random variate from the Laplace distribution with width @var{a}. + The distribution is + p(x) dx = {1 \over 2 a} \exp(-|x/a|) dx + for @math{-\infty < x < \infty}.") + +(defun-gsl laplace-pdf (x a) + "gsl_ran_laplace_pdf" ((x :double) (a :double)) + :c-return :double + :documentation + "The probability density @math{p(x)} at @var{x} + for a Laplace distribution with width @var{a}, using the formula + given for #'laplace.") + +(defun-gsl laplace-P (x a) + "gsl_cdf_laplace_P" ((x :double) (a :double)) + :c-return :double + :documentation "The cumulative distribution function + @math{P(x)} for the laplace distribution with width @var{a}.") + +(defun-gsl laplace-Q (x a) + "gsl_cdf_laplace_Q" ((x :double) (a :double)) + :c-return :double + :documentation "The cumulative distribution function + @math{Q(x)} for the laplace distribution with width @var{a}.") + +(defun-gsl laplace-Pinv (P a) + "gsl_cdf_laplace_Pinv" ((P :double) (a :double)) + :c-return :double + :documentation "The inverse cumulative distribution function + @math{P(x)} for the laplace distribution with width @var{a}.") + +(defun-gsl laplace-Qinv (Q a) + "gsl_cdf_laplace_Qinv" ((Q :double) (a :double)) + :c-return :double + :documentation "The inverse cumulative distribution function + @math{Q(x)} for the laplace distribution with width @var{a}.") + +;;; Examples and unit test +(lisp-unit:define-test laplace + (lisp-unit:assert-equal + '("0.516635619858d-02" "-0.394257771749d+01" "-0.832951028160d+01" + "0.111599757046d+01" "-0.622340381488d+01" "-0.350480039842d+02" + "0.888815832003d+00" "0.716189249197d+01" "0.252463778091d+02" + "0.734165104806d+01" "0.654142651602d+01") + (lisp-unit:fp-sequence + (progn + (rng-set *rng-mt19937* 0) + (loop for i from 0 to 10 + collect + (laplace *rng-mt19937* 10.0d0))))) + (lisp-unit:assert-first-fp-equal + "0.500000000000d-01" + (laplace-pdf 0.0 10.0)) + (lisp-unit:assert-first-fp-equal + "0.696734670144d+00" + (laplace-p 1.0 2.0)) + (lisp-unit:assert-first-fp-equal + "0.303265329856d+00" + (laplace-q 1.0 2.0)) + (lisp-unit:assert-first-fp-equal + "0.100000000000d+01" + (laplace-pinv 0.6967346701436833 2.0)) + (lisp-unit:assert-first-fp-equal + "0.100000000000d+01" + (laplace-qinv 0.3032653298563167 2.0))) -- GitLab