Skip to content

Commit

Permalink
unit tests against cmdstan 2.35.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mitzimorris committed Oct 22, 2024
1 parent 2950a00 commit edbc96e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
19 changes: 5 additions & 14 deletions src/test/unit/analyze/mcmc/ess_basic_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <stan/analyze/mcmc/compute_effective_sample_size.hpp>
#include <stan/analyze/mcmc/ess.hpp>
#include <stan/io/stan_csv_reader.hpp>
#include <gtest/gtest.h>
Expand Down Expand Up @@ -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);
}
24 changes: 8 additions & 16 deletions src/test/unit/analyze/mcmc/rhat_basic_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <stan/analyze/mcmc/compute_potential_scale_reduction.hpp>
#include <stan/analyze/mcmc/rhat.hpp>
#include <stan/analyze/mcmc/split_chains.hpp>
#include <stan/io/stan_csv_reader.hpp>
#include <gtest/gtest.h>
#include <fstream>
Expand Down Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
@@ -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).

0 comments on commit edbc96e

Please sign in to comment.