| 1 | ;; Chi-squared distribution |
|---|
| 2 | ;; Liam Healy, Sat Oct 7 2006 - 16:13 |
|---|
| 3 | ;; Time-stamp: <2008-02-17 13:12:43EST chi-squared.lisp> |
|---|
| 4 | ;; $Id$ |
|---|
| 5 | |
|---|
| 6 | (in-package :gsl) |
|---|
| 7 | |
|---|
| 8 | (defmfun chi-squared (generator nu) |
|---|
| 9 | "gsl_ran_chisq" |
|---|
| 10 | (((generator generator) :pointer) (nu :double)) |
|---|
| 11 | :c-return :double |
|---|
| 12 | :documentation ; FDL |
|---|
| 13 | "A random variate from the chi-squared distribution |
|---|
| 14 | with nu degrees of freedom. The distribution function is |
|---|
| 15 | p(x) dx = {1 \over 2 \Gamma(\nu/2) } (x/2)^{\nu/2 - 1} \exp(-x/2) dx |
|---|
| 16 | x >= 0. ") |
|---|
| 17 | |
|---|
| 18 | (defmfun chi-squared-pdf (x nu) |
|---|
| 19 | "gsl_ran_chisq_pdf" ((x :double) (nu :double)) |
|---|
| 20 | :c-return :double |
|---|
| 21 | :documentation ; FDL |
|---|
| 22 | "The probability density p(x) at x |
|---|
| 23 | for a chi-squared distribution with nu degrees of freedom, using |
|---|
| 24 | the formula given in #'chi-squared.") |
|---|
| 25 | |
|---|
| 26 | (defmfun chi-squared-P (x nu) |
|---|
| 27 | "gsl_cdf_chisq_P" ((x :double) (nu :double)) |
|---|
| 28 | :c-return :double |
|---|
| 29 | :documentation ; FDL |
|---|
| 30 | "The cumulative distribution functions |
|---|
| 31 | P(x) for the chi-squared distribution with nu degrees of freedom.") |
|---|
| 32 | |
|---|
| 33 | (defmfun chi-squared-Q (x nu) |
|---|
| 34 | "gsl_cdf_chisq_Q" ((x :double) (nu :double)) |
|---|
| 35 | :c-return :double |
|---|
| 36 | :documentation ; FDL |
|---|
| 37 | "The cumulative distribution functions |
|---|
| 38 | Q(x) for the chi-squared distribution with nu degrees of freedom.") |
|---|
| 39 | |
|---|
| 40 | (defmfun chi-squared-Pinv (P nu) |
|---|
| 41 | "gsl_cdf_chisq_Pinv" ((P :double) (nu :double)) |
|---|
| 42 | :c-return :double |
|---|
| 43 | :documentation ; FDL |
|---|
| 44 | "The inverse cumulative distribution functions |
|---|
| 45 | P(x) for the chi-squared distribution with nu degrees of freedom.") |
|---|
| 46 | |
|---|
| 47 | (defmfun chi-squared-Qinv (Q nu) |
|---|
| 48 | "gsl_cdf_chisq_Qinv" ((Q :double) (nu :double)) |
|---|
| 49 | :c-return :double |
|---|
| 50 | :documentation ; FDL |
|---|
| 51 | "The inverse cumulative distribution functions |
|---|
| 52 | Q(x) for the chi-squared distribution with nu degrees of freedom.") |
|---|
| 53 | |
|---|
| 54 | ;;; Examples and unit test |
|---|
| 55 | #| |
|---|
| 56 | (make-tests chi-squared |
|---|
| 57 | (letm ((rng (random-number-generator *mt19937* 0))) |
|---|
| 58 | (loop for i from 0 to 10 |
|---|
| 59 | collect |
|---|
| 60 | (chi-squared rng 10.0d0))) |
|---|
| 61 | (chi-squared-pdf 0.5d0 1.0d0) |
|---|
| 62 | (chi-squared-P 0.5d0 1.0d0) |
|---|
| 63 | (chi-squared-Q 0.5d0 1.0d0) |
|---|
| 64 | (chi-squared-Pinv 0.5204998778130463d0 1.0d0) |
|---|
| 65 | (chi-squared-Qinv 0.4795001221869537d0 1.0d0)) |
|---|
| 66 | |# |
|---|
| 67 | |
|---|
| 68 | (LISP-UNIT:DEFINE-TEST CHI-SQUARED |
|---|
| 69 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 70 | (LIST |
|---|
| 71 | (LIST 9.190439057230117d0 3.9593845312309113d0 |
|---|
| 72 | 5.434933385075541d0 13.023107419896268d0 |
|---|
| 73 | 13.670690841729192d0 6.982359715352029d0 |
|---|
| 74 | 13.75663972164132d0 11.359458045417648d0 |
|---|
| 75 | 16.06828259064875d0 12.999514252538129d0 |
|---|
| 76 | 11.147347424355443d0)) |
|---|
| 77 | (MULTIPLE-VALUE-LIST |
|---|
| 78 | (LETM ((RNG (RANDOM-NUMBER-GENERATOR *MT19937* 0))) |
|---|
| 79 | (LOOP FOR I FROM 0 TO 10 COLLECT |
|---|
| 80 | (CHI-SQUARED RNG 10.0d0))))) |
|---|
| 81 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 82 | (LIST 0.43939128946772227d0) |
|---|
| 83 | (MULTIPLE-VALUE-LIST (CHI-SQUARED-PDF 0.5d0 1.0d0))) |
|---|
| 84 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 85 | (LIST 0.5204998778130462d0) |
|---|
| 86 | (MULTIPLE-VALUE-LIST (CHI-SQUARED-P 0.5d0 1.0d0))) |
|---|
| 87 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 88 | (LIST 0.4795001221869538d0) |
|---|
| 89 | (MULTIPLE-VALUE-LIST (CHI-SQUARED-Q 0.5d0 1.0d0))) |
|---|
| 90 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 91 | (LIST 0.5000000000000003d0) |
|---|
| 92 | (MULTIPLE-VALUE-LIST |
|---|
| 93 | (CHI-SQUARED-PINV 0.5204998778130463d0 1.0d0))) |
|---|
| 94 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 95 | (LIST 0.5000000000000003d0) |
|---|
| 96 | (MULTIPLE-VALUE-LIST |
|---|
| 97 | (CHI-SQUARED-QINV 0.4795001221869537d0 1.0d0)))) |
|---|
| 98 | |
|---|