Skip to content

Commit

Permalink
Also catch exceptions in MAD calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
WardBrian committed Nov 29, 2024
1 parent 42731ba commit 3a76d5a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/stan/mcmc/chainset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,11 @@ class chainset {
auto center = median(index);
Eigen::MatrixXd abs_dev = (draws.array() - center).abs();
Eigen::Map<Eigen::VectorXd> map(abs_dev.data(), abs_dev.size());
return 1.4826 * stan::math::quantile(map, 0.5);
try {
return 1.4826 * stan::math::quantile(map, 0.5);
} catch (const std::logic_error& e) {
return std::numeric_limits<double>::quiet_NaN();
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/test/unit/mcmc/chainset_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,15 @@ TEST_F(McmcChains, quantile_tests) {
EXPECT_TRUE(std::isnan(stepsize_quantiles(i)));
}

double stepsize_MAD = datagen_chains.med_abs_deviation("stepsize__");
EXPECT_TRUE(std::isnan(stepsize_MAD));

Eigen::VectorXd bad_probs(3);
bad_probs << 5, 50, 95;
Eigen::VectorXd y_sim_quantiles
= datagen_chains.quantiles("y_sim[1]", bad_probs);
for (size_t i = 0; i < bad_probs.size(); ++i) {
EXPECT_TRUE(std::isnan(y_sim_quantiles(i)));
}

}

0 comments on commit 3a76d5a

Please sign in to comment.