chi-squared.lisp 2.13 KB
Newer Older
1 2
;; Chi-squared distribution
;; Liam Healy, Sat Oct  7 2006 - 16:13
Liam Healy's avatar
Liam Healy committed
3
;; Time-stamp: <2009-02-16 10:10:33EST chi-squared.lisp>
lhealy's avatar
lhealy committed
4
;; $Id$
liam's avatar
liam committed
5 6 7

(in-package :gsl)

8 9 10
;;; /usr/include/gsl/gsl_randist.h
;;; /usr/include/gsl/gsl_cdf.h

liam's avatar
liam committed
11
(defmfun chi-squared (generator nu)
liam's avatar
liam committed
12
  "gsl_ran_chisq"
13
  (((mpointer generator) :pointer) (nu :double))
liam's avatar
liam committed
14
  :c-return :double
15
  :documentation			; FDL
liam's avatar
liam committed
16
  "A random variate from the chi-squared distribution
17
  with nu degrees of freedom. The distribution function is
liam's avatar
liam committed
18
  p(x) dx = {1 \over 2 \Gamma(\nu/2) } (x/2)^{\nu/2 - 1} \exp(-x/2) dx
19
  x >= 0. ")
liam's avatar
liam committed
20

liam's avatar
liam committed
21
(defmfun chi-squared-pdf (x nu)
liam's avatar
liam committed
22 23
  "gsl_ran_chisq_pdf" ((x :double) (nu :double))
  :c-return :double
24 25 26
  :documentation			; FDL
  "The probability density p(x) at x
   for a chi-squared distribution with nu degrees of freedom, using
liam's avatar
liam committed
27 28
   the formula given in #'chi-squared.")

liam's avatar
liam committed
29
(defmfun chi-squared-P (x nu)
liam's avatar
liam committed
30 31
  "gsl_cdf_chisq_P" ((x :double) (nu :double))
  :c-return :double
32 33 34
  :documentation			; FDL
  "The cumulative distribution functions
  P(x) for the chi-squared distribution with nu degrees of freedom.")
liam's avatar
liam committed
35

liam's avatar
liam committed
36
(defmfun chi-squared-Q (x nu)
liam's avatar
liam committed
37 38
  "gsl_cdf_chisq_Q" ((x :double) (nu :double))
  :c-return :double
39 40 41
  :documentation			; FDL
  "The cumulative distribution functions
  Q(x) for the chi-squared distribution with nu degrees of freedom.")
liam's avatar
liam committed
42

liam's avatar
liam committed
43
(defmfun chi-squared-Pinv (P nu)
liam's avatar
liam committed
44 45
  "gsl_cdf_chisq_Pinv" ((P :double) (nu :double))
  :c-return :double
46 47 48
  :documentation			; FDL
  "The inverse cumulative distribution functions
  P(x) for the chi-squared distribution with nu degrees of freedom.")
liam's avatar
liam committed
49

liam's avatar
liam committed
50
(defmfun chi-squared-Qinv (Q nu)
liam's avatar
liam committed
51 52
  "gsl_cdf_chisq_Qinv" ((Q :double) (nu :double))
  :c-return :double
53 54 55
  :documentation 			; FDL
  "The inverse cumulative distribution functions
   Q(x) for the chi-squared distribution with nu degrees of freedom.")
liam's avatar
liam committed
56 57

;;; Examples and unit test
Liam Healy's avatar
Liam Healy committed
58
(save-test chi-squared
Liam Healy's avatar
Liam Healy committed
59
  (let ((rng (make-random-number-generator +mt19937+ 0)))
liam's avatar
liam committed
60 61
      (loop for i from 0 to 10
	    collect
liam's avatar
liam committed
62 63 64 65 66 67 68
	    (chi-squared rng 10.0d0)))
  (chi-squared-pdf 0.5d0 1.0d0)
  (chi-squared-P 0.5d0 1.0d0)
  (chi-squared-Q 0.5d0 1.0d0)
  (chi-squared-Pinv 0.5204998778130463d0 1.0d0)
  (chi-squared-Qinv 0.4795001221869537d0 1.0d0))

Liam Healy's avatar
Liam Healy committed
69

liam's avatar
liam committed
70