NEST  2.6.0,not_revisioned_source_dir@0
gamma_randomdev.h
Go to the documentation of this file.
1 /*
2  * gamma_randomdev.h
3  *
4  * This file is part of NEST.
5  *
6  * Copyright (C) 2004 The NEST Initiative
7  *
8  * NEST is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * NEST is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef GAMMA_RANDOMDEV_H
24 #define GAMMA_RANDOMDEV_H
25 
26 #include <cmath>
27 #include "randomgen.h"
28 #include "randomdev.h"
29 #include "lockptr.h"
30 
31 /************************************************************/
32 /* Class GammaRNG */
33 /* */
34 /* Generates an RNG which returns gamma(x; a) distributed */
35 /* random numbers out of an RNG which returns uniformly */
36 /* distributed random numbers: */
37 /* */
38 /* gamma(x; a) = x^(a-1) * exp(-x) / Gamma(a) */
39 /* */
40 /* gamma(x; a, b) distributed random number are obtained by */
41 /* scaling: if X ~ gamma(x; a), then b*X ~ gamma(x; a, b). */
42 /* */
43 /* Arguments: */
44 /* - pointer to an RNG */
45 /* - parameters a (optional, default = 1) */
46 /* */
47 /* Algorithm: */
48 /* a < 1 : Johnk's algorithm [1, p. 418] */
49 /* a = 1 : direct transformation (exponential distribution)*/
50 /* a > 1 : Best's algorithm [1, p. 410] */
51 /* */
52 /* Verification: */
53 /* Kolmogorov-Smirnov test at p=0.01 passed for sample */
54 /* size 10,000 and orders 0.01 - 25 (MATLAB Stats Toolbox) */
55 /* */
56 /* Author: */
57 /* Hans Ekkehard Plesser */
58 /* */
59 /* References: */
60 /* [0] always reserved for Stroustrup */
61 /* [1] L. Devroye, "Non-Uniform Random Variate Generation",*/
62 /* Springer, 1986 */
63 /* */
64 /************************************************************/
65 
66 namespace librandom {
67 
68 /*BeginDocumentation
69 Name: rdevdict::gamma - gamma random deviate generator
70 Description:
71  Generates gamma-distributed random numbers.
72 
73  gamma(x; order, b) = x^(order-1) * exp(-x/b) / b^order Gamma(order) , x >= 0
74 
75 Parameters:
76  order - order of the gamma distribution (default: 1)
77  b - scale parameter (default: 1)
78 
79 SeeAlso: CreateRDV, RandomArray, rdevdict
80 Author: Hans Ekkehard Plesser
81 */
82 
89  class GammaRandomDev : public RandomDev
90  {
91 
92  public:
93 
94  // accept only lockPTRs for initialization,
95  // otherwise creation of a lock ptr would
96  // occur as side effect---might be unhealthy
97  GammaRandomDev(RngPtr, double a_in = 1.0);
98  GammaRandomDev(double a_in = 1.0);
99 
100  void set_order(double);
101  void set_scale(double);
102 
104  void set_status(const DictionaryDatum&);
105 
107  void get_status(DictionaryDatum&) const;
108 
109  using RandomDev::operator();
110  double operator()(RngPtr) const;
111  double operator()(RngPtr, double);
112 
113  private:
114  double unscaled_gamma(RngPtr r) const;
115 
116  double a; // gamma density order
117  double b_; // gamma scale parameter
118 
119  double bb; // parameters b, c of Best's algorithm
120  double bc;
121  double ju; // exponents of U, V of Johnk's algorithm
122  double jv;
123  };
124 
125  inline
126  double GammaRandomDev::operator()(RngPtr rthrd, double a)
127  {
128  set_order(a);
129  return (*this)(rthrd);
130  }
131 
132  inline
134  {
135  return b_ * unscaled_gamma(r);
136  }
137 
138  inline
139  void GammaRandomDev::set_order(double a_in=1.0)
140  {
141  assert(a_in > 0);
142 
143  a = a_in;
144 
145  bb = a-1.0;
146  bc = 3.0*(a-0.25);
147 
148  ju = 1.0 / a;
149  jv = a != 1 ? 1.0 / (1-a) : 0;
150  }
151 
152 }
153 
154 # endif
155 
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
170 
171 
172 
Class GammaRandomDev Create gamma distributed random numbers.
Definition: gamma_randomdev.h:89
virtual double operator()(void)
Operator delivering doubles.
Definition: randomdev.h:199
void set_status(const DictionaryDatum &)
set distribution parameters from SLI dict
Definition: gamma_randomdev.cpp:89
double b_
Definition: gamma_randomdev.h:117
double jv
Definition: gamma_randomdev.h:122
const Name a("a")
Specific to Brette & Gerstner 2005 (aeif_cond-*)
Definition: nest_names.h:41
assert(pNet!=0)
double a
worker function creating Gamma(x; order, 1) number
Definition: gamma_randomdev.h:116
GammaRandomDev(RngPtr, double a_in=1.0)
create with fixed RNG
Definition: gamma_randomdev.cpp:30
void set_order(double)
set order
Definition: gamma_randomdev.h:139
void set_scale(double)
set scale parameter
double bc
Definition: gamma_randomdev.h:120
Abstract base class for access to non-uniform random deviate generators.
Definition: randomdev.h:131
double ju
Definition: gamma_randomdev.h:121
double bb
Definition: gamma_randomdev.h:119
void get_status(DictionaryDatum &) const
get distribution parameters from SLI dict
Definition: gamma_randomdev.cpp:107
double unscaled_gamma(RngPtr r) const
Definition: gamma_randomdev.cpp:42