forqs module reference
Forward simulation of Recombination, Quantitative traits, and Selection
 All Classes Groups Pages
Random.hpp
1 //
2 // Random.hpp
3 //
4 // Created by Darren Kessner with John Novembre
5 //
6 // Copyright (c) 2013 Regents of the University of California
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // * Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // * Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // * Neither UCLA nor the names of its contributors may be used to endorse or
20 // promote products derived from this software without specific prior
21 // written permission.
22 //
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
27 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 //
34 
35 
36 #ifndef _RANDOM_HPP_
37 #define _RANDOM_HPP_
38 
39 
40 #include "Configurable.hpp"
41 #include "shared_ptr.hpp"
42 
43 
44 //
45 // simple wrapper for Boost.Random
46 //
47 
48 
49 class Random
50 {
51  public:
52 
53  // set seed
54  static void seed(unsigned int value);
55 
56  // return random integer N with a <= N <= b
57  static int uniform_integer(int a, int b);
58 
59  // return random long integer N with a <= N <= b
60  static long uniform_long(long a, long b);
61 
62  // return random double in [a,b)
63  static double uniform_real(double a, double b);
64 
65  // return random double in [0,1)
66  static double uniform_01();
67 
68  // return 1 with probability p, else 0
69  static int bernoulli(double p = .5);
70 
71  // return random sample of indices without replacement
72  static std::vector<size_t> random_indices_without_replacement(size_t population_size, size_t sample_size);
73 
74  // distributions
75 
76  class Distribution;
77  typedef shared_ptr<Distribution> DistributionPtr;
78 
79  static DistributionPtr create_constant_distribution(const std::string& id,
80  double value = 0);
81 
82  static DistributionPtr create_uniform_real_distribution(const std::string& id,
83  double min = 0, double max = 1);
84 
85  static DistributionPtr create_normal_distribution(const std::string& id,
86  double mean = 0, double variance = 1);
87 
88  static DistributionPtr create_exponential_distribution(const std::string& id,
89  double rate = 1);
90 
91  static DistributionPtr create_poisson_distribution(const std::string& id,
92  double rate = 1);
93 
94  static DistributionPtr create_discrete_distribution(const std::string& id,
95  std::vector<double> frequencies = std::vector<double>(),
96  std::vector<double> values = std::vector<double>());
97 
98  static DistributionPtr create_neutral_frequency_distribution(const std::string& id,
99  unsigned int sample_size = 0);
100 };
101 
102 
108 
109 
110 //
111 // Random::Distribution
112 //
113 
114 
118 
119 
121 {
122  public:
123 
124  Distribution(const std::string& id) : Configurable(id) {}
125 
126  virtual double random_value() const = 0;
127 
128  // Configurable interface
129 
130  virtual std::string class_name() const;
131  virtual Parameters parameters() const;
132  virtual void configure(const Parameters& parameters, const Registry& registry);
133 };
134 
135 
136 #endif // _RANDOM_HPP_
137