Skip to content

Commit

Permalink
fix, unit tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
mitzimorris committed Nov 27, 2024
1 parent 162cf9f commit 1c9c66d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/stan/mcmc/chainset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,15 @@ class chainset {
* @return parameter value at quantile
*/
double quantile(const int index, const double prob) const {
// Ensure the probability is within [0, 1]
Eigen::MatrixXd draws = samples(index);
Eigen::Map<Eigen::VectorXd> map(draws.data(), draws.size());
return stan::math::quantile(map, prob);
double result;
try {
result = stan::math::quantile(map, prob);
} catch (const std::logic_error& e) {
return std::numeric_limits<double>::quiet_NaN();
}
return result;
}

/**
Expand Down Expand Up @@ -332,7 +337,14 @@ class chainset {
Eigen::MatrixXd draws = samples(index);
Eigen::Map<Eigen::VectorXd> map(draws.data(), draws.size());
std::vector<double> probs_vec(probs.data(), probs.data() + probs.size());
std::vector<double> quantiles = stan::math::quantile(map, probs_vec);
std::vector<double> quantiles;
try {
quantiles = stan::math::quantile(map, probs_vec);
} catch (const std::logic_error& e) {
Eigen::VectorXd nans(probs.size());
nans.setConstant(std::numeric_limits<double>::quiet_NaN());
return nans;
}
return Eigen::Map<Eigen::VectorXd>(quantiles.data(), quantiles.size());
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/unit/mcmc/chainset_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,6 @@ TEST_F(McmcChains, quantile_tests) {
probs << 0.0, 0.01, 0.05, 0.95, 0.99, 1.0;
Eigen::VectorXd stepsize_quantiles = datagen_chains.quantiles("stepsize__", probs);
for (size_t i = 0; i < probs.size(); ++i) {
EXPECT_EQ(stepsize_quantiles(i), std::numeric_limits<double>::quiet_NaN());
EXPECT_TRUE(std::isnan(stepsize_quantiles(i)));
}
}

0 comments on commit 1c9c66d

Please sign in to comment.