numerics 0.1.0
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>
21 DeltaE delta_energy,
22 Apply apply_flip,
23 real beta,
24 RNG& rng) {
25 std::uniform_real_distribution<real> u01(0.0, 1.0);
26 std::uniform_int_distribution<idx> site_dist(0, n_sites - 1);
27
28 MetropolisStats stats;
29 stats.total = n_sites;
30
31 for (idx k = 0; k < n_sites; ++k) {
32 idx i = site_dist(rng);
33 real dE = delta_energy(i);
34 real p = (dE <= 0.0) ? 1.0 : std::exp(-beta * dE);
35 if (p >= 1.0 || u01(rng) < p) {
36 apply_flip(i);
37 ++stats.accepted;
38 }
39 }
40 return stats;
41}
42
43/// @brief Metropolis sweep with caller-supplied acceptance probabilities.
44///
45/// Use this variant when acceptance probabilities are precomputed (e.g. a
46/// Boltzmann table for a discrete-dE system like the Ising model), avoiding
47/// a runtime exp() call per proposed flip.
48///
49/// @tparam ProbFn Callable: idx -> real. Returns min(1, exp(-beta*dE)) for
50/// site i.
51/// @tparam Apply Callable: idx -> void. Applies the flip at site i.
52/// @tparam RNG Random number generator.
53template<typename ProbFn, typename Apply, typename RNG>
55 ProbFn acceptance_prob,
56 Apply apply_flip,
57 RNG& rng) {
58 std::uniform_real_distribution<real> u01(0.0, 1.0);
59 std::uniform_int_distribution<idx> site_dist(0, n_sites - 1);
60
61 MetropolisStats stats;
62 stats.total = n_sites;
63
64 for (idx k = 0; k < n_sites; ++k) {
65 idx i = site_dist(rng);
66 real p = acceptance_prob(i);
67 if (p >= 1.0 || u01(rng) < p) {
68 apply_flip(i);
69 ++stats.accepted;
70 }
71 }
72 return stats;
73}
74
75/// @brief Umbrella-sampled Metropolis sweep (dE variant).
76///
77/// Performs a sweep, measures the order parameter, then restores the saved
78/// state if the order parameter falls outside [window.lo, window.hi].
79///
80/// @tparam DeltaE Callable: idx -> real
81/// @tparam Apply Callable: idx -> void
82/// @tparam Save Callable: () -> void. Saves system state before the sweep.
83/// @tparam Restore Callable: () -> void. Restores saved state.
84/// @tparam Measure Callable: () -> idx. Returns the order parameter.
85/// @tparam RNG Random number generator.
86template<typename DeltaE,
87 typename Apply,
88 typename Save,
89 typename Restore,
90 typename Measure,
91 typename RNG>
93 DeltaE delta_energy,
94 Apply apply_flip,
95 Save save_state,
96 Restore restore_state,
97 Measure measure_order,
98 UmbrellaWindow window,
99 real beta,
100 RNG& rng) {
101 save_state();
102 MetropolisStats mc =
103 metropolis_sweep(n_sites, delta_energy, apply_flip, beta, rng);
104 idx op = measure_order();
105
106 UmbrellaStats stats;
107 stats.mc = mc;
108 stats.order_param = op;
109
110 if (!window.contains(op)) {
111 restore_state();
112 stats.reverted = true;
113 stats.order_param = measure_order();
114 }
115 return stats;
116}
117
118/// @brief Umbrella-sampled Metropolis sweep (precomputed probability variant).
119///
120/// Same as umbrella_sweep but uses caller-supplied acceptance probabilities.
121///
122/// @tparam ProbFn Callable: idx -> real
123/// @tparam Apply Callable: idx -> void
124/// @tparam Save Callable: () -> void
125/// @tparam Restore Callable: () -> void
126/// @tparam Measure Callable: () -> idx
127/// @tparam RNG Random number generator.
128template<typename ProbFn,
129 typename Apply,
130 typename Save,
131 typename Restore,
132 typename Measure,
133 typename RNG>
135 ProbFn acceptance_prob,
136 Apply apply_flip,
137 Save save_state,
138 Restore restore_state,
139 Measure measure_order,
140 UmbrellaWindow window,
141 RNG& rng) {
142 save_state();
143 MetropolisStats mc =
144 metropolis_sweep_prob(n_sites, acceptance_prob, apply_flip, rng);
145 idx op = measure_order();
146
147 UmbrellaStats stats;
148 stats.mc = mc;
149 stats.order_param = op;
150
151 if (!window.contains(op)) {
152 restore_state();
153 stats.reverted = true;
154 stats.order_param = measure_order();
155 }
156 return stats;
157}
158
159} // 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:54
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:248
std::size_t idx
Definition types.hpp:11
Statistics returned by a single Metropolis sweep.
Definition mcmc.hpp:39
idx accepted
Number of accepted proposals.
Definition mcmc.hpp:40
idx total
Total proposals (= n_sites)
Definition mcmc.hpp:41
Statistics returned by an umbrella sampling sweep.
Definition mcmc.hpp:48
MetropolisStats mc
Metropolis sweep statistics.
Definition mcmc.hpp:49
bool reverted
true if state was restored
Definition mcmc.hpp:50
idx order_param
Order parameter value after sweep.
Definition mcmc.hpp:51
Window constraint for umbrella sampling.
Definition mcmc.hpp:55
bool contains(idx v) const
Definition mcmc.hpp:58