| 1 | ;; Hypergeometric distribution |
|---|
| 2 | ;; Liam Healy, Sat Nov 25 2006 - 16:00 |
|---|
| 3 | ;; Time-stamp: <2008-02-17 15:35:14EST hypergeometric.lisp> |
|---|
| 4 | ;; $Id$ |
|---|
| 5 | |
|---|
| 6 | (in-package :gsl) |
|---|
| 7 | |
|---|
| 8 | (defmfun hypergeometric (generator n1 n2 tt) |
|---|
| 9 | "gsl_ran_hypergeometric" |
|---|
| 10 | (((generator generator) :pointer) (n1 :uint) (n2 :uint)(tt :uint)) |
|---|
| 11 | :c-return :uint |
|---|
| 12 | :documentation ; FDL |
|---|
| 13 | "A random integer from the hypergeometric |
|---|
| 14 | distribution. The probability distribution for hypergeometric |
|---|
| 15 | random variates is |
|---|
| 16 | p(k) = C(n_1, k) C(n_2, t - k) / C(n_1 + n_2, t) |
|---|
| 17 | where C(a,b) = a!/(b!(a-b)!) and |
|---|
| 18 | t <= n_1 + n_2. The domain of k is |
|---|
| 19 | max(0,t-n_2), ..., min(t,n_1). |
|---|
| 20 | If a population contains n_1 elements of ``type 1'' and |
|---|
| 21 | n_2 elements of ``type 2'' then the hypergeometric |
|---|
| 22 | distribution gives the probability of obtaining k elements of |
|---|
| 23 | ``type 1'' in t samples from the population without |
|---|
| 24 | replacement.") |
|---|
| 25 | |
|---|
| 26 | (defmfun hypergeometric-pdf (k n1 n2 tt) |
|---|
| 27 | "gsl_ran_hypergeometric_pdf" ((k :uint) (n1 :uint) (n2 :uint)(tt :uint)) |
|---|
| 28 | :c-return :double |
|---|
| 29 | :documentation ; FDL |
|---|
| 30 | "The probability p(k) of obtaining k |
|---|
| 31 | from a hypergeometric distribution with parameters n1, n2, |
|---|
| 32 | tt, using the formula given in #'hypergeometric.") |
|---|
| 33 | |
|---|
| 34 | (defmfun hypergeometric-P (k n1 n2 tt) |
|---|
| 35 | "gsl_cdf_hypergeometric_P" ((k :uint) (n1 :uint) (n2 :uint)(tt :uint)) |
|---|
| 36 | :c-return :double |
|---|
| 37 | :documentation ; FDL |
|---|
| 38 | "The cumulative distribution functions P(k) for the |
|---|
| 39 | hypergeometric distribution with parameters n1, n2 and tt.") |
|---|
| 40 | |
|---|
| 41 | (defmfun hypergeometric-Q (k n1 n2 tt) |
|---|
| 42 | "gsl_cdf_hypergeometric_Q" ((k :uint) (n1 :uint) (n2 :uint)(tt :uint)) |
|---|
| 43 | :c-return :double |
|---|
| 44 | :documentation ; FDL |
|---|
| 45 | "The cumulative distribution functions Q(k) for the |
|---|
| 46 | hypergeometric distribution with parameters n1, n2, and tt.") |
|---|
| 47 | |
|---|
| 48 | ;;; Examples and unit test |
|---|
| 49 | #| |
|---|
| 50 | (make-tests hypergeometric-randist |
|---|
| 51 | (letm ((rng (random-number-generator *mt19937* 0))) |
|---|
| 52 | (loop for i from 0 to 10 |
|---|
| 53 | collect |
|---|
| 54 | (hypergeometric rng 3 6 3))) |
|---|
| 55 | (hypergeometric-pdf 0 2 6 3) |
|---|
| 56 | (hypergeometric-P 1 2 6 3) |
|---|
| 57 | (hypergeometric-Q 1 2 6 3)) |
|---|
| 58 | |# |
|---|
| 59 | |
|---|
| 60 | (LISP-UNIT:DEFINE-TEST HYPERGEOMETRIC-RANDIST |
|---|
| 61 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 62 | (LIST (LIST 2 1 0 0 1 1 3 1 0 1 3)) |
|---|
| 63 | (MULTIPLE-VALUE-LIST |
|---|
| 64 | (LETM ((RNG (RANDOM-NUMBER-GENERATOR *MT19937* 0))) |
|---|
| 65 | (LOOP FOR I FROM 0 TO 10 COLLECT |
|---|
| 66 | (HYPERGEOMETRIC RNG 3 6 3))))) |
|---|
| 67 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 68 | (LIST 0.35714285714285693d0) |
|---|
| 69 | (MULTIPLE-VALUE-LIST (HYPERGEOMETRIC-PDF 0 2 6 3))) |
|---|
| 70 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 71 | (LIST 0.892857142857143d0) |
|---|
| 72 | (MULTIPLE-VALUE-LIST (HYPERGEOMETRIC-P 1 2 6 3))) |
|---|
| 73 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 74 | (LIST 0.10714285714285704d0) |
|---|
| 75 | (MULTIPLE-VALUE-LIST (HYPERGEOMETRIC-Q 1 2 6 3)))) |
|---|
| 76 | |
|---|