32 #ifndef MCKL_ALGORITHM_MCMC_HPP 33 #define MCKL_ALGORITHM_MCMC_HPP 44 template <
typename T,
typename U =
double>
57 template <
typename,
typename =
double>
60 template <
typename T,
typename U>
64 using eval_type = std::function<std::size_t(std::size_t, T &)>;
70 template <
typename T,
typename U>
78 template <
typename... Args>
81 , state_(
std::forward<Args>(args)...)
89 return accept_history_.size() == 0 ? 0 :
90 accept_history_.front().size();
97 for (
auto &a : accept_history_) {
115 accept_history_.clear();
119 template <
typename Eval>
121 std::enable_if_t<!std::is_integral<Eval>::value> * =
nullptr)
123 return this->
eval(0, std::forward<Eval>(
eval));
130 template <
typename Estimator>
134 0, std::forward<Estimator>(estimator));
153 for (std::size_t i = 0; i != n; ++i) {
165 template <
typename OutputIter>
169 "**MCMCSampler::read_accept_history** index out of range");
172 accept_history_[i].
begin(), accept_history_[i].
end(), first);
176 template <
typename OutputIter>
180 if (accept_history_.size() > 0) {
181 const std::size_t n = accept_history_.front().size();
182 const std::size_t d = accept_history_.size();
183 for (std::size_t i = 0; i != n; ++i) {
184 for (std::size_t j = 0; j != d; ++j) {
185 *first++ = accept_history_[j][i];
190 for (std::size_t i = 0; i != accept_history_.size(); ++i) {
191 first = read_accept_history(i, first);
205 accept_history_.resize(this->
eval(0).
size());
206 for (std::size_t i = 0; i != this->
eval(0).size(); ++i) {
207 accept_history_[i].push_back(this->
eval(0)[i](iter_, state_));
211 e.estimate(iter_, state_);
220 #endif // MCKL_ALGORITHM_MCMC_HPP std::size_t num_iter() const noexcept
The number of iterations stored in the estimate matrix.
void clear()
Clear all history.
std::function< std::size_t(std::size_t, T &)> eval_type
void reserve(std::size_t n)
Reserve space for additional iterations.
void eval(CallArgs &&... args)
void clear()
Clear the estimate matrix but preserve the dimension.
MCMCSampler(Args &&... args)
std::vector< T, Alloc > Vector
std::vector with Allocator as the default allocator
Sampler for iterative Monte Carlo algorithms.
const estimator_type & estimator(std::size_t k) const
const eval_type & mutation(std::size_t k) const
typename Sampler< MCMCSampler< T, U > >::eval_type eval_type
estimator_type & estimator(std::size_t k)
iterator end()
Iterator to one pass the lower right corner of the matrix.
static constexpr MatrixLayout layout()
The layout of the matrix.
state_type & state()
Read and write access to the state object.
size_type size() const
The total number of elements.
iterator begin()
Iterator to the upper left corner of the matrix.
typename Sampler< MCMCSampler< T, U > >::estimator_type estimator_type
OutputIter read_accept_history(std::size_t i, OutputIter first) const
Read accept count history given mutation step index.
std::size_t num_iter() const
The number of iterations already performed.
U * insert_estimate()
Add space for a new estimate, return a pointer to the new row.
const state_type & state() const
Read only access to the state object.
void reserve(std::size_t n)
Reserve space for a specified number of iterations.
void estimate(std::size_t iter, T &state)
std::size_t mutation(Eval &&eval, std::enable_if_t<!std::is_integral< Eval >::value > *=nullptr)
Add a new evaluation object for the mutation step.
eval_type & mutation(std::size_t k)
Estimator for iterative Monte Carlo algorithms.
void reset()
Reset the sampler by clear all history, evaluation objects, and estimators.
std::size_t dim() const noexcept
The dimension of the estimator.
void iterate(std::size_t n=1)
Iterate the sampler.
std::size_t estimator(Estimator &&estimator)
OutputIter read_accept_history(MatrixLayout layout, OutputIter first) const
Read all accept count history into a matrix.
void runtime_assert(bool cond, const char *msg, bool soft=false)
MatrixLayout
Matrix layout.