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