vSMC
vSMC: Scalable Monte Carlo
sampler.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/core/sampler.hpp
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2016, 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 VSMC_CORE_SAMPLER_HPP
33 #define VSMC_CORE_SAMPLER_HPP
34 
35 #include <vsmc/internal/common.hpp>
36 #include <vsmc/core/monitor.hpp>
37 #include <vsmc/core/particle.hpp>
38 
39 #define VSMC_RUNTIME_ASSERT_CORE_SAMPLER_MONITOR_NAME(iter, map, func) \
40  VSMC_RUNTIME_ASSERT( \
41  (iter != map.end()), "**Sampler::" #func "** INVALID MONITOR NAME")
42 
43 #define VSMC_RUNTIME_ASSERT_CORE_SAMPLER_FUNCTOR(func, caller, name) \
44  VSMC_RUNTIME_ASSERT(static_cast<bool>(func), \
45  "**Sampler::" #caller "** INVALID " #name " OBJECT")
46 
47 #define VSMC_RUNTIME_WARNING_CORE_SAMPLER_INIT_BY_ITER \
48  VSMC_RUNTIME_WARNING((!static_cast<bool>(init_)), \
49  "**Sampler::initialize** A VALID INIT OBJECT IS SET " \
50  "BUT INITILIALIZED BY ITERATING")
51 
52 namespace vsmc
53 {
54 
57 template <typename T>
58 class Sampler
59 {
60  public:
63  using value_type = T;
64  using init_type = std::function<std::size_t(Particle<T> &, void *)>;
65  using move_type = std::function<std::size_t(std::size_t, Particle<T> &)>;
66  using mcmc_type = std::function<std::size_t(std::size_t, Particle<T> &)>;
67  using monitor_map_type = std::map<std::string, Monitor<T>>;
68 
70  explicit Sampler(size_type N)
71  : particle_(N)
72  , init_by_iter_(false)
73  , resample_threshold_(resample_threshold_never())
74  , iter_num_(0)
75  {
77  }
78 
84  : particle_(N)
85  , init_by_iter_(false)
86  , resample_threshold_(resample_threshold_always())
87  , iter_num_(0)
88  {
89  resample_scheme(scheme);
90  }
91 
96  Sampler(size_type N, const resample_type &res_op)
97  : particle_(N)
98  , init_by_iter_(false)
99  , resample_threshold_(resample_threshold_always())
100  , iter_num_(0)
101  {
102  resample_scheme(res_op);
103  }
104 
108  : particle_(N)
109  , init_by_iter_(false)
110  , resample_threshold_(resample_threshold)
111  , iter_num_(0)
112  {
113  resample_scheme(scheme);
114  }
115 
119  size_type N, const resample_type &res_op, double resample_threshold)
120  : particle_(N)
121  , init_by_iter_(false)
122  , resample_threshold_(resample_threshold)
123  , iter_num_(0)
124  {
125  resample_scheme(res_op);
126  }
127 
132  Sampler<T> clone(bool new_rng) const
133  {
134  Sampler<T> sampler(*this);
135  if (new_rng) {
136  sampler.particle().rng_set().seed();
137  Seed::instance().seed_rng(sampler.particle().resample_rng());
138  }
139 
140  return sampler;
141  }
142 
148  Sampler<T> &clone(const Sampler<T> &other, bool retain_rng)
149  {
150  if (this != &other) {
151  particle_.clone(other.particle_, retain_rng);
152  init_by_iter_ = other.init_by_iter_;
153  init_ = other.init_;
154  move_queue_ = other.move_queue_;
155  mcmc_queue_ = other.mcmc_queue_;
156  resample_op_ = other.resample_op_;
157  resample_threshold_ = other.resample_threshold_;
158  iter_num_ = other.iter_num_;
159  size_history_ = other.size_history_;
160  ess_history_ = other.ess_history_;
161  resampled_history_ = other.resampled_history_;
162  accept_history_ = other.accept_history_;
163  }
164 
165  return *this;
166  }
167 
168  Sampler<T> &clone(Sampler<T> &&other, bool retain_rng)
169  {
170  if (this != &other) {
171  particle_.clone(std::move(other.particle_), retain_rng);
172  init_by_iter_ = other.init_by_iter_;
173  init_ = std::move(other.init_);
174  move_queue_ = std::move(other.move_queue_);
175  mcmc_queue_ = std::move(other.mcmc_queue_);
176  resample_op_ = std::move(other.resample_op_);
177  resample_threshold_ = other.resample_threshold_;
178  iter_num_ = other.iter_num_;
179  size_history_ = std::move(other.size_history_);
180  ess_history_ = std::move(other.ess_history_);
181  resampled_history_ = std::move(other.resampled_history_);
182  accept_history_ = std::move(other.accept_history_);
183  }
184 
185  return *this;
186  }
187 
189  size_type size() const { return particle_.size(); }
190 
192  void reserve(std::size_t num)
193  {
194  size_history_.reserve(num);
195  ess_history_.reserve(num);
196  resampled_history_.reserve(num);
197  for (auto &a : accept_history_)
198  a.reserve(num);
199  for (auto &m : monitor_)
200  if (!m.second.empty())
201  m.second.reserve(num);
202  }
203 
205  std::size_t iter_size() const { return size_history_.size(); }
206 
208  std::size_t iter_num() const { return iter_num_; }
209 
212  {
213  particle_.resample(resample_op_, resample_threshold_always());
214 
215  return *this;
216  }
217 
220  {
221  resample_op_ = res_op;
222 
223  return *this;
224  }
225 
229  {
230  switch (scheme) {
231  case Multinomial: resample_op_ = ResampleMultinomial(); break;
232  case Residual: resample_op_ = ResampleResidual(); break;
233  case Stratified: resample_op_ = ResampleStratified(); break;
234  case Systematic: resample_op_ = ResampleSystematic(); break;
235  case ResidualStratified:
236  resample_op_ = ResampleResidualStratified();
237  break;
238  case ResidualSystematic:
239  resample_op_ = ResampleResidualSystematic();
240  break;
241  }
242 
243  return *this;
244  }
245 
247  double resample_threshold() const { return resample_threshold_; }
248 
250  Sampler<T> &resample_threshold(double threshold)
251  {
252  resample_threshold_ = threshold;
253  return *this;
254  }
255 
258  static double resample_threshold_never()
259  {
260  return -std::numeric_limits<double>::infinity();
261  }
262 
266  {
267  return std::numeric_limits<double>::infinity();
268  }
269 
272  double size_history(std::size_t iter) const { return size_history_[iter]; }
273 
275  template <typename OutputIter>
276  void read_size_history(OutputIter first) const
277  {
278  std::copy(size_history_.begin(), size_history_.end(), first);
279  }
280 
282  double ess_history(std::size_t iter) const { return ess_history_[iter]; }
283 
285  template <typename OutputIter>
286  void read_ess_history(OutputIter first) const
287  {
288  std::copy(ess_history_.begin(), ess_history_.end(), first);
289  }
290 
292  bool resampled_history(std::size_t iter) const
293  {
294  return resampled_history_[iter];
295  }
296 
298  template <typename OutputIter>
299  void read_resampled_history(OutputIter first) const
300  {
301  std::copy(resampled_history_.begin(), resampled_history_.end(), first);
302  }
303 
305  std::size_t accept_history(std::size_t id, std::size_t iter) const
306  {
307  return accept_history_[id][iter];
308  }
309 
311  Particle<T> &particle() { return particle_; }
312 
314  const Particle<T> &particle() const { return particle_; }
315 
317  Sampler<T> &init(const init_type &new_init)
318  {
320 
321  init_ = new_init;
322 
323  return *this;
324  }
325 
332  Sampler<T> &init_by_iter(bool initialize_by_iterate)
333  {
334  init_by_iter_ = initialize_by_iterate;
335 
336  return *this;
337  }
338 
345  {
347 
348  init_ = init_op([new_init](
349  Particle<T> &particle, void *) { new_init(0, particle); });
350 
351  return *this;
352  }
353 
356  {
357  move_queue_.clear();
358  return *this;
359  }
360 
362  bool move_queue_empty() const { return move_queue_.empty(); }
363 
365  std::size_t move_queue_size() const { return move_queue_.size(); }
366 
368  Sampler<T> &move(const move_type &new_move, bool append)
369  {
371 
372  if (!append)
373  move_queue_.clear();
374  move_queue_.push_back(new_move);
375 
376  return *this;
377  }
378 
380  template <typename InputIter>
381  Sampler<T> &move(InputIter first, InputIter last, bool append)
382  {
383  if (!append)
384  move_queue_.clear();
385  while (first != last) {
387  move_queue_.push_back(*first);
388  ++first;
389  }
390 
391  return *this;
392  }
393 
396  {
397  mcmc_queue_.clear();
398 
399  return *this;
400  }
401 
403  bool mcmc_queue_empty() const { return mcmc_queue_.empty(); }
404 
406  std::size_t mcmc_queue_size() const { return mcmc_queue_.size(); }
407 
409  Sampler<T> &mcmc(const mcmc_type &new_mcmc, bool append)
410  {
412 
413  if (!append)
414  mcmc_queue_.clear();
415  mcmc_queue_.push_back(new_mcmc);
416 
417  return *this;
418  }
419 
421  template <typename InputIter>
422  Sampler<T> &mcmc(InputIter first, InputIter last, bool append)
423  {
424  if (!append)
425  mcmc_queue_.clear();
426  while (first != last) {
428  mcmc_queue_.push_back(*first);
429  ++first;
430  }
431 
432  return *this;
433  }
434 
443  Sampler<T> &initialize(void *param = nullptr)
444  {
445  do_reset();
446  do_acch();
447  if (init_by_iter_) {
449  do_iter();
450  } else {
451  do_init(param);
452  }
453  do_acch();
454 
455  return *this;
456  }
457 
464  Sampler<T> &iterate(std::size_t num = 1)
465  {
466  do_acch();
467  if (num > 1)
468  reserve(iter_size() + num);
469  for (std::size_t i = 0; i != num; ++i) {
470  ++iter_num_;
471  do_iter();
472  }
473  do_acch();
474 
475  return *this;
476  }
477 
482  Sampler<T> &monitor(const std::string &name, const Monitor<T> &mon)
483  {
484  monitor_.insert(std::make_pair(name, mon));
485 
486  return *this;
487  }
488 
498  Sampler<T> &monitor(const std::string &name, std::size_t dim,
499  const typename Monitor<T>::eval_type &eval, bool record_only = false,
500  MonitorStage stage = MonitorMCMC)
501  {
502  monitor_.insert(typename monitor_map_type::value_type(
503  name, Monitor<T>(dim, eval, record_only, stage)));
504 
505  return *this;
506  }
507 
509  Monitor<T> &monitor(const std::string &name)
510  {
511  typename monitor_map_type::iterator iter = monitor_.find(name);
512 
514 
515  return iter->second;
516  }
517 
519  const Monitor<T> &monitor(const std::string &name) const
520  {
521  typename monitor_map_type::const_iterator citer = monitor_.find(name);
522 
524  citer, monitor_, monitor);
525 
526  return citer->second;
527  }
528 
531  monitor_map_type &monitor() { return monitor_; }
532 
535  const monitor_map_type &monitor() const { return monitor_; }
536 
538  bool clear_monitor(const std::string &name)
539  {
540  return monitor_.erase(name) ==
541  static_cast<typename monitor_map_type::size_type>(1);
542  }
543 
546  {
547  monitor_.clear();
548 
549  return *this;
550  }
551 
553  std::size_t summary_header_size_int() const
554  {
555  if (iter_size() == 0)
556  return 0;
557 
558  return accept_history_.size() + 2;
559  }
560 
562  std::size_t summary_header_size() const
563  {
564  if (iter_size() == 0)
565  return 0;
566 
567  std::size_t header_size = 1;
568  for (const auto &m : monitor_)
569  if (m.second.iter_size() > 0)
570  header_size += m.second.dim();
571 
572  return header_size;
573  }
574 
576  template <typename OutputIter>
577  void summary_header_int(OutputIter first) const
578  {
579  if (summary_header_size_int() == 0)
580  return;
581 
582  *first++ = std::string("Size");
583  *first++ = std::string("Resampled");
584  for (std::size_t i = 0; i != accept_history_.size(); ++i)
585  *first++ = "Accept." + internal::itos(i);
586  }
587 
589  template <typename OutputIter>
590  void summary_header(OutputIter first) const
591  {
592  if (summary_header_size() == 0)
593  return;
594 
595  *first++ = std::string("ESS");
596  for (const auto &m : monitor_) {
597  if (m.second.iter_size() > 0) {
598  unsigned md = static_cast<unsigned>(m.second.dim());
599  for (unsigned d = 0; d != md; ++d) {
600  if (m.second.name(d).empty())
601  *first++ = m.first + "." + internal::itos(d);
602  else
603  *first++ = m.second.name(d);
604  }
605  }
606  }
607  }
608 
610  std::size_t summary_data_size_int() const
611  {
612  return summary_header_size_int() * iter_size();
613  }
614 
616  std::size_t summary_data_size() const
617  {
618  return summary_header_size() * iter_size();
619  }
620 
622  template <MatrixLayout Layout, typename OutputIter>
623  void summary_data_int(OutputIter first) const
624  {
625  if (summary_data_size_int() == 0)
626  return;
627 
628  if (Layout == RowMajor)
629  summary_data_row_int(first);
630  if (Layout == ColMajor)
631  summary_data_col_int(first);
632  }
633 
635  template <MatrixLayout Layout, typename OutputIter>
636  void summary_data(OutputIter first) const
637  {
638  if (summary_data_size() == 0)
639  return;
640 
641  if (Layout == RowMajor)
642  summary_data_row(first);
643  if (Layout == ColMajor)
644  summary_data_col(first);
645  }
646 
651  template <typename CharT, typename Traits>
652  std::basic_ostream<CharT, Traits> &print(
653  std::basic_ostream<CharT, Traits> &os, char sepchar = '\t') const
654  {
655  if (iter_size() == 0 || !os.good())
656  return os;
657 
658  std::size_t nrow = iter_size();
659 
660  std::size_t ncol_int = summary_header_size_int();
661  Vector<std::string> header_int(ncol_int);
662  Vector<size_type> data_int(nrow * ncol_int);
663  summary_header_int(header_int.begin());
664  summary_data_int<RowMajor>(data_int.begin());
665 
666  std::size_t ncol = summary_header_size();
667  Vector<std::string> header(ncol);
668  Vector<double> data(nrow * ncol);
669  summary_header(header.begin());
670  summary_data<RowMajor>(data.begin());
671 
672  for (const auto &h : header_int)
673  os << h << sepchar;
674  for (const auto &h : header)
675  os << h << sepchar;
676  os << '\n';
677 
678  std::size_t offset_int = 0;
679  std::size_t offset = 0;
680  for (std::size_t r = 0; r != nrow; ++r) {
681  for (std::size_t c = 0; c != ncol_int; ++c)
682  os << data_int[offset_int++] << sepchar;
683  for (std::size_t c = 0; c != ncol; ++c)
684  os << data[offset++] << sepchar;
685  os << '\n';
686  }
687 
688  return os;
689  }
690 
691  private:
692  Particle<T> particle_;
693 
694  bool init_by_iter_;
695  init_type init_;
696  Vector<move_type> move_queue_;
697  Vector<mcmc_type> mcmc_queue_;
698 
699  resample_type resample_op_;
700  double resample_threshold_;
701 
702  std::size_t iter_num_;
703  Vector<std::size_t> size_history_;
704  Vector<double> ess_history_;
705  Vector<bool> resampled_history_;
706  Vector<Vector<std::size_t>> accept_history_;
707  monitor_map_type monitor_;
708 
709  void do_acch()
710  {
711  if (accept_history_.empty())
712  accept_history_.push_back(Vector<std::size_t>());
713  std::size_t acc_size = move_queue_.size() + mcmc_queue_.size();
714  if (accept_history_.size() < acc_size) {
715  std::size_t diff = acc_size - accept_history_.size();
716  for (std::size_t d = 0; d != diff; ++d)
717  accept_history_.push_back(Vector<std::size_t>());
718  }
719  for (std::size_t i = 0; i != accept_history_.size(); ++i)
720  accept_history_[i].resize(iter_size());
721  }
722 
723  void do_reset()
724  {
725  size_history_.clear();
726  ess_history_.clear();
727  resampled_history_.clear();
728  accept_history_.clear();
729  for (auto &m : monitor_)
730  m.second.clear();
731  iter_num_ = 0;
732  particle_.weight().set_equal();
733  }
734 
735  void do_init(void *param)
736  {
738  accept_history_[0].push_back(init_(particle_, param));
739  do_monitor(MonitorMove);
740  do_resample();
741  do_monitor(MonitorResample);
742  do_mcmc(1);
743  do_monitor(MonitorMCMC);
744  }
745 
746  void do_iter()
747  {
748  std::size_t ia = do_move(0);
749  do_monitor(MonitorMove);
750  do_resample();
751  do_monitor(MonitorResample);
752  do_mcmc(ia);
753  do_monitor(MonitorMCMC);
754  }
755 
756  std::size_t do_move(std::size_t ia)
757  {
758  for (auto &m : move_queue_)
759  accept_history_[ia++].push_back(m(iter_num_, particle_));
760 
761  return ia;
762  }
763 
764  std::size_t do_mcmc(std::size_t ia)
765  {
766  for (auto &m : mcmc_queue_)
767  accept_history_[ia++].push_back(m(iter_num_, particle_));
768 
769  return ia;
770  }
771 
772  void do_resample()
773  {
774  size_history_.push_back(size());
775  ess_history_.push_back(particle_.weight().ess());
776  resampled_history_.push_back(
777  particle_.resample(resample_op_, resample_threshold_));
778  }
779 
780  void do_monitor(MonitorStage stage)
781  {
782  for (auto &m : monitor_)
783  if (!m.second.empty())
784  m.second.eval(iter_num_, particle_, stage);
785  }
786 
787  template <typename OutputIter>
788  void summary_data_row_int(OutputIter first) const
789  {
790  using int_type = typename std::iterator_traits<OutputIter>::value_type;
791  for (std::size_t iter = 0; iter != iter_size(); ++iter) {
792  *first++ = static_cast<int_type>(size_history_[iter]);
793  *first++ = static_cast<int_type>(resampled_history_[iter]);
794  for (std::size_t i = 0; i != accept_history_.size(); ++i)
795  *first++ = static_cast<int_type>(accept_history_[i][iter]);
796  }
797  }
798 
799  template <typename OutputIter>
800  void summary_data_col_int(OutputIter first) const
801  {
802  first = std::copy(size_history_.begin(), size_history_.end(), first);
803  first = std::copy(
804  resampled_history_.begin(), resampled_history_.end(), first);
805  for (std::size_t i = 0; i != accept_history_.size(); ++i) {
806  first = std::copy(
807  accept_history_[i].begin(), accept_history_[i].end(), first);
808  }
809  }
810 
811  template <typename OutputIter>
812  void summary_data_row(OutputIter first) const
813  {
814  double missing_data = std::numeric_limits<double>::quiet_NaN();
815 
817  for (const auto &m : monitor_)
818  if (m.second.iter_size() > 0)
819  miter.push_back(std::make_pair(0, &m.second));
820 
821  for (std::size_t iter = 0; iter != iter_size(); ++iter) {
822  *first++ = ess_history_[iter];
823  for (auto &m : miter) {
824  std::size_t md = m.second->dim();
825  if (m.first != m.second->iter_size() &&
826  iter == m.second->index(m.first)) {
827  first = std::copy_n(
828  m.second->record_data(m.first++), md, first);
829  } else {
830  first = std::fill_n(first, md, missing_data);
831  }
832  }
833  }
834  }
835 
836  template <typename OutputIter>
837  void summary_data_col(OutputIter first) const
838  {
839  double missing_data = std::numeric_limits<double>::quiet_NaN();
840 
841  first = std::copy(ess_history_.begin(), ess_history_.end(), first);
842  for (const auto &m : monitor_) {
843  if (m.second.iter_size() > 0) {
844  for (std::size_t d = 0; d != m.second.dim(); ++d) {
845  std::size_t miter = 0;
846  for (std::size_t iter = 0; iter != iter_size(); ++iter) {
847  if (miter != m.second.iter_size() ||
848  iter == m.second.index(miter)) {
849  *first++ = m.second.record(d, miter++);
850  } else {
851  *first++ = missing_data;
852  }
853  }
854  }
855  }
856  }
857  }
858 }; // class Sampler
859 
860 template <typename CharT, typename Traits, typename T>
861 inline std::basic_ostream<CharT, Traits> &operator<<(
862  std::basic_ostream<CharT, Traits> &os, const Sampler<T> &sampler)
863 {
864  return sampler.print(os);
865 }
866 
867 } // namespace vsmc
868 
869 #endif // VSMC_CORE_SAMPLER_HPP
Residual stratified resampling.
static SeedGenerator< ID, ResultType > & instance()
Definition: seed.hpp:100
Definition: monitor.hpp:49
SizeType< T > size_type
Definition: particle.hpp:51
Particle class representing the whole particle set.
Definition: particle.hpp:48
Sampler(size_type N, const resample_type &res_op)
Construct a Sampler with a user defined resampling operation.
Definition: sampler.hpp:96
std::basic_ostream< CharT, Traits > & print(std::basic_ostream< CharT, Traits > &os, char sepchar= '\t') const
Print the history of the Sampler.
Definition: sampler.hpp:652
void summary_data_int(OutputIter first) const
Sampler summary data (integer data)
Definition: sampler.hpp:623
typename std::conditional< std::is_scalar< T >::value, AlignedVector< T >, std::vector< T >>::type Vector
AlignedVector for scalar type and std::vector for others.
Sampler< T > & mcmc_queue_clear()
Clear the mcmc queue.
Definition: sampler.hpp:395
Stratified resampling.
Definition: stratified.hpp:43
Sampler< T > & resample()
Force resample.
Definition: sampler.hpp:211
double ess_history(std::size_t iter) const
Get ESS of a given iteration, initialization count as iter 0.
Definition: sampler.hpp:282
Sampler< T > & init_by_move(const move_type &new_init)
Set the initialization object with a type move_type object.
Definition: sampler.hpp:344
typename Particle< T >::resample_type resample_type
Definition: sampler.hpp:62
Sampler< T > & clear_monitor()
Erase all monitors.
Definition: sampler.hpp:545
size_type size() const
Number of particles.
Definition: sampler.hpp:189
MonitorStage
Monitor stage.
Definition: monitor.hpp:54
Sampler(size_type N, ResampleScheme scheme)
Construct a Sampler with a built-in resampling scheme.
Definition: sampler.hpp:83
#define VSMC_RUNTIME_WARNING_CORE_SAMPLER_INIT_BY_ITER
Definition: sampler.hpp:47
const Particle< T > & particle() const
Read only access to the Particle<T> object.
Definition: sampler.hpp:314
monitor_map_type & monitor()
Read and write access to all monitors to the monitor_map_type object.
Definition: sampler.hpp:531
std::size_t summary_header_size() const
The size of Sampler summary header (floating point data)
Definition: sampler.hpp:562
double size_history(std::size_t iter) const
Get sampler size of a given iteration (initialization count as iteration zero)
Definition: sampler.hpp:272
ResampleScheme
Resampling schemes.
Definition: defines.hpp:57
weight_type & weight()
Read and write access to the weight collection object.
Definition: particle.hpp:131
Sampler< T > & clone(Sampler< T > &&other, bool retain_rng)
Definition: sampler.hpp:168
std::size_t summary_data_size_int() const
The size of Sampler summary data (integer data)
Definition: sampler.hpp:610
Stratified resampling on residuals.
Definition: defines.hpp:62
Sampler(size_type N, ResampleScheme scheme, double resample_threshold)
Construct a Sampler with a built-in resampling scheme and a threshold for resampling.
Definition: sampler.hpp:107
Sampler< T > & initialize(void *param=nullptr)
Initialization.
Definition: sampler.hpp:443
std::size_t iter_size() const
Number of iterations (including initialization)
Definition: sampler.hpp:205
Sampler< T > & mcmc(const mcmc_type &new_mcmc, bool append)
Add a new mcmc.
Definition: sampler.hpp:409
void summary_header(OutputIter first) const
Sampler summary header (floating point data)
Definition: sampler.hpp:590
Multinomial resampling.
Definition: defines.hpp:58
Monitor evaluated after moves.
Definition: monitor.hpp:55
std::string itos(UIntType i, std::true_type)
Definition: common.hpp:100
bool resample(const resample_type &op, double threshold)
Performing resampling if ESS/N < threshold.
Definition: particle.hpp:170
void summary_header_int(OutputIter first) const
Sampler summary header (integer data)
Definition: sampler.hpp:577
std::size_t move_queue_size() const
Check the size of the move queue.
Definition: sampler.hpp:365
double resample_threshold() const
Get resampling threshold.
Definition: sampler.hpp:247
Stratified resampling.
Definition: defines.hpp:59
std::function< std::size_t(std::size_t, Particle< T > &)> mcmc_type
Definition: sampler.hpp:66
#define VSMC_RUNTIME_ASSERT_CORE_SAMPLER_MONITOR_NAME(iter, map, func)
Definition: sampler.hpp:39
Sampler< T > & iterate(std::size_t num=1)
Iteration.
Definition: sampler.hpp:464
Sampler< T > & move(const move_type &new_move, bool append)
Add a new move.
Definition: sampler.hpp:368
static double resample_threshold_always()
Special value of resampling threshold that indicate no resampling will always be performed.
Definition: sampler.hpp:265
void read_resampled_history(OutputIter first) const
Read resampling indicator history through an output iterator.
Definition: sampler.hpp:299
std::size_t summary_data_size() const
The size of Sampler summary data (floating point data)
Definition: sampler.hpp:616
typename Particle< T >::size_type size_type
Definition: sampler.hpp:61
void read_ess_history(OutputIter first) const
Read ESS history through an output iterator.
Definition: sampler.hpp:286
Sampler< T > & move(InputIter first, InputIter last, bool append)
Add a sequence of new moves.
Definition: sampler.hpp:381
void reserve(std::size_t num)
Reserve space for a specified number of iterations.
Definition: sampler.hpp:192
Residual resampling.
Definition: defines.hpp:61
bool clear_monitor(const std::string &name)
Erase a named monitor.
Definition: sampler.hpp:538
Sampler< T > & resample_scheme(const resample_type &res_op)
Set resampling method by a resample_type object.
Definition: sampler.hpp:219
std::function< void(std::size_t, std::size_t, rng_type &, const double *, size_type *)> resample_type
Definition: particle.hpp:57
Systematic resampling.
Definition: systematic.hpp:43
Systematic resampling on residuals.
Definition: defines.hpp:63
std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const Sampler< T > &sampler)
Definition: sampler.hpp:861
Sampler< T > clone(bool new_rng) const
Clone the sampler system except the RNG engines.
Definition: sampler.hpp:132
Sampler< T > & monitor(const std::string &name, std::size_t dim, const typename Monitor< T >::eval_type &eval, bool record_only=false, MonitorStage stage=MonitorMCMC)
Add a monitor with an evaluation object.
Definition: sampler.hpp:498
const monitor_map_type & monitor() const
Read only access to all monitors to the the monitor_map_type object.
Definition: sampler.hpp:535
bool mcmc_queue_empty() const
Check if mcmc queue is empty.
Definition: sampler.hpp:403
void read_size_history(OutputIter first) const
Read sampler size history through an output iterator.
Definition: sampler.hpp:276
std::map< std::string, Monitor< T >> monitor_map_type
Definition: sampler.hpp:67
Residual systematic resampling.
bool move_queue_empty() const
Check if move queue is empty.
Definition: sampler.hpp:362
Sampler< T > & mcmc(InputIter first, InputIter last, bool append)
Add a sequence of new mcmcs.
Definition: sampler.hpp:422
std::size_t summary_header_size_int() const
The size of Sampler summary header (integer data, size etc.)
Definition: sampler.hpp:553
void resize(std::array< T, N > &, std::size_t)
Definition: common.hpp:138
Monitor evaluated after MCMC moves.
Definition: monitor.hpp:57
Residual resampling.
Definition: residual.hpp:43
Sampler< T > & init(const init_type &new_init)
Set the initialization object of type init_type.
Definition: sampler.hpp:317
Monitor for Monte Carlo integration.
Definition: monitor.hpp:63
std::size_t iter_num() const
Current iteration number (initialization count as zero)
Definition: sampler.hpp:208
Sampler(size_type N, const resample_type &res_op, double resample_threshold)
Construct a Sampler with a user defined resampling scheme and a threshold for resampling.
Definition: sampler.hpp:118
Systematic resampling.
Definition: defines.hpp:60
Sampler< T > & resample_threshold(double threshold)
Set resampling threshold.
Definition: sampler.hpp:250
std::size_t accept_history(std::size_t id, std::size_t iter) const
Get the accept count of a given move id and the iteration.
Definition: sampler.hpp:305
Sampler(size_type N)
Construct a Sampler without selection of resampling method.
Definition: sampler.hpp:70
static double resample_threshold_never()
Special value of resampling threshold that indicate no resampling will be ever performed.
Definition: sampler.hpp:258
Particle< T > & particle()
Read and write access to the Particle<T> object.
Definition: sampler.hpp:311
Sampler< T > & init_by_iter(bool initialize_by_iterate)
Set if initialization should use the move and mcmc queue.
Definition: sampler.hpp:332
std::function< std::size_t(Particle< T > &, void *)> init_type
Definition: sampler.hpp:64
Sampler< T > & move_queue_clear()
Clear the move queue.
Definition: sampler.hpp:355
Sampler< T > & resample_scheme(ResampleScheme scheme)
Set resampling method by a built-in ResampleScheme scheme name.
Definition: sampler.hpp:228
#define VSMC_RUNTIME_ASSERT_CORE_SAMPLER_FUNCTOR(func, caller, name)
Definition: sampler.hpp:43
Multinomial resampling.
Definition: multinomial.hpp:43
SMC Sampler.
Definition: sampler.hpp:58
std::function< void(std::size_t, std::size_t, Particle< T > &, double *)> eval_type
Definition: monitor.hpp:68
Monitor< T > & monitor(const std::string &name)
Read and write access to a named monitor.
Definition: sampler.hpp:509
bool resampled_history(std::size_t iter) const
Get resampling indicator of a given iteration.
Definition: sampler.hpp:292
const Monitor< T > & monitor(const std::string &name) const
Read only access to a named monitor.
Definition: sampler.hpp:519
std::function< std::size_t(std::size_t, Particle< T > &)> move_type
Definition: sampler.hpp:65
Sampler< T > & monitor(const std::string &name, const Monitor< T > &mon)
Add a monitor.
Definition: sampler.hpp:482
std::size_t mcmc_queue_size() const
Check the size of the mcmc queue.
Definition: sampler.hpp:406
void summary_data(OutputIter first) const
Sampler summary data (floating point data)
Definition: sampler.hpp:636
Sampler< T > & clone(const Sampler< T > &other, bool retain_rng)
Clone another sampler system except the RNG engines.
Definition: sampler.hpp:148
Monitor evaluated after resampling.
Definition: monitor.hpp:56