| 1 | ;; Flat distribution |
|---|
| 2 | ;; Liam Healy, Oct 7 2006 |
|---|
| 3 | ;; Time-stamp: <2008-02-17 13:08:10EST flat.lisp> |
|---|
| 4 | ;; $Id$ |
|---|
| 5 | |
|---|
| 6 | (in-package :gsl) |
|---|
| 7 | |
|---|
| 8 | (defmfun flat (generator a b) |
|---|
| 9 | "gsl_ran_flat" |
|---|
| 10 | (((generator generator) :pointer) (a :double) (b :double)) |
|---|
| 11 | :c-return :double |
|---|
| 12 | :documentation ; FDL |
|---|
| 13 | "A random variate from the flat (uniform) |
|---|
| 14 | distribution from a to b. The distribution is |
|---|
| 15 | p(x) dx = {1 \over (b-a)} dx |
|---|
| 16 | if a <= x < b, and 0 otherwise.") |
|---|
| 17 | |
|---|
| 18 | (defmfun flat-pdf (x a b) |
|---|
| 19 | "gsl_ran_flat_pdf" ((x :double) (a :double) (b :double)) |
|---|
| 20 | :c-return :double |
|---|
| 21 | :documentation ; FDL |
|---|
| 22 | "The probability density p(x) at x |
|---|
| 23 | for a uniform distribution from a to b, using the formula |
|---|
| 24 | given for #'flat.") |
|---|
| 25 | |
|---|
| 26 | (defmfun flat-P (x a b) |
|---|
| 27 | "gsl_cdf_flat_P" ((x :double) (a :double) (b :double)) |
|---|
| 28 | :c-return :double |
|---|
| 29 | :documentation ; FDL |
|---|
| 30 | "The cumulative distribution functions |
|---|
| 31 | P(x) for a uniform distribution from a to b.") |
|---|
| 32 | |
|---|
| 33 | (defmfun flat-Q (x a b) |
|---|
| 34 | "gsl_cdf_flat_Q" ((x :double) (a :double) (b :double)) |
|---|
| 35 | :c-return :double |
|---|
| 36 | :documentation ; FDL |
|---|
| 37 | "The cumulative distribution functions |
|---|
| 38 | Q(x) for a uniform distribution from a to b.") |
|---|
| 39 | |
|---|
| 40 | (defmfun flat-Pinv (P a b) |
|---|
| 41 | "gsl_cdf_flat_Pinv" ((P :double) (a :double) (b :double)) |
|---|
| 42 | :c-return :double |
|---|
| 43 | :documentation ; FDL |
|---|
| 44 | "The inverse cumulative distribution functions |
|---|
| 45 | P(x) for a uniform distribution from a to b.") |
|---|
| 46 | |
|---|
| 47 | (defmfun flat-Qinv (Q a b) |
|---|
| 48 | "gsl_cdf_flat_Qinv" ((Q :double) (a :double) (b :double)) |
|---|
| 49 | :c-return :double |
|---|
| 50 | :documentation ; FDL |
|---|
| 51 | "The inverse cumulative distribution functions |
|---|
| 52 | Q(x) for a uniform distribution from a to b.") |
|---|
| 53 | |
|---|
| 54 | ;;; Examples and unit test |
|---|
| 55 | #| |
|---|
| 56 | (make-tests flat |
|---|
| 57 | (letm ((rng (random-number-generator *mt19937* 0))) |
|---|
| 58 | (loop for i from 0 to 10 |
|---|
| 59 | collect |
|---|
| 60 | (flat rng 1.0d0 2.0d0))) |
|---|
| 61 | (flat-pdf 1.2d0 1.0d0 2.0d0) |
|---|
| 62 | (flat-P 1.2d0 1.0d0 2.0d0) |
|---|
| 63 | (flat-Q 1.2d0 1.0d0 2.0d0) |
|---|
| 64 | (flat-Pinv 0.19999999999999996d0 1.0d0 2.0d0) |
|---|
| 65 | (flat-Qinv 0.8d0 1.0d0 2.0d0)) |
|---|
| 66 | |# |
|---|
| 67 | |
|---|
| 68 | (LISP-UNIT:DEFINE-TEST FLAT |
|---|
| 69 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 70 | (LIST |
|---|
| 71 | (LIST 1.999741748906672d0 1.1629098753910512d0 |
|---|
| 72 | 1.2826178052928299d0 1.9472010820172727d0 |
|---|
| 73 | 1.2316565427463502d0 1.4849736143369228d0 |
|---|
| 74 | 1.9574769565369934d0 1.7443053431343287d0 |
|---|
| 75 | 1.540043658344075d0 1.7399529814720154d0 |
|---|
| 76 | 1.7599437981843948d0)) |
|---|
| 77 | (MULTIPLE-VALUE-LIST |
|---|
| 78 | (LETM ((RNG (RANDOM-NUMBER-GENERATOR *MT19937* 0))) |
|---|
| 79 | (LOOP FOR I FROM 0 TO 10 COLLECT |
|---|
| 80 | (FLAT RNG 1.0d0 2.0d0))))) |
|---|
| 81 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 82 | (LIST 1.0d0) |
|---|
| 83 | (MULTIPLE-VALUE-LIST (FLAT-PDF 1.2d0 1.0d0 2.0d0))) |
|---|
| 84 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 85 | (LIST 0.19999999999999996d0) |
|---|
| 86 | (MULTIPLE-VALUE-LIST (FLAT-P 1.2d0 1.0d0 2.0d0))) |
|---|
| 87 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 88 | (LIST 0.8d0) |
|---|
| 89 | (MULTIPLE-VALUE-LIST (FLAT-Q 1.2d0 1.0d0 2.0d0))) |
|---|
| 90 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 91 | (LIST 1.2d0) |
|---|
| 92 | (MULTIPLE-VALUE-LIST |
|---|
| 93 | (FLAT-PINV 0.19999999999999996d0 1.0d0 2.0d0))) |
|---|
| 94 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 95 | (LIST 1.2d0) |
|---|
| 96 | (MULTIPLE-VALUE-LIST |
|---|
| 97 | (FLAT-QINV 0.8d0 1.0d0 2.0d0)))) |
|---|
| 98 | |
|---|