numerics
Loading...
Searching...
No Matches
stats.hpp
Go to the documentation of this file.
1/// @file stats/stats.hpp
2/// @brief Online statistics for Monte Carlo observables
3///
4/// RunningStats: Welford online mean + variance, no data storage needed.
5/// Histogram: Fixed-bin histogram with reweighting for WHAM analysis.
6/// autocorr_time: Integrated autocorrelation time from a stored time series.
7#pragma once
8
9#include "core/types.hpp"
10#include <vector>
11#include <cmath>
12
13namespace num {
14
15// -- RunningStats -------------------------------------------------------------
16/// Online mean and variance via Welford's algorithm.
17/// One-pass, numerically stable, O(1) memory.
19 real mean = 0.0;
20 real M2 = 0.0;
22
23 /// Incorporate one new sample.
24 void update(real x) {
25 ++count;
26 real delta = x - mean;
27 mean += delta / static_cast<real>(count);
28 real delta2 = x - mean;
29 M2 += delta * delta2;
30 }
31
32 /// Unbiased sample variance (n-1 denominator). Returns 0 for n < 2.
33 real variance() const {
34 return (count < 2) ? 0.0 : M2 / static_cast<real>(count - 1);
35 }
36
37 real std_dev() const { return std::sqrt(variance()); }
38
39 /// Standard error of the mean (uncorrelated samples).
40 real stderr_mean() const {
41 return (count < 2) ? 0.0 : std_dev() / std::sqrt(static_cast<real>(count));
42 }
43
44 void reset() { mean = M2 = 0.0; count = 0; }
45};
46
47// -- Histogram ----------------------------------------------------------------
48/// Fixed-bin histogram over [lo, hi).
49/// Useful for umbrella sampling -- each window collects a Histogram of the
50/// reaction coordinate (e.g. nucleus size), then WHAM stitches them together.
51struct Histogram {
52 std::vector<real> counts;
55
56 /// @param nbins Number of bins
57 /// @param lo,hi Range of the histogram [lo, hi)
60
61 /// Map value to bin index. Returns nbins (out of range sentinel) if outside.
62 idx bin(real x) const {
63 if (x < lo || x >= hi) return nbins;
64 return static_cast<idx>((x - lo) / (hi - lo) * static_cast<real>(nbins));
65 }
66
68 return lo + (static_cast<real>(b) + 0.5) * (hi - lo) / static_cast<real>(nbins);
69 }
70
71 real bin_width() const { return (hi - lo) / static_cast<real>(nbins); }
72
73 void fill(real x, real weight = 1.0) {
74 idx b = bin(x);
75 if (b < nbins) counts[b] += weight;
76 }
77
78 void reset() { std::fill(counts.begin(), counts.end(), 0.0); }
79
80 real total() const {
81 real s = 0.0;
82 for (real c : counts) s += c;
83 return s;
84 }
85
86 /// Normalise so that the histogram integrates to 1 (probability density).
87 std::vector<real> pdf() const {
88 real norm = total() * bin_width();
89 std::vector<real> p(nbins);
90 if (norm > 0.0)
91 for (idx b = 0; b < nbins; ++b) p[b] = counts[b] / norm;
92 return p;
93 }
94};
95
96// -- Autocorrelation time ------------------------------------------------------
97/// Integrated autocorrelation time tau_int from a stored time series.
98///
99/// Uses the automatic windowing criterion (Madras & Sokal 1988):
100/// accumulate C(t)/C(0) until W > c*tau_int (c = 6 default).
101/// Returns tau_int >= 0.5; returns 0.5 on failure.
102///
103/// @param data Pointer to time series of length n
104/// @param n Length of the series
105/// @param c Window parameter (default 6)
106real autocorr_time(const real* data, idx n, real c = 6.0);
107
108} // namespace num
Core type definitions.
double real
Definition types.hpp:10
constexpr T ipow(T x) noexcept
Compute x^N at compile time via repeated squaring.
std::size_t idx
Definition types.hpp:11
real norm(const Vector &x, Backend b=default_backend)
Euclidean norm.
Definition vector.cpp:69
real autocorr_time(const real *data, idx n, real c=6.0)
Definition stats.cpp:6
real bin_centre(idx b) const
Definition stats.hpp:67
Histogram(idx nbins, real lo, real hi)
Definition stats.hpp:58
void reset()
Definition stats.hpp:78
std::vector< real > pdf() const
Normalise so that the histogram integrates to 1 (probability density).
Definition stats.hpp:87
idx bin(real x) const
Map value to bin index. Returns nbins (out of range sentinel) if outside.
Definition stats.hpp:62
void fill(real x, real weight=1.0)
Definition stats.hpp:73
real bin_width() const
Definition stats.hpp:71
std::vector< real > counts
Definition stats.hpp:52
real total() const
Definition stats.hpp:80
real variance() const
Unbiased sample variance (n-1 denominator). Returns 0 for n < 2.
Definition stats.hpp:33
real std_dev() const
Definition stats.hpp:37
void update(real x)
Incorporate one new sample.
Definition stats.hpp:24
real stderr_mean() const
Standard error of the mean (uncorrelated samples).
Definition stats.hpp:40