numerics 0.1.0
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 {
38 return std::sqrt(variance());
39 }
40
41 /// Standard error of the mean (uncorrelated samples).
42 real stderr_mean() const {
43 return (count < 2) ? 0.0
44 : std_dev() / std::sqrt(static_cast<real>(count));
45 }
46
47 void reset() {
48 mean = M2 = 0.0;
49 count = 0;
50 }
51};
52
53// Histogram
54/// Fixed-bin histogram over [lo, hi).
55/// Useful for umbrella sampling -- each window collects a Histogram of the
56/// reaction coordinate (e.g. nucleus size), then WHAM stitches them together.
57struct Histogram {
58 std::vector<real> counts;
59 real lo = 0.0;
60 real hi = 0.0;
62
63 /// @param nbins Number of bins
64 /// @param lo,hi Range of the histogram [lo, hi)
66 : counts(nbins, 0.0)
67 , lo(lo)
68 , hi(hi)
69 , nbins(nbins) {}
70
71 /// Map value to bin index. Returns nbins (out of range sentinel) if
72 /// outside.
73 idx bin(real x) const {
74 if (x < lo || x >= hi)
75 return nbins;
76 return static_cast<idx>((x - lo) / (hi - lo)
77 * static_cast<real>(nbins));
78 }
79
80 real bin_centre(idx b) const {
81 return lo
82 + (static_cast<real>(b) + 0.5) * (hi - lo)
83 / static_cast<real>(nbins);
84 }
85
86 real bin_width() const {
87 return (hi - lo) / static_cast<real>(nbins);
88 }
89
90 void fill(real x, real weight = 1.0) {
91 idx b = bin(x);
92 if (b < nbins)
93 counts[b] += weight;
94 }
95
96 void reset() {
97 std::fill(counts.begin(), counts.end(), 0.0);
98 }
99
100 real total() const {
101 real s = 0.0;
102 for (real c : counts)
103 s += c;
104 return s;
105 }
106
107 /// Normalise so that the histogram integrates to 1 (probability density).
108 std::vector<real> pdf() const {
109 real norm = total() * bin_width();
110 std::vector<real> p(nbins);
111 if (norm > 0.0)
112 for (idx b = 0; b < nbins; ++b)
113 p[b] = counts[b] / norm;
114 return p;
115 }
116};
117
118// Autocorrelation time
119/// Integrated autocorrelation time tau_int from a stored time series.
120///
121/// Uses the automatic windowing criterion (Madras & Sokal 1988):
122/// accumulate C(t)/C(0) until W > c*tau_int (c = 6 default).
123/// Returns tau_int >= 0.5; returns 0.5 on failure.
124///
125/// @param data Pointer to time series of length n
126/// @param n Length of the series
127/// @param c Window parameter (default 6)
128real autocorr_time(const real* data, idx n, real c = 6.0);
129
130} // namespace num
Core type definitions.
double real
Definition types.hpp:10
std::size_t idx
Definition types.hpp:11
real norm(const Vector &x, Backend b=default_backend)
Euclidean norm.
Definition vector.cpp:97
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:80
Histogram(idx nbins, real lo, real hi)
Definition stats.hpp:65
void reset()
Definition stats.hpp:96
std::vector< real > pdf() const
Normalise so that the histogram integrates to 1 (probability density).
Definition stats.hpp:108
idx bin(real x) const
Definition stats.hpp:73
void fill(real x, real weight=1.0)
Definition stats.hpp:90
real bin_width() const
Definition stats.hpp:86
std::vector< real > counts
Definition stats.hpp:58
real total() const
Definition stats.hpp:100
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:42