|
| 1 | +#ifndef STAN_ANALYZE_MCMC_ESS_HPP |
| 2 | +#define STAN_ANALYZE_MCMC_ESS_HPP |
| 3 | + |
| 4 | +#include <stan/math/prim.hpp> |
| 5 | +#include <stan/analyze/mcmc/autocovariance.hpp> |
| 6 | +#include <algorithm> |
| 7 | +#include <cmath> |
| 8 | +#include <vector> |
| 9 | +#include <limits> |
| 10 | + |
| 11 | +namespace stan { |
| 12 | +namespace analyze { |
| 13 | + |
| 14 | +/** |
| 15 | + * Computes the effective sample size (ESS) for the specified |
| 16 | + * parameter across all chains. The number of draws per chain must be > 3, |
| 17 | + * and the values across all draws must be finite and not constant. |
| 18 | + * See https://arxiv.org/abs/1903.08008, section 3.2 for discussion. |
| 19 | + * |
| 20 | + * Sample autocovariance is computed using the implementation in this namespace |
| 21 | + * which normalizes lag-k autocorrelation estimators by N instead of (N - k), |
| 22 | + * yielding biased but more stable estimators as discussed in Geyer (1992); see |
| 23 | + * https://projecteuclid.org/euclid.ss/1177011137. |
| 24 | + * |
| 25 | + * @param chains matrix of draws across all chains |
| 26 | + * @return effective sample size for the specified parameter |
| 27 | + */ |
| 28 | +double ess(const Eigen::MatrixXd& chains) { |
| 29 | + const Eigen::Index num_chains = chains.cols(); |
| 30 | + const Eigen::Index draws_per_chain = chains.rows(); |
| 31 | + Eigen::MatrixXd acov(draws_per_chain, num_chains); |
| 32 | + Eigen::VectorXd chain_mean(num_chains); |
| 33 | + Eigen::VectorXd chain_var(num_chains); |
| 34 | + |
| 35 | + // compute the per-chain autocovariance |
| 36 | + for (size_t i = 0; i < num_chains; ++i) { |
| 37 | + chain_mean(i) = chains.col(i).mean(); |
| 38 | + Eigen::Map<const Eigen::VectorXd> draw_col(chains.col(i).data(), |
| 39 | + draws_per_chain); |
| 40 | + Eigen::VectorXd cov_col(draws_per_chain); |
| 41 | + autocovariance<double>(draw_col, cov_col); |
| 42 | + acov.col(i) = cov_col; |
| 43 | + chain_var(i) = cov_col(0) * draws_per_chain / (draws_per_chain - 1); |
| 44 | + } |
| 45 | + |
| 46 | + // compute var_plus, eqn (3) |
| 47 | + double w_chain_var = math::mean(chain_var); // W (within chain var) |
| 48 | + double var_plus |
| 49 | + = w_chain_var * (draws_per_chain - 1) / draws_per_chain; // \hat{var}^{+} |
| 50 | + if (num_chains > 1) { |
| 51 | + var_plus += math::variance(chain_mean); // B (between chain var) |
| 52 | + } |
| 53 | + |
| 54 | + // Geyer's initial positive sequence, eqn (11) |
| 55 | + Eigen::VectorXd rho_hat_t = Eigen::VectorXd::Zero(draws_per_chain); |
| 56 | + double rho_hat_even = 1.0; |
| 57 | + rho_hat_t(0) = rho_hat_even; // lag 0 |
| 58 | + |
| 59 | + Eigen::VectorXd acov_t(num_chains); |
| 60 | + for (size_t i = 0; i < num_chains; ++i) { |
| 61 | + acov_t(i) = acov(1, i); |
| 62 | + } |
| 63 | + double rho_hat_odd = 1 - (w_chain_var - acov_t.mean()) / var_plus; |
| 64 | + rho_hat_t(1) = rho_hat_odd; // lag 1 |
| 65 | + |
| 66 | + // compute autocorrelation at lag t for pair (t, t+1) |
| 67 | + // paired autocorrelation is guaranteed to be positive, monotone and convex |
| 68 | + size_t t = 1; |
| 69 | + while (t < draws_per_chain - 4 && (rho_hat_even + rho_hat_odd > 0) |
| 70 | + && !std::isnan(rho_hat_even + rho_hat_odd)) { |
| 71 | + for (size_t i = 0; i < num_chains; ++i) { |
| 72 | + acov_t(i) = acov.col(i)(t + 1); |
| 73 | + } |
| 74 | + rho_hat_even = 1 - (w_chain_var - acov_t.mean()) / var_plus; |
| 75 | + for (size_t i = 0; i < num_chains; ++i) { |
| 76 | + acov_t(i) = acov.col(i)(t + 2); |
| 77 | + } |
| 78 | + rho_hat_odd = 1 - (w_chain_var - acov_t.mean()) / var_plus; |
| 79 | + if ((rho_hat_even + rho_hat_odd) >= 0) { |
| 80 | + rho_hat_t(t + 1) = rho_hat_even; |
| 81 | + rho_hat_t(t + 2) = rho_hat_odd; |
| 82 | + } |
| 83 | + // convert initial positive sequence into an initial monotone sequence |
| 84 | + if (rho_hat_t(t + 1) + rho_hat_t(t + 2) > rho_hat_t(t - 1) + rho_hat_t(t)) { |
| 85 | + rho_hat_t(t + 1) = (rho_hat_t(t - 1) + rho_hat_t(t)) / 2; |
| 86 | + rho_hat_t(t + 2) = rho_hat_t(t + 1); |
| 87 | + } |
| 88 | + t += 2; |
| 89 | + } |
| 90 | + |
| 91 | + auto max_t = t; // max lag, used for truncation |
| 92 | + // see discussion p. 8, par "In extreme antithetic cases, " |
| 93 | + if (rho_hat_even > 0) { |
| 94 | + rho_hat_t(max_t + 1) = rho_hat_even; |
| 95 | + } |
| 96 | + |
| 97 | + double draws_total = num_chains * draws_per_chain; |
| 98 | + // eqn (13): Geyer's truncation rule, w/ modification |
| 99 | + double tau_hat = -1 + 2 * rho_hat_t.head(max_t).sum() + rho_hat_t(max_t + 1); |
| 100 | + // safety check for negative values and with max ess equal to ess*log10(ess) |
| 101 | + tau_hat = std::max(tau_hat, 1 / std::log10(draws_total)); |
| 102 | + return (draws_total / tau_hat); |
| 103 | +} |
| 104 | + |
| 105 | +} // namespace analyze |
| 106 | +} // namespace stan |
| 107 | + |
| 108 | +#endif |
0 commit comments