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-2015, 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 #include <vsmc/core/path.hpp>
39 
40 #define VSMC_RUNTIME_ASSERT_CORE_SAMPLER_MONITOR_NAME(iter, map, func) \
41  VSMC_RUNTIME_ASSERT( \
42  (iter != map.end()), "**Sampler::" #func "** INVALID MONITOR NAME")
43 
44 #define VSMC_RUNTIME_ASSERT_CORE_SAMPLER_FUNCTOR(func, caller, name) \
45  VSMC_RUNTIME_ASSERT(static_cast<bool>(func), \
46  "**Sampler::" #caller "** INVALID " #name " OBJECT")
47 
48 #define VSMC_RUNTIME_WARNING_CORE_SAMPLER_INIT_BY_ITER \
49  VSMC_RUNTIME_WARNING((!static_cast<bool>(init_)), \
50  "**Sampler::initialize** A VALID INIT OBJECT IS SET " \
51  "BUT INITILIALIZED BY ITERATING")
52 
53 namespace vsmc
54 {
55 
58 template <typename T>
59 class Sampler
60 {
61  public:
64  using value_type = T;
65  using init_type = std::function<std::size_t(Particle<T> &, void *)>;
66  using move_type = std::function<std::size_t(std::size_t, Particle<T> &)>;
67  using mcmc_type = std::function<std::size_t(std::size_t, Particle<T> &)>;
68  using monitor_map_type = std::map<std::string, Monitor<T>>;
69 
71  explicit Sampler(size_type N)
72  : particle_(N)
73  , init_by_iter_(false)
74  , resample_threshold_(resample_threshold_never())
75  , iter_num_(0)
76  , path_(typename Path<T>::eval_type())
77  {
79  }
80 
86  : particle_(N)
87  , init_by_iter_(false)
88  , resample_threshold_(resample_threshold_always())
89  , iter_num_(0)
90  , path_(typename Path<T>::eval_type())
91  {
92  resample_scheme(scheme);
93  }
94 
99  Sampler(size_type N, const resample_type &res_op)
100  : particle_(N)
101  , init_by_iter_(false)
102  , resample_threshold_(resample_threshold_always())
103  , iter_num_(0)
104  , path_(typename Path<T>::eval_type())
105  {
106  resample_scheme(res_op);
107  }
108 
112  : particle_(N)
113  , init_by_iter_(false)
114  , resample_threshold_(resample_threshold)
115  , iter_num_(0)
116  , path_(typename Path<T>::eval_type())
117  {
118  resample_scheme(scheme);
119  }
120 
124  size_type N, const resample_type &res_op, double resample_threshold)
125  : particle_(N)
126  , init_by_iter_(false)
127  , resample_threshold_(resample_threshold)
128  , iter_num_(0)
129  , path_(typename Path<T>::eval_type())
130  {
131  resample_scheme(res_op);
132  }
133 
138  Sampler<T> clone(bool new_rng) const
139  {
140  Sampler<T> sampler(*this);
141  if (new_rng) {
142  sampler.particle().rng_set().seed();
143  Seed::instance().seed_rng(sampler.particle().resample_rng());
144  }
145 
146  return sampler;
147  }
148 
154  Sampler<T> &clone(const Sampler<T> &other, bool retain_rng)
155  {
156  if (this != &other) {
157  particle_.clone(other.particle_, retain_rng);
158  init_by_iter_ = other.init_by_iter_;
159  init_ = other.init_;
160  move_queue_ = other.move_queue_;
161  mcmc_queue_ = other.mcmc_queue_;
162  resample_op_ = other.resample_op_;
163  resample_threshold_ = other.resample_threshold_;
164  iter_num_ = other.iter_num_;
165  size_history_ = other.size_history_;
166  ess_history_ = other.ess_history_;
167  resampled_history_ = other.resampled_history_;
168  accept_history_ = other.accept_history_;
169  }
170 
171  return *this;
172  }
173 
174  Sampler<T> &clone(Sampler<T> &&other, bool retain_rng)
175  {
176  if (this != &other) {
177  particle_.clone(std::move(other.particle_), retain_rng);
178  init_by_iter_ = other.init_by_iter_;
179  init_ = std::move(other.init_);
180  move_queue_ = std::move(other.move_queue_);
181  mcmc_queue_ = std::move(other.mcmc_queue_);
182  resample_op_ = std::move(other.resample_op_);
183  resample_threshold_ = other.resample_threshold_;
184  iter_num_ = other.iter_num_;
185  size_history_ = std::move(other.size_history_);
186  ess_history_ = std::move(other.ess_history_);
187  resampled_history_ = std::move(other.resampled_history_);
188  accept_history_ = std::move(other.accept_history_);
189  }
190 
191  return *this;
192  }
193 
195  size_type size() const { return particle_.size(); }
196 
198  void reserve(std::size_t num)
199  {
200  size_history_.reserve(num);
201  ess_history_.reserve(num);
202  resampled_history_.reserve(num);
203  for (auto &a : accept_history_)
204  a.reserve(num);
205  if (!path_.empty())
206  path_.reserve(num);
207  for (auto &m : monitor_)
208  if (!m.second.empty())
209  m.second.reserve(num);
210  }
211 
213  std::size_t iter_size() const { return size_history_.size(); }
214 
216  std::size_t iter_num() const { return iter_num_; }
217 
220  {
221  particle_.resample(resample_op_, resample_threshold_always());
222 
223  return *this;
224  }
225 
228  {
229  resample_op_ = res_op;
230 
231  return *this;
232  }
233 
237  {
238  switch (scheme) {
239  case Multinomial: resample_op_ = ResampleMultinomial(); break;
240  case Residual: resample_op_ = ResampleResidual(); break;
241  case Stratified: resample_op_ = ResampleStratified(); break;
242  case Systematic: resample_op_ = ResampleSystematic(); break;
243  case ResidualStratified:
244  resample_op_ = ResampleResidualStratified();
245  break;
246  case ResidualSystematic:
247  resample_op_ = ResampleResidualSystematic();
248  break;
249  }
250 
251  return *this;
252  }
253 
255  double resample_threshold() const { return resample_threshold_; }
256 
258  Sampler<T> &resample_threshold(double threshold)
259  {
260  resample_threshold_ = threshold;
261  return *this;
262  }
263 
266  static double resample_threshold_never()
267  {
268  return -std::numeric_limits<double>::infinity();
269  }
270 
274  {
275  return std::numeric_limits<double>::infinity();
276  }
277 
280  double size_history(std::size_t iter) const { return size_history_[iter]; }
281 
283  template <typename OutputIter>
284  void read_size_history(OutputIter first) const
285  {
286  std::copy(size_history_.begin(), size_history_.end(), first);
287  }
288 
290  double ess_history(std::size_t iter) const { return ess_history_[iter]; }
291 
293  template <typename OutputIter>
294  void read_ess_history(OutputIter first) const
295  {
296  std::copy(ess_history_.begin(), ess_history_.end(), first);
297  }
298 
300  bool resampled_history(std::size_t iter) const
301  {
302  return resampled_history_[iter];
303  }
304 
306  template <typename OutputIter>
307  void read_resampled_history(OutputIter first) const
308  {
309  std::copy(resampled_history_.begin(), resampled_history_.end(), first);
310  }
311 
313  std::size_t accept_history(std::size_t id, std::size_t iter) const
314  {
315  return accept_history_[id][iter];
316  }
317 
319  Particle<T> &particle() { return particle_; }
320 
322  const Particle<T> &particle() const { return particle_; }
323 
325  Sampler<T> &init(const init_type &new_init)
326  {
328 
329  init_ = new_init;
330 
331  return *this;
332  }
333 
340  Sampler<T> &init_by_iter(bool initialize_by_iterate)
341  {
342  init_by_iter_ = initialize_by_iterate;
343 
344  return *this;
345  }
346 
353  {
355 
356  init_ = init_op([new_init](
357  Particle<T> &particle, void *) { new_init(0, particle); });
358 
359  return *this;
360  }
361 
364  {
365  move_queue_.clear();
366  return *this;
367  }
368 
370  bool move_queue_empty() const { return move_queue_.empty(); }
371 
373  std::size_t move_queue_size() const { return move_queue_.size(); }
374 
376  Sampler<T> &move(const move_type &new_move, bool append)
377  {
379 
380  if (!append)
381  move_queue_.clear();
382  move_queue_.push_back(new_move);
383 
384  return *this;
385  }
386 
388  template <typename InputIter>
389  Sampler<T> &move(InputIter first, InputIter last, bool append)
390  {
391  if (!append)
392  move_queue_.clear();
393  while (first != last) {
395  move_queue_.push_back(*first);
396  ++first;
397  }
398 
399  return *this;
400  }
401 
404  {
405  mcmc_queue_.clear();
406 
407  return *this;
408  }
409 
411  bool mcmc_queue_empty() const { return mcmc_queue_.empty(); }
412 
414  std::size_t mcmc_queue_size() const { return mcmc_queue_.size(); }
415 
417  Sampler<T> &mcmc(const mcmc_type &new_mcmc, bool append)
418  {
420 
421  if (!append)
422  mcmc_queue_.clear();
423  mcmc_queue_.push_back(new_mcmc);
424 
425  return *this;
426  }
427 
429  template <typename InputIter>
430  Sampler<T> &mcmc(InputIter first, InputIter last, bool append)
431  {
432  if (!append)
433  mcmc_queue_.clear();
434  while (first != last) {
436  mcmc_queue_.push_back(*first);
437  ++first;
438  }
439 
440  return *this;
441  }
442 
451  Sampler<T> &initialize(void *param = nullptr)
452  {
453  do_reset();
454  do_acch();
455  if (init_by_iter_) {
457  do_iter();
458  } else {
459  do_init(param);
460  }
461  do_acch();
462 
463  return *this;
464  }
465 
472  Sampler<T> &iterate(std::size_t num = 1)
473  {
474  do_acch();
475  if (num > 1)
476  reserve(iter_size() + num);
477  for (std::size_t i = 0; i != num; ++i) {
478  ++iter_num_;
479  do_iter();
480  }
481  do_acch();
482 
483  return *this;
484  }
485 
487  Path<T> &path() { return path_; }
488 
490  const Path<T> &path() const { return path_; }
491 
494  const typename Path<T>::eval_type &eval, bool record_only = false)
495  {
496  path_.set_eval(eval, record_only);
497 
498  return *this;
499  }
500 
504  double path_sampling() const { return path_.log_zconst(); }
505 
510  Sampler<T> &monitor(const std::string &name, const Monitor<T> &mon)
511  {
512  monitor_.insert(std::make_pair(name, mon));
513 
514  return *this;
515  }
516 
526  Sampler<T> &monitor(const std::string &name, std::size_t dim,
527  const typename Monitor<T>::eval_type &eval, bool record_only = false,
528  MonitorStage stage = MonitorMCMC)
529  {
530  monitor_.insert(typename monitor_map_type::value_type(
531  name, Monitor<T>(dim, eval, record_only, stage)));
532 
533  return *this;
534  }
535 
537  Monitor<T> &monitor(const std::string &name)
538  {
539  typename monitor_map_type::iterator iter = monitor_.find(name);
540 
542 
543  return iter->second;
544  }
545 
547  const Monitor<T> &monitor(const std::string &name) const
548  {
549  typename monitor_map_type::const_iterator citer = monitor_.find(name);
550 
552  citer, monitor_, monitor);
553 
554  return citer->second;
555  }
556 
559  monitor_map_type &monitor() { return monitor_; }
560 
563  const monitor_map_type &monitor() const { return monitor_; }
564 
566  bool clear_monitor(const std::string &name)
567  {
568  return monitor_.erase(name) ==
569  static_cast<typename monitor_map_type::size_type>(1);
570  }
571 
574  {
575  monitor_.clear();
576 
577  return *this;
578  }
579 
581  std::size_t summary_header_size_int() const
582  {
583  if (iter_size() == 0)
584  return 0;
585 
586  return accept_history_.size() + 2;
587  }
588 
590  std::size_t summary_header_size() const
591  {
592  if (iter_size() == 0)
593  return 0;
594 
595  std::size_t header_size = 1;
596  if (path_.iter_size() > 0)
597  header_size += 2;
598  for (const auto &m : monitor_)
599  if (m.second.iter_size() > 0)
600  header_size += m.second.dim();
601 
602  return header_size;
603  }
604 
606  template <typename OutputIter>
607  void summary_header_int(OutputIter first) const
608  {
609  if (summary_header_size_int() == 0)
610  return;
611 
612  *first++ = std::string("Size");
613  *first++ = std::string("Resampled");
614  for (std::size_t i = 0; i != accept_history_.size(); ++i)
615  *first++ = "Accept." + internal::itos(i);
616  }
617 
619  template <typename OutputIter>
620  void summary_header(OutputIter first) const
621  {
622  if (summary_header_size() == 0)
623  return;
624 
625  *first++ = std::string("ESS");
626  if (path_.iter_size() > 0) {
627  *first++ = std::string("Path.Integrand");
628  *first++ = std::string("Path.Grid");
629  }
630  for (const auto &m : monitor_) {
631  if (m.second.iter_size() > 0) {
632  unsigned md = static_cast<unsigned>(m.second.dim());
633  for (unsigned d = 0; d != md; ++d) {
634  if (m.second.name(d).empty())
635  *first++ = m.first + "." + internal::itos(d);
636  else
637  *first++ = m.second.name(d);
638  }
639  }
640  }
641  }
642 
644  std::size_t summary_data_size_int() const
645  {
646  return summary_header_size_int() * iter_size();
647  }
648 
650  std::size_t summary_data_size() const
651  {
652  return summary_header_size() * iter_size();
653  }
654 
656  template <MatrixOrder Order, typename OutputIter>
657  void summary_data_int(OutputIter first) const
658  {
659  if (summary_data_size_int() == 0)
660  return;
661 
662  if (Order == RowMajor)
663  summary_data_row_int(first);
664  if (Order == ColMajor)
665  summary_data_col_int(first);
666  }
667 
669  template <MatrixOrder Order, typename OutputIter>
670  void summary_data(OutputIter first) const
671  {
672  if (summary_data_size() == 0)
673  return;
674 
675  if (Order == RowMajor)
676  summary_data_row(first);
677  if (Order == ColMajor)
678  summary_data_col(first);
679  }
680 
685  template <typename CharT, typename Traits>
686  std::basic_ostream<CharT, Traits> &print(
687  std::basic_ostream<CharT, Traits> &os, char sepchar = '\t') const
688  {
689  if (iter_size() == 0 || !os.good())
690  return os;
691 
692  std::size_t nrow = iter_size();
693 
694  std::size_t ncol_int = summary_header_size_int();
695  Vector<std::string> header_int(ncol_int);
696  Vector<size_type> data_int(nrow * ncol_int);
697  summary_header_int(header_int.begin());
698  summary_data_int<RowMajor>(data_int.begin());
699 
700  std::size_t ncol = summary_header_size();
701  Vector<std::string> header(ncol);
702  Vector<double> data(nrow * ncol);
703  summary_header(header.begin());
704  summary_data<RowMajor>(data.begin());
705 
706  for (const auto &h : header_int)
707  os << h << sepchar;
708  for (const auto &h : header)
709  os << h << sepchar;
710  os << '\n';
711 
712  std::size_t offset_int = 0;
713  std::size_t offset = 0;
714  for (std::size_t r = 0; r != nrow; ++r) {
715  for (std::size_t c = 0; c != ncol_int; ++c)
716  os << data_int[offset_int++] << sepchar;
717  for (std::size_t c = 0; c != ncol; ++c)
718  os << data[offset++] << sepchar;
719  os << '\n';
720  }
721 
722  return os;
723  }
724 
725  private:
726  Particle<T> particle_;
727 
728  bool init_by_iter_;
729  init_type init_;
730  Vector<move_type> move_queue_;
731  Vector<mcmc_type> mcmc_queue_;
732 
733  resample_type resample_op_;
734  double resample_threshold_;
735 
736  std::size_t iter_num_;
737  Vector<std::size_t> size_history_;
738  Vector<double> ess_history_;
739  Vector<bool> resampled_history_;
740  Vector<Vector<std::size_t>> accept_history_;
741 
742  Path<T> path_;
743  monitor_map_type monitor_;
744 
745  void do_acch()
746  {
747  if (accept_history_.empty())
748  accept_history_.push_back(Vector<std::size_t>());
749  std::size_t acc_size = move_queue_.size() + mcmc_queue_.size();
750  if (accept_history_.size() < acc_size) {
751  std::size_t diff = acc_size - accept_history_.size();
752  for (std::size_t d = 0; d != diff; ++d)
753  accept_history_.push_back(Vector<std::size_t>());
754  }
755  for (std::size_t i = 0; i != accept_history_.size(); ++i)
756  accept_history_[i].resize(iter_size());
757  }
758 
759  void do_reset()
760  {
761  size_history_.clear();
762  ess_history_.clear();
763  resampled_history_.clear();
764  accept_history_.clear();
765  path_.clear();
766  for (auto &m : monitor_)
767  m.second.clear();
768  iter_num_ = 0;
769  particle_.weight().set_equal();
770  }
771 
772  void do_init(void *param)
773  {
775  accept_history_[0].push_back(init_(particle_, param));
776  do_monitor(MonitorMove);
777  do_resample();
778  do_monitor(MonitorResample);
779  do_mcmc(1);
780  do_monitor(MonitorMCMC);
781  }
782 
783  void do_iter()
784  {
785  std::size_t ia = do_move(0);
786  do_monitor(MonitorMove);
787  do_resample();
788  do_monitor(MonitorResample);
789  do_mcmc(ia);
790  do_monitor(MonitorMCMC);
791  }
792 
793  std::size_t do_move(std::size_t ia)
794  {
795  for (auto &m : move_queue_)
796  accept_history_[ia++].push_back(m(iter_num_, particle_));
797 
798  return ia;
799  }
800 
801  std::size_t do_mcmc(std::size_t ia)
802  {
803  for (auto &m : mcmc_queue_)
804  accept_history_[ia++].push_back(m(iter_num_, particle_));
805 
806  return ia;
807  }
808 
809  void do_resample()
810  {
811  size_history_.push_back(size());
812  ess_history_.push_back(particle_.weight().ess());
813  resampled_history_.push_back(
814  particle_.resample(resample_op_, resample_threshold_));
815  }
816 
817  void do_monitor(MonitorStage stage)
818  {
819  if (!path_.empty() && stage == MonitorMCMC)
820  path_.eval(iter_num_, particle_);
821 
822  for (auto &m : monitor_)
823  if (!m.second.empty())
824  m.second.eval(iter_num_, particle_, stage);
825  }
826 
827  template <typename OutputIter>
828  void summary_data_row_int(OutputIter first) const
829  {
830  using int_type = typename std::iterator_traits<OutputIter>::value_type;
831  for (std::size_t iter = 0; iter != iter_size(); ++iter) {
832  *first++ = static_cast<int_type>(size_history_[iter]);
833  *first++ = static_cast<int_type>(resampled_history_[iter]);
834  for (std::size_t i = 0; i != accept_history_.size(); ++i)
835  *first++ = static_cast<int_type>(accept_history_[i][iter]);
836  }
837  }
838 
839  template <typename OutputIter>
840  void summary_data_col_int(OutputIter first) const
841  {
842  first = std::copy(size_history_.begin(), size_history_.end(), first);
843  first = std::copy(
844  resampled_history_.begin(), resampled_history_.end(), first);
845  for (std::size_t i = 0; i != accept_history_.size(); ++i) {
846  first = std::copy(
847  accept_history_[i].begin(), accept_history_[i].end(), first);
848  }
849  }
850 
851  template <typename OutputIter>
852  void summary_data_row(OutputIter first) const
853  {
854  double missing_data = std::numeric_limits<double>::quiet_NaN();
855 
856  std::size_t piter = 0;
857 
859  for (const auto &m : monitor_)
860  if (m.second.iter_size() > 0)
861  miter.push_back(std::make_pair(0, &m.second));
862 
863  for (std::size_t iter = 0; iter != iter_size(); ++iter) {
864  *first++ = ess_history_[iter];
865  if (path_.iter_size() > 0) {
866  if (piter != path_.iter_size() && iter == path_.index(piter)) {
867  *first++ = path_.integrand(piter);
868  *first++ = path_.grid(piter);
869  ++piter;
870  } else {
871  *first++ = missing_data;
872  *first++ = missing_data;
873  }
874  }
875  for (auto &m : miter) {
876  std::size_t md = m.second->dim();
877  if (m.first != m.second->iter_size() &&
878  iter == m.second->index(m.first)) {
879  first = std::copy_n(
880  m.second->record_data(m.first++), md, first);
881  } else {
882  first = std::fill_n(first, md, missing_data);
883  }
884  }
885  }
886  }
887 
888  template <typename OutputIter>
889  void summary_data_col(OutputIter first) const
890  {
891  double missing_data = std::numeric_limits<double>::quiet_NaN();
892 
893  first = std::copy(ess_history_.begin(), ess_history_.end(), first);
894  if (path_.iter_size() > 0) {
895  std::size_t piter = 0;
896  for (std::size_t iter = 0; iter != iter_size(); ++iter) {
897  if (piter != path_.iter_size() || iter == path_.index(piter))
898  *first++ = path_.integrand(piter++);
899  else
900  *first = missing_data;
901  }
902  piter = 0;
903  for (std::size_t iter = 0; iter != iter_size(); ++iter) {
904  if (piter != path_.iter_size() || iter == path_.index(piter))
905  *first++ = path_.grid(piter++);
906  else
907  *first = missing_data;
908  }
909  }
910  for (const auto &m : monitor_) {
911  if (m.second.iter_size() > 0) {
912  for (std::size_t d = 0; d != m.second.dim(); ++d) {
913  std::size_t miter = 0;
914  for (std::size_t iter = 0; iter != iter_size(); ++iter) {
915  if (miter != m.second.iter_size() ||
916  iter == m.second.index(miter)) {
917  *first++ = m.second.record(d, miter++);
918  } else {
919  *first++ = missing_data;
920  }
921  }
922  }
923  }
924  }
925  }
926 }; // class Sampler
927 
928 template <typename CharT, typename Traits, typename T>
929 inline std::basic_ostream<CharT, Traits> &operator<<(
930  std::basic_ostream<CharT, Traits> &os, const Sampler<T> &sampler)
931 {
932  return sampler.print(os);
933 }
934 
935 } // namespace vsmc
936 
937 #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
Path< T > & path()
Read and write access to the Path sampling monitor.
Definition: sampler.hpp:487
Particle class representing the whole particle set.
Definition: particle.hpp:48
const Path< T > & path() const
Read only access to the Path sampling monitor.
Definition: sampler.hpp:490
Sampler(size_type N, const resample_type &res_op)
Construct a Sampler with a user defined resampling operation.
Definition: sampler.hpp:99
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:686
Sampler< T > & path_sampling(const typename Path< T >::eval_type &eval, bool record_only=false)
Set the Path sampling evaluation object.
Definition: sampler.hpp:493
void summary_data_int(OutputIter first) const
Sampler summary data (integer data)
Definition: sampler.hpp:657
Sampler< T > & mcmc_queue_clear()
Clear the mcmc queue.
Definition: sampler.hpp:403
Stratified resampling.
Definition: stratified.hpp:43
Sampler< T > & resample()
Force resample.
Definition: sampler.hpp:219
double ess_history(std::size_t iter) const
Get ESS of a given iteration, initialization count as iter 0.
Definition: sampler.hpp:290
Sampler< T > & init_by_move(const move_type &new_init)
Set the initialization object with a type move_type object.
Definition: sampler.hpp:352
typename Particle< T >::resample_type resample_type
Definition: sampler.hpp:63
Sampler< T > & clear_monitor()
Erase all monitors.
Definition: sampler.hpp:573
size_type size() const
Number of particles.
Definition: sampler.hpp:195
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:85
double grid(std::size_t iter) const
Get the Path sampling grid value of a given Path iteration.
Definition: path.hpp:139
#define VSMC_RUNTIME_WARNING_CORE_SAMPLER_INIT_BY_ITER
Definition: sampler.hpp:48
const Particle< T > & particle() const
Read only access to the Particle<T> object.
Definition: sampler.hpp:322
monitor_map_type & monitor()
Read and write access to all monitors to the monitor_map_type object.
Definition: sampler.hpp:559
void eval(std::size_t iter, Particle< T > &particle)
Definition: path.hpp:188
std::size_t summary_header_size() const
The size of Sampler summary header (floating point data)
Definition: sampler.hpp:590
double size_history(std::size_t iter) const
Get sampler size of a given iteration (initialization count as iteration zero)
Definition: sampler.hpp:280
ResampleScheme
Resampling schemes.
Definition: defines.hpp:67
typename std::conditional< std::is_scalar< T >::value, std::vector< T, AlignedAllocator< T >>, std::vector< T >>::type Vector
AlignedVector for scalar type and std::vector for others.
std::size_t index(std::size_t iter) const
Get the iteration index of the sampler of a given monitor iteration.
Definition: path.hpp:123
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:174
std::size_t summary_data_size_int() const
The size of Sampler summary data (integer data)
Definition: sampler.hpp:644
Stratified resampling on residuals.
Definition: defines.hpp:72
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:111
Sampler< T > & initialize(void *param=nullptr)
Initialization.
Definition: sampler.hpp:451
std::size_t iter_size() const
Number of iterations (including initialization)
Definition: sampler.hpp:213
std::function< double(std::size_t, Particle< T > &, double *)> eval_type
Definition: path.hpp:56
Sampler< T > & mcmc(const mcmc_type &new_mcmc, bool append)
Add a new mcmc.
Definition: sampler.hpp:417
void summary_header(OutputIter first) const
Sampler summary header (floating point data)
Definition: sampler.hpp:620
Multinomial resampling.
Definition: defines.hpp:68
Monitor evaluated after moves.
Definition: monitor.hpp:55
std::string itos(UIntType i, std::true_type)
Definition: common.hpp:89
bool resample(const resample_type &op, double threshold)
Performing resampling if ESS/N < threshold.
Definition: particle.hpp:155
double path_sampling() const
Path sampling estimate of the logarithm of normalizing constants ratio.
Definition: sampler.hpp:504
void summary_header_int(OutputIter first) const
Sampler summary header (integer data)
Definition: sampler.hpp:607
std::size_t move_queue_size() const
Check the size of the move queue.
Definition: sampler.hpp:373
void copy(std::size_t n, const T *x, std::size_t incx, T *y, std::size_t incy)
Copies vector to another vector.
Definition: cblas.hpp:82
double resample_threshold() const
Get resampling threshold.
Definition: sampler.hpp:255
Stratified resampling.
Definition: defines.hpp:69
Data are stored column by column in memory.
Definition: defines.hpp:55
std::function< std::size_t(std::size_t, Particle< T > &)> mcmc_type
Definition: sampler.hpp:67
bool empty() const
Whether the evaluation object is valid.
Definition: path.hpp:117
#define VSMC_RUNTIME_ASSERT_CORE_SAMPLER_MONITOR_NAME(iter, map, func)
Definition: sampler.hpp:40
Sampler< T > & iterate(std::size_t num=1)
Iteration.
Definition: sampler.hpp:472
Sampler< T > & move(const move_type &new_move, bool append)
Add a new move.
Definition: sampler.hpp:376
static double resample_threshold_always()
Special value of resampling threshold that indicate no resampling will always be performed.
Definition: sampler.hpp:273
void read_resampled_history(OutputIter first) const
Read resampling indicator history through an output iterator.
Definition: sampler.hpp:307
std::size_t summary_data_size() const
The size of Sampler summary data (floating point data)
Definition: sampler.hpp:650
typename Particle< T >::size_type size_type
Definition: sampler.hpp:62
void read_ess_history(OutputIter first) const
Read ESS history through an output iterator.
Definition: sampler.hpp:294
Sampler< T > & move(InputIter first, InputIter last, bool append)
Add a sequence of new moves.
Definition: sampler.hpp:389
void reserve(std::size_t num)
Reserve space for a specified number of iterations.
Definition: sampler.hpp:198
Residual resampling.
Definition: defines.hpp:71
bool clear_monitor(const std::string &name)
Erase a named monitor.
Definition: sampler.hpp:566
Sampler< T > & resample_scheme(const resample_type &res_op)
Set resampling method by a resample_type object.
Definition: sampler.hpp:227
void clear()
Clear all records of the index and integrations.
Definition: path.hpp:218
Systematic resampling.
Definition: systematic.hpp:43
Systematic resampling on residuals.
Definition: defines.hpp:73
Data are stored row by row in memory.
Definition: defines.hpp:54
std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const Sampler< T > &sampler)
Definition: sampler.hpp:929
Sampler< T > clone(bool new_rng) const
Clone the sampler system except the RNG engines.
Definition: sampler.hpp:138
std::function< void(std::size_t, std::size_t, resample_rng_type &, const double *, size_type *)> resample_type
Definition: particle.hpp:58
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:526
const monitor_map_type & monitor() const
Read only access to all monitors to the the monitor_map_type object.
Definition: sampler.hpp:563
bool mcmc_queue_empty() const
Check if mcmc queue is empty.
Definition: sampler.hpp:411
void read_size_history(OutputIter first) const
Read sampler size history through an output iterator.
Definition: sampler.hpp:284
std::map< std::string, Monitor< T >> monitor_map_type
Definition: sampler.hpp:68
Monitor for Path sampling.
Definition: path.hpp:51
std::size_t iter_size() const
The number of iterations has been recorded.
Definition: path.hpp:106
Residual systematic resampling.
bool move_queue_empty() const
Check if move queue is empty.
Definition: sampler.hpp:370
Sampler< T > & mcmc(InputIter first, InputIter last, bool append)
Add a sequence of new mcmcs.
Definition: sampler.hpp:430
std::size_t summary_header_size_int() const
The size of Sampler summary header (integer data, size etc.)
Definition: sampler.hpp:581
Monitor evaluated after MCMC moves.
Definition: monitor.hpp:57
double integrand(std::size_t iter) const
Get the Path sampling integrand of a given Path iteration.
Definition: path.hpp:131
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:325
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:216
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:123
Systematic resampling.
Definition: defines.hpp:70
Sampler< T > & resample_threshold(double threshold)
Set resampling threshold.
Definition: sampler.hpp:258
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:313
Sampler(size_type N)
Construct a Sampler without selection of resampling method.
Definition: sampler.hpp:71
static double resample_threshold_never()
Special value of resampling threshold that indicate no resampling will be ever performed.
Definition: sampler.hpp:266
Particle< T > & particle()
Read and write access to the Particle<T> object.
Definition: sampler.hpp:319
Sampler< T > & init_by_iter(bool initialize_by_iterate)
Set if initialization should use the move and mcmc queue.
Definition: sampler.hpp:340
std::function< std::size_t(Particle< T > &, void *)> init_type
Definition: sampler.hpp:65
Sampler< T > & move_queue_clear()
Clear the move queue.
Definition: sampler.hpp:363
Sampler< T > & resample_scheme(ResampleScheme scheme)
Set resampling method by a built-in ResampleScheme scheme name.
Definition: sampler.hpp:236
#define VSMC_RUNTIME_ASSERT_CORE_SAMPLER_FUNCTOR(func, caller, name)
Definition: sampler.hpp:44
Multinomial resampling.
Definition: multinomial.hpp:43
SMC Sampler.
Definition: sampler.hpp:59
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:537
bool resampled_history(std::size_t iter) const
Get resampling indicator of a given iteration.
Definition: sampler.hpp:300
const Monitor< T > & monitor(const std::string &name) const
Read only access to a named monitor.
Definition: sampler.hpp:547
std::function< std::size_t(std::size_t, Particle< T > &)> move_type
Definition: sampler.hpp:66
Sampler< T > & monitor(const std::string &name, const Monitor< T > &mon)
Add a monitor.
Definition: sampler.hpp:510
std::size_t mcmc_queue_size() const
Check the size of the mcmc queue.
Definition: sampler.hpp:414
void summary_data(OutputIter first) const
Sampler summary data (floating point data)
Definition: sampler.hpp:670
Sampler< T > & clone(const Sampler< T > &other, bool retain_rng)
Clone another sampler system except the RNG engines.
Definition: sampler.hpp:154
Monitor evaluated after resampling.
Definition: monitor.hpp:56