| 1 | ;; Gamma distribution |
|---|
| 2 | ;; Liam Healy, Sat Sep 30 2006 |
|---|
| 3 | ;; Time-stamp: <2008-02-17 13:10:14EST gamma.lisp> |
|---|
| 4 | ;; $Id$ |
|---|
| 5 | |
|---|
| 6 | (in-package :gsl) |
|---|
| 7 | |
|---|
| 8 | (defmfun gamma-rd (generator a b) |
|---|
| 9 | ;; Named #'gamma-rd to avoid confusion with the special function #'gamma. |
|---|
| 10 | "gsl_ran_gamma" |
|---|
| 11 | (((generator generator) :pointer) (a :double) (b :double)) |
|---|
| 12 | :c-return :double |
|---|
| 13 | :documentation ; FDL |
|---|
| 14 | "A random variate from the gamma distribution. |
|---|
| 15 | The distribution function is |
|---|
| 16 | p(x) dx = {1 \over \Gamma(a) b^a} x^{a-1} e^{-x/b} dx |
|---|
| 17 | for x > 0. The gamma distribution with an integer parameter a |
|---|
| 18 | is known as the Erlang distribution. The variates are computed using |
|---|
| 19 | the algorithms from Knuth (vol 2).") |
|---|
| 20 | |
|---|
| 21 | (defmfun gamma-mt (generator a b) |
|---|
| 22 | "gsl_ran_gamma_mt" |
|---|
| 23 | (((generator generator) :pointer) (a :double) (b :double)) |
|---|
| 24 | :c-return :double |
|---|
| 25 | :documentation ; FDL |
|---|
| 26 | "A gamma variate using the Marsaglia-Tsang fast gamma method.") |
|---|
| 27 | |
|---|
| 28 | (defmfun gamma-pdf (x a b) |
|---|
| 29 | "gsl_ran_gamma_pdf" ((x :double) (a :double) (b :double)) |
|---|
| 30 | :c-return :double |
|---|
| 31 | :documentation ; FDL |
|---|
| 32 | "The probability density p(x) at x |
|---|
| 33 | for a gamma distribution with parameters a and b, using the |
|---|
| 34 | formula given in #'gamma.") |
|---|
| 35 | |
|---|
| 36 | (defmfun gamma-P (x a b) |
|---|
| 37 | "gsl_cdf_gamma_P" ((x :double) (a :double) (b :double)) |
|---|
| 38 | :c-return :double |
|---|
| 39 | :documentation ; FDL |
|---|
| 40 | "The cumulative distribution functions |
|---|
| 41 | P(x) for the Gamma distribution with parameters a and b.") |
|---|
| 42 | |
|---|
| 43 | (defmfun gamma-Q (x a b) |
|---|
| 44 | "gsl_cdf_gamma_Q" ((x :double) (a :double) (b :double)) |
|---|
| 45 | :c-return :double |
|---|
| 46 | :documentation ; FDL |
|---|
| 47 | "The cumulative distribution functions |
|---|
| 48 | Q(x) for the Gamma distribution with parameters a and b.") |
|---|
| 49 | |
|---|
| 50 | (defmfun gamma-Pinv (P a b) |
|---|
| 51 | "gsl_cdf_gamma_Pinv" ((P :double) (a :double) (b :double)) |
|---|
| 52 | :c-return :double |
|---|
| 53 | :documentation ; FDL |
|---|
| 54 | "The inverse cumulative distribution functions |
|---|
| 55 | P(x) for the Gamma distribution with parameters a and b.") |
|---|
| 56 | |
|---|
| 57 | (defmfun gamma-Qinv (Q a b) |
|---|
| 58 | "gsl_cdf_gamma_Qinv" ((Q :double) (a :double) (b :double)) |
|---|
| 59 | :c-return :double |
|---|
| 60 | :documentation ; FDL |
|---|
| 61 | "The inverse cumulative distribution functions |
|---|
| 62 | Q(x) for the Gamma distribution with parameters a and b.") |
|---|
| 63 | |
|---|
| 64 | ;;; Examples and unit test |
|---|
| 65 | #| |
|---|
| 66 | (make-tests gamma-randist |
|---|
| 67 | (letm ((rng (random-number-generator *mt19937* 0))) |
|---|
| 68 | (loop for i from 0 to 10 |
|---|
| 69 | collect |
|---|
| 70 | (gamma-rd rng 1.0d0 2.0d0))) |
|---|
| 71 | (letm ((rng (random-number-generator *mt19937* 0))) |
|---|
| 72 | (loop for i from 0 to 10 |
|---|
| 73 | collect |
|---|
| 74 | (gamma-mt rng 1.0d0 2.0d0))) |
|---|
| 75 | (gamma-pdf 0.1d0 1.0d0 2.0d0) |
|---|
| 76 | (gamma-P 0.1d0 1.0d0 2.0d0) |
|---|
| 77 | (gamma-Q 0.1d0 1.0d0 2.0d0) |
|---|
| 78 | (gamma-Pinv 0.048770575499286005d0 1.0d0 2.0d0) |
|---|
| 79 | (gamma-Qinv 0.951229424500714d0 1.0d0 2.0d0)) |
|---|
| 80 | |# |
|---|
| 81 | |
|---|
| 82 | (LISP-UNIT:DEFINE-TEST GAMMA-RANDIST |
|---|
| 83 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 84 | (LIST |
|---|
| 85 | (LIST 5.165688917678959d-4 3.6291162855975294d0 |
|---|
| 86 | 2.5273196108679516d0 0.10848774504124709d0 |
|---|
| 87 | 2.9249988468316213d0 1.4473215859071986d0 |
|---|
| 88 | 0.08690724899367205d0 0.590607841809058d0 |
|---|
| 89 | 1.2322105878131593d0 0.6023372667078228d0 |
|---|
| 90 | 0.5490215963871072d0)) |
|---|
| 91 | (MULTIPLE-VALUE-LIST |
|---|
| 92 | (LETM ((RNG (RANDOM-NUMBER-GENERATOR *MT19937* 0))) |
|---|
| 93 | (LOOP FOR I FROM 0 TO 10 COLLECT |
|---|
| 94 | (GAMMA-RD RNG 1.0d0 2.0d0))))) |
|---|
| 95 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 96 | (LIST |
|---|
| 97 | (LIST 2.6001378761310767d0 2.5222666954201554d0 |
|---|
| 98 | 7.731422092125877d0 4.227279926486253d0 |
|---|
| 99 | 0.09519304347489156d0 0.5710920106874436d0 |
|---|
| 100 | 0.8910637719460512d0 0.8263221202545431d0 |
|---|
| 101 | 3.1830665720573026d0 0.0038084003613199297d0 |
|---|
| 102 | 1.0320117334083205d0)) |
|---|
| 103 | (MULTIPLE-VALUE-LIST |
|---|
| 104 | (LETM ((RNG (RANDOM-NUMBER-GENERATOR *MT19937* 0))) |
|---|
| 105 | (LOOP FOR I FROM 0 TO 10 COLLECT |
|---|
| 106 | (GAMMA-MT RNG 1.0d0 2.0d0))))) |
|---|
| 107 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 108 | (LIST 0.475614712250357d0) |
|---|
| 109 | (MULTIPLE-VALUE-LIST (GAMMA-PDF 0.1d0 1.0d0 2.0d0))) |
|---|
| 110 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 111 | (LIST 0.04877057549928599d0) |
|---|
| 112 | (MULTIPLE-VALUE-LIST (GAMMA-P 0.1d0 1.0d0 2.0d0))) |
|---|
| 113 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 114 | (LIST 0.951229424500714d0) |
|---|
| 115 | (MULTIPLE-VALUE-LIST (GAMMA-Q 0.1d0 1.0d0 2.0d0))) |
|---|
| 116 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 117 | (LIST 0.10000000000000006d0) |
|---|
| 118 | (MULTIPLE-VALUE-LIST |
|---|
| 119 | (GAMMA-PINV 0.048770575499286005d0 1.0d0 2.0d0))) |
|---|
| 120 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 121 | (LIST 0.10000000000000003d0) |
|---|
| 122 | (MULTIPLE-VALUE-LIST |
|---|
| 123 | (GAMMA-QINV 0.951229424500714d0 1.0d0 2.0d0)))) |
|---|
| 124 | |
|---|