Skip to content

Commit

Permalink
more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mitzimorris committed Oct 7, 2024
1 parent a5fe9d2 commit 1ebd1e2
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/test/unit/analyze/mcmc/check_chains_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <stan/analyze/mcmc/check_chains.hpp>
#include <stan/io/stan_csv_reader.hpp>
#include <gtest/gtest.h>
#include <fstream>
#include <sstream>
#include <string>
#include <cmath>

TEST(CheckChains, good_and_bad) {
std::stringstream out;
std::ifstream eight_schools_1_stream, eight_schools_2_stream;
stan::io::stan_csv eight_schools_1, eight_schools_2;
eight_schools_1_stream.open(
"src/test/unit/mcmc/test_csv_files/eight_schools_1.csv",
std::ifstream::in);
eight_schools_1
= stan::io::stan_csv_reader::parse(eight_schools_1_stream, &out);
eight_schools_1_stream.close();

eight_schools_2_stream.open(
"src/test/unit/mcmc/test_csv_files/eight_schools_2.csv",
std::ifstream::in);
eight_schools_2
= stan::io::stan_csv_reader::parse(eight_schools_2_stream, &out);
eight_schools_2_stream.close();

Eigen::MatrixXd chain_1(eight_schools_1.samples.rows(), 1);
Eigen::MatrixXd chains(eight_schools_1.samples.rows(), 2);

// stepsize - constant after adaptation
chain_1.col(0) = eight_schools_1.samples.col(2);
EXPECT_FALSE(stan::analyze::is_finite_and_varies(chain_1));

chains.col(0) = eight_schools_1.samples.col(2);
chains.col(1) = eight_schools_2.samples.col(2);
EXPECT_FALSE(stan::analyze::is_finite_and_varies(chains));

for (size_t i = 0; i < 10; ++i) {
chains.col(0) = eight_schools_1.samples.col(i + 7);
chains.col(1) = eight_schools_2.samples.col(i + 7);
EXPECT_TRUE(stan::analyze::is_finite_and_varies(chains));
}

// above test shows that column 7 is - make it non-finite
chain_1.col(0) = eight_schools_1.samples.col(7);
chain_1(0,0) = std::numeric_limits<double>::infinity();
EXPECT_FALSE(stan::analyze::is_finite_and_varies(chain_1));
}

0 comments on commit 1ebd1e2

Please sign in to comment.