numerics
Loading...
Searching...
No Matches
mcmc_impl.hpp
Go to the documentation of this file.
1/// @file markov/detail/mcmc_impl.hpp
2/// @brief Template implementations for markov/mcmc.hpp.
3/// Included at the bottom of mcmc.hpp -- do not include directly.
4#pragma once
5#include "stochastic/mcmc.hpp"
6#include <random>
7#include <cmath>
8
9namespace num::markov {
10
11/// @brief Single Metropolis sweep: n_sites random single-site updates.
12///
13/// The acceptance probability is min(1, exp(-beta * dE)) computed from
14/// the value returned by delta_energy(i).
15///
16/// @tparam DeltaE Callable: idx -> real. Returns dE for proposing a flip at i.
17/// @tparam Apply Callable: idx -> void. Applies the flip at site i.
18/// @tparam RNG Random number generator (e.g., std::mt19937).
19template<typename DeltaE, typename Apply, typename RNG>
24 real beta,
25 RNG& rng)
26{
27 std::uniform_real_distribution<real> u01(0.0, 1.0);
28 std::uniform_int_distribution<idx> site_dist(0, n_sites - 1);
29
30 MetropolisStats stats;
31 stats.total = n_sites;
32
33 for (idx k = 0; k < n_sites; ++k) {
34 idx i = site_dist(rng);
36 real p = (dE <= 0.0) ? 1.0 : std::exp(-beta * dE);
37 if (p >= 1.0 || u01(rng) < p) {
39 ++stats.accepted;
40 }
41 }
42 return stats;
43}
44
45/// @brief Metropolis sweep with caller-supplied acceptance probabilities.
46///
47/// Use this variant when acceptance probabilities are precomputed (e.g. a
48/// Boltzmann table for a discrete-dE system like the Ising model), avoiding
49/// a runtime exp() call per proposed flip.
50///
51/// @tparam ProbFn Callable: idx -> real. Returns min(1, exp(-beta*dE)) for site i.
52/// @tparam Apply Callable: idx -> void. Applies the flip at site i.
53/// @tparam RNG Random number generator.
54template<typename ProbFn, typename Apply, typename RNG>
59 RNG& rng)
60{
61 std::uniform_real_distribution<real> u01(0.0, 1.0);
62 std::uniform_int_distribution<idx> site_dist(0, n_sites - 1);
63
64 MetropolisStats stats;
65 stats.total = n_sites;
66
67 for (idx k = 0; k < n_sites; ++k) {
68 idx i = site_dist(rng);
70 if (p >= 1.0 || u01(rng) < p) {
72 ++stats.accepted;
73 }
74 }
75 return stats;
76}
77
78/// @brief Umbrella-sampled Metropolis sweep (dE variant).
79///
80/// Performs a sweep, measures the order parameter, then restores the saved
81/// state if the order parameter falls outside [window.lo, window.hi].
82///
83/// @tparam DeltaE Callable: idx -> real
84/// @tparam Apply Callable: idx -> void
85/// @tparam Save Callable: () -> void. Saves system state before the sweep.
86/// @tparam Restore Callable: () -> void. Restores saved state.
87/// @tparam Measure Callable: () -> idx. Returns the order parameter.
88/// @tparam RNG Random number generator.
89template<typename DeltaE, typename Apply,
90 typename Save, typename Restore, typename Measure,
91 typename RNG>
100 real beta,
101 RNG& rng)
102{
103 save_state();
105 idx op = measure_order();
106
107 UmbrellaStats stats;
108 stats.mc = mc;
109 stats.order_param = op;
110
111 if (!window.contains(op)) {
113 stats.reverted = true;
114 stats.order_param = measure_order();
115 }
116 return stats;
117}
118
119/// @brief Umbrella-sampled Metropolis sweep (precomputed probability variant).
120///
121/// Same as umbrella_sweep but uses caller-supplied acceptance probabilities.
122///
123/// @tparam ProbFn Callable: idx -> real
124/// @tparam Apply Callable: idx -> void
125/// @tparam Save Callable: () -> void
126/// @tparam Restore Callable: () -> void
127/// @tparam Measure Callable: () -> idx
128/// @tparam RNG Random number generator.
129template<typename ProbFn, typename Apply,
130 typename Save, typename Restore, typename Measure,
131 typename RNG>
133 idx n_sites,
140 RNG& rng)
141{
142 save_state();
144 idx op = measure_order();
145
146 UmbrellaStats stats;
147 stats.mc = mc;
148 stats.order_param = op;
149
150 if (!window.contains(op)) {
152 stats.reverted = true;
153 stats.order_param = measure_order();
154 }
155 return stats;
156}
157
158} // namespace num::markov
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:55
UmbrellaStats umbrella_sweep(idx n_sites, DeltaE delta_energy, Apply apply_flip, Save save_state, Restore restore_state, Measure measure_order, UmbrellaWindow window, real beta, RNG &rng)
Umbrella-sampled Metropolis sweep (dE variant).
Definition mcmc_impl.hpp:92
UmbrellaStats umbrella_sweep_prob(idx n_sites, ProbFn acceptance_prob, Apply apply_flip, Save save_state, Restore restore_state, Measure measure_order, UmbrellaWindow window, RNG &rng)
Umbrella-sampled Metropolis sweep (precomputed probability variant).
MetropolisStats metropolis_sweep(idx n_sites, DeltaE delta_energy, Apply apply_flip, real beta, RNG &rng)
Single Metropolis sweep: n_sites random single-site updates.
Definition mcmc_impl.hpp:20
double real
Definition types.hpp:10
real beta(real a, real b)
B(a, b) – beta function.
Definition math.hpp:242
constexpr T ipow(T x) noexcept
Compute x^N at compile time via repeated squaring.
std::size_t idx
Definition types.hpp:11
Statistics returned by a single Metropolis sweep.
Definition mcmc.hpp:38
idx accepted
Number of accepted proposals.
Definition mcmc.hpp:39
idx total
Total proposals (= n_sites)
Definition mcmc.hpp:40
Statistics returned by an umbrella sampling sweep.
Definition mcmc.hpp:47
MetropolisStats mc
Metropolis sweep statistics.
Definition mcmc.hpp:48
bool reverted
true if state was restored
Definition mcmc.hpp:49
idx order_param
Order parameter value after sweep.
Definition mcmc.hpp:50
Window constraint for umbrella sampling.
Definition mcmc.hpp:54