numerics
Loading...
Searching...
No Matches
stats.cpp
Go to the documentation of this file.
1/// @file stats/stats.cpp
2#include "stats/stats.hpp"
3
4namespace num {
5
6real autocorr_time(const real* data, idx n, real c) {
7 if (n < 4) return 0.5;
8
9 // Compute mean
10 real mean = 0.0;
11 for (idx i = 0; i < n; ++i) mean += data[i];
12 mean /= static_cast<real>(n);
13
14 // C(0) -- variance
15 real c0 = 0.0;
16 for (idx i = 0; i < n; ++i) {
17 real d = data[i] - mean;
18 c0 += d * d;
19 }
20 c0 /= static_cast<real>(n);
21 if (c0 < 1e-15) return 0.5;
22
23 // Accumulate C(t)/C(0) with automatic windowing
24 real tau = 0.5;
25 for (idx t = 1; t < n / 2; ++t) {
26 real ct = 0.0;
27 for (idx i = 0; i < n - t; ++i)
28 ct += (data[i] - mean) * (data[i + t] - mean);
29 ct /= static_cast<real>(n - t);
30 tau += ct / c0;
31 // Madras-Sokal window: stop when W > c * tau
32 if (static_cast<real>(t) >= c * tau) break;
33 }
34 return (tau < 0.5) ? 0.5 : tau;
35}
36
37} // namespace num
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
constexpr real e
Definition math.hpp:41
real autocorr_time(const real *data, idx n, real c=6.0)
Definition stats.cpp:6
Online statistics for Monte Carlo observables.