Skip to content

Commit

Permalink
Merge pull request #3279 from stan-dev/fix/3268-pathfinder-too-many-d…
Browse files Browse the repository at this point in the history
…raws

Pathfinder: stop returning more draws than requested
  • Loading branch information
SteveBronder committed Apr 15, 2024
2 parents ed65c9e + eb2dffc commit c582de8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
13 changes: 9 additions & 4 deletions src/stan/services/pathfinder/psis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <stan/callbacks/logger.hpp>
#include <stan/services/error_codes.hpp>
#include <tbb/parallel_invoke.h>
#include <iomanip>
#include <sstream>

namespace stan {
namespace services {
Expand Down Expand Up @@ -261,10 +263,13 @@ inline Eigen::Array<double, Eigen::Dynamic, 1> psis_weights(
llr_weights.coeffRef(idx.coeff(i)) = smoothed.first.coeff(i);
}
if (smoothed.second > 0.7) {
logger.warn(std::string("Pareto k value (") +
std::to_string(smoothed.second) + ") is greater than 0.7."
" Importance resampling was not able to improve the approximation,"
" which may indicate that the approximation itself is poor.");
std::stringstream s;
s << "Pareto k value (" << std::setprecision(2) << smoothed.second
<< ") is greater than 0.7. Importance resampling was not able to "
<< "improve the approximation, which may indicate that the "
<< "approximation itself is poor.";

logger.warn(s.str());
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/stan/services/pathfinder/single.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -933,20 +933,21 @@ inline auto pathfinder_lbfgs_single(
lp_ratio = std::move(elbo_best.lp_ratio);
}
} else {
// output only first num_draws from what we computed for ELBO
constrained_draws_mat
= Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>(
names.size(), elbo_draws.cols());
= Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>(names.size(),
num_draws);
Eigen::VectorXd approx_samples_constrained_col;
Eigen::VectorXd unconstrained_col;
for (Eigen::Index i = 0; i < elbo_draws.cols(); ++i) {
for (Eigen::Index i = 0; i < num_draws; ++i) {
constrained_draws_mat.col(i).head(2) = elbo_lp_mat.row(i).matrix();
unconstrained_col = elbo_draws.col(i);
constrained_draws_mat.col(i).tail(num_unconstrained_params)
= constrain_fun(rng, unconstrained_col,
approx_samples_constrained_col)
.matrix();
}
lp_ratio = std::move(elbo_best.lp_ratio);
lp_ratio = std::move(elbo_best.lp_ratio.head(num_draws));
}
parameter_writer(constrained_draws_mat);
parameter_writer();
Expand Down

0 comments on commit c582de8

Please sign in to comment.