MCKL
Monte Carlo Kernel Library
sampler.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/core/sampler.hpp
3 //----------------------------------------------------------------------------
4 // MCKL: Monte Carlo Kernel Library
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2018, Yan Zhou
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 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef MCKL_CORE_SAMPLER_HPP
33 #define MCKL_CORE_SAMPLER_HPP
34 
35 #include <mckl/internal/common.hpp>
36 #include <mckl/core/matrix.hpp>
37 
38 namespace mckl {
39 
40 template <typename>
42 
45 template <typename Derived>
46 class Sampler
47 {
48  public:
51 
53  void reserve(std::size_t n)
54  {
55  for (auto &est : estimator_) {
56  for (auto &e : est) {
57  e.reserve(n);
58  }
59  }
60  }
61 
63  void reset()
64  {
65  for (auto &eval : eval_) {
66  eval.clear();
67  }
68  for (auto &est : estimator_) {
69  est.clear();
70  }
71  }
72 
74  void clear()
75  {
76  for (auto &est : estimator_) {
77  for (auto &e : est) {
78  e.clear();
79  }
80  }
81  }
82 
84  template <typename T, MatrixLayout Layout>
86  {
87  const std::size_t nrow =
88  static_cast<const Derived *>(this)->num_iter();
89 
90  std::size_t ncol = 0;
91  for (auto &est : estimator_) {
92  for (auto &e : est) {
93  ncol += e.dim();
94  }
95  }
96 
97  Matrix<T, Layout> mat(nrow, ncol);
98  if (nrow * ncol == 0) {
99  return mat;
100  }
101 
102  if (Layout == RowMajor) {
103  for (std::size_t i = 0; i != nrow; ++i) {
104  T *first = mat.row_data(i);
105  for (auto &est : estimator_) {
106  for (auto &e : est) {
107  first = std::copy(e.row_begin(i), e.row_end(i), first);
108  }
109  }
110  }
111  } else {
112  T *first = mat.col_data(0);
113  for (auto &est : estimator_) {
114  for (auto &e : est) {
115  for (std::size_t j = 0; j != e.dim(); ++j) {
116  first = std::copy(e.col_begin(j), e.col_end(j), first);
117  }
118  }
119  }
120  }
121 
122  return mat;
123  }
124 
129  template <typename CharT, typename Traits>
130  std::basic_ostream<CharT, Traits> &print(
131  std::basic_ostream<CharT, Traits> &os, char sepchar = ' ') const
132  {
133  const auto mat = summary<double, RowMajor>();
134  const std::size_t n = mat.nrow();
135  const std::size_t m = mat.ncol();
136 
137  if (!os || n * m == 0) {
138  return os;
139  }
140 
141  auto v = mat.data();
142  for (std::size_t i = 0; i != n; ++i) {
143  for (std::size_t j = 0; j != m; ++j) {
144  os << *v++ << sepchar;
145  }
146  os << '\n';
147  }
148 
149  return os;
150  }
151 
153  template <typename CharT, typename Traits>
154  friend std::basic_ostream<CharT, Traits> &operator<<(
155  std::basic_ostream<CharT, Traits> &os, const Sampler<Derived> &sampler)
156  {
157  return sampler.print(os);
158  }
159 
160  protected:
161  Sampler(std::size_t steps) : num_iter_(0), eval_(steps), estimator_(steps)
162  {
163  }
164 
165  Vector<eval_type> &eval(std::size_t step) { return eval_.at(step); }
166 
167  const Vector<eval_type> &eval(std::size_t step) const
168  {
169  return eval_.at(step);
170  }
171 
173  {
174  return estimator_.at(step);
175  }
176 
177  const Vector<estimator_type> &estimator(std::size_t step) const
178  {
179  return estimator_.at(step);
180  }
181 
182  template <typename Eval>
183  std::size_t eval(std::size_t step, Eval &&eval,
184  std::enable_if_t<!std::is_integral<Eval>::value> * = nullptr)
185  {
186  eval_.at(step).push_back(std::forward<Eval>(eval));
187 
188  return eval_.at(step).size() - 1;
189  }
190 
191  eval_type &eval(std::size_t step, std::size_t k)
192  {
193  return eval_.at(step).at(k);
194  }
195 
196  const eval_type &eval(std::size_t step, std::size_t k) const
197  {
198  return eval_.at(step).at(k);
199  }
200 
201  template <typename Estimator>
202  std::size_t estimator(std::size_t step, Estimator &&estimator,
203  std::enable_if_t<!std::is_integral<Estimator>::value> * = nullptr)
204  {
205  estimator_.at(step).push_back(std::forward<Estimator>(estimator));
206 
207  return estimator_.at(step).size() - 1;
208  }
209 
210  estimator_type &estimator(std::size_t step, std::size_t k)
211  {
212  return estimator_.at(step).at(k);
213  }
214 
215  const estimator_type &estimator(std::size_t step, std::size_t k) const
216  {
217  return estimator_.at(step).at(k);
218  }
219 
220  private:
221  std::size_t num_iter_;
223  Vector<Vector<estimator_type>> estimator_;
224 }; // class Sampler
225 
226 } // namespace mckl
227 
228 #endif // MCKL_CORE_SAMPLER_HPP
typename SamplerTrait< MCMCSampler< T, U > >::eval_type eval_type
Definition: sampler.hpp:49
std::vector< T, Alloc > Vector
std::vector with Allocator as the default allocator
Definition: memory.hpp:435
Sampler for iterative Monte Carlo algorithms.
Definition: sampler.hpp:46
estimator_type & estimator(std::size_t step, std::size_t k)
Definition: sampler.hpp:210
Vector< eval_type > & eval(std::size_t step)
Definition: sampler.hpp:165
const eval_type & eval(std::size_t step, std::size_t k) const
Definition: sampler.hpp:196
Vector< estimator_type > & estimator(std::size_t step)
Definition: sampler.hpp:172
pointer row_data(size_type i)
Pointer to the first element of a row.
Definition: matrix.hpp:443
pointer col_data(size_type j)
Pointer to the beginning of a column.
Definition: matrix.hpp:461
void reset()
Remove all evaluation and estimation methods.
Definition: sampler.hpp:63
typename SamplerTrait< MCMCSampler< T, U > >::estimator_type estimator_type
Definition: sampler.hpp:50
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const Sampler< Derived > &sampler)
Output the summary mtrix through an output stream.
Definition: sampler.hpp:154
eval_type & eval(std::size_t step, std::size_t k)
Definition: sampler.hpp:191
Matrix< T, Layout > summary() const
Return a combined matrix of all estimates.
Definition: sampler.hpp:85
std::size_t eval(std::size_t step, Eval &&eval, std::enable_if_t<!std::is_integral< Eval >::value > *=nullptr)
Definition: sampler.hpp:183
void reserve(std::size_t n)
Reserve space for additional iterations.
Definition: sampler.hpp:53
void clear()
Clear all estimator histories.
Definition: sampler.hpp:74
const estimator_type & estimator(std::size_t step, std::size_t k) const
Definition: sampler.hpp:215
Definition: mcmc.hpp:40
const Vector< estimator_type > & estimator(std::size_t step) const
Definition: sampler.hpp:177
Estimator for iterative Monte Carlo algorithms.
Definition: estimator.hpp:45
std::size_t estimator(std::size_t step, Estimator &&estimator, std::enable_if_t<!std::is_integral< Estimator >::value > *=nullptr)
Definition: sampler.hpp:202
Sampler(std::size_t steps)
Definition: sampler.hpp:161
const Vector< eval_type > & eval(std::size_t step) const
Definition: sampler.hpp:167
std::basic_ostream< CharT, Traits > & print(std::basic_ostream< CharT, Traits > &os, char sepchar=' ') const
Print the summary matrix in textual form.
Definition: sampler.hpp:130