From edbc96e47ef9025e1843b5796d17124e9632694d Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Tue, 22 Oct 2024 08:53:35 -0400 Subject: [PATCH] unit tests against cmdstan 2.35.0 --- src/test/unit/analyze/mcmc/ess_basic_test.cpp | 19 ++++----------- .../unit/analyze/mcmc/rhat_basic_test.cpp | 24 +++++++------------ .../cmdstan-2-35-0-stansummary.csv | 18 ++++++++++++++ 3 files changed, 31 insertions(+), 30 deletions(-) create mode 100644 src/test/unit/analyze/mcmc/test_csv_files/cmdstan-2-35-0-stansummary.csv diff --git a/src/test/unit/analyze/mcmc/ess_basic_test.cpp b/src/test/unit/analyze/mcmc/ess_basic_test.cpp index 6cf1226e20..2a1e910f59 100644 --- a/src/test/unit/analyze/mcmc/ess_basic_test.cpp +++ b/src/test/unit/analyze/mcmc/ess_basic_test.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -42,21 +41,13 @@ class EssBasic : public testing::Test { }; TEST_F(EssBasic, test_basic_ess) { - // computed via R pkg posterior - double ess_lp_expect = 1335.4137; - double ess_theta_expect = 1377.503; + // computed via cmdstan 2.35.0 stansummary + double ess_lp_expect = 1335.41366787; + double ess_theta_expect = 1377.5030282; auto ess_basic_lp = stan::analyze::ess(chains_lp); - auto old_ess_basic_lp - = stan::analyze::compute_effective_sample_size(draws_lp, sizes); - auto ess_basic_theta = stan::analyze::ess(chains_theta); - auto old_ess_basic_theta - = stan::analyze::compute_effective_sample_size(draws_theta, sizes); - - EXPECT_NEAR(ess_lp_expect, ess_basic_lp, 1e-4); - EXPECT_NEAR(ess_theta_expect, ess_basic_theta, 1e-4); - EXPECT_NEAR(old_ess_basic_lp, ess_basic_lp, 1e-12); - EXPECT_NEAR(old_ess_basic_theta, ess_basic_theta, 1e-12); + EXPECT_NEAR(ess_lp_expect, ess_basic_lp, 1e-8); + EXPECT_NEAR(ess_theta_expect, ess_basic_theta, 1e-8); } diff --git a/src/test/unit/analyze/mcmc/rhat_basic_test.cpp b/src/test/unit/analyze/mcmc/rhat_basic_test.cpp index 48d4d5bdf7..e22aff0c0a 100644 --- a/src/test/unit/analyze/mcmc/rhat_basic_test.cpp +++ b/src/test/unit/analyze/mcmc/rhat_basic_test.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include #include #include @@ -40,21 +40,13 @@ class RhatBasic : public testing::Test { }; TEST_F(RhatBasic, test_basic_rhat) { - // computed via R pkg posterior - double rhat_lp_basic_expect = 1.0001296; - double rhat_theta_basic_expect = 1.0029197; + // computed via cmdstan 2.35.0 stansummary + double rhat_lp_basic_expect = 1.00035489482; + double rhat_theta_basic_expect = 1.00721797217; - auto rhat_basic_lp = stan::analyze::rhat(chains_lp); - auto old_rhat_basic_lp - = stan::analyze::compute_potential_scale_reduction(draws_lp, sizes); + auto rhat_basic_lp = stan::analyze::rhat(stan::analyze::split_chains(chains_lp)); + auto rhat_basic_theta = stan::analyze::rhat(stan::analyze::split_chains(chains_theta)); - auto rhat_basic_theta = stan::analyze::rhat(chains_theta); - auto old_rhat_basic_theta - = stan::analyze::compute_potential_scale_reduction(draws_theta, sizes); - - EXPECT_NEAR(rhat_lp_basic_expect, rhat_basic_lp, 1e-5); - EXPECT_NEAR(rhat_theta_basic_expect, rhat_basic_theta, 1e-5); - - EXPECT_NEAR(old_rhat_basic_lp, rhat_basic_lp, 1e-12); - EXPECT_NEAR(old_rhat_basic_theta, rhat_basic_theta, 1e-12); + EXPECT_NEAR(rhat_lp_basic_expect, rhat_basic_lp, 1e-10); + EXPECT_NEAR(rhat_theta_basic_expect, rhat_basic_theta, 1e-10); } diff --git a/src/test/unit/analyze/mcmc/test_csv_files/cmdstan-2-35-0-stansummary.csv b/src/test/unit/analyze/mcmc/test_csv_files/cmdstan-2-35-0-stansummary.csv new file mode 100644 index 0000000000..7537b4cb0b --- /dev/null +++ b/src/test/unit/analyze/mcmc/test_csv_files/cmdstan-2-35-0-stansummary.csv @@ -0,0 +1,18 @@ +name,Mean,MCSE,StdDev,5%,50%,95%,N_Eff,N_Eff/s,R_hat +"lp__",-7.2867579275,0.0202120721354,0.738616062616,-8.7825,-6.99001,-6.7503,1335.41366787,35142.4649438,1.00035489482 +"accept_stat__",0.92587778475,0.00177759974411,0.116280973122,0.672417,0.974476,1,4279.0696651,112607.09645,1.00155471132 +"stepsize__",0.90426575,0.0139260619795,0.0197240339958,0.883328,0.911571,0.933117,2.00601805416,52.7899487937,6.91946682912e+12 +"treedepth__",1.3515,0.00798399449516,0.479066932987,1,1,2,3600.40978064,94747.6258062,1.00017741661 +"n_leapfrog__",2.515,0.0230857538371,1.31423240019,1,3,3,3240.82976278,85284.9937574,1.00632559786 +"divergent__",0,nan,0,0,0,0,nan,nan,nan +"energy__",7.76890199,0.0271304159659,1.00790019677,6.80125,7.46009,9.72873,1380.13686038,36319.3910627,1.00204956973 +"theta",0.251297410482,0.00327489095264,0.121546686658,0.0769898,0.237539,0.473863,1377.5030282,36250.0796895,1.00721797217 +# Inference for Stan model: bernoulli_model +# 4 chains: each with iter=(1000,1000,1000,1000); warmup=(0,0,0,0); thin=(1,1,1,1); 4000 iterations saved. +# +# Warmup took (0.0030, 0.0030, 0.0040, 0.0030) seconds, 0.013 seconds total +# Sampling took (0.0090, 0.010, 0.010, 0.0090) seconds, 0.038 seconds total +# Samples were drawn using hmc with nuts. +# For each parameter, N_Eff is a crude measure of effective sample size, +# and R_hat is the potential scale reduction factor on split chains (at +# convergence, R_hat=1).