| 1 | ;; Negative binomial and Pascal distributions |
|---|
| 2 | ;; Liam Healy, Sat Nov 25 2006 - 16:00 |
|---|
| 3 | ;; Time-stamp: <2008-02-17 13:44:16EST negative-binomial.lisp> |
|---|
| 4 | ;; $Id$ |
|---|
| 5 | |
|---|
| 6 | (in-package :gsl) |
|---|
| 7 | |
|---|
| 8 | ;;;;**************************************************************************** |
|---|
| 9 | ;;;; Negative binomial |
|---|
| 10 | ;;;;**************************************************************************** |
|---|
| 11 | |
|---|
| 12 | (defmfun negative-binomial (generator p n) |
|---|
| 13 | "gsl_ran_negative_binomial" |
|---|
| 14 | (((generator generator) :pointer) (p :double) (n :double)) |
|---|
| 15 | :c-return :uint |
|---|
| 16 | :documentation ; FDL |
|---|
| 17 | "A random integer from the negative binomial |
|---|
| 18 | distribution, the number of failures occurring before n successes |
|---|
| 19 | in independent trials with probability p of success. The |
|---|
| 20 | probability distribution for negative binomial variates is, |
|---|
| 21 | p(k) = {\Gamma(n + k) \over \Gamma(k+1) \Gamma(n) } p^n (1-p)^k |
|---|
| 22 | Note that n is not required to be an integer.") |
|---|
| 23 | |
|---|
| 24 | (defmfun negative-binomial-pdf (k p n) |
|---|
| 25 | "gsl_ran_negative_binomial_pdf" ((k :uint) (p :double) (n :double)) |
|---|
| 26 | :c-return :double |
|---|
| 27 | :documentation ; FDL |
|---|
| 28 | "The probability p(k) of obtaining k |
|---|
| 29 | from a negative binomial distribution with parameters p and |
|---|
| 30 | n, using the formula given in #'negative-binomial.") |
|---|
| 31 | |
|---|
| 32 | (defmfun negative-binomial-P (k p n) |
|---|
| 33 | "gsl_cdf_negative_binomial_P" ((k :uint) (p :double) (n :double)) |
|---|
| 34 | :c-return :double |
|---|
| 35 | :documentation ; FDL |
|---|
| 36 | "The cumulative distribution functions |
|---|
| 37 | P(k) for the negative binomial distribution |
|---|
| 38 | with parameters p and n.") |
|---|
| 39 | |
|---|
| 40 | (defmfun negative-binomial-Q (k p n) |
|---|
| 41 | "gsl_cdf_negative_binomial_Q" ((k :uint) (p :double) (n :double)) |
|---|
| 42 | :c-return :double |
|---|
| 43 | :documentation ; FDL |
|---|
| 44 | "The cumulative distribution functions |
|---|
| 45 | Q(k) for the negative binomial distribution |
|---|
| 46 | with parameters p and n.") |
|---|
| 47 | |
|---|
| 48 | ;;;;**************************************************************************** |
|---|
| 49 | ;;;; Pascal |
|---|
| 50 | ;;;;**************************************************************************** |
|---|
| 51 | |
|---|
| 52 | (defmfun pascal (generator p n) |
|---|
| 53 | "gsl_ran_pascal" |
|---|
| 54 | (((generator generator) :pointer) (p :double) (n :uint)) |
|---|
| 55 | :c-return :uint |
|---|
| 56 | :documentation ; FDL |
|---|
| 57 | "A random integer from the Pascal distribution. The |
|---|
| 58 | Pascal distribution is simply a negative binomial distribution with an |
|---|
| 59 | integer value of n. |
|---|
| 60 | p(k) = {(n + k - 1)! \over k! (n - 1)! } p^n (1-p)^k |
|---|
| 61 | k >= 0.") |
|---|
| 62 | |
|---|
| 63 | (defmfun pascal-pdf (k p n) |
|---|
| 64 | "gsl_ran_pascal_pdf" ((k :uint) (p :double) (n :uint)) |
|---|
| 65 | :c-return :double |
|---|
| 66 | :documentation ; FDL |
|---|
| 67 | "The probability p(k) of obtaining k |
|---|
| 68 | from a Pascal distribution with parameters p and |
|---|
| 69 | n, using the formula given in #'pascal.") |
|---|
| 70 | |
|---|
| 71 | (defmfun pascal-P (k p n) |
|---|
| 72 | "gsl_cdf_pascal_P" ((k :uint) (p :double) (n :uint)) |
|---|
| 73 | :c-return :double |
|---|
| 74 | :documentation ; FDL |
|---|
| 75 | "The cumulative distribution functions |
|---|
| 76 | P(k) for the Pascal distribution |
|---|
| 77 | with parameters p and n.") |
|---|
| 78 | |
|---|
| 79 | (defmfun pascal-Q (k p n) |
|---|
| 80 | "gsl_cdf_pascal_Q" ((k :uint) (p :double) (n :uint)) |
|---|
| 81 | :c-return :double |
|---|
| 82 | :documentation ; FDL |
|---|
| 83 | "The cumulative distribution functions |
|---|
| 84 | Q(k) for the Pascal distribution |
|---|
| 85 | with parameters p and n.") |
|---|
| 86 | |
|---|
| 87 | ;;;;**************************************************************************** |
|---|
| 88 | ;;;; Examples and unit test |
|---|
| 89 | ;;;;**************************************************************************** |
|---|
| 90 | |
|---|
| 91 | #| |
|---|
| 92 | (make-tests negative-binomial |
|---|
| 93 | (letm ((rng (random-number-generator *mt19937* 0))) |
|---|
| 94 | (loop for i from 0 to 10 |
|---|
| 95 | collect |
|---|
| 96 | (negative-binomial rng 0.4d0 12.0d0))) |
|---|
| 97 | (negative-binomial-pdf 5 0.4d0 12.0d0) |
|---|
| 98 | (negative-binomial-P 5 0.4d0 12.0d0) |
|---|
| 99 | (negative-binomial-Q 5 0.4d0 12.0d0) |
|---|
| 100 | (letm ((rng (random-number-generator *mt19937* 0))) |
|---|
| 101 | (loop for i from 0 to 10 |
|---|
| 102 | collect |
|---|
| 103 | (pascal rng 0.4d0 12))) |
|---|
| 104 | (pascal-pdf 5 0.4d0 12) |
|---|
| 105 | (pascal-P 5 0.4d0 12) |
|---|
| 106 | (pascal-Q 5 0.4d0 12)) |
|---|
| 107 | |# |
|---|
| 108 | |
|---|
| 109 | (LISP-UNIT:DEFINE-TEST NEGATIVE-BINOMIAL |
|---|
| 110 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 111 | (LIST (LIST 10 7 12 23 20 24 18 12 4 22 15)) |
|---|
| 112 | (MULTIPLE-VALUE-LIST |
|---|
| 113 | (LETM ((RNG (RANDOM-NUMBER-GENERATOR *MT19937* 0))) |
|---|
| 114 | (LOOP FOR I FROM 0 TO 10 COLLECT |
|---|
| 115 | (NEGATIVE-BINOMIAL RNG 0.4d0 12.0d0))))) |
|---|
| 116 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 117 | (LIST 0.0056984767089869d0) |
|---|
| 118 | (MULTIPLE-VALUE-LIST |
|---|
| 119 | (NEGATIVE-BINOMIAL-PDF 5 0.4d0 12.0d0))) |
|---|
| 120 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 121 | (LIST 0.010594202555514881d0) |
|---|
| 122 | (MULTIPLE-VALUE-LIST |
|---|
| 123 | (NEGATIVE-BINOMIAL-P 5 0.4d0 12.0d0))) |
|---|
| 124 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 125 | (LIST 0.9894057974444851d0) |
|---|
| 126 | (MULTIPLE-VALUE-LIST |
|---|
| 127 | (NEGATIVE-BINOMIAL-Q 5 0.4d0 12.0d0))) |
|---|
| 128 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 129 | (LIST (LIST 10 7 12 23 20 24 18 12 4 22 15)) |
|---|
| 130 | (MULTIPLE-VALUE-LIST |
|---|
| 131 | (LETM ((RNG (RANDOM-NUMBER-GENERATOR *MT19937* 0))) |
|---|
| 132 | (LOOP FOR I FROM 0 TO 10 COLLECT |
|---|
| 133 | (PASCAL RNG 0.4d0 12))))) |
|---|
| 134 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 135 | (LIST 0.0056984767089869d0) |
|---|
| 136 | (MULTIPLE-VALUE-LIST (PASCAL-PDF 5 0.4d0 12))) |
|---|
| 137 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 138 | (LIST 0.010594202555514881d0) |
|---|
| 139 | (MULTIPLE-VALUE-LIST (PASCAL-P 5 0.4d0 12))) |
|---|
| 140 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 141 | (LIST 0.9894057974444851d0) |
|---|
| 142 | (MULTIPLE-VALUE-LIST (PASCAL-Q 5 0.4d0 12)))) |
|---|
| 143 | |
|---|