public
Description: Random number generation for common lisp
Homepage: http://code.google.com/p/cl-randist/
Clone URL: git://github.com/lvaruzza/cl-randist.git
cl-randist / chisq.lisp
100644 29 lines (18 sloc) 0.697 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
(in-package :randist)
 
(declaim (optimize (speed 3) (debug 0) (safety 1)))
 
;; /* The chisq distribution has the form
 
;; p(x) dx = (1/(2*Gamma(nu/2))) (x/2)^(nu/2 - 1) exp(-x/2) dx
 
;; for x = 0 ... +infty */
 
;; double
;; gsl_ran_chisq (const gsl_rng * r, const double nu)
;; {
;; double chisq = 2 * gsl_ran_gamma (r, nu / 2, 1.0);
;; return chisq;
;; }
 
 
(declaim (ftype (function (double-float) double-float) random-chi-square)
(inline random-chi-square))
 
(defun random-chi-square (nu)
  "Generate random variable for chi square distribution:
 
p(x) dx = (1/(2*Gamma(nu/2))) (x/2)^(nu/2 - 1) exp(-x/2) dx"
  
  (declare (type double-float nu))
  (* 2d0 (random-gamma (/ nu 2d0))))