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,2014, 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((iter != map.end()), \
42  ("**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 INITIALIZATION OBJECT IS SET " \
51  "BUT INITILIALIZED BY ITERATING"))
52 
53 namespace vsmc {
54 
57 template <typename T>
58 class Sampler
59 {
60  public :
61 
64  typedef T value_type;
65  typedef cxx11::function<std::size_t (Particle<T> &, void *)> init_type;
66  typedef cxx11::function<std::size_t (std::size_t, Particle<T> &)>
68  typedef cxx11::function<std::size_t (std::size_t, Particle<T> &)>
70  typedef std::map<std::string, Monitor<T> > monitor_map_type;
71 
80  explicit Sampler (size_type N) :
81  init_by_iter_(false), resample_threshold_(resample_threshold_never()),
82  particle_(N), iter_num_(0), path_(typename Path<T>::eval_type())
84 
90  Sampler (size_type N, ResampleScheme scheme) :
91  init_by_iter_(false), resample_threshold_(resample_threshold_always()),
92  particle_(N), iter_num_(0), path_(typename Path<T>::eval_type())
93  {resample_scheme(scheme);}
94 
102  Sampler (size_type N, ResampleScheme scheme, double resample_threshold) :
103  init_by_iter_(false), resample_threshold_(resample_threshold),
104  particle_(N), iter_num_(0), path_(typename Path<T>::eval_type())
105  {resample_scheme(scheme);}
106 
113  Sampler (size_type N, const resample_type &res_op,
114  double resample_threshold = 0.5) :
115  init_by_iter_(false), resample_threshold_(resample_threshold),
116  particle_(N), iter_num_(0), path_(typename Path<T>::eval_type())
117  {resample_scheme(res_op);}
118 
123  Sampler<T> clone (bool new_rng) const
124  {
125  Sampler<T> sampler(*this);
126 
127  if (new_rng) {
128  sampler.particle().rng_set().seed();
129  sampler.particle().resample_rng().seed(Seed::instance().get());
130  }
131 
132  return sampler;
133  }
134 
140  Sampler<T> &clone (const Sampler<T> &other, bool retain_rng)
141  {
142  if (this != &other) {
143  if (retain_rng) {
144 #if VSMC_HAS_CXX11_RVALUE_REFERENCES
145  typename Particle<T>::rng_set_type rset(
146  cxx11::move(particle_.rng_set()));
147  typename Particle<T>::resample_rng_type rrng(
148  cxx11::move(particle_.resample_rng()));
149  *this = other;
150  particle_.rng_set() = cxx11::move(rset);
151  particle_.resample_rng() = cxx11::move(rrng);
152 #else
153  using std::swap;
154 
155  typename Particle<T>::rng_set_type rset(0);
156  swap(rset, particle_.rng_set());
157  typename Particle<T>::resample_rng_type rrng(
158  particle_.resample_rng());
159  *this = other;
160  swap(rset, particle_.rng_set());
161  particle_.resample_rng() = rrng;
162 #endif
163  particle_.rng_set().resize(other.size());
164  } else {
165  *this = other;
166  }
167  }
168 
169  return *this;
170  }
171 
172 #if VSMC_HAS_CXX11_RVALUE_REFERENCES
173  Sampler<T> &clone (Sampler<T> &&other, bool retain_rng)
174  {
175  if (this != &other) {
176  if (retain_rng) {
177  typename Particle<T>::rng_set_type rset(
178  cxx11::move(particle_.rng_set()));
179  typename Particle<T>::resample_rng_type rrng(
180  cxx11::move(particle_.resample_rng()));
181  *this = cxx11::move(other);
182  particle_.rng_set() = cxx11::move(rset);
183  particle_.resample_rng() = cxx11::move(rrng);
184  particle_.rng_set().resize(other.size());
185  } else {
186  *this = cxx11::move(other);
187  }
188  }
189 
190  return *this;
191  }
192 #endif
193 
195  size_type size () const {return particle_.size();}
196 
198  void reserve (std::size_t num)
199  {
200  ess_history_.reserve(num);
201  resampled_history_.reserve(num);
202  for (std::size_t i = 0; i != accept_history_.size(); ++i)
203  accept_history_[i].reserve(num);
204  if (!path_.empty())
205  path_.reserve(num);
206  for (typename monitor_map_type::iterator
207  m = monitor_.begin(); m != monitor_.end(); ++m) {
208  if (!m->second.empty())
209  m->second.reserve(num);
210  }
211  }
212 
214  std::size_t iter_size () const {return ess_history_.size();}
215 
222  std::size_t iter_num () const {return iter_num_;}
223 
226  {
227  particle_.resample(resample_op_,
228  std::numeric_limits<double>::max VSMC_MNE ());
229 
230  return *this;
231  }
232 
234  Sampler<T> &resample_scheme (const resample_type &res_op)
235  {resample_op_ = res_op; return *this;}
236 
240  {
241  switch (scheme) {
242  case Multinomial :
243  resample_op_ = ResampleType<Multinomial>::type();
244  break;
245  case Residual :
246  resample_op_ = ResampleType<Residual>::type();
247  break;
248  case Stratified :
249  resample_op_ = ResampleType<Stratified>::type();
250  break;
251  case Systematic :
252  resample_op_ = ResampleType<Systematic>::type();
253  break;
254  case ResidualStratified :
256  break;
257  case ResidualSystematic :
259  break;
260  }
261 
262  return *this;
263  }
264 
266  double resample_threshold () const {return resample_threshold_;}
267 
269  Sampler<T> &resample_threshold (double threshold)
270  {resample_threshold_ = threshold; return *this;}
271 
274  static double resample_threshold_never ()
275  {return -std::numeric_limits<double>::infinity();}
276 
279  static double resample_threshold_always ()
280  {return std::numeric_limits<double>::infinity();}
281 
283  double ess_history (std::size_t iter) const {return ess_history_[iter];}
284 
286  template <typename OutputIter>
287  void read_ess_history (OutputIter first) const
288  {std::copy(ess_history_.begin(), ess_history_.end(), first);}
289 
291  bool resampled_history (std::size_t iter) const
292  {return resampled_history_[iter];}
293 
295  template <typename OutputIter>
296  void read_resampled_history (OutputIter first) const
297  {std::copy(resampled_history_.begin(), resampled_history_.end(), first);}
298 
300  std::size_t accept_history (std::size_t id, std::size_t iter) const
301  {return accept_history_[id][iter];}
302 
304  Particle<T> &particle () {return particle_;}
305 
307  const Particle<T> &particle () const {return particle_;}
308 
310  Sampler<T> &init (const init_type &new_init)
311  {
312  VSMC_RUNTIME_ASSERT_CORE_SAMPLER_FUNCTOR(new_init, init, INITIALIZE);
313 
314  init_ = new_init;
315 
316  return *this;
317  }
318 
325  Sampler<T> &init_by_iter (bool initialize_by_iterate)
326  {
327  init_by_iter_ = initialize_by_iterate;
328 
329  return *this;
330  }
331 
337  Sampler<T> &init_by_move (const move_type &new_init)
338  {
340 
341  class init_op
342  {
343  public :
344 
345  init_op (const move_type &new_move) : move_(new_move) {}
346 
347  std::size_t operator() (Particle<T> &particle, void *)
348  {return move_(0, particle);}
349 
350  private :
351 
352  move_type move_;
353  }; // class init_op
354 
355  init_ = init_op(new_init);
356 
357  return *this;
358  }
359 
361  Sampler<T> &move_queue_clear () {move_queue_.clear(); return *this;}
362 
364  bool move_queue_empty () const {return move_queue_.empty();}
365 
367  std::size_t move_queue_size () const {return move_queue_.size();}
368 
370  Sampler<T> &move (const move_type &new_move, bool append)
371  {
373 
374  if (!append)
375  move_queue_.clear();
376  move_queue_.push_back(new_move);
377 
378  return *this;
379  }
380 
382  template <typename InputIter>
383  Sampler<T> &move (InputIter first, InputIter last, bool append)
384  {
385  if (!append)
386  move_queue_.clear();
387  while (first != last) {
389  move_queue_.push_back(*first);
390  ++first;
391  }
392 
393  return *this;
394  }
395 
397  Sampler<T> &mcmc_queue_clear () {mcmc_queue_.clear(); return *this;}
398 
400  bool mcmc_queue_empty () const {return mcmc_queue_.empty();}
401 
403  std::size_t mcmc_queue_size () const {return mcmc_queue_.size();}
404 
406  Sampler<T> &mcmc (const mcmc_type &new_mcmc, bool append)
407  {
409 
410  if (!append)
411  mcmc_queue_.clear();
412  mcmc_queue_.push_back(new_mcmc);
413 
414  return *this;
415  }
416 
418  template <typename InputIter>
419  Sampler<T> &mcmc (InputIter first, InputIter last, bool append)
420  {
421  if (!append)
422  mcmc_queue_.clear();
423  while (first != last) {
425  mcmc_queue_.push_back(*first);
426  ++first;
427  }
428 
429  return *this;
430  }
431 
441  {
442  do_init();
443  if (init_by_iter_) {
445  do_iter();
446  } else {
448  init_, initialize, INITIALIZE);
449  std::size_t acc = init_(particle_, param);
450  accept_history_.push_back(std::vector<std::size_t>(1, acc));
451  do_resample();
452  }
453  do_monitor();
454 
455  return *this;
456  }
457 
464  Sampler<T> &iterate (std::size_t num = 1)
465  {
466  do_acch();
467 
468  if (num > 1)
469  reserve(iter_size() + num);
470 
471  for (std::size_t i = 0; i != num; ++i) {
472  ++iter_num_;
473  do_iter();
474  do_monitor();
475  }
476 
477  do_acch();
478 
479  return *this;
480  }
481 
483  Path<T> &path () {return path_;}
484 
486  const Path<T> &path () const {return path_;}
487 
490  bool record_only = false)
491  {path_.set_eval(eval, record_only); return *this;}
492 
495  double path_sampling () const {return path_.log_zconst();}
496 
501  Sampler<T> &monitor (const std::string &name, const Monitor<T> &mon)
502  {monitor_.insert(std::make_pair(name, mon)); return *this;}
503 
512  Sampler<T> &monitor (const std::string &name, std::size_t dim,
513  const typename Monitor<T>::eval_type &eval,
514  bool record_only = false)
515  {
516  monitor_.insert(typename monitor_map_type::value_type(
517  name, Monitor<T>(dim, eval, record_only)));
518 
519  return *this;
520  }
521 
523  Monitor<T> &monitor (const std::string &name)
524  {
525  typename monitor_map_type::iterator iter = monitor_.find(name);
526 
528  iter, monitor_, monitor);
529 
530  return iter->second;
531  }
532 
534  const Monitor<T> &monitor (const std::string &name) const
535  {
536  typename monitor_map_type::const_iterator citer = monitor_.find(name);
537 
539  citer, monitor_, monitor);
540 
541  return citer->second;
542  }
543 
546  monitor_map_type &monitor () {return monitor_;}
547 
550  const monitor_map_type &monitor () const {return monitor_;}
551 
553  bool clear_monitor (const std::string &name)
554  {
555  return monitor_.erase(name) ==
556  static_cast<typename monitor_map_type::size_type>(1);
557  }
558 
560  Sampler<T> &clear_monitor () {monitor_.clear(); return *this;}
561 
563  std::size_t summary_header_size () const
564  {
565  if (iter_size() == 0)
566  return 0;
567 
568  std::size_t header_size = 1; // ESS
569  header_size += accept_history_.size();
570  if (path_.iter_size() > 0)
571  header_size += 2;
572  for (typename monitor_map_type::const_iterator
573  m = monitor_.begin(); m != monitor_.end(); ++m) {
574  if (m->second.iter_size() > 0)
575  header_size += m->second.dim();
576  }
577 
578  return header_size;
579  }
580 
582  template <typename OutputIter>
583  void summary_header (OutputIter first) const
584  {
585  if (summary_header_size() == 0)
586  return;
587 
588  *first = std::string("ESS");
589  ++first;
590 
591  std::stringstream ss;
592 
593  unsigned accd = static_cast<unsigned>(accept_history_.size());
594  for (unsigned i = 0; i != accd; ++i) {
595  ss.str(std::string());
596  ss << "Accept." << i + 1;
597  *first = ss.str();
598  ++first;
599  }
600 
601  if (path_.iter_size() > 0) {
602  *first = std::string("Path.Integrand");
603  ++first;
604  *first = std::string("Path.Grid");
605  ++first;
606  }
607 
608  for (typename monitor_map_type::const_iterator
609  m = monitor_.begin(); m != monitor_.end(); ++m) {
610  if (m->second.iter_size() > 0) {
611  unsigned mond = static_cast<unsigned>(m->second.dim());
612  for (unsigned i = 0; i != mond; ++i, ++first) {
613  if (!m->second.name(i).empty()) {
614  *first = m->second.name(i);
615  } else {
616  ss.str(std::string());
617  ss << m->first << '.' << i + 1;
618  *first = ss.str();
619  }
620  }
621  }
622  }
623  }
624 
626  std::size_t summary_data_size () const
627  {return summary_header_size() * iter_size();}
628 
632  template <MatrixOrder Order, typename OutputIter>
633  void summary_data (OutputIter first) const
634  {
635  if (summary_data_size() == 0)
636  return;
637 
638  if (Order == RowMajor)
639  summary_data_row(first);
640 
641  if (Order == ColMajor)
642  summary_data_col(first);
643  }
644 
649  template <typename CharT, typename Traits>
650  std::basic_ostream<CharT, Traits> &print (
651  std::basic_ostream<CharT, Traits> &os, char sepchar = '\t') const
652  {
653  if (iter_size() == 0 || !os.good())
654  return os;
655 
656  std::size_t var_num = summary_header_size();
657  std::size_t dat_num = summary_data_size();
658  std::vector<std::string> header(var_num);
659  std::vector<double> data(dat_num);
660  summary_header(header.begin());
661  summary_data<RowMajor>(data.begin());
662 
663  os << "Resampled";
664  for (std::size_t i = 0; i != header.size(); ++i)
665  os << sepchar << header[i];
666  os << '\n';
667 
668  std::size_t offset = 0;
669  for (std::size_t iter = 0; iter != iter_size(); ++iter) {
670  os << resampled_history_[iter];
671  for (std::size_t i = 0; i != var_num; ++i)
672  os << sepchar << data[offset++];
673  os << '\n';
674  }
675 
676  return os;
677  }
678 
679  private :
680 
681  bool init_by_iter_;
682  init_type init_;
683  std::vector<move_type> move_queue_;
684  std::vector<mcmc_type> mcmc_queue_;
685 
686  resample_type resample_op_;
687  double resample_threshold_;
688 
689  Particle<T> particle_;
690  std::size_t iter_num_;
691  std::vector<double> ess_history_;
692  std::vector<bool> resampled_history_;
693  std::vector<std::vector<std::size_t> > accept_history_;
694 
695  Path<T> path_;
696  monitor_map_type monitor_;
697 
698  void do_acch ()
699  {
700  if (accept_history_.empty())
701  accept_history_.push_back(std::vector<std::size_t>());
702 
703  std::size_t acc_size = move_queue_.size() + mcmc_queue_.size();
704  if (accept_history_.size() < acc_size) {
705  std::size_t diff = acc_size - accept_history_.size();
706  for (std::size_t d = 0; d != diff; ++d)
707  accept_history_.push_back(std::vector<std::size_t>());
708  }
709  for (std::size_t i = 0; i != accept_history_.size(); ++i)
710  accept_history_[i].resize(iter_size());
711  }
712 
713  void do_init ()
714  {
715  ess_history_.clear();
716  resampled_history_.clear();
717  accept_history_.clear();
718  path_.clear();
719  for (typename monitor_map_type::iterator
720  m = monitor_.begin(); m != monitor_.end(); ++m)
721  m->second.clear();
722 
723  iter_num_ = 0;
724  particle_.weight_set().set_equal_weight();
725  }
726 
727  void do_iter ()
728  {
729  std::size_t ia = 0;
730  ia = do_move(ia);
731  do_resample();
732  ia = do_mcmc(ia);
733  for (; ia != accept_history_.size(); ++ia)
734  accept_history_[ia].push_back(0);
735  }
736 
737  std::size_t do_move (std::size_t ia)
738  {
739  for (typename std::vector<move_type>::iterator
740  m = move_queue_.begin(); m != move_queue_.end(); ++m) {
741  std::size_t acc = (*m)(iter_num_, particle_);
742  accept_history_[ia].push_back(acc);
743  ++ia;
744  }
745 
746  return ia;
747  }
748 
749  std::size_t do_mcmc (std::size_t ia)
750  {
751  for (typename std::vector<mcmc_type>::iterator
752  m = mcmc_queue_.begin(); m != mcmc_queue_.end(); ++m) {
753  std::size_t acc = (*m)(iter_num_, particle_);
754  accept_history_[ia].push_back(acc);
755  ++ia;
756  }
757 
758  return ia;
759  }
760 
761  void do_resample ()
762  {
763  ess_history_.push_back(particle_.weight_set().ess());
764  resampled_history_.push_back(particle_.resample(
765  resample_op_, resample_threshold_));
766  }
767 
768  void do_monitor ()
769  {
770  if (!path_.empty())
771  path_.eval(iter_num_, particle_);
772 
773  for (typename monitor_map_type::iterator
774  m = monitor_.begin(); m != monitor_.end(); ++m) {
775  if (!m->second.empty())
776  m->second.eval(iter_num_, particle_);
777  }
778  }
779 
780  template <typename OutputIter>
781  void summary_data_row (OutputIter first) const
782  {
783  double missing_data = std::numeric_limits<double>::quiet_NaN();
784 
785  std::size_t piter = 0;
786  std::vector<std::size_t> miter(monitor_.size(), 0);
787  for (std::size_t iter = 0; iter != iter_size(); ++iter) {
788  *first = ess_history_[iter] / size();
789  ++first;
790  for (std::size_t i = 0; i != accept_history_.size(); ++i) {
791  *first = accept_history_[i][iter] /
792  static_cast<double>(size());
793  ++first;
794  }
795  if (path_.iter_size() > 0) {
796  if (piter == path_.iter_size() || iter != path_.index(piter)) {
797  *first = missing_data;
798  ++first;
799  *first = missing_data;
800  ++first;
801  } else {
802  *first = path_.integrand(piter);
803  ++first;
804  *first = path_.grid(piter);
805  ++first;
806  ++piter;
807  }
808  }
809  std::size_t mm = 0;
810  for (typename monitor_map_type::const_iterator
811  m = monitor_.begin(); m != monitor_.end(); ++m, ++mm) {
812  if (m->second.iter_size() > 0) {
813  if (miter[mm] == m->second.iter_size()
814  || iter != m->second.index(miter[mm])) {
815  for (std::size_t i = 0; i != m->second.dim();
816  ++i, ++first)
817  *first = missing_data;
818  } else {
819  for (std::size_t i = 0; i != m->second.dim();
820  ++i, ++first)
821  *first = m->second.record(i, miter[mm]);
822  ++miter[mm];
823  }
824  }
825  }
826  }
827  }
828 
829  template <typename OutputIter>
830  void summary_data_col (OutputIter first) const
831  {
832  double missing_data = std::numeric_limits<double>::quiet_NaN();
833 
834  for (std::size_t iter = 0; iter != iter_size(); ++iter, ++first)
835  *first = ess_history_[iter] / size();
836  for (std::size_t i = 0; i != accept_history_.size(); ++i) {
837  for (std::size_t iter = 0; iter != iter_size(); ++iter, ++first)
838  *first = accept_history_[i][iter] /
839  static_cast<double>(size());
840  }
841  if (path_.iter_size() > 0) {
842  std::size_t piter;
843  piter = 0;
844  for (std::size_t iter = 0; iter != iter_size(); ++iter, ++first) {
845  if (piter == path_.iter_size() ||iter != path_.index(piter)) {
846  *first = missing_data;
847  } else {
848  *first = path_.integrand(piter);
849  ++piter;
850  }
851  }
852  piter = 0;
853  for (std::size_t iter = 0; iter != iter_size(); ++iter, ++first) {
854  if (piter == path_.iter_size() ||iter != path_.index(piter)) {
855  *first = missing_data;
856  } else {
857  *first = path_.grid(piter);
858  ++piter;
859  }
860  }
861  }
862  for (typename monitor_map_type::const_iterator
863  m = monitor_.begin(); m != monitor_.end(); ++m) {
864  if (m->second.iter_size() > 0) {
865  for (std::size_t i = 0; i != m->second.dim(); ++i) {
866  std::size_t miter = 0;
867  for (std::size_t iter = 0; iter != iter_size();
868  ++iter, ++first) {
869  if (miter == m->second.iter_size()
870  || iter != m->second.index(miter)) {
871  *first = missing_data;
872  } else {
873  *first = m->second.record(i, miter);
874  ++miter;
875  }
876  }
877  }
878  }
879  }
880  }
881 }; // class Sampler
882 
883 template <typename CharT, typename Traits, typename T>
884 inline std::basic_ostream<CharT, Traits> &operator<< (
885  std::basic_ostream<CharT, Traits> &os, const Sampler<T> &sampler)
886 {return sampler.print(os);}
887 
888 } // namespace vsmc
889 
890 #endif // VSMC_CORE_SAMPLER_HPP
cxx11::function< double(std::size_t, const Particle< T > &, double *)> eval_type
Definition: path.hpp:57
static SeedGenerator< ID, ResultType > & instance()
Definition: seed.hpp:94
Definition: adapter.hpp:37
Path< T > & path()
Read and write access to the Path sampling monitor.
Definition: sampler.hpp:483
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:486
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:650
Sampler< T > & monitor(const std::string &name, std::size_t dim, const typename Monitor< T >::eval_type &eval, bool record_only=false)
Add a monitor with an evaluation object.
Definition: sampler.hpp:512
Sampler< T > & path_sampling(const typename Path< T >::eval_type &eval, bool record_only=false)
Set the Path sampling evaluation object.
Definition: sampler.hpp:489
Sampler< T > & mcmc_queue_clear()
Clear the mcmc queue.
Definition: sampler.hpp:397
Sampler< T > & resample()
Force resample.
Definition: sampler.hpp:225
double ess_history(std::size_t iter) const
Get ESS of a given iteration, initialization count as iter 0.
Definition: sampler.hpp:283
Sampler< T > & init_by_move(const move_type &new_init)
Set the initialization object with a type move_type object.
Definition: sampler.hpp:337
Sampler< T > & clear_monitor()
Erase all monitors.
Definition: sampler.hpp:560
size_type size() const
Number of particles.
Definition: sampler.hpp:195
Sampler(size_type N, ResampleScheme scheme)
Construct a Sampler with a built-in resampling scheme.
Definition: sampler.hpp:90
double grid(std::size_t iter) const
Get the Path sampling grid value of a given Path iteration.
Definition: path.hpp:130
#define VSMC_RUNTIME_WARNING_CORE_SAMPLER_INIT_BY_ITER
Definition: sampler.hpp:48
const Particle< T > & particle() const
Read only access to the Particle object.
Definition: sampler.hpp:307
monitor_map_type & monitor()
Read and write access to all monitors to the monitor_map_type object.
Definition: sampler.hpp:546
std::size_t summary_header_size() const
The size of Sampler summary header.
Definition: sampler.hpp:563
void eval(std::size_t iter, const Particle< T > &particle)
Definition: path.hpp:170
std::size_t index(std::size_t iter) const
Get the iteration index of the sampler of a given monitor iteration.
Definition: path.hpp:114
traits::RngSetTypeTrait< T >::type rng_set_type
Definition: particle.hpp:55
Sampler< T > & clone(Sampler< T > &&other, bool retain_rng)
Definition: sampler.hpp:173
traits::ResampleRngTypeTrait< T >::type resample_rng_type
Definition: particle.hpp:56
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:102
Sampler< T > & initialize(void *param=nullptr)
Initialization.
Definition: sampler.hpp:440
std::size_t iter_size() const
Number of iterations (including initialization)
Definition: sampler.hpp:214
ResampleScheme
Resampling schemes.
Definition: common.hpp:108
Sampler< T > & mcmc(const mcmc_type &new_mcmc, bool append)
Add a new mcmc.
Definition: sampler.hpp:406
void summary_header(OutputIter first) const
Sampler summary header.
Definition: sampler.hpp:583
#define VSMC_RUNTIME_ASSERT_CORE_SAMPLER_MONITOR_NAME(iter, map, func)
Definition: sampler.hpp:40
cxx11::function< std::size_t(Particle< T > &, void *)> init_type
Definition: sampler.hpp:65
cxx11::function< void(std::size_t, std::size_t, resample_rng_type &, const double *, size_type *)> resample_type
Definition: particle.hpp:62
Multinomial resampling.
Definition: common.hpp:109
bool resample(const resample_type &op, double threshold)
Performing resampling if ESS/N < threshold.
Definition: particle.hpp:234
double path_sampling() const
Path sampling estimate of the logarithm of normalizing constants ratio.
Definition: sampler.hpp:495
std::size_t move_queue_size() const
Check the size of the move queue.
Definition: sampler.hpp:367
double resample_threshold() const
Get resampling threshold.
Definition: sampler.hpp:266
Data are stored column by column in memory.
Definition: defines.hpp:104
cxx11::function< void(std::size_t, std::size_t, const Particle< T > &, double *)> eval_type
Definition: monitor.hpp:63
bool empty() const
Whether the evaluation object is valid.
Definition: path.hpp:108
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:370
static double resample_threshold_always()
Special value of resampling threshold that indicate no resampling will always be performed.
Definition: sampler.hpp:279
void read_resampled_history(OutputIter first) const
Read resampling indicator history through an output iterator.
Definition: sampler.hpp:296
std::size_t summary_data_size() const
The size of Sampler summary data.
Definition: sampler.hpp:626
void read_ess_history(OutputIter first) const
Read ESS history through an output iterator.
Definition: sampler.hpp:287
Sampler< T > & move(InputIter first, InputIter last, bool append)
Add a sequence of new moves.
Definition: sampler.hpp:383
Stratified resampling.
Definition: common.hpp:111
#define VSMC_MNE
Avoid MSVC stupid behavior: MNE = Macro No Expansion.
Definition: defines.hpp:38
void reserve(std::size_t num)
Reserve space for a specified number of iterations.
Definition: sampler.hpp:198
bool clear_monitor(const std::string &name)
Erase a named monitor.
Definition: sampler.hpp:553
cxx11::function< std::size_t(std::size_t, Particle< T > &)> move_type
Definition: sampler.hpp:67
Sampler< T > & resample_scheme(const resample_type &res_op)
Set resampling method by a resample_type object.
Definition: sampler.hpp:234
void clear()
Clear all records of the index and integrations.
Definition: path.hpp:201
Residual resampling.
Definition: common.hpp:110
Data are stored row by row in memory.
Definition: defines.hpp:103
#define VSMC_RUNTIME_ASSERT_CORE_SAMPLER_FUNCTOR(func, caller, name)
Definition: sampler.hpp:44
remove_reference< T >::type && move(T &&t) noexcept
std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const Sampler< T > &sampler)
Definition: sampler.hpp:884
Sampler< T > clone(bool new_rng) const
Clone the sampler system except the RNG engines.
Definition: sampler.hpp:123
Resample< cxx11::integral_constant< ResampleScheme, Scheme > > type
Definition: common.hpp:125
const monitor_map_type & monitor() const
Read only access to all monitors to the the monitor_map_type object.
Definition: sampler.hpp:550
Particle< T >::resample_type resample_type
Definition: sampler.hpp:63
#define VSMC_NULLPTR
nullptr
Definition: defines.hpp:79
bool mcmc_queue_empty() const
Check if mcmc queue is empty.
Definition: sampler.hpp:400
Monitor for Path sampling.
Definition: path.hpp:51
std::size_t iter_size() const
The number of iterations has been recorded.
Definition: path.hpp:97
void swap(Array< T, N > &ary1, Array< T, N > &ary2)
Array ADL of swap.
Definition: array.hpp:317
bool move_queue_empty() const
Check if move queue is empty.
Definition: sampler.hpp:364
Sampler< T > & mcmc(InputIter first, InputIter last, bool append)
Add a sequence of new mcmcs.
Definition: sampler.hpp:419
traits::SizeTypeTrait< T >::type size_type
Definition: particle.hpp:52
Particle< T >::size_type size_type
Definition: sampler.hpp:62
double integrand(std::size_t iter) const
Get the Path sampling integrand of a given Path iteration.
Definition: path.hpp:122
Sampler< T > & init(const init_type &new_init)
Set the initialization object of type init_type.
Definition: sampler.hpp:310
Monitor for Monte Carlo integration.
Definition: monitor.hpp:56
cxx11::function< std::size_t(std::size_t, Particle< T > &)> mcmc_type
Definition: sampler.hpp:69
std::size_t iter_num() const
Current iteration number (initialization count as zero)
Definition: sampler.hpp:222
Sampler< T > & resample_threshold(double threshold)
Set resampling threshold.
Definition: sampler.hpp:269
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:300
Sampler(size_type N)
Construct a Sampler without selection of resampling method.
Definition: sampler.hpp:80
Systematic resampling on residuals.
Definition: common.hpp:114
static double resample_threshold_never()
Special value of resampling threshold that indicate no resampling will be ever performed.
Definition: sampler.hpp:274
Particle< T > & particle()
Read and write access to the Particle object.
Definition: sampler.hpp:304
Sampler< T > & init_by_iter(bool initialize_by_iterate)
Set if initialization should use the move and mcmc queue.
Definition: sampler.hpp:325
Sampler< T > & move_queue_clear()
Clear the move queue.
Definition: sampler.hpp:361
Stratified resampling on residuals.
Definition: common.hpp:113
Sampler< T > & resample_scheme(ResampleScheme scheme)
Set resampling method by a built-in ResampleScheme scheme name.
Definition: sampler.hpp:239
SMC Sampler.
Definition: sampler.hpp:58
Monitor< T > & monitor(const std::string &name)
Read and write access to a named monitor.
Definition: sampler.hpp:523
bool resampled_history(std::size_t iter) const
Get resampling indicator of a given iteration.
Definition: sampler.hpp:291
Sampler(size_type N, const resample_type &res_op, double resample_threshold=0.5)
Construct a Sampler with a user defined resampling operation.
Definition: sampler.hpp:113
std::map< std::string, Monitor< T > > monitor_map_type
Definition: sampler.hpp:70
const Monitor< T > & monitor(const std::string &name) const
Read only access to a named monitor.
Definition: sampler.hpp:534
Sampler< T > & monitor(const std::string &name, const Monitor< T > &mon)
Add a monitor.
Definition: sampler.hpp:501
weight_set_type & weight_set()
Read and write access to the weight collection object.
Definition: particle.hpp:155
Systematic resampling.
Definition: common.hpp:112
std::size_t mcmc_queue_size() const
Check the size of the mcmc queue.
Definition: sampler.hpp:403
void summary_data(OutputIter first) const
Sampler summary data.
Definition: sampler.hpp:633
Sampler< T > & clone(const Sampler< T > &other, bool retain_rng)
Clone another sampler system except the RNG engines.
Definition: sampler.hpp:140