ʻOhana
Population structure, admixture history, and selection using learning methods.
jade.randomizer.hpp
1 /* -------------------------------------------------------------------------
2  Ohana
3  Copyright (c) 2015-2020 Jade Cheng (\___/)
4  Jade Cheng <info@jade-cheng.com> (='.'=)
5  ------------------------------------------------------------------------- */
6 
7 #ifndef JADE_RANDOMIZER_HPP__
8 #define JADE_RANDOMIZER_HPP__
9 
10 #include "jade.matrix.hpp"
11 #include "jade.verification.hpp"
12 
13 namespace jade
14 {
15  ///
16  /// A template for a class that randomizes matrices.
17  ///
18  template <typename TValue>
20  {
21  public:
22  /// The value type.
23  typedef TValue value_type;
24 
25  /// The matrix type.
27 
28  /// The verification type.
30 
31  ///
32  /// Initializes a new instance of the class.
33  ///
35  : _engine ()
36  {
37  }
38 
39  ///
40  /// \return The random number engine.
41  ///
42  inline std::default_random_engine & get_engine()
43  {
44  return _engine;
45  }
46 
47  ///
48  /// \return A random F matrix.
49  ///
51  const size_t K, ///< The component count.
52  const matrix_type & mu) ///< The mu vector.
53  {
54  static const auto sigma = value_type(0.1);
55  static const auto epsilon = value_type(1.0e-6);
56  static const auto min = value_type(0.0) + epsilon;
57  static const auto max = value_type(1.0) - epsilon;
58 
59  assert(K > 0);
60  assert(mu.is_column_vector());
61  assert(mu.all_of([](const value_type n)
62  { return n >= min && n <= max; }));
63 
64  const auto J = mu.get_length();
65 
66  matrix_type f (K, J);
67 
68  for (size_t j = 0; j < J; j++)
69  {
70  static std::normal_distribution<value_type>
71  dist (mu[j], sigma);
72 
73  for (size_t k = 0; k < K; k++)
74  f(k, j) = std::min(std::max(
75  min, dist(_engine)), max);
76  }
77 
78  return f;
79  }
80 
81  ///
82  /// \return A random F matrix.
83  ///
85  const size_t K, ///< The component count.
86  const matrix_type & mu, ///< The mu vector.
87  const matrix_type & fif) ///< The fin-force matrix.
88  {
89  static const auto sigma = value_type(0.1);
90  static const auto epsilon = value_type(1.0e-6);
91  static const auto min = value_type(0.0) + epsilon;
92  static const auto max = value_type(1.0) - epsilon;
93 
94  assert(K > 0);
95  assert(mu.is_column_vector());
96  assert(mu.all_of([](const value_type n)
97  { return n >= min && n <= max; }));
98 
99  const auto J = mu.get_length();
100  assert(verification_type::validate_fif_size(fif, K, J));
101 
102  matrix_type f (K, J);
103 
104  for (size_t j = 0; j < J; j++)
105  {
106  static std::normal_distribution<value_type>
107  dist (mu[j], sigma);
108 
109  for (size_t k = 0; k < fif.get_height(); k++)
110  f(k, j) = fif(k, j);
111 
112  for (size_t k = fif.get_height(); k < K; k++)
113  f(k, j) = std::min(std::max(
114  min, dist(_engine)), max);
115  }
116 
117  return f;
118  }
119 
120  ///
121  /// \return A random Q matrix.
122  ///
124  const size_t I, ///< The number of individuals.
125  const size_t K) ///< The number of components.
126  {
127  static const auto K_0 = value_type(0);
128  static const auto K_1 = value_type(1);
129 
130  static std::uniform_real_distribution<value_type>
131  distribution (K_0, K_1);
132 
133  matrix_type q (I, K);
134 
135  for (size_t i = 0; i < I; i++)
136  {
137  auto sum = K_0;
138  for (size_t k = 0; k < K; k++)
139  sum += (q(i, k) = distribution(_engine));
140 
141  const auto factor = K_1 / sum;
142  for (size_t k = 0; k < K; k++)
143  q(i, k) *= factor;
144  }
145 
146  return q;
147  }
148 
149  private:
150  std::default_random_engine _engine;
151  };
152 }
153 
154 #endif // JADE_RANDOMIZER_HPP__
jade::basic_verification
A template for a class that performs validation on various types of matrices.
Definition: jade.verification.hpp:20
jade::basic_randomizer
A template for a class that randomizes matrices.
Definition: jade.randomizer.hpp:20
jade::basic_matrix::get_length
size_t get_length() const
Definition: jade.matrix.hpp:624
jade::basic_randomizer::get_engine
std::default_random_engine & get_engine()
Definition: jade.randomizer.hpp:42
jade::basic_matrix::all_of
bool all_of(const TPredicate predicate) const
Definition: jade.matrix.hpp:117
jade::basic_randomizer::randomize_q
matrix_type randomize_q(const size_t I, const size_t K)
Definition: jade.randomizer.hpp:123
jade::basic_randomizer::matrix_type
basic_matrix< value_type > matrix_type
The matrix type.
Definition: jade.randomizer.hpp:26
jade::basic_randomizer::randomize_f
matrix_type randomize_f(const size_t K, const matrix_type &mu)
Definition: jade.randomizer.hpp:50
jade::basic_randomizer::verification_type
basic_verification< value_type > verification_type
The verification type.
Definition: jade.randomizer.hpp:29
jade::basic_matrix::get_height
size_t get_height() const
Definition: jade.matrix.hpp:603
jade::basic_randomizer::value_type
TValue value_type
The value type.
Definition: jade.randomizer.hpp:23
jade::basic_matrix::is_column_vector
bool is_column_vector() const
Definition: jade.matrix.hpp:852
jade::basic_randomizer::randomize_f
matrix_type randomize_f(const size_t K, const matrix_type &mu, const matrix_type &fif)
Definition: jade.randomizer.hpp:84
jade::basic_verification::validate_fif_size
static bool validate_fif_size(const matrix_type &fif, const size_t k, const size_t j)
Validates the size of the Fin-force matrix and throws an exception if validation fails.
Definition: jade.verification.hpp:131
jade::basic_randomizer::basic_randomizer
basic_randomizer()
Initializes a new instance of the class.
Definition: jade.randomizer.hpp:34
jade::basic_matrix< value_type >