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