Skip to content

Commit

Permalink
Merge branch 'feature/3299-chainset' of https://github.com/stan-dev/stan
Browse files Browse the repository at this point in the history
 into feature/3299-chainset
  • Loading branch information
mitzimorris committed Oct 21, 2024
2 parents c38b15f + 199155d commit 32b7fec
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
19 changes: 11 additions & 8 deletions src/stan/analyze/mcmc/compute_effective_sample_size.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ __attribute__((deprecated))
#elif defined(_MSC_VER)
__declspec(deprecated)
#endif
inline double compute_effective_sample_size(std::vector<const double*> draws,
std::vector<size_t> sizes) {
inline double
compute_effective_sample_size(std::vector<const double*> draws,
std::vector<size_t> sizes) {
int num_chains = sizes.size();
size_t num_draws = sizes[0];
for (int chain = 1; chain < num_chains; ++chain) {
Expand Down Expand Up @@ -170,8 +171,8 @@ __attribute__((deprecated))
#elif defined(_MSC_VER)
__declspec(deprecated)
#endif
inline double compute_effective_sample_size(std::vector<const double*> draws,
size_t size) {
inline double
compute_effective_sample_size(std::vector<const double*> draws, size_t size) {
int num_chains = draws.size();
std::vector<size_t> sizes(num_chains, size);
return compute_effective_sample_size(draws, sizes);
Expand Down Expand Up @@ -203,8 +204,9 @@ __attribute__((deprecated))
#elif defined(_MSC_VER)
__declspec(deprecated)
#endif
inline double compute_split_effective_sample_size(
std::vector<const double*> draws, std::vector<size_t> sizes) {
inline double
compute_split_effective_sample_size(std::vector<const double*> draws,
std::vector<size_t> sizes) {
int num_chains = sizes.size();
size_t num_draws = sizes[0];
for (int chain = 1; chain < num_chains; ++chain) {
Expand Down Expand Up @@ -246,8 +248,9 @@ __attribute__((deprecated))
#elif defined(_MSC_VER)
__declspec(deprecated)
#endif
inline double compute_split_effective_sample_size(
std::vector<const double*> draws, size_t size) {
inline double
compute_split_effective_sample_size(std::vector<const double*> draws,
size_t size) {
int num_chains = draws.size();
std::vector<size_t> sizes(num_chains, size);
return compute_split_effective_sample_size(draws, sizes);
Expand Down
20 changes: 12 additions & 8 deletions src/stan/analyze/mcmc/compute_potential_scale_reduction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ __attribute__((deprecated))
#elif defined(_MSC_VER)
__declspec(deprecated)
#endif
inline double compute_potential_scale_reduction(
std::vector<const double*> draws, std::vector<size_t> sizes) {
inline double
compute_potential_scale_reduction(std::vector<const double*> draws,
std::vector<size_t> sizes) {
int num_chains = sizes.size();
size_t num_draws = sizes[0];
if (num_draws == 0) {
Expand Down Expand Up @@ -131,8 +132,9 @@ __attribute__((deprecated))
#elif defined(_MSC_VER)
__declspec(deprecated)
#endif
inline double compute_potential_scale_reduction(
std::vector<const double*> draws, size_t size) {
inline double
compute_potential_scale_reduction(std::vector<const double*> draws,
size_t size) {
int num_chains = draws.size();
std::vector<size_t> sizes(num_chains, size);
return compute_potential_scale_reduction(draws, sizes);
Expand Down Expand Up @@ -161,8 +163,9 @@ __attribute__((deprecated))
#elif defined(_MSC_VER)
__declspec(deprecated)
#endif
inline double compute_split_potential_scale_reduction(
std::vector<const double*> draws, std::vector<size_t> sizes) {
inline double
compute_split_potential_scale_reduction(std::vector<const double*> draws,
std::vector<size_t> sizes) {
int num_chains = sizes.size();
size_t num_draws = sizes[0];
for (int chain = 1; chain < num_chains; ++chain) {
Expand Down Expand Up @@ -201,8 +204,9 @@ __attribute__((deprecated))
#elif defined(_MSC_VER)
__declspec(deprecated)
#endif
inline double compute_split_potential_scale_reduction(
std::vector<const double*> draws, size_t size) {
inline double
compute_split_potential_scale_reduction(std::vector<const double*> draws,
size_t size) {
int num_chains = draws.size();
std::vector<size_t> sizes(num_chains, size);
return compute_split_potential_scale_reduction(draws, sizes);
Expand Down
2 changes: 1 addition & 1 deletion src/stan/analyze/mcmc/ess.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace analyze {
* which normalizes lag-k autocorrelation estimators by N instead of (N - k),
* yielding biased but more stable estimators as discussed in Geyer (1992); see
* https://projecteuclid.org/euclid.ss/1177011137.
*
*
* @param chains matrix of draws across all chains
* @return effective sample size for the specified parameter
*/
Expand Down
14 changes: 6 additions & 8 deletions src/test/unit/mcmc/chainset_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@ TEST_F(McmcChains, summary_stats) {
EXPECT_NEAR(theta_sd_expect, bern_chains.sd("theta"), 1e-5);

double theta_mad_expect = 0.12309;
EXPECT_NEAR(theta_mad_expect, bern_chains.max_abs_deviation("theta"),
1e-5);
EXPECT_NEAR(theta_mad_expect, bern_chains.max_abs_deviation("theta"), 1e-5);

double theta_mcse_mean_expect = 0.003234;
EXPECT_NEAR(theta_mcse_mean_expect, bern_chains.mcse_mean("theta"), 1e-4);
Expand All @@ -182,8 +181,8 @@ TEST_F(McmcChains, summary_stats) {
Eigen::VectorXd probs(6);
probs << 0.0, 0.01, 0.05, 0.95, 0.99, 1.0;
Eigen::VectorXd quantiles_expect(6);
quantiles_expect << 0.004072, 0.046281, 0.077169, 0.473885,
0.574524, 0.698401;
quantiles_expect << 0.004072, 0.046281, 0.077169, 0.473885, 0.574524,
0.698401;
Eigen::VectorXd theta_quantiles = bern_chains.quantiles("theta", probs);
for (size_t i = 0; i < probs.size(); ++i) {
EXPECT_NEAR(quantiles_expect(i), theta_quantiles(i), 1e-5);
Expand All @@ -196,14 +195,13 @@ TEST_F(McmcChains, summary_stats) {
double theta_ess_bulk_expect = 1407.5124;
double theta_ess_tail_expect = 1291.7131;
auto ess = bern_chains.split_rank_normalized_ess("theta");
EXPECT_NEAR(theta_ess_bulk_expect, ess.first, 1e-4);
EXPECT_NEAR(theta_ess_bulk_expect, ess.first, 1e-4);
EXPECT_NEAR(theta_ess_tail_expect, ess.second, 1e-4);

// autocorrelation - first 10 lags
Eigen::VectorXd theta_ac_expect(10);
theta_ac_expect << 1.00000, 0.42204, 0.20683,
0.08383, 0.037326, 0.02507, 0.02003,
0.01347, 0.00476, 0.029495;
theta_ac_expect << 1.00000, 0.42204, 0.20683, 0.08383, 0.037326, 0.02507,
0.02003, 0.01347, 0.00476, 0.029495;
auto theta_ac = bern_chains.autocorrelation(0, "theta");
for (size_t i = 0; i < 10; ++i) {
EXPECT_NEAR(theta_ac(i), theta_ac_expect(i), 0.0005);
Expand Down

0 comments on commit 32b7fec

Please sign in to comment.