32 #ifndef VSMC_CORE_SAMPLER_HPP    33 #define VSMC_CORE_SAMPLER_HPP    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")    43 #define VSMC_RUNTIME_ASSERT_CORE_SAMPLER_FUNCTOR(func, caller, name)          \    44     VSMC_RUNTIME_ASSERT(                                                      \    45         (func), "**Sampler::" #caller "** INVALID " #name " OBJECT")    61         static_cast<int>(s1) & static_cast<int>(s2));
    67         static_cast<int>(s1) | static_cast<int>(s2));
    73         static_cast<int>(s1) ^ static_cast<int>(s2));
   103     using eval_type = std::function<std::size_t(std::size_t, Particle<T> &)>;
   109     template <
typename... Args>
   111         : particle_(
std::forward<Args>(args)...)
   121         sampler.
particle().rng_set().seed();
   134         size_history_.reserve(num);
   135         ess_history_.reserve(num);
   136         resampled_history_.reserve(num);
   137         status_history_.reserve(num);
   138         for (
auto &m : monitor_)
   139             m.second.reserve(num);
   143     std::size_t 
iter_size()
 const { 
return size_history_.size(); }
   154         eval_.push_back(std::make_pair(new_eval, stage));
   193         resample_eval_ = res_eval;
   205         resample_threshold_ = threshold;
   213         return -std::numeric_limits<double>::infinity();
   220         return std::numeric_limits<double>::infinity();
   229         monitor_.insert(std::make_pair(name, mon));
   237         typename monitor_map_type::iterator iter = monitor_.find(name);
   247         typename monitor_map_type::const_iterator citer = monitor_.find(name);
   252         return citer->second;
   258         return monitor_.erase(name) ==
   259             static_cast<typename monitor_map_type::size_type
>(1);
   283         for (std::size_t i = 0; i != num; ++i)
   299         return size_history_[iter];
   303     template <
typename OutputIter>
   306         return std::copy(size_history_.begin(), size_history_.end(), first);
   311     double ess_history(std::size_t iter)
 const { 
return ess_history_[iter]; }
   314     template <
typename OutputIter>
   317         return std::copy(ess_history_.begin(), ess_history_.end(), first);
   323         return resampled_history_[iter];
   327     template <
typename OutputIter>
   331             resampled_history_.begin(), resampled_history_.end(), first);
   337         return status_history_[iter][id];
   341     std::map<std::string, Vector<double>> 
summary()
 const   343         const double missing_data = std::numeric_limits<double>::quiet_NaN();
   345         std::map<std::string, Vector<double>> df;
   348         std::copy(size_history_.begin(), size_history_.end(), data.begin());
   351         std::copy(resampled_history_.begin(), resampled_history_.end(),
   353         df[
"Resampled"] = data;
   355         df[
"ESS"] = ess_history_;
   357         std::size_t ssize = 0;
   358         for (std::size_t i = 0; i != 
iter_size(); ++i)
   359             if (ssize < status_history_[i].
size())
   360                 ssize = status_history_[i].
size();
   361         for (std::size_t j = 0; j != ssize; ++j) {
   362             for (std::size_t i = 0; i != 
iter_size(); ++i) {
   363                 data[i] = status_history_[i].size() > j ?
   364                     status_history_[i][j] :
   367             df[
"Status." + std::to_string(j)] = data;
   370         for (
const auto &m : monitor_) {
   371             if (m.second.iter_size() > 0) {
   372                 for (std::size_t d = 0; d != m.second.dim(); ++d) {
   373                     std::size_t miter = 0;
   374                     for (std::size_t i = 0; i != 
iter_size(); ++i) {
   375                         if (miter != m.second.iter_size() &&
   376                             i == m.second.index(miter)) {
   377                             data[i] = m.second.record(d, miter++);
   379                             data[i] = missing_data;
   382                     if (m.second.name(d).empty())
   383                         df[m.first + 
"." + std::to_string(d)] = data;
   385                         df[m.second.name(d)] = data;
   397     template <
typename CharT, 
typename Traits>
   398     std::basic_ostream<CharT, Traits> &
print(
   399         std::basic_ostream<CharT, Traits> &os, 
char sepchar = 
'\t')
 const   404         const std::map<std::string, Vector<double>> df = 
summary();
   406         for (
const auto &v : df)
   407             os << v.first << sepchar;
   409         for (std::size_t i = 0; i != 
iter_size(); ++i) {
   410             for (
const auto &v : df)
   411                 os << v.second[i] << sepchar;
   419     using monitor_map_type = std::map<std::string, Monitor<T>>;
   422     std::size_t iter_num_;
   426     double resample_threshold_;
   433     monitor_map_type monitor_;
   438         particle_.
weight().set_equal();
   439         for (
auto &m : monitor_)
   442         size_history_.clear();
   443         ess_history_.clear();
   444         resampled_history_.clear();
   445         status_history_.clear();
   468         do_resample(resample_threshold_);
   472         status_history_.push_back(std::move(status_));
   477         for (
auto &e : eval_)
   479                 status_.push_back(e.first(iter_num_, particle_));
   484         for (
auto &e : eval_)
   486                 status_.push_back(e.first(iter_num_, particle_));
   491         for (
auto &e : eval_)
   493                 status_.push_back(e.first(iter_num_, particle_));
   496     void do_resample(
double threshold)
   498         size_history_.push_back(
size());
   499         ess_history_.push_back(particle_.
weight().ess());
   501         if (ess_history_.back() < 
size() * threshold) {
   502             resampled_history_.push_back(
true);
   503             resample_eval_(iter_num_, particle_);
   505             resampled_history_.push_back(
false);
   511         for (
auto &m : monitor_)
   512             if (!m.second.empty())
   513                 m.second(iter_num_, particle_, stage);
   517 template <
typename CharT, 
typename Traits, 
typename T>
   519     std::basic_ostream<CharT, Traits> &os, 
const Sampler<T> &sampler)
   521     return sampler.
print(os);
   526 #endif // VSMC_CORE_SAMPLER_HPP std::vector< T, Alloc > Vector
std::vector with Allocator as default allocator 
 
ResampleAlgorithm< U01SequenceSorted, false > ResampleMultinomial
Multinomial resampling. 
 
OutputIter read_size_history(OutputIter first) const 
Read sampler size history through an output iterator. 
 
static SeedGenerator< ID, ResultType > & instance()
 
Particle class representing the whole particle set. 
 
SamplerStage & operator&=(SamplerStage &s1, SamplerStage s2)
 
std::basic_ostream< CharT, Traits > & print(std::basic_ostream< CharT, Traits > &os, char sepchar= '\t') const 
Print the history of the Sampler. 
 
double ess_history(std::size_t iter) const 
Get ESS of a given iteration, (initialization count as iter zero) 
 
size_type size() const 
Number of particles. 
 
MonitorStage
Monitor stage. 
 
Sampler< T > clone() const 
Clone the Sampler except the RNG engines. 
 
const Particle< T > & particle() const 
Read only access to the Particle<T> object. 
 
Evaluation at iteration before resampling. 
 
Sampler< T > & resample_method(const eval_type &res_eval, double threshold=resample_threshold_always())
Set resampling method by a eval_type object. 
 
Sampler(Args &&...args)
Construct a Sampler. 
 
ResampleScheme
Resampling schemes. 
 
weight_type & weight()
Read and write access to the weight collection object. 
 
Stratified resampling on residuals. 
 
bool monitor_clear(const std::string &name)
Erase a named monitor. 
 
OutputIter read_resampled_history(OutputIter first) const 
Read resampling indicator history through an output iterator. 
 
std::size_t iter_size() const 
Number of iterations (including initialization) 
 
ResampleAlgorithm< U01SequenceStratified, false > ResampleStratified
Stratified resampling. 
 
Sampler< T > & monitor_clear()
Erase all monitors. 
 
ResampleAlgorithm< U01SequenceSystematic, true > ResampleResidualSystematic
Residual systematic resampling. 
 
Monitor evaluated after moves. 
 
Sampler< T > & eval(const eval_type &new_eval, SamplerStage stage, bool append=true)
Add a new evaluation object. 
 
Sampler<T>::eval_type subtype. 
 
double resample_threshold() const 
Get resampling threshold. 
 
#define VSMC_RUNTIME_ASSERT_CORE_SAMPLER_MONITOR_NAME(iter, map, func)            
 
Sampler< T > & iterate(std::size_t num=1)
Iteration. 
 
static double resample_threshold_always()
Special value of resampling threshold that indicate no resampling will always be performed. 
 
typename Particle< T >::size_type size_type
 
ResampleAlgorithm< U01SequenceStratified, true > ResampleResidualStratified
Residual stratified resampling. 
 
std::function< std::size_t(std::size_t, Particle< T > &)> eval_type
 
OutputIter read_ess_history(OutputIter first) const 
Read ESS history through an output iterator. 
 
void reserve(std::size_t num)
Reserve space for a specified number of iterations. 
 
size_type size_history(std::size_t iter) const 
Get sampler size of a given iteration (initialization count as iteration zero) 
 
Systematic resampling on residuals. 
 
constexpr SamplerStage operator~(SamplerStage s)
 
std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const Sampler< T > &sampler)
 
constexpr SamplerStage operator&(SamplerStage s1, SamplerStage s2)
 
SamplerStage
Sampler evaluaiton stages. 
 
SamplerStage & operator^=(SamplerStage &s1, SamplerStage s2)
 
std::size_t status_history(std::size_t iter, std::size_t id) const 
Get the status of a given move id and the iteration. 
 
constexpr SamplerStage operator^(SamplerStage s1, SamplerStage s2)
 
Sampler< T > & resample_method(ResampleScheme scheme, double threshold=resample_threshold_always())
Set resampling method by a built-in ResampleScheme scheme name. 
 
std::map< std::string, Vector< double > > summary() const 
Summary of sampler history. 
 
Monitor evaluated after MCMC moves. 
 
Monitor for Monte Carlo integration. 
 
Evaluation at initialization before resampling. 
 
std::size_t iter_num() const 
Current iteration number (initialization count as zero) 
 
Sampler< T > & resample_threshold(double threshold)
Set resampling threshold. 
 
SamplerStage & operator|=(SamplerStage &s1, SamplerStage s2)
 
constexpr SamplerStage operator|(SamplerStage s1, SamplerStage s2)
 
static double resample_threshold_never()
Special value of resampling threshold that indicate no resampling will be ever performed. 
 
Particle< T > & particle()
Read and write access to the Particle<T> object. 
 
ResampleAlgorithm< U01SequenceSorted, true > ResampleResidual
Residual resampling. 
 
Sampler< T > & initialize()
Initialization. 
 
Monitor< T > & monitor(const std::string &name)
Read and write access to a named monitor. 
 
Evaluation at after resampling. 
 
bool resampled_history(std::size_t iter) const 
Get resampling indicator of a given iteration. 
 
const Monitor< T > & monitor(const std::string &name) const 
Read only access to a named monitor. 
 
Sampler< T > & monitor(const std::string &name, const Monitor< T > &mon)
Add a monitor. 
 
Monitor evaluated after resampling. 
 
ResampleAlgorithm< U01SequenceSystematic, false > ResampleSystematic
Systematic resampling.