| 1 | ;; Levy distribution |
|---|
| 2 | ;; Liam Healy, Sat Sep 30 2006 |
|---|
| 3 | ;; Time-stamp: <2008-02-17 13:03:36EST levy.lisp> |
|---|
| 4 | ;; $Id$ |
|---|
| 5 | |
|---|
| 6 | (in-package :gsl) |
|---|
| 7 | |
|---|
| 8 | (defmfun levy (generator c alpha) |
|---|
| 9 | "gsl_ran_levy" |
|---|
| 10 | (((generator generator) :pointer) (c :double) (alpha :double)) |
|---|
| 11 | :c-return :double |
|---|
| 12 | :documentation ; FDL |
|---|
| 13 | "A random variate from the Levy symmetric stable |
|---|
| 14 | distribution with scale c and exponent alpha. The symmetric |
|---|
| 15 | stable probability distribution is defined by a fourier transform, |
|---|
| 16 | p(x) = {1 \over 2 \pi} \int_{-\infty}^{+\infty} dt \exp(-it x - |c t|^\alpha) |
|---|
| 17 | There is no explicit solution for the form of p(x) and the |
|---|
| 18 | library does not define a corresponding pdf function. For |
|---|
| 19 | \alpha = 1 the distribution reduces to the Cauchy distribution. For |
|---|
| 20 | \alpha = 2 it is a Gaussian distribution with \sigma = \sqrt{2} c |
|---|
| 21 | For \alpha < 1 the tails of the distribution become extremely wide. |
|---|
| 22 | The algorithm only works for 0 < alpha <= 2.") |
|---|
| 23 | |
|---|
| 24 | (defmfun levy-skew (generator c alpha beta) |
|---|
| 25 | "gsl_ran_levy_skew" |
|---|
| 26 | (((generator generator) :pointer) (c :double) (alpha :double) (beta :double)) |
|---|
| 27 | :c-return :double |
|---|
| 28 | :documentation ; FDL |
|---|
| 29 | "A random variate from the Levy skew stable |
|---|
| 30 | distribution with scale c exponent alpha and skewness |
|---|
| 31 | parameter beta. The skewness parameter must lie in the range |
|---|
| 32 | [-1,1]. The Levy skew stable probability distribution is defined |
|---|
| 33 | by a fourier transform, |
|---|
| 34 | p(x) = {1 \over 2 \pi} \int_{-\infty}^{+\infty} dt |
|---|
| 35 | \exp(-it x - |c t|^\alpha (1-i \beta \sign(t) \tan(\pi\alpha/2))) |
|---|
| 36 | When \alpha = 1 the term \tan(\pi \alpha/2) is replaced by |
|---|
| 37 | -(2/\pi)\log|t|. There is no explicit solution for the form of |
|---|
| 38 | p(x)} and the library does not define a corresponding pdf |
|---|
| 39 | function. For \alpha = 2 the distribution reduces to a Gaussian |
|---|
| 40 | distribution with \sigma = \sqrt{2} c and the skewness parameter |
|---|
| 41 | has no effect. For \alpha < 1 the tails of the distribution |
|---|
| 42 | become extremely wide. The symmetric distribution corresponds to \beta = 0. |
|---|
| 43 | The algorithm only works for 0 < \alpha \le 2.") |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | ;;; Examples and unit test |
|---|
| 47 | #| |
|---|
| 48 | (make-tests levy |
|---|
| 49 | (letm ((rng (random-number-generator *mt19937* 0))) |
|---|
| 50 | (loop for i from 0 to 10 |
|---|
| 51 | collect |
|---|
| 52 | (levy rng 1.0d0 2.0d0))) |
|---|
| 53 | (letm ((rng (random-number-generator *mt19937* 0))) |
|---|
| 54 | (loop for i from 0 to 10 |
|---|
| 55 | collect |
|---|
| 56 | (levy-skew rng 1.0d0 2.0d0 1.0d0)))) |
|---|
| 57 | |# |
|---|
| 58 | |
|---|
| 59 | (LISP-UNIT:DEFINE-TEST LEVY |
|---|
| 60 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 61 | (LIST |
|---|
| 62 | (LIST 2.6941098332360465d0 -0.29395438644676647d0 |
|---|
| 63 | -1.2703401352272083d0 1.0771538640113538d0 |
|---|
| 64 | 0.13771218406916103d0 0.9419728438107844d0 |
|---|
| 65 | -0.5107134674789159d0 0.1648207853689268d0 |
|---|
| 66 | -0.14857899041035147d0 -1.9074885744364487d0 |
|---|
| 67 | -2.086195213997167d0)) |
|---|
| 68 | (MULTIPLE-VALUE-LIST |
|---|
| 69 | (LETM ((RNG (RANDOM-NUMBER-GENERATOR *MT19937* 0))) |
|---|
| 70 | (LOOP FOR I FROM 0 TO 10 COLLECT |
|---|
| 71 | (LEVY RNG 1.0d0 2.0d0))))) |
|---|
| 72 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 73 | (LIST |
|---|
| 74 | (LIST 2.6941098332360465d0 -0.2939543864467665d0 |
|---|
| 75 | -1.2703401352272083d0 1.0771538640113538d0 |
|---|
| 76 | 0.13771218406916097d0 0.9419728438107844d0 |
|---|
| 77 | -0.510713467478916d0 0.1648207853689266d0 |
|---|
| 78 | -0.14857899041035158d0 -1.907488574436449d0 |
|---|
| 79 | -2.086195213997167d0)) |
|---|
| 80 | (MULTIPLE-VALUE-LIST |
|---|
| 81 | (LETM ((RNG (RANDOM-NUMBER-GENERATOR *MT19937* 0))) |
|---|
| 82 | (LOOP FOR I FROM 0 TO 10 COLLECT |
|---|
| 83 | (LEVY-SKEW RNG 1.0d0 2.0d0 1.0d0)))))) |
|---|
| 84 | |
|---|