SedSat3 1.1.6
Sediment Source Apportionment Tool - Advanced statistical methods for environmental pollution research
Loading...
Searching...
No Matches
mcmcdiagnostics.h
Go to the documentation of this file.
1// SedSAT3 – Sediment Source Assessment Tool, version 3
2// U.S. Geological Survey. See LICENSE.md and DISCLAIMER.md in the repository root.
3
4#ifndef MCMCDIAGNOSTICS_H
5#define MCMCDIAGNOSTICS_H
6
18
19#include <vector>
20#include <cmath>
21#include <algorithm>
22#include <limits>
23#include <gsl/gsl_fft_complex.h>
24
26{
27
29{
30 double rhat = std::numeric_limits<double>::quiet_NaN();
31 double effective_sample_size = std::numeric_limits<double>::quiet_NaN();
32};
33
35inline std::vector<double> Autocovariance(const std::vector<double>& x)
36{
37 const size_t n = x.size();
38 std::vector<double> result(n, 0.0);
39 if (n == 0)
40 return result;
41 double mean = 0;
42 for (double v : x)
43 mean += v;
44 mean /= double(n);
45
46 size_t length = 1;
47 while (length < 2 * n)
48 length <<= 1;
49 std::vector<double> data(2 * length, 0.0); // interleaved complex values
50 for (size_t i = 0; i < n; i++)
51 data[2 * i] = x[i] - mean;
52 gsl_fft_complex_radix2_forward(data.data(), 1, length);
53 for (size_t i = 0; i < length; i++)
54 {
55 const double re = data[2 * i], im = data[2 * i + 1];
56 data[2 * i] = re * re + im * im;
57 data[2 * i + 1] = 0;
58 }
59 gsl_fft_complex_radix2_inverse(data.data(), 1, length);
60 for (size_t t = 0; t < n; t++)
61 result[t] = data[2 * t] / double(n);
62 return result;
63}
64
72inline Diagnostics Diagnose(const std::vector<std::vector<double>>& chains)
73{
74 Diagnostics out;
75
76 // Split every chain into two halves of equal length
77 size_t length = std::numeric_limits<size_t>::max();
78 for (const auto& chain : chains)
79 length = std::min(length, chain.size());
80 if (chains.size() < 2 || length < 4 || length == std::numeric_limits<size_t>::max())
81 return out;
82 const size_t half = length / 2;
83 std::vector<std::vector<double>> split;
84 for (const auto& chain : chains)
85 {
86 split.emplace_back(chain.begin(), chain.begin() + half);
87 split.emplace_back(chain.begin() + (length - half), chain.begin() + length);
88 }
89 const size_t m = split.size();
90 const double n = double(half);
91
92 // Between- and within-chain variances
93 std::vector<double> means(m, 0.0), variances(m, 0.0);
94 for (size_t c = 0; c < m; c++)
95 {
96 for (double v : split[c])
97 means[c] += v;
98 means[c] /= n;
99 for (double v : split[c])
100 variances[c] += (v - means[c]) * (v - means[c]);
101 variances[c] /= (n - 1);
102 }
103 double grand_mean = 0;
104 for (double v : means)
105 grand_mean += v;
106 grand_mean /= double(m);
107 double B = 0;
108 for (double v : means)
109 B += (v - grand_mean) * (v - grand_mean);
110 B *= n / double(m - 1);
111 double W = 0;
112 for (double v : variances)
113 W += v;
114 W /= double(m);
115 if (!(W > 0))
116 return out;
117 const double var_plus = (n - 1) / n * W + B / n;
118 out.rhat = std::sqrt(var_plus / W);
119
120 // Effective sample size from the autocorrelations of all chains,
121 // truncated with Geyer's initial monotone sequence
122 std::vector<double> mean_autocovariance(half, 0.0);
123 for (const auto& chain : split)
124 {
125 const std::vector<double> acov = Autocovariance(chain);
126 for (size_t t = 0; t < half; t++)
127 mean_autocovariance[t] += acov[t] / double(m);
128 }
129 auto rho = [&](size_t t) {
130 return 1.0 - (W - mean_autocovariance[t] * n / (n - 1)) / var_plus;
131 };
132 double tau = -1.0;
133 double previous_pair = std::numeric_limits<double>::infinity();
134 for (size_t t = 0; t + 1 < half; t += 2)
135 {
136 double pair = rho(t) + rho(t + 1);
137 if (pair < 0)
138 break;
139 pair = std::min(pair, previous_pair); // monotone
140 tau += 2.0 * pair;
141 previous_pair = pair;
142 }
143 const double total = double(m) * n;
144 tau = std::max(tau, 1.0 / std::log10(total));
145 out.effective_sample_size = total / tau;
146 return out;
147}
148
149} // namespace mcmcdiagnostics
150
151#endif // MCMCDIAGNOSTICS_H
Diagnostics Diagnose(const std::vector< std::vector< double > > &chains)
Split R-hat and effective sample size of one quantity.
std::vector< double > Autocovariance(const std::vector< double > &x)
Autocovariance of x at lags 0..n-1 (divided by n), computed with an FFT.
double effective_sample_size
multi-chain ESS