Stochastic routines cover random-number setup, Metropolis sweeps, Boltzmann acceptance tables, online statistics, histograms, and autocorrelation estimates.
Metropolis Driver
std::vector<int> spin(N, 1);
auto accept = [&](int i) {
const double dE = energy_change_for_flip(spin, i);
return std::min(1.0, std::exp(-beta * dE));
};
auto propose = [&](int i) {
spin[i] = -spin[i];
};
auto measure = [&]() {
double m = 0.0;
for (int s : spin) m += s;
return m / static_cast<double>(N);
};
auto rng = num::make_seeded_rng(1234);
rng);
double sample(const MCMCModel &model, const A &alg, RNG &rng)
Run equilibration then measurement sweeps; return the mean observable.
Umbrella include for the numerics library.
An MCMC target: site count, per-site acceptance/proposal, observable.
The model stores the acceptance, proposal, and measurement functions. The sampler tag stores the equilibration and measurement counts. Sampling uses sample(), the stochastic counterpart to solve().
Precomputed Boltzmann Probabilities
auto lookup = [&](double dE) {
const int slot = static_cast<int>((dE + 8.0) / 4.0);
return table[static_cast<std::size_t>(slot)];
};
std::vector< double > make_boltzmann_table(const std::vector< double > &dEs, double beta)
Use this when \(\Delta E\) takes values from a small discrete set.
Running Statistics
for (double sample : samples) {
}
double mean = stats.
mean;
Welford updates for mean and variance.
Welford updates avoid storing the full sample stream.
Histograms and Autocorrelation
for (double x : samples) {
H.fill(x);
}
std::vector<double> pdf = H.pdf();
real autocorr_time(const real *data, idx n, real c=6.0)
Fixed-bin histogram over .
autocorr_time estimates the integrated autocorrelation time from a stored time series.