Skip to content

Commit

Permalink
cpplint
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveBronder committed Jan 2, 2024
1 parent 53790e3 commit ef527c9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
23 changes: 12 additions & 11 deletions src/stan/services/pathfinder/multi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ namespace pathfinder {
* `error_codes::SOFTWARE` for failures
* @param[in] return_lp Whether single pathfinder should return lp calculations.
* If `true`, calculates the joint log probability for each sample.
* If `false`, (`num_draws` - `num_elbo_draws`) of the joint log probability calculations will be `NA` and psis resampling will not be performed.
* @param[in] psis_resampling If `true`, psis resampling is performed over the
* samples returned by all of the individual pathfinders and `num_multi_draws`
* samples are returned. If `false`, no psis resampling is performed
* If `false`, (`num_draws` - `num_elbo_draws`) of the joint log probability
* calculations will be `NA` and psis resampling will not be performed.
* @param[in] psis_resampling If `true`, psis resampling is performed over the
* samples returned by all of the individual pathfinders and `num_multi_draws`
* samples are returned. If `false`, no psis resampling is performed
* and (`num_paths` * `num_draws`) samples are returned.
* @return error_codes::OK if successful
*/
Expand Down Expand Up @@ -166,7 +167,7 @@ inline int pathfinder_lbfgs_multi(
for (auto&& ilpr : individual_lp_ratios) {
num_returned_samples += ilpr.size();
}
// Rows are individual parameters and columns are samples per iteration
// Rows are individual parameters and columns are samples per iteration
Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic> samples(
individual_samples[0].rows(), num_returned_samples);
Eigen::Index filling_start_row = 0;
Expand All @@ -188,19 +189,19 @@ inline int pathfinder_lbfgs_multi(
}

const auto tail_len = std::min(0.2 * num_returned_samples,
3 * std::sqrt(num_returned_samples));
3 * std::sqrt(num_returned_samples));
Eigen::Array<double, Eigen::Dynamic, 1> weight_vals
= stan::services::psis::psis_weights(lp_ratios, tail_len, logger);
boost::ecuyer1988 rng
= util::create_rng<boost::ecuyer1988>(random_seed, stride_id);
boost::variate_generator<
boost::ecuyer1988&,
boost::random::discrete_distribution<Eigen::Index, double>>
rand_psis_idx(rng,
boost::random::discrete_distribution<Eigen::Index, double>(
boost::iterator_range<double*>(
weight_vals.data(),
weight_vals.data() + weight_vals.size())));
rand_psis_idx(
rng, boost::random::discrete_distribution<Eigen::Index, double>(
boost::iterator_range<double*>(
weight_vals.data(),
weight_vals.data() + weight_vals.size())));
for (size_t i = 0; i <= num_multi_draws - 1; ++i) {
parameter_writer(samples.col(rand_psis_idx()));
}
Expand Down
23 changes: 13 additions & 10 deletions src/stan/services/pathfinder/single.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ inline elbo_est_t est_approx_draws(LPF&& lp_fun, ConstrainF&& constrain_fun,
RNG&& rng,
const taylor_approx_t& taylor_approx,
size_t num_samples, const EigVec& alpha,
const std::string& iter_msg,
Logger&& logger, bool return_lp = true) {
const std::string& iter_msg, Logger&& logger,
bool return_lp = true) {
boost::variate_generator<boost::ecuyer1988&, boost::normal_distribution<>>
rand_unit_gaus(rng, boost::normal_distribution<>());
const auto num_params = taylor_approx.x_center.size();
Expand Down Expand Up @@ -253,10 +253,12 @@ inline elbo_est_t est_approx_draws(LPF&& lp_fun, ConstrainF&& constrain_fun,
}
log_stream(logger, pathfinder_ss, iter_msg);
}
lp_ratio = lp_mat.col(1) - lp_mat.col(0);
lp_ratio = lp_mat.col(1) - lp_mat.col(0);
} else {
lp_ratio = Eigen::Array<double, Eigen::Dynamic, 1>::Constant(lp_mat.rows(), std::numeric_limits<double>::quiet_NaN());
lp_mat.col(1) = Eigen::Matrix<double, Eigen::Dynamic, 1>::Constant(lp_mat.rows(), std::numeric_limits<double>::quiet_NaN());
lp_ratio = Eigen::Array<double, Eigen::Dynamic, 1>::Constant(
lp_mat.rows(), std::numeric_limits<double>::quiet_NaN());
lp_mat.col(1) = Eigen::Matrix<double, Eigen::Dynamic, 1>::Constant(
lp_mat.rows(), std::numeric_limits<double>::quiet_NaN());
}
if (ReturnElbo) {
double elbo = lp_ratio.mean();
Expand Down Expand Up @@ -582,8 +584,9 @@ auto pathfinder_impl(RNG&& rng, LPFun&& lp_fun, ConstrainFun&& constrain_fun,
* @param[in,out] diagnostic_writer output for diagnostics values
* @param[in] return_lp Whether single pathfinder should return lp calculations.
* If `true`, calculates the joint log probability for each sample.
* If `false`, (`num_draws` - `num_elbo_draws`) of the joint log probability calculations will be `NA` and psis resampling will not be performed.
* Setting this parameter to `false` will also set all of the lp ratios to `NaN`.
* If `false`, (`num_draws` - `num_elbo_draws`) of the joint log probability
* calculations will be `NA` and psis resampling will not be performed. Setting
* this parameter to `false` will also set all of the lp ratios to `NaN`.
* @return If `ReturnLpSamples` is `true`, returns a tuple of the error code,
* approximate draws, and a vector of the lp ratio. If `false`, only returns an
* error code `error_codes::OK` if successful, `error_codes::SOFTWARE`
Expand All @@ -596,7 +599,7 @@ inline auto pathfinder_lbfgs_single(
unsigned int stride_id, double init_radius, int max_history_size,
double init_alpha, double tol_obj, double tol_rel_obj, double tol_grad,
double tol_rel_grad, double tol_param, int num_iterations,
int num_elbo_draws, int num_draws, bool save_iterations, int refresh,
int num_elbo_draws, int num_draws, bool save_iterations, int refresh,
callbacks::interrupt& interrupt, callbacks::logger& logger,
callbacks::writer& init_writer, ParamWriter& parameter_writer,
DiagnosticWriter& diagnostic_writer, bool return_lp = true) {
Expand Down Expand Up @@ -868,8 +871,8 @@ inline auto pathfinder_lbfgs_single(
auto&& new_lp_ratio = est_draws.lp_ratio;
auto&& lp_draws = est_draws.lp_mat;
auto&& new_draws = est_draws.repeat_draws;
lp_ratio = Eigen::Array<double, Eigen::Dynamic, 1>(
elbo_lp_ratio.size() + new_lp_ratio.size());
lp_ratio = Eigen::Array<double, Eigen::Dynamic, 1>(elbo_lp_ratio.size()
+ new_lp_ratio.size());
lp_ratio.head(elbo_lp_ratio.size()) = elbo_lp_ratio.array();
lp_ratio.tail(new_lp_ratio.size()) = new_lp_ratio.array();
const auto total_size = elbo_draws.cols() + new_draws.cols();
Expand Down

0 comments on commit ef527c9

Please sign in to comment.