numerics 0.1.0
Loading...
Searching...
No Matches
sample.hpp
Go to the documentation of this file.
1/// @file solve/sample.hpp
2/// @brief sample(model, sampler, rng): the stochastic-sampling verb (MCMC), the
3/// counterpart to solve() for targets that are sampled rather than solved.
4#pragma once
5
7#include "stochastic/mcmc.hpp"
8#include <functional>
9
10namespace num {
11
12/// @brief An MCMC target: site count, per-site acceptance/proposal, observable.
13struct MCMCModel {
14 std::function<double(int)> accept_prob; ///< acceptance probability at a site
15 std::function<void(int)> propose; ///< propose and apply a move at a site
17 std::function<double()> measure; ///< observable estimator, sampled each sweep
18};
19
20/// @brief Run equilibration then measurement sweeps; return the mean observable.
21template<IsMCMCAlg A, typename RNG>
22double sample(const MCMCModel& model, const A& alg, RNG& rng) {
23 for (int s = 0; s < alg.equilibration; ++s)
25 double acc = 0.0;
26 for (int s = 0; s < alg.measurements; ++s) {
28 acc += model.measure();
29 }
30 return acc / alg.measurements;
31}
32
33} // namespace num
Algorithm tags: carry the numerics, not the mathematics.
Metropolis-Hastings sweep API.
MetropolisStats metropolis_sweep_prob(idx n_sites, ProbFn acceptance_prob, Apply apply_flip, RNG &rng)
Metropolis sweep with caller-supplied acceptance probabilities.
Definition mcmc_impl.hpp:54
double sample(const MCMCModel &model, const A &alg, RNG &rng)
Run equilibration then measurement sweeps; return the mean observable.
Definition sample.hpp:22
An MCMC target: site count, per-site acceptance/proposal, observable.
Definition sample.hpp:13
std::function< double()> measure
observable estimator, sampled each sweep
Definition sample.hpp:17
std::function< void(int)> propose
propose and apply a move at a site
Definition sample.hpp:15
std::function< double(int)> accept_prob
acceptance probability at a site
Definition sample.hpp:14