| 1 | ;; Geometric distribution |
|---|
| 2 | ;; Liam Healy, Sat Nov 25 2006 - 16:00 |
|---|
| 3 | ;; Time-stamp: <2008-02-17 13:45:43EST geometric.lisp> |
|---|
| 4 | ;; $Id$ |
|---|
| 5 | |
|---|
| 6 | (in-package :gsl) |
|---|
| 7 | |
|---|
| 8 | (defmfun geometric (generator p) |
|---|
| 9 | "gsl_ran_geometric" |
|---|
| 10 | (((generator generator) :pointer) (p :double)) |
|---|
| 11 | :c-return :uint |
|---|
| 12 | :documentation ; FDL |
|---|
| 13 | "A random integer from the geometric distribution, |
|---|
| 14 | the number of independent trials with probability p until the |
|---|
| 15 | first success. The probability distribution for geometric variates |
|---|
| 16 | is p(k) = p (1-p)^{k-1} for k >= 1. |
|---|
| 17 | Note that the distribution begins with k=1 with this |
|---|
| 18 | definition. There is another convention in which the exponent k-1 |
|---|
| 19 | is replaced by k.") |
|---|
| 20 | |
|---|
| 21 | (defmfun geometric-pdf (k p) |
|---|
| 22 | "gsl_ran_geometric_pdf" ((k :uint) (p :double)) |
|---|
| 23 | :c-return :double |
|---|
| 24 | :documentation ; FDL |
|---|
| 25 | "The probability p(k) of obtaining k |
|---|
| 26 | from a geometric distribution with probability parameter p, using |
|---|
| 27 | the formula given in #'geometric.") |
|---|
| 28 | |
|---|
| 29 | (defmfun geometric-P (k p) |
|---|
| 30 | "gsl_cdf_geometric_P" ((k :uint) (p :double)) |
|---|
| 31 | :c-return :double |
|---|
| 32 | :documentation ; FDL |
|---|
| 33 | "The cumulative distribution functions |
|---|
| 34 | P(k) for the geometric distribution with parameter p.") |
|---|
| 35 | |
|---|
| 36 | (defmfun geometric-Q (k p) |
|---|
| 37 | "gsl_cdf_geometric_Q" ((k :uint) (p :double)) |
|---|
| 38 | :c-return :double |
|---|
| 39 | :documentation ; FDL |
|---|
| 40 | "The cumulative distribution functions |
|---|
| 41 | Q(k) for the geometric distribution with parameters p.") |
|---|
| 42 | |
|---|
| 43 | ;;; Examples and unit test |
|---|
| 44 | #| |
|---|
| 45 | (make-tests geometric |
|---|
| 46 | (letm ((rng (random-number-generator *mt19937* 0))) |
|---|
| 47 | (loop for i from 0 to 10 |
|---|
| 48 | collect |
|---|
| 49 | (geometric rng 0.4d0))) |
|---|
| 50 | (geometric-pdf 2 0.4d0) |
|---|
| 51 | (geometric-P 2 0.4d0) |
|---|
| 52 | (geometric-Q 2 0.4d0)) |
|---|
| 53 | |# |
|---|
| 54 | |
|---|
| 55 | (LISP-UNIT:DEFINE-TEST GEOMETRIC |
|---|
| 56 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 57 | (LIST (LIST 1 4 3 1 3 2 1 1 2 1 1)) |
|---|
| 58 | (MULTIPLE-VALUE-LIST |
|---|
| 59 | (LETM ((RNG (RANDOM-NUMBER-GENERATOR *MT19937* 0))) |
|---|
| 60 | (LOOP FOR I FROM 0 TO 10 COLLECT |
|---|
| 61 | (GEOMETRIC RNG 0.4d0))))) |
|---|
| 62 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 63 | (LIST 0.24d0) |
|---|
| 64 | (MULTIPLE-VALUE-LIST (GEOMETRIC-PDF 2 0.4d0))) |
|---|
| 65 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 66 | (LIST 0.64d0) |
|---|
| 67 | (MULTIPLE-VALUE-LIST (GEOMETRIC-P 2 0.4d0))) |
|---|
| 68 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 69 | (LIST 0.36d0) |
|---|
| 70 | (MULTIPLE-VALUE-LIST (GEOMETRIC-Q 2 0.4d0)))) |
|---|
| 71 | |
|---|