| 1 | ;; Gaussian distribution |
|---|
| 2 | ;; Liam Healy, Sun Jul 16 2006 - 22:09 |
|---|
| 3 | ;; Time-stamp: <2008-02-17 12:23:52EST gaussian.lisp> |
|---|
| 4 | ;; $Id$ |
|---|
| 5 | |
|---|
| 6 | (in-package :gsl) |
|---|
| 7 | |
|---|
| 8 | (defmfun gaussian (generator sigma) |
|---|
| 9 | "gsl_ran_gaussian" |
|---|
| 10 | (((generator generator) :pointer) (sigma :double)) |
|---|
| 11 | :c-return :double |
|---|
| 12 | :documentation ; FDL |
|---|
| 13 | "A Gaussian random variate, with mean zero and |
|---|
| 14 | standard deviation sigma. The probability distribution for |
|---|
| 15 | Gaussian random variates is |
|---|
| 16 | p(x) dx = {1 \over \sqrt{2 \pi \sigma^2}} \exp (-x^2 / 2\sigma^2) dx |
|---|
| 17 | for x in the range -\infty to +\infty. Use the |
|---|
| 18 | transformation z = \mu + x on the numbers returned by |
|---|
| 19 | #'gaussian to obtain a Gaussian distribution with mean |
|---|
| 20 | mu. This function uses the Box-Mueller algorithm which requires two |
|---|
| 21 | calls to the random number generator r.") |
|---|
| 22 | |
|---|
| 23 | (defmfun gaussian-pdf (x sigma) |
|---|
| 24 | "gsl_ran_gaussian_pdf" ((x :double) (sigma :double)) |
|---|
| 25 | :c-return :double |
|---|
| 26 | :documentation ; FDL |
|---|
| 27 | "Compute the probability density p(x) at x |
|---|
| 28 | for a Gaussian distribution with standard deviation sigma.") |
|---|
| 29 | |
|---|
| 30 | (defmfun gaussian-ziggurat (generator sigma) |
|---|
| 31 | "gsl_ran_gaussian_ziggurat" |
|---|
| 32 | (((generator generator) :pointer) (sigma :double)) |
|---|
| 33 | :c-return :double |
|---|
| 34 | :documentation ; FDL |
|---|
| 35 | "Compute a Gaussian random variate using the alternative |
|---|
| 36 | Marsaglia-Tsang ziggurat method. The Ziggurat algorithm |
|---|
| 37 | is the fastest available algorithm in most cases.") |
|---|
| 38 | |
|---|
| 39 | (defmfun gaussian-ratio-method (generator sigma) |
|---|
| 40 | "gsl_ran_gaussian_ratio_method" |
|---|
| 41 | (((generator generator) :pointer) (sigma :double)) |
|---|
| 42 | :c-return :double |
|---|
| 43 | :documentation ; FDL |
|---|
| 44 | "Compute a Gaussian random variate using the Kinderman-Monahan-Leva |
|---|
| 45 | ratio method.") |
|---|
| 46 | |
|---|
| 47 | (defmfun ugaussian (generator) |
|---|
| 48 | "gsl_ran_ugaussian" (((generator generator) :pointer)) |
|---|
| 49 | :c-return :double |
|---|
| 50 | :documentation ; FDL |
|---|
| 51 | "Compute results for the unit Gaussian distribution, |
|---|
| 52 | equivalent to the #'gaussian with a standard deviation of one, |
|---|
| 53 | sigma = 1.") |
|---|
| 54 | |
|---|
| 55 | (defmfun ugaussian-pdf (x) |
|---|
| 56 | "gsl_ran_ugaussian_pdf" ((x :double)) |
|---|
| 57 | :c-return :double |
|---|
| 58 | :documentation ; FDL |
|---|
| 59 | "Compute results for the unit Gaussian distribution, |
|---|
| 60 | equivalent to the #'gaussian-pdf with a standard deviation of one, |
|---|
| 61 | sigma = 1.") |
|---|
| 62 | |
|---|
| 63 | (defmfun ugaussian-ratio-method (generator) |
|---|
| 64 | "gsl_ran_ugaussian_ratio_method" |
|---|
| 65 | (((generator generator) :pointer)) |
|---|
| 66 | :c-return :double |
|---|
| 67 | :documentation ; FDL |
|---|
| 68 | "Compute results for the unit Gaussian distribution, |
|---|
| 69 | equivalent to the #'gaussian-ration-method with a |
|---|
| 70 | standard deviation of one, sigma = 1.") |
|---|
| 71 | |
|---|
| 72 | (defmfun gaussian-P (x sigma) |
|---|
| 73 | "gsl_cdf_gaussian_P" ((x :double) (sigma :double)) |
|---|
| 74 | :c-return :double |
|---|
| 75 | :documentation ; FDL |
|---|
| 76 | "The cumulative distribution function P(x) for the Gaussian |
|---|
| 77 | distribution with standard deviation sigma.") |
|---|
| 78 | |
|---|
| 79 | (defmfun gaussian-Q (x sigma) |
|---|
| 80 | "gsl_cdf_gaussian_Q" ((x :double) (sigma :double)) |
|---|
| 81 | :c-return :double |
|---|
| 82 | :documentation ; FDL |
|---|
| 83 | "The cumulative distribution function Q(x) for the Gaussian |
|---|
| 84 | distribution with standard deviation sigma.") |
|---|
| 85 | |
|---|
| 86 | (defmfun gaussian-Pinv (P sigma) |
|---|
| 87 | "gsl_cdf_gaussian_Pinv" ((P :double) (sigma :double)) |
|---|
| 88 | :c-return :double |
|---|
| 89 | :documentation ; FDL |
|---|
| 90 | "The inverse cumulative distribution function P(x) for the Gaussian |
|---|
| 91 | distribution with standard deviation sigma.") |
|---|
| 92 | |
|---|
| 93 | (defmfun gaussian-Qinv (Q sigma) |
|---|
| 94 | "gsl_cdf_gaussian_Qinv" ((Q :double) (sigma :double)) |
|---|
| 95 | :c-return :double |
|---|
| 96 | :documentation ; FDL |
|---|
| 97 | "The inverse cumulative distribution function Q(x) for the Gaussian |
|---|
| 98 | distribution with standard deviation sigma.") |
|---|
| 99 | |
|---|
| 100 | (defmfun ugaussian-P (x) |
|---|
| 101 | "gsl_cdf_ugaussian_P" ((x :double)) |
|---|
| 102 | :c-return :double |
|---|
| 103 | :documentation ; FDL |
|---|
| 104 | "The cumulative distribution function P(x) for the Gaussian |
|---|
| 105 | distribution with unit standard deviation.") |
|---|
| 106 | |
|---|
| 107 | (defmfun ugaussian-Q (x) |
|---|
| 108 | "gsl_cdf_ugaussian_Q" ((x :double)) |
|---|
| 109 | :c-return :double |
|---|
| 110 | :documentation ; FDL |
|---|
| 111 | "The cumulative distribution function Q(x) for the Gaussian |
|---|
| 112 | distribution with unit standard deviation.") |
|---|
| 113 | |
|---|
| 114 | (defmfun ugaussian-Pinv (P) |
|---|
| 115 | "gsl_cdf_ugaussian_Pinv" ((P :double)) |
|---|
| 116 | :c-return :double |
|---|
| 117 | :documentation ; FDL |
|---|
| 118 | "The inverse cumulative distribution function P(x) for the Gaussian |
|---|
| 119 | distribution with unit standard deviation.") |
|---|
| 120 | |
|---|
| 121 | (defmfun ugaussian-Qinv (Q) |
|---|
| 122 | "gsl_cdf_ugaussian_Qinv" ((Q :double)) |
|---|
| 123 | :c-return :double |
|---|
| 124 | :documentation ; FDL |
|---|
| 125 | "The inverse cumulative distribution function Q(x) for the Gaussian |
|---|
| 126 | distribution with unit standard deviation.") |
|---|
| 127 | |
|---|
| 128 | ;;; Examples and unit test |
|---|
| 129 | #| |
|---|
| 130 | (make-tests |
|---|
| 131 | gaussian |
|---|
| 132 | (letm ((rng (random-number-generator *mt19937* 0))) |
|---|
| 133 | (loop for i from 0 to 10 |
|---|
| 134 | collect |
|---|
| 135 | (gaussian rng 10.0d0))) |
|---|
| 136 | (gaussian-pdf 0.0d0 10.0d0) |
|---|
| 137 | (letm ((rng (random-number-generator *mt19937* 0))) |
|---|
| 138 | (loop for i from 0 to 10 |
|---|
| 139 | collect |
|---|
| 140 | (gaussian-ziggurat rng 10.0d0))) |
|---|
| 141 | ;; Given in examples in GSL documentation |
|---|
| 142 | (ugaussian-p 2.0d0) |
|---|
| 143 | (ugaussian-q 2.0d0) |
|---|
| 144 | (ugaussian-pinv 0.9772498680518208d0) |
|---|
| 145 | (ugaussian-qinv 0.02275013194817921d0)) |
|---|
| 146 | |# |
|---|
| 147 | |
|---|
| 148 | (LISP-UNIT:DEFINE-TEST GAUSSIAN |
|---|
| 149 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 150 | (LIST |
|---|
| 151 | (LIST 1.3391860811867589d0 -0.8810099183143839d0 |
|---|
| 152 | 16.744084062537738d0 7.336411072925795d0 |
|---|
| 153 | 9.975246316020124d0 -12.775020810027664d0 |
|---|
| 154 | -23.967152827332075d0 -6.79280164729211d0 |
|---|
| 155 | -0.3909131843358723d0 8.935555455208181d0 |
|---|
| 156 | -0.17647794589783283d0)) |
|---|
| 157 | (MULTIPLE-VALUE-LIST |
|---|
| 158 | (LETM ((RNG (RANDOM-NUMBER-GENERATOR *MT19937* 0))) |
|---|
| 159 | (LOOP FOR I FROM 0 TO 10 COLLECT |
|---|
| 160 | (GAUSSIAN RNG 10.0d0))))) |
|---|
| 161 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 162 | (LIST 0.039894228040143274d0) |
|---|
| 163 | (MULTIPLE-VALUE-LIST (GAUSSIAN-PDF 0.0d0 10.0d0))) |
|---|
| 164 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 165 | (LIST |
|---|
| 166 | (LIST 6.107926637756835d0 -15.764917894916914d0 |
|---|
| 167 | -7.200971392533084d0 19.5115024682162d0 |
|---|
| 168 | 5.248202921580308d0 10.408510262302668d0 |
|---|
| 169 | -14.333242326151595d0 0.9455088719230575d0 |
|---|
| 170 | -11.120343985926329d0 -3.0791957427954d0 |
|---|
| 171 | -2.090680773068803d0)) |
|---|
| 172 | (MULTIPLE-VALUE-LIST |
|---|
| 173 | (LETM ((RNG (RANDOM-NUMBER-GENERATOR *MT19937* 0))) |
|---|
| 174 | (LOOP FOR I FROM 0 TO 10 COLLECT |
|---|
| 175 | (GAUSSIAN-ZIGGURAT RNG 10.0d0))))) |
|---|
| 176 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 177 | (LIST 0.9772498680518208d0) |
|---|
| 178 | (MULTIPLE-VALUE-LIST (UGAUSSIAN-P 2.0d0))) |
|---|
| 179 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 180 | (LIST 0.022750131948179212d0) |
|---|
| 181 | (MULTIPLE-VALUE-LIST (UGAUSSIAN-Q 2.0d0))) |
|---|
| 182 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 183 | (LIST 2.0d0) |
|---|
| 184 | (MULTIPLE-VALUE-LIST (UGAUSSIAN-PINV 0.9772498680518208d0))) |
|---|
| 185 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 186 | (LIST 2.0d0) |
|---|
| 187 | (MULTIPLE-VALUE-LIST (UGAUSSIAN-QINV 0.02275013194817921d0)))) |
|---|