Chris@16
|
1 /* boost random/gamma_distribution.hpp header file
|
Chris@16
|
2 *
|
Chris@16
|
3 * Copyright Jens Maurer 2002
|
Chris@16
|
4 * Copyright Steven Watanabe 2010
|
Chris@16
|
5 * Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
6 * accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
7 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8 *
|
Chris@16
|
9 * See http://www.boost.org for most recent version including documentation.
|
Chris@16
|
10 *
|
Chris@101
|
11 * $Id$
|
Chris@16
|
12 *
|
Chris@16
|
13 */
|
Chris@16
|
14
|
Chris@16
|
15 #ifndef BOOST_RANDOM_GAMMA_DISTRIBUTION_HPP
|
Chris@16
|
16 #define BOOST_RANDOM_GAMMA_DISTRIBUTION_HPP
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/config/no_tr1/cmath.hpp>
|
Chris@16
|
19 #include <istream>
|
Chris@16
|
20 #include <iosfwd>
|
Chris@16
|
21 #include <boost/assert.hpp>
|
Chris@16
|
22 #include <boost/limits.hpp>
|
Chris@16
|
23 #include <boost/static_assert.hpp>
|
Chris@16
|
24 #include <boost/random/detail/config.hpp>
|
Chris@16
|
25 #include <boost/random/exponential_distribution.hpp>
|
Chris@16
|
26
|
Chris@16
|
27 namespace boost {
|
Chris@16
|
28 namespace random {
|
Chris@16
|
29
|
Chris@16
|
30 // The algorithm is taken from Knuth
|
Chris@16
|
31
|
Chris@16
|
32 /**
|
Chris@16
|
33 * The gamma distribution is a continuous distribution with two
|
Chris@16
|
34 * parameters alpha and beta. It produces values > 0.
|
Chris@16
|
35 *
|
Chris@16
|
36 * It has
|
Chris@16
|
37 * \f$\displaystyle p(x) = x^{\alpha-1}\frac{e^{-x/\beta}}{\beta^\alpha\Gamma(\alpha)}\f$.
|
Chris@16
|
38 */
|
Chris@16
|
39 template<class RealType = double>
|
Chris@16
|
40 class gamma_distribution
|
Chris@16
|
41 {
|
Chris@16
|
42 public:
|
Chris@16
|
43 typedef RealType input_type;
|
Chris@16
|
44 typedef RealType result_type;
|
Chris@16
|
45
|
Chris@16
|
46 class param_type
|
Chris@16
|
47 {
|
Chris@16
|
48 public:
|
Chris@16
|
49 typedef gamma_distribution distribution_type;
|
Chris@16
|
50
|
Chris@16
|
51 /**
|
Chris@16
|
52 * Constructs a @c param_type object from the "alpha" and "beta"
|
Chris@16
|
53 * parameters.
|
Chris@16
|
54 *
|
Chris@16
|
55 * Requires: alpha > 0 && beta > 0
|
Chris@16
|
56 */
|
Chris@16
|
57 param_type(const RealType& alpha_arg = RealType(1.0),
|
Chris@16
|
58 const RealType& beta_arg = RealType(1.0))
|
Chris@16
|
59 : _alpha(alpha_arg), _beta(beta_arg)
|
Chris@16
|
60 {
|
Chris@16
|
61 }
|
Chris@16
|
62
|
Chris@16
|
63 /** Returns the "alpha" parameter of the distribution. */
|
Chris@16
|
64 RealType alpha() const { return _alpha; }
|
Chris@16
|
65 /** Returns the "beta" parameter of the distribution. */
|
Chris@16
|
66 RealType beta() const { return _beta; }
|
Chris@16
|
67
|
Chris@16
|
68 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
|
Chris@16
|
69 /** Writes the parameters to a @c std::ostream. */
|
Chris@16
|
70 template<class CharT, class Traits>
|
Chris@16
|
71 friend std::basic_ostream<CharT, Traits>&
|
Chris@16
|
72 operator<<(std::basic_ostream<CharT, Traits>& os,
|
Chris@16
|
73 const param_type& parm)
|
Chris@16
|
74 {
|
Chris@16
|
75 os << parm._alpha << ' ' << parm._beta;
|
Chris@16
|
76 return os;
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@16
|
79 /** Reads the parameters from a @c std::istream. */
|
Chris@16
|
80 template<class CharT, class Traits>
|
Chris@16
|
81 friend std::basic_istream<CharT, Traits>&
|
Chris@16
|
82 operator>>(std::basic_istream<CharT, Traits>& is, param_type& parm)
|
Chris@16
|
83 {
|
Chris@16
|
84 is >> parm._alpha >> std::ws >> parm._beta;
|
Chris@16
|
85 return is;
|
Chris@16
|
86 }
|
Chris@16
|
87 #endif
|
Chris@16
|
88
|
Chris@16
|
89 /** Returns true if the two sets of parameters are the same. */
|
Chris@16
|
90 friend bool operator==(const param_type& lhs, const param_type& rhs)
|
Chris@16
|
91 {
|
Chris@16
|
92 return lhs._alpha == rhs._alpha && lhs._beta == rhs._beta;
|
Chris@16
|
93 }
|
Chris@16
|
94 /** Returns true if the two sets fo parameters are different. */
|
Chris@16
|
95 friend bool operator!=(const param_type& lhs, const param_type& rhs)
|
Chris@16
|
96 {
|
Chris@16
|
97 return !(lhs == rhs);
|
Chris@16
|
98 }
|
Chris@16
|
99 private:
|
Chris@16
|
100 RealType _alpha;
|
Chris@16
|
101 RealType _beta;
|
Chris@16
|
102 };
|
Chris@16
|
103
|
Chris@16
|
104 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
|
Chris@16
|
105 BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
|
Chris@16
|
106 #endif
|
Chris@16
|
107
|
Chris@16
|
108 /**
|
Chris@16
|
109 * Creates a new gamma_distribution with parameters "alpha" and "beta".
|
Chris@16
|
110 *
|
Chris@16
|
111 * Requires: alpha > 0 && beta > 0
|
Chris@16
|
112 */
|
Chris@16
|
113 explicit gamma_distribution(const result_type& alpha_arg = result_type(1.0),
|
Chris@16
|
114 const result_type& beta_arg = result_type(1.0))
|
Chris@16
|
115 : _exp(result_type(1)), _alpha(alpha_arg), _beta(beta_arg)
|
Chris@16
|
116 {
|
Chris@16
|
117 BOOST_ASSERT(_alpha > result_type(0));
|
Chris@16
|
118 BOOST_ASSERT(_beta > result_type(0));
|
Chris@16
|
119 init();
|
Chris@16
|
120 }
|
Chris@16
|
121
|
Chris@16
|
122 /** Constructs a @c gamma_distribution from its parameters. */
|
Chris@16
|
123 explicit gamma_distribution(const param_type& parm)
|
Chris@16
|
124 : _exp(result_type(1)), _alpha(parm.alpha()), _beta(parm.beta())
|
Chris@16
|
125 {
|
Chris@16
|
126 init();
|
Chris@16
|
127 }
|
Chris@16
|
128
|
Chris@16
|
129 // compiler-generated copy ctor and assignment operator are fine
|
Chris@16
|
130
|
Chris@16
|
131 /** Returns the "alpha" paramter of the distribution. */
|
Chris@16
|
132 RealType alpha() const { return _alpha; }
|
Chris@16
|
133 /** Returns the "beta" parameter of the distribution. */
|
Chris@16
|
134 RealType beta() const { return _beta; }
|
Chris@16
|
135 /** Returns the smallest value that the distribution can produce. */
|
Chris@16
|
136 RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; }
|
Chris@16
|
137 /* Returns the largest value that the distribution can produce. */
|
Chris@16
|
138 RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
|
Chris@16
|
139 { return (std::numeric_limits<RealType>::infinity)(); }
|
Chris@16
|
140
|
Chris@16
|
141 /** Returns the parameters of the distribution. */
|
Chris@16
|
142 param_type param() const { return param_type(_alpha, _beta); }
|
Chris@16
|
143 /** Sets the parameters of the distribution. */
|
Chris@16
|
144 void param(const param_type& parm)
|
Chris@16
|
145 {
|
Chris@16
|
146 _alpha = parm.alpha();
|
Chris@16
|
147 _beta = parm.beta();
|
Chris@16
|
148 init();
|
Chris@16
|
149 }
|
Chris@16
|
150
|
Chris@16
|
151 /**
|
Chris@16
|
152 * Effects: Subsequent uses of the distribution do not depend
|
Chris@16
|
153 * on values produced by any engine prior to invoking reset.
|
Chris@16
|
154 */
|
Chris@16
|
155 void reset() { _exp.reset(); }
|
Chris@16
|
156
|
Chris@16
|
157 /**
|
Chris@16
|
158 * Returns a random variate distributed according to
|
Chris@16
|
159 * the gamma distribution.
|
Chris@16
|
160 */
|
Chris@16
|
161 template<class Engine>
|
Chris@16
|
162 result_type operator()(Engine& eng)
|
Chris@16
|
163 {
|
Chris@16
|
164 #ifndef BOOST_NO_STDC_NAMESPACE
|
Chris@16
|
165 // allow for Koenig lookup
|
Chris@16
|
166 using std::tan; using std::sqrt; using std::exp; using std::log;
|
Chris@16
|
167 using std::pow;
|
Chris@16
|
168 #endif
|
Chris@16
|
169 if(_alpha == result_type(1)) {
|
Chris@16
|
170 return _exp(eng) * _beta;
|
Chris@16
|
171 } else if(_alpha > result_type(1)) {
|
Chris@16
|
172 // Can we have a boost::mathconst please?
|
Chris@16
|
173 const result_type pi = result_type(3.14159265358979323846);
|
Chris@16
|
174 for(;;) {
|
Chris@16
|
175 result_type y = tan(pi * uniform_01<RealType>()(eng));
|
Chris@16
|
176 result_type x = sqrt(result_type(2)*_alpha-result_type(1))*y
|
Chris@16
|
177 + _alpha-result_type(1);
|
Chris@16
|
178 if(x <= result_type(0))
|
Chris@16
|
179 continue;
|
Chris@16
|
180 if(uniform_01<RealType>()(eng) >
|
Chris@16
|
181 (result_type(1)+y*y) * exp((_alpha-result_type(1))
|
Chris@16
|
182 *log(x/(_alpha-result_type(1)))
|
Chris@16
|
183 - sqrt(result_type(2)*_alpha
|
Chris@16
|
184 -result_type(1))*y))
|
Chris@16
|
185 continue;
|
Chris@16
|
186 return x * _beta;
|
Chris@16
|
187 }
|
Chris@16
|
188 } else /* alpha < 1.0 */ {
|
Chris@16
|
189 for(;;) {
|
Chris@16
|
190 result_type u = uniform_01<RealType>()(eng);
|
Chris@16
|
191 result_type y = _exp(eng);
|
Chris@16
|
192 result_type x, q;
|
Chris@16
|
193 if(u < _p) {
|
Chris@16
|
194 x = exp(-y/_alpha);
|
Chris@16
|
195 q = _p*exp(-x);
|
Chris@16
|
196 } else {
|
Chris@16
|
197 x = result_type(1)+y;
|
Chris@16
|
198 q = _p + (result_type(1)-_p) * pow(x,_alpha-result_type(1));
|
Chris@16
|
199 }
|
Chris@16
|
200 if(u >= q)
|
Chris@16
|
201 continue;
|
Chris@16
|
202 return x * _beta;
|
Chris@16
|
203 }
|
Chris@16
|
204 }
|
Chris@16
|
205 }
|
Chris@16
|
206
|
Chris@16
|
207 template<class URNG>
|
Chris@16
|
208 RealType operator()(URNG& urng, const param_type& parm) const
|
Chris@16
|
209 {
|
Chris@16
|
210 return gamma_distribution(parm)(urng);
|
Chris@16
|
211 }
|
Chris@16
|
212
|
Chris@16
|
213 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
|
Chris@16
|
214 /** Writes a @c gamma_distribution to a @c std::ostream. */
|
Chris@16
|
215 template<class CharT, class Traits>
|
Chris@16
|
216 friend std::basic_ostream<CharT,Traits>&
|
Chris@16
|
217 operator<<(std::basic_ostream<CharT,Traits>& os,
|
Chris@16
|
218 const gamma_distribution& gd)
|
Chris@16
|
219 {
|
Chris@16
|
220 os << gd.param();
|
Chris@16
|
221 return os;
|
Chris@16
|
222 }
|
Chris@16
|
223
|
Chris@16
|
224 /** Reads a @c gamma_distribution from a @c std::istream. */
|
Chris@16
|
225 template<class CharT, class Traits>
|
Chris@16
|
226 friend std::basic_istream<CharT,Traits>&
|
Chris@16
|
227 operator>>(std::basic_istream<CharT,Traits>& is, gamma_distribution& gd)
|
Chris@16
|
228 {
|
Chris@16
|
229 gd.read(is);
|
Chris@16
|
230 return is;
|
Chris@16
|
231 }
|
Chris@16
|
232 #endif
|
Chris@16
|
233
|
Chris@16
|
234 /**
|
Chris@16
|
235 * Returns true if the two distributions will produce identical
|
Chris@16
|
236 * sequences of random variates given equal generators.
|
Chris@16
|
237 */
|
Chris@16
|
238 friend bool operator==(const gamma_distribution& lhs,
|
Chris@16
|
239 const gamma_distribution& rhs)
|
Chris@16
|
240 {
|
Chris@16
|
241 return lhs._alpha == rhs._alpha
|
Chris@16
|
242 && lhs._beta == rhs._beta
|
Chris@16
|
243 && lhs._exp == rhs._exp;
|
Chris@16
|
244 }
|
Chris@16
|
245
|
Chris@16
|
246 /**
|
Chris@16
|
247 * Returns true if the two distributions can produce different
|
Chris@16
|
248 * sequences of random variates, given equal generators.
|
Chris@16
|
249 */
|
Chris@16
|
250 friend bool operator!=(const gamma_distribution& lhs,
|
Chris@16
|
251 const gamma_distribution& rhs)
|
Chris@16
|
252 {
|
Chris@16
|
253 return !(lhs == rhs);
|
Chris@16
|
254 }
|
Chris@16
|
255
|
Chris@16
|
256 private:
|
Chris@16
|
257 /// \cond hide_private_members
|
Chris@16
|
258
|
Chris@16
|
259 template<class CharT, class Traits>
|
Chris@16
|
260 void read(std::basic_istream<CharT, Traits>& is)
|
Chris@16
|
261 {
|
Chris@16
|
262 param_type parm;
|
Chris@16
|
263 if(is >> parm) {
|
Chris@16
|
264 param(parm);
|
Chris@16
|
265 }
|
Chris@16
|
266 }
|
Chris@16
|
267
|
Chris@16
|
268 void init()
|
Chris@16
|
269 {
|
Chris@16
|
270 #ifndef BOOST_NO_STDC_NAMESPACE
|
Chris@16
|
271 // allow for Koenig lookup
|
Chris@16
|
272 using std::exp;
|
Chris@16
|
273 #endif
|
Chris@16
|
274 _p = exp(result_type(1)) / (_alpha + exp(result_type(1)));
|
Chris@16
|
275 }
|
Chris@16
|
276 /// \endcond
|
Chris@16
|
277
|
Chris@16
|
278 exponential_distribution<RealType> _exp;
|
Chris@16
|
279 result_type _alpha;
|
Chris@16
|
280 result_type _beta;
|
Chris@16
|
281 // some data precomputed from the parameters
|
Chris@16
|
282 result_type _p;
|
Chris@16
|
283 };
|
Chris@16
|
284
|
Chris@16
|
285
|
Chris@16
|
286 } // namespace random
|
Chris@16
|
287
|
Chris@16
|
288 using random::gamma_distribution;
|
Chris@16
|
289
|
Chris@16
|
290 } // namespace boost
|
Chris@16
|
291
|
Chris@16
|
292 #endif // BOOST_RANDOM_GAMMA_DISTRIBUTION_HPP
|