3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 James Theiler, Brian Gough
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <gsl/gsl_math.h>
23 #include <gsl/gsl_sf_gamma.h>
24 #include <gsl/gsl_rng.h>
25 #include <gsl/gsl_randist.h>
27 static double gamma_large (const gsl_rng * r, const double a);
28 static double gamma_frac (const gsl_rng * r, const double a);
30 /* The Gamma distribution of order a>0 is defined by:
32 p(x) dx = {1 / \Gamma(a) b^a } x^{a-1} e^{-x/b} dx
34 for x>0. If X and Y are independent gamma-distributed random
35 variables of order a1 and a2 with the same scale parameter b, then
36 X+Y has gamma distribution of order a1+a2.
38 The algorithms below are from Knuth, vol 2, 2nd ed, p. 129. */
41 gsl_ran_gamma_knuth (const gsl_rng * r, const double a, const double b)
44 unsigned int na = floor (a);
48 return b * gsl_ran_gamma_int (r, na);
52 return b * gamma_frac (r, a);
56 return b * (gsl_ran_gamma_int (r, na) + gamma_frac (r, a - na)) ;
61 gsl_ran_gamma_int (const gsl_rng * r, const unsigned int a)
68 for (i = 0; i < a; i++)
70 prod *= gsl_rng_uniform_pos (r);
73 /* Note: for 12 iterations we are safe against underflow, since
74 the smallest positive random number is O(2^-32). This means
75 the smallest possible product is 2^(-12*32) = 10^-116 which
76 is within the range of double precision. */
82 return gamma_large (r, (double) a);
87 gamma_large (const gsl_rng * r, const double a)
89 /* Works only if a > 1, and is most efficient if a is large
91 This algorithm, reported in Knuth, is attributed to Ahrens. A
92 faster one, we are told, can be found in: J. H. Ahrens and
93 U. Dieter, Computing 12 (1974) 223-246. */
96 sqa = sqrt (2 * a - 1);
101 y = tan (M_PI * gsl_rng_uniform (r));
105 v = gsl_rng_uniform (r);
107 while (v > (1 + y * y) * exp ((a - 1) * log (x / (a - 1)) - sqa * y));
113 gamma_frac (const gsl_rng * r, const double a)
115 /* This is exercise 16 from Knuth; see page 135, and the solution is
118 double p, q, x, u, v;
122 u = gsl_rng_uniform (r);
123 v = gsl_rng_uniform_pos (r);
127 x = exp ((1 / a) * log (v));
133 q = exp ((a - 1) * log (x));
136 while (gsl_rng_uniform (r) >= q);
142 gsl_ran_gamma_pdf (const double x, const double a, const double b)
162 double lngamma = gsl_sf_lngamma (a);
163 p = exp ((a - 1) * log (x/b) - x/b - lngamma)/b;
169 /* New version based on Marsaglia and Tsang, "A Simple Method for
170 * generating gamma variables", ACM Transactions on Mathematical
171 * Software, Vol 26, No 3 (2000), p363-372.
173 * Implemented by J.D.Lamb@btinternet.com, minor modifications for GSL
178 gsl_ran_gamma_mt (const gsl_rng * r, const double a, const double b)
180 return gsl_ran_gamma (r, a, b);
184 gsl_ran_gamma (const gsl_rng * r, const double a, const double b)
190 double u = gsl_rng_uniform_pos (r);
191 return gsl_ran_gamma (r, 1.0 + a, b) * pow (u, 1.0 / a);
196 double d = a - 1.0 / 3.0;
197 double c = (1.0 / 3.0) / sqrt (d);
203 x = gsl_ran_gaussian_ziggurat (r, 1.0);
209 u = gsl_rng_uniform_pos (r);
211 if (u < 1 - 0.0331 * x * x * x * x)
214 if (log (u) < 0.5 * x * x + d * (1 - v + log (v)))