1 | #ifndef _theplu_yat_random_ |
---|
2 | #define _theplu_yat_random_ |
---|
3 | |
---|
4 | // $Id: random.h 1486 2008-09-09 21:17:19Z jari $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2005, 2006, 2007 Jari Häkkinen, Peter Johansson |
---|
8 | Copyright (C) 2008 Peter Johansson |
---|
9 | |
---|
10 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
11 | |
---|
12 | The yat library is free software; you can redistribute it and/or |
---|
13 | modify it under the terms of the GNU General Public License as |
---|
14 | published by the Free Software Foundation; either version 3 of the |
---|
15 | License, or (at your option) any later version. |
---|
16 | |
---|
17 | The yat library is distributed in the hope that it will be useful, |
---|
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
20 | General Public License for more details. |
---|
21 | |
---|
22 | You should have received a copy of the GNU General Public License |
---|
23 | along with this program; if not, write to the Free Software |
---|
24 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
25 | 02111-1307, USA. |
---|
26 | */ |
---|
27 | |
---|
28 | #include "yat/statistics/Histogram.h" |
---|
29 | |
---|
30 | #include <gsl/gsl_rng.h> |
---|
31 | #include <gsl/gsl_randist.h> |
---|
32 | |
---|
33 | #include <algorithm> |
---|
34 | #include <string> |
---|
35 | |
---|
36 | namespace theplu { |
---|
37 | namespace yat { |
---|
38 | namespace random { |
---|
39 | |
---|
40 | //forward declarion |
---|
41 | class RNG_state; |
---|
42 | |
---|
43 | /// |
---|
44 | /// @brief Random Number Generator |
---|
45 | /// |
---|
46 | /// The RNG class is wrapper to the GSL random number generator |
---|
47 | /// (rng). This class provides a single global instance of the rng, |
---|
48 | /// and makes sure there is only one point of access to the |
---|
49 | /// generator. |
---|
50 | /// |
---|
51 | /// There is information about how to change seeding and generators |
---|
52 | /// at run time without recompilation using environment variables in |
---|
53 | /// the GSL manual (Chapter on random number generators). RNG of |
---|
54 | /// course support seeding at compile time if you don't want to |
---|
55 | /// bother about environment variables and GSL. |
---|
56 | /// |
---|
57 | /// There are many different rng's available in GSL. Currently only |
---|
58 | /// the default generator is implemented and no other one is |
---|
59 | /// choosable through the class interface. This means that you have |
---|
60 | /// to fall back to the use of environment variables as described in |
---|
61 | /// the GSL documentation, or be bold and request support for other |
---|
62 | /// rng's through the class interface. |
---|
63 | /// |
---|
64 | /// Not all GSL functionality is implemented, we'll add |
---|
65 | /// functionality when needed and may do it when requested. Better |
---|
66 | /// yet, supply us with code and we will probably add it to the code |
---|
67 | /// (BUT remember to implement reasonable tests for your code and |
---|
68 | /// follow the coding style.) |
---|
69 | /// |
---|
70 | /// The current implementation is NOT thread safe since the RNG is |
---|
71 | /// implemented as a singleton. However, the underlying GSL rng's |
---|
72 | /// support thread safety since each instance of GSL rng's keep |
---|
73 | /// track of their own state accordning to GSL documentation. |
---|
74 | /// |
---|
75 | /// @see Design Patterns (the singleton and adapter pattern). GSL |
---|
76 | /// documentation. |
---|
77 | /// |
---|
78 | class RNG |
---|
79 | { |
---|
80 | public: |
---|
81 | |
---|
82 | /// |
---|
83 | /// @brief Get an instance of the random number generator. |
---|
84 | /// |
---|
85 | /// Get an instance of the random number generator. If the random |
---|
86 | /// number generator is not already created, the call will create |
---|
87 | /// a new generator and use the default seed. The seed must be |
---|
88 | /// changed with the seed or seed_from_devurandom member |
---|
89 | /// functions. |
---|
90 | /// |
---|
91 | /// @return A pointer to the random number generator. |
---|
92 | /// |
---|
93 | /// @see seed and seed_from_devurandom |
---|
94 | /// |
---|
95 | static RNG* instance(void); |
---|
96 | |
---|
97 | /// |
---|
98 | /// @brief Returns the largest number that the random number |
---|
99 | /// generator can return. |
---|
100 | /// |
---|
101 | unsigned long max(void) const; |
---|
102 | |
---|
103 | /// |
---|
104 | /// @brief Returns the smallest number that the random number |
---|
105 | /// generator can return. |
---|
106 | /// |
---|
107 | unsigned long min(void) const; |
---|
108 | |
---|
109 | /// |
---|
110 | /// @brief Returns the name of the random number generator |
---|
111 | /// |
---|
112 | std::string name(void) const; |
---|
113 | |
---|
114 | /// |
---|
115 | /// @return const pointer to underlying GSL random generator. |
---|
116 | /// |
---|
117 | const gsl_rng* rng(void) const; |
---|
118 | |
---|
119 | /// |
---|
120 | /// @brief Set the seed \a s for the rng. |
---|
121 | /// |
---|
122 | /// Set the seed \a s for the rng. If \a s is zero, a default |
---|
123 | /// value from the rng's original implementation is used (cf. GSL |
---|
124 | /// documentation). |
---|
125 | /// |
---|
126 | /// @see seed_from_devurandom |
---|
127 | /// |
---|
128 | void seed(unsigned long s) const; |
---|
129 | |
---|
130 | /// |
---|
131 | /// @brief Seed the rng using the /dev/urandom device. |
---|
132 | /// |
---|
133 | /// @return The seed acquired from /dev/urandom. |
---|
134 | /// |
---|
135 | unsigned long seed_from_devurandom(void); |
---|
136 | |
---|
137 | /** |
---|
138 | \brief Set the state to \a state. |
---|
139 | |
---|
140 | \return 0 on success, non-zero otherwise. |
---|
141 | |
---|
142 | \see gsl_rng_memcpy |
---|
143 | */ |
---|
144 | int set_state(const RNG_state&); |
---|
145 | |
---|
146 | private: |
---|
147 | RNG(void); |
---|
148 | |
---|
149 | /** |
---|
150 | \brief Not implemented. |
---|
151 | |
---|
152 | This copy contructor is not implemented. The constructor is |
---|
153 | declared in order to avoid compiler generated default copy |
---|
154 | constructor. |
---|
155 | */ |
---|
156 | RNG(const RNG&); |
---|
157 | |
---|
158 | virtual ~RNG(void); |
---|
159 | |
---|
160 | static RNG* instance_; |
---|
161 | gsl_rng* rng_; |
---|
162 | }; |
---|
163 | |
---|
164 | |
---|
165 | /// |
---|
166 | /// @brief Class holding state of a random generator |
---|
167 | /// |
---|
168 | class RNG_state |
---|
169 | { |
---|
170 | public: |
---|
171 | /// |
---|
172 | /// @brief Constructor |
---|
173 | /// |
---|
174 | RNG_state(const RNG*); |
---|
175 | |
---|
176 | /// |
---|
177 | /// @brief Destructor |
---|
178 | /// |
---|
179 | ~RNG_state(void); |
---|
180 | |
---|
181 | /// |
---|
182 | /// @return const pointer to underlying GSL random generator. |
---|
183 | /// |
---|
184 | const gsl_rng* rng(void) const; |
---|
185 | |
---|
186 | private: |
---|
187 | gsl_rng* rng_; |
---|
188 | |
---|
189 | }; |
---|
190 | |
---|
191 | |
---|
192 | // --------------------- Discrete distribtuions --------------------- |
---|
193 | |
---|
194 | /// |
---|
195 | /// @brief Discrete random number distributions. |
---|
196 | /// |
---|
197 | /// Abstract base class for discrete random number |
---|
198 | /// distributions. Given K discrete events with different |
---|
199 | /// probabilities \f$ P[k] \f$, produce a random value k consistent |
---|
200 | /// with its probability. |
---|
201 | /// |
---|
202 | class Discrete |
---|
203 | { |
---|
204 | public: |
---|
205 | /// |
---|
206 | /// @brief Constructor |
---|
207 | /// |
---|
208 | Discrete(void); |
---|
209 | |
---|
210 | /// |
---|
211 | /// @brief The destructor |
---|
212 | /// |
---|
213 | virtual ~Discrete(void); |
---|
214 | |
---|
215 | /// |
---|
216 | /// @brief Set the seed to \a s. |
---|
217 | /// |
---|
218 | /// Set the seed to \a s in the underlying rng. If \a s is zero, a |
---|
219 | /// default value from the rng's original implementation is used |
---|
220 | /// (cf. GSL documentation). |
---|
221 | /// |
---|
222 | /// @see seed_from_devurandom, RNG::seed_from_devurandom, RNG::seed |
---|
223 | /// |
---|
224 | void seed(unsigned long s) const; |
---|
225 | |
---|
226 | /// |
---|
227 | /// @brief Set the seed using the /dev/urandom device. |
---|
228 | /// |
---|
229 | /// @return The seed acquired from /dev/urandom. |
---|
230 | /// |
---|
231 | /// @see seed, RNG::seed_from_devurandom, RNG::seed |
---|
232 | /// |
---|
233 | unsigned long seed_from_devurandom(void); |
---|
234 | |
---|
235 | /// |
---|
236 | /// @return A random number. |
---|
237 | /// |
---|
238 | virtual unsigned long operator()(void) const = 0; |
---|
239 | |
---|
240 | protected: |
---|
241 | /// GSL random gererator |
---|
242 | RNG* rng_; |
---|
243 | }; |
---|
244 | |
---|
245 | /// |
---|
246 | /// @brief General |
---|
247 | /// |
---|
248 | class DiscreteGeneral : public Discrete |
---|
249 | { |
---|
250 | public: |
---|
251 | /// |
---|
252 | /// @brief Constructor |
---|
253 | /// |
---|
254 | /// @param hist is a Histogram defining the probability distribution |
---|
255 | /// |
---|
256 | DiscreteGeneral(const statistics::Histogram& hist); |
---|
257 | |
---|
258 | /// |
---|
259 | /// @brief Destructor |
---|
260 | /// |
---|
261 | ~DiscreteGeneral(void); |
---|
262 | |
---|
263 | /// |
---|
264 | /// The generated number is an integer and proportinal to the |
---|
265 | /// frequency in the corresponding histogram bin. In other words, |
---|
266 | /// the probability that 0 is returned is proportinal to the size |
---|
267 | /// of the first bin. |
---|
268 | /// |
---|
269 | /// @return A random number. |
---|
270 | /// |
---|
271 | unsigned long operator()(void) const; |
---|
272 | |
---|
273 | private: |
---|
274 | gsl_ran_discrete_t* gen_; |
---|
275 | }; |
---|
276 | |
---|
277 | /** |
---|
278 | @brief Discrete uniform distribution |
---|
279 | |
---|
280 | Discrete uniform distribution also known as the "equally likely |
---|
281 | outcomes" distribution. Each outcome, in this case an integer |
---|
282 | from [0,n-1] , have equal probability to occur. |
---|
283 | |
---|
284 | Distribution function \f$ p(k) = \frac{1}{n+1} \f$ for \f$ 0 \le |
---|
285 | k < n \f$ \n |
---|
286 | Expectation value: \f$ \frac{n-1}{2} \f$ \n |
---|
287 | Variance: \f$ \frac{1}{12}(n-1)(n+1) \f$ |
---|
288 | */ |
---|
289 | class DiscreteUniform : public Discrete |
---|
290 | { |
---|
291 | public: |
---|
292 | /** |
---|
293 | \brief Constructor. |
---|
294 | |
---|
295 | The generator will generate integers within the range \f$ |
---|
296 | [0,n-1] \f$. If \a n is zero, then the whole range of the |
---|
297 | underlying RNG will be used \f$ [min,max] \f$. Setting \a n to |
---|
298 | zero is the preferred way to sample the whole range of the |
---|
299 | underlying RNG, i.e. not setting \n to RNG.max. |
---|
300 | |
---|
301 | \throw If \a n is larger than the maximum number the underlying |
---|
302 | random number generator can return, then a GSL_error exception |
---|
303 | is thrown. |
---|
304 | */ |
---|
305 | DiscreteUniform(unsigned long n=0); |
---|
306 | |
---|
307 | /** |
---|
308 | \brief Get a random number |
---|
309 | |
---|
310 | The returned integer is either in the range [RNG.min,RNG.max] |
---|
311 | or [0,n-1] depending on how the random number generator was |
---|
312 | created. |
---|
313 | |
---|
314 | \see DiscreteUniform(const unsigned long n=0) |
---|
315 | */ |
---|
316 | unsigned long operator()(void) const; |
---|
317 | |
---|
318 | /** |
---|
319 | \brief Get a random integer in the range \f$ [0,n-1] \f$. |
---|
320 | |
---|
321 | All integers in the range [0,n-1] are equally likely. This |
---|
322 | function should be avoided for sampling the whole range of the |
---|
323 | underlying RNG. |
---|
324 | |
---|
325 | \throw GSL_error if \a n is larger than the range of the |
---|
326 | underlying generator. |
---|
327 | */ |
---|
328 | unsigned long operator()(unsigned long n) const; |
---|
329 | |
---|
330 | private: |
---|
331 | unsigned long range_; |
---|
332 | }; |
---|
333 | |
---|
334 | /** |
---|
335 | @brief Poisson Distribution |
---|
336 | |
---|
337 | Having a Poisson process (i.e. no memory), number of occurences |
---|
338 | within a given time window is Poisson distributed. This |
---|
339 | distribution is the limit of a Binomial distribution when number |
---|
340 | of attempts is large, and the probability for one attempt to be |
---|
341 | succesful is small (in such a way that the expected number of |
---|
342 | succesful attempts is \f$ m \f$. |
---|
343 | |
---|
344 | Probability function \f$ p(k) = e^{-m}\frac{m^k}{k!} \f$ for \f$ 0 \le |
---|
345 | k \f$ \n |
---|
346 | Expectation value: \f$ m \f$ \n |
---|
347 | Variance: \f$ m \f$ |
---|
348 | */ |
---|
349 | class Poisson : public Discrete |
---|
350 | { |
---|
351 | public: |
---|
352 | /// |
---|
353 | /// @brief Constructor |
---|
354 | /// |
---|
355 | /// @param m is expectation value |
---|
356 | /// |
---|
357 | Poisson(const double m=1); |
---|
358 | |
---|
359 | /// |
---|
360 | /// @return A Poisson distributed number. |
---|
361 | /// |
---|
362 | unsigned long operator()(void) const; |
---|
363 | |
---|
364 | /// |
---|
365 | /// @return A Poisson distributed number with expectation value |
---|
366 | /// \a m |
---|
367 | /// |
---|
368 | /// @note this operator ignores parameters set in Constructor |
---|
369 | /// |
---|
370 | unsigned long operator()(const double m) const; |
---|
371 | |
---|
372 | private: |
---|
373 | double m_; |
---|
374 | }; |
---|
375 | |
---|
376 | // --------------------- Continuous distribtuions --------------------- |
---|
377 | |
---|
378 | /// |
---|
379 | /// @brief Continuous random number distributions. |
---|
380 | /// |
---|
381 | /// Abstract base class for continuous random number distributions. |
---|
382 | /// |
---|
383 | class Continuous |
---|
384 | { |
---|
385 | public: |
---|
386 | |
---|
387 | /// |
---|
388 | /// @brief Constructor |
---|
389 | /// |
---|
390 | Continuous(void); |
---|
391 | |
---|
392 | /// |
---|
393 | /// @brief The destructor |
---|
394 | /// |
---|
395 | virtual ~Continuous(void); |
---|
396 | |
---|
397 | /// |
---|
398 | /// @brief Set the seed to \a s. |
---|
399 | /// |
---|
400 | /// Set the seed to \a s in the underlying rng. If \a s is zero, a |
---|
401 | /// default value from the rng's original implementation is used |
---|
402 | /// (cf. GSL documentation). |
---|
403 | /// |
---|
404 | /// @see seed_from_devurandom, RNG::seed_from_devurandom, RNG::seed |
---|
405 | /// |
---|
406 | void seed(unsigned long s) const; |
---|
407 | |
---|
408 | /// |
---|
409 | /// @brief Set the seed using the /dev/urandom device. |
---|
410 | /// |
---|
411 | /// @return The seed acquired from /dev/urandom. |
---|
412 | /// |
---|
413 | /// @see seed, RNG::seed_from_devurandom, RNG::seed |
---|
414 | /// |
---|
415 | unsigned long seed_from_devurandom(void) |
---|
416 | { return rng_->seed_from_devurandom(); } |
---|
417 | |
---|
418 | /// |
---|
419 | /// @return A random number |
---|
420 | /// |
---|
421 | virtual double operator()(void) const = 0; |
---|
422 | |
---|
423 | protected: |
---|
424 | /// pointer to GSL random generator |
---|
425 | RNG* rng_; |
---|
426 | }; |
---|
427 | |
---|
428 | // ContinuousUniform is declared before ContinuousGeneral to avoid |
---|
429 | // forward declaration |
---|
430 | /// |
---|
431 | /// @brief Uniform distribution |
---|
432 | /// |
---|
433 | /// Class for generating a random number from a uniform distribution |
---|
434 | /// in the range [0,1), i.e. zero is included but not 1. |
---|
435 | /// |
---|
436 | /// Distribution function \f$ f(x) = 1 \f$ for \f$ 0 \le x < 1 \f$ \n |
---|
437 | /// Expectation value: 0.5 \n |
---|
438 | /// Variance: \f$ \frac{1}{12} \f$ |
---|
439 | /// |
---|
440 | class ContinuousUniform : public Continuous |
---|
441 | { |
---|
442 | public: |
---|
443 | double operator()(void) const; |
---|
444 | }; |
---|
445 | |
---|
446 | /// |
---|
447 | /// @brief Generates numbers from a histogram in a continuous manner. |
---|
448 | /// |
---|
449 | class ContinuousGeneral : public Continuous |
---|
450 | { |
---|
451 | public: |
---|
452 | /// |
---|
453 | /// @brief Constructor |
---|
454 | /// |
---|
455 | /// @param hist is a Histogram defining the probability distribution |
---|
456 | /// |
---|
457 | ContinuousGeneral(const statistics::Histogram& hist); |
---|
458 | |
---|
459 | /// |
---|
460 | /// The number is generated in a two step process. First the bin |
---|
461 | /// in the histogram is randomly selected (see |
---|
462 | /// DiscreteGeneral). Then a number is generated uniformly from |
---|
463 | /// the interval defined by the bin. |
---|
464 | /// |
---|
465 | /// @return A random number. |
---|
466 | /// |
---|
467 | double operator()(void) const; |
---|
468 | |
---|
469 | private: |
---|
470 | const DiscreteGeneral discrete_; |
---|
471 | const statistics::Histogram hist_; |
---|
472 | ContinuousUniform u_; |
---|
473 | }; |
---|
474 | |
---|
475 | /** |
---|
476 | \brief Generator of random numbers from an exponential |
---|
477 | distribution. |
---|
478 | |
---|
479 | The distribution function is \f$ f(x) = \frac{1}{m}\exp(-x/a) |
---|
480 | \f$ for \f$ x \f$ with the expectation value \f$ m \f$ and |
---|
481 | variance \f$ m^2 \f$ |
---|
482 | */ |
---|
483 | class Exponential : public Continuous |
---|
484 | { |
---|
485 | public: |
---|
486 | /// |
---|
487 | /// @brief Constructor |
---|
488 | /// |
---|
489 | /// @param m is the expectation value of the distribution. |
---|
490 | /// |
---|
491 | Exponential(const double m=1); |
---|
492 | |
---|
493 | /// |
---|
494 | /// @return A random number from exponential distribution. |
---|
495 | /// |
---|
496 | double operator()(void) const; |
---|
497 | |
---|
498 | /// |
---|
499 | /// @return A random number from exponential distribution, with |
---|
500 | /// expectation value \a m |
---|
501 | /// |
---|
502 | /// @note This operator ignores parameters given in constructor. |
---|
503 | /// |
---|
504 | double operator()(const double m) const; |
---|
505 | |
---|
506 | private: |
---|
507 | double m_; |
---|
508 | }; |
---|
509 | |
---|
510 | /** |
---|
511 | @brief Gaussian distribution |
---|
512 | |
---|
513 | Class for generating a random number from a Gaussian distribution |
---|
514 | between zero and unity. Utilizes the Box-Muller algorithm, which |
---|
515 | needs two calls to random generator. |
---|
516 | |
---|
517 | Distribution function \f$ f(x) = |
---|
518 | \frac{1}{\sqrt{2\pi\sigma^2}}\exp(-\frac{(x-\mu)^2}{2\sigma^2}) |
---|
519 | \f$ \n |
---|
520 | Expectation value: \f$ \mu \f$ \n |
---|
521 | Variance: \f$ \sigma^2 \f$ |
---|
522 | */ |
---|
523 | class Gaussian : public Continuous |
---|
524 | { |
---|
525 | public: |
---|
526 | /// |
---|
527 | /// @brief Constructor |
---|
528 | /// |
---|
529 | /// @param s is the standard deviation \f$ \sigma \f$ of distribution |
---|
530 | /// @param m is the expectation value \f$ \mu \f$ of the distribution |
---|
531 | /// |
---|
532 | Gaussian(const double s=1, const double m=0); |
---|
533 | |
---|
534 | /// |
---|
535 | /// @return A random Gaussian number |
---|
536 | /// |
---|
537 | double operator()(void) const; |
---|
538 | |
---|
539 | /// |
---|
540 | /// @return A random Gaussian number with standard deviation \a s |
---|
541 | /// and expectation value 0. |
---|
542 | /// |
---|
543 | /// @note this operator ignores parameters given in Constructor |
---|
544 | /// |
---|
545 | double operator()(const double s) const; |
---|
546 | |
---|
547 | /// |
---|
548 | /// @return A random Gaussian number with standard deviation \a s |
---|
549 | /// and expectation value \a m. |
---|
550 | /// |
---|
551 | /// @note this operator ignores parameters given in Constructor |
---|
552 | /// |
---|
553 | double operator()(const double s, const double m) const; |
---|
554 | |
---|
555 | private: |
---|
556 | double m_; |
---|
557 | double s_; |
---|
558 | }; |
---|
559 | |
---|
560 | /** |
---|
561 | \brief Convenience function to shuffle a range with singleton RNG. |
---|
562 | |
---|
563 | Wrapper around std::random_shuffle using DiscreteUniform as |
---|
564 | random generator and thereby using the underlying RNG class, |
---|
565 | which is singleton. |
---|
566 | */ |
---|
567 | template<typename T> |
---|
568 | void random_shuffle(T first, T last) |
---|
569 | { |
---|
570 | DiscreteUniform rnd; |
---|
571 | std::random_shuffle(first, last, rnd); |
---|
572 | } |
---|
573 | |
---|
574 | }}} // of namespace random, yat, and theplu |
---|
575 | |
---|
576 | #endif |
---|