0
(inline random-gamma1))
0
(defun random-gamma1 (a b)
0
+"The Gamma distribution of order a>0 is defined by:
0
+ p(x) dx = {1 / \Gamma(a) b^a } x^{a-1} e^{-x/b} dx
0
+ for x>0. If X and Y are independent gamma-distributed random
0
+ variables of order a1 and a2 with the same scale parameter b, then
0
+ X+Y has gamma distribution of order a1+a2.
0
+ The algorithms below are from Knuth, vol 2, 2nd ed, p. 129.
0
+ Works only if a > 1, and is most efficient if a is large
0
+ This algorithm, reported in Knuth, is attributed to Ahrens. A
0
+ faster one, we are told, can be found in: J. H. Ahrens and
0
+ U. Dieter, Computing 12 (1974) 223-246."
0
(declare (double-float a b))
0
(multiple-value-bind (na frac) (truncate a)
0
(declaim (ftype (function (double-float double-float) double-float)
0
(defun random-gamma-mt (a b)
0
+"New version based on Marsaglia and Tsang, 'A Simple Method for
0
+generating gamma variables', ACM Transactions on Mathematical
0
+Software, Vol 26, No 3 (2000), p363-372."
0
(declare (double-float a b))
0
(* (random-gamma-mt (+ 1d0 a) b) (expt (random-uniform) (/ a)))
0
(declaim (inline random-gamma))
0
(defun random-gamma (a &optional (b 1d0))
0
+ "[syntax suggar] Generate a random variable with gamma distribution using the MT method (see random-gamma-mt)"
Comments
No one has commented yet.