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#pragma once
4
5#include "core/types.hpp"
6#include <cmath>
7#include <vector>
8
9namespace num {
10
11/// @brief Welford updates for mean and variance.
13 real mean = 0.0;
14 real M2 = 0.0;
16
17 void update(real x) {
18 ++count;
19 real delta = x - mean;
20 mean += delta / static_cast<real>(count);
21 real delta2 = x - mean;
22 M2 += delta * delta2;
23 }
24
25 real variance() const {
26 return (count < 2) ? 0.0 : M2 / static_cast<real>(count - 1);
27 }
28
29 real std_dev() const { return std::sqrt(variance()); }
30
31 real stderr_mean() const {
32 return (count < 2) ? 0.0 : std_dev() / std::sqrt(static_cast<real>(count));
33 }
34
35 void reset() {
36 mean = M2 = 0.0;
37 count = 0;
38 }
39};
40
41/// @brief Fixed-bin histogram over \f$[\ell,h)\f$.
42struct Histogram {
43 std::vector<real> counts;
44 real lo = 0.0;
45 real hi = 0.0;
47
49 : counts(nbins, 0.0),
50 lo(lo),
51 hi(hi),
52 nbins(nbins) {}
53
54 idx bin(real x) const {
55 if (x < lo || x >= hi)
56 return nbins;
57 return static_cast<idx>((x - lo) / (hi - lo) * static_cast<real>(nbins));
58 }
59
60 real bin_centre(idx b) const {
61 return lo + (static_cast<real>(b) + 0.5) * (hi - lo) / static_cast<real>(nbins);
62 }
63
64 real bin_width() const { return (hi - lo) / static_cast<real>(nbins); }
65
66 void fill(real x, real weight = 1.0) {
67 idx b = bin(x);
68 if (b < nbins)
69 counts[b] += weight;
70 }
71
72 void reset() { std::fill(counts.begin(), counts.end(), 0.0); }
73
74 real total() const {
75 real s = 0.0;
76 for (real c : counts)
77 s += c;
78 return s;
79 }
80
81 /// Normalise so that the histogram integrates to 1 (probability density).
82 std::vector<real> pdf() const {
83 real norm = total() * bin_width();
84 std::vector<real> p(nbins);
85 if (norm > 0.0)
86 for (idx b = 0; b < nbins; ++b)
87 p[b] = counts[b] / norm;
88 return p;
89 }
90};
91
92// Autocorrelation time
93/// Integrated autocorrelation time tau_int from a stored time series.
94///
95/// Uses the automatic windowing criterion (Madras & Sokal 1988):
96/// accumulate C(t)/C(0) until W > c*tau_int (c = 6 default).
97/// Returns tau_int >= 0.5; returns 0.5 on failure.
98///
99/// @param data Pointer to time series of length n
100/// @param n Length of the series
101/// @param c Window parameter (default 6)
102real autocorr_time(const real* data, idx n, real c = 6.0);
103
104} // 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)
Compute .
Definition vector.cpp:83
real autocorr_time(const real *data, idx n, real c=6.0)
Definition stats.cpp:6
Fixed-bin histogram over .
Definition stats.hpp:42
real bin_centre(idx b) const
Definition stats.hpp:60
Histogram(idx nbins, real lo, real hi)
Definition stats.hpp:48
void reset()
Definition stats.hpp:72
std::vector< real > pdf() const
Normalise so that the histogram integrates to 1 (probability density).
Definition stats.hpp:82
idx bin(real x) const
Definition stats.hpp:54
void fill(real x, real weight=1.0)
Definition stats.hpp:66
real bin_width() const
Definition stats.hpp:64
std::vector< real > counts
Definition stats.hpp:43
real total() const
Definition stats.hpp:74
Welford updates for mean and variance.
Definition stats.hpp:12
real variance() const
Definition stats.hpp:25
real std_dev() const
Definition stats.hpp:29
void update(real x)
Definition stats.hpp:17
real stderr_mean() const
Definition stats.hpp:31