ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsl_ran_gamma.cpp
Go to the documentation of this file.
1 
5 #include <admodel.h>
6 
17 double rgamma(double alpha, random_number_generator& rng)
18 {
19  double b=1.0;
20  double a=alpha;
21 
22  if (a<1)
23  {
24  double u = randu(rng);
25  return rgamma (1.0 + a,rng) * pow (u, 1.0 / a);
26  }
27 
28  double x, v, u;
29  double d = a - 1.0 / 3.0;
30  double c = (1.0 / 3.0) / sqrt (d);
31 
32  while (1)
33  {
34  do
35  {
36  x = randn(rng);
37  v = 1.0 + c * x;
38  } while (v<0);
39 
40  v = v * v * v;
41  u = randu(rng);
42 
43  if (u < (1 - 0.0331 * x * x * x * x))
44  break;
45 
46  if (log (u) < (0.5 * x * x + d * (1 - v + log (v))))
47  break;
48  }
49 
50  return b * d * v;
51 }
#define x
double randu(const random_number_generator &rng)
Uniform random number generator.
Definition: rngen.cpp:198
Description not yet available.
Definition: fvar.hpp:7951
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
Description not yet available.
double randn(const random_number_generator &rng)
Normal number generator.
Definition: rngen.cpp:183
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13
d3_array pow(const d3_array &m, int e)
Description not yet available.
Definition: d3arr6.cpp:17
double rgamma(double alpha, random_number_generator &rng)
Copyright (c) 2016 ADMB Foundation.