Skip to content

Commit

Permalink
Remove array from writer overloads and have pathfinder only write wit…
Browse files Browse the repository at this point in the history
…h matrices
  • Loading branch information
SteveBronder committed Jan 5, 2024
1 parent d69a2df commit 1e776fa
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 99 deletions.
51 changes: 1 addition & 50 deletions src/stan/callbacks/unique_stream_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class unique_stream_writer final : public writer {
return;
write_vector(values);
}

/**
* Writes a set of values in csv format followed by a newline.
*
Expand Down Expand Up @@ -109,38 +110,6 @@ class unique_stream_writer final : public writer {
*output_ << values.format(CommaInitFmt);
}

/**
* Writes a set of values in csv format followed by a newline.
*
* Note: the precision of the output is determined by the settings
* of the stream on construction.
*
* @param[in] v Values in an Eigen column array
*/
void operator()(const Eigen::Array<double, -1, 1>& values) {
if (output_ == nullptr)
return;
Eigen::IOFormat CommaInitFmt(Eigen::StreamPrecision, Eigen::DontAlignCols,
", ", "", "", "\n", "", "");
*output_ << values.transpose().format(CommaInitFmt);
}

/**
* Writes a set of values in csv format followed by a newline.
*
* Note: the precision of the output is determined by the settings
* of the stream on construction.
*
* @param[in] v Values in an Eigen row array
*/
void operator()(const Eigen::Array<double, 1, -1>& values) {
if (output_ == nullptr)
return;
Eigen::IOFormat CommaInitFmt(Eigen::StreamPrecision, Eigen::DontAlignCols,
", ", "", "", "\n", "", "");
*output_ << values.format(CommaInitFmt);
}

/**
* Writes multiple rows and columns of values in csv format.
*
Expand All @@ -159,24 +128,6 @@ class unique_stream_writer final : public writer {
*output_ << values.transpose().format(CommaInitFmt);
}

/**
* Writes multiple rows and columns of values in csv format.
*
* Note: the precision of the output is determined by the settings
* of the stream on construction.
*
* @param[in] values An array of values. The input is expected to have
* parameters in the rows and samples in the columns. The array is then
* transposed for the output.
*/
void operator()(const Eigen::Array<double, -1, -1>& values) {
if (output_ == nullptr)
return;
Eigen::IOFormat CommaInitFmt(Eigen::StreamPrecision, Eigen::DontAlignCols,
", ", "", "", "\n", "", "");
*output_ << values.transpose().format(CommaInitFmt);
}

/**
* Writes the comment_prefix to the stream followed by a newline.
*/
Expand Down
33 changes: 1 addition & 32 deletions src/stan/callbacks/writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,7 @@ class writer {
* parameters in the rows and samples in the columns. The matrix is then
* transposed for the output.
*/
virtual void operator()(const Eigen::MatrixXd& values) {}

/**
* Writes multiple rows and columns of values in csv format.
*
* Note: the precision of the output is determined by the settings
* of the stream on construction.
*
* @param[in] values An array of values. The input is expected to have
* parameters in the rows and samples in the columns. The array is then
* transposed for the output.
*/
virtual void operator()(const Eigen::Array<double, -1, -1>& vals) {}
virtual void operator()(const Eigen::Matrix<double, -1, -1>& values) {}

/**
* Writes a set of values in csv format followed by a newline.
Expand All @@ -90,25 +78,6 @@ class writer {
*/
virtual void operator()(const Eigen::Matrix<double, 1, -1>& values) {}

/**
* Writes a set of values in csv format followed by a newline.
*
* Note: the precision of the output is determined by the settings
* of the stream on construction.
*
* @param[in] v Values in an Eigen column array
*/
virtual void operator()(const Eigen::Array<double, -1, 1>& values) {}

/**
* Writes a set of values in csv format followed by a newline.
*
* Note: the precision of the output is determined by the settings
* of the stream on construction.
*
* @param[in] v Values in an Eigen row array
*/
virtual void operator()(const Eigen::Array<double, 1, -1>& values) {}
};

} // namespace callbacks
Expand Down
4 changes: 2 additions & 2 deletions src/stan/services/pathfinder/multi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ inline int pathfinder_lbfgs_multi(
num_returned_samples += ilpr.size();
}
// Rows are individual parameters and columns are samples per iteration
Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic> samples(
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> samples(
individual_samples[0].rows(), num_returned_samples);
Eigen::Index filling_start_row = 0;
for (size_t i = 0; i < successful_pathfinders; ++i) {
const Eigen::Index individ_num_samples = individual_samples[i].cols();
samples.middleCols(filling_start_row, individ_num_samples)
= individual_samples[i];
= individual_samples[i].matrix();
filling_start_row += individ_num_samples;
}
double psis_delta_time = 0;
Expand Down
30 changes: 15 additions & 15 deletions src/stan/services/pathfinder/single.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ inline auto pathfinder_lbfgs_single(
logger.error(e.what());
return internal::ret_pathfinder<ReturnLpSamples>(
error_codes::SOFTWARE, Eigen::Array<double, Eigen::Dynamic, 1>(0),
Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic>(0, 0), 0);
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>(0, 0), 0);
}

const auto num_parameters = cont_vector.size();
Expand Down Expand Up @@ -836,7 +836,7 @@ inline auto pathfinder_lbfgs_single(
+ " Optimization failed to start, pathfinder cannot be run.");
return internal::ret_pathfinder<ReturnLpSamples>(
error_codes::SOFTWARE, Eigen::Array<double, Eigen::Dynamic, 1>(0),
Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic>(0, 0),
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>(0, 0),
std::atomic<size_t>{num_evals + lbfgs.grad_evals()});
} else {
logger.warn(prefix_err_msg +
Expand All @@ -850,15 +850,15 @@ inline auto pathfinder_lbfgs_single(
"successfully");
return internal::ret_pathfinder<ReturnLpSamples>(
error_codes::SOFTWARE, Eigen::Array<double, Eigen::Dynamic, 1>(0),
Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic>(0, 0), num_evals);
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>(0, 0), num_evals);
} else {
if (refresh != 0) {
logger.info(path_num + "Best Iter: [" + std::to_string(best_iteration)
+ "] ELBO (" + std::to_string(elbo_best.elbo) + ")"
+ " evaluations: (" + std::to_string(num_evals) + ")");
}
}
Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic> constrained_draws_mat;
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> constrained_draws_mat;
Eigen::Array<double, Eigen::Dynamic, 1> lp_ratio;
auto&& elbo_draws = elbo_best.repeat_draws;
auto&& elbo_lp_ratio = elbo_best.lp_ratio;
Expand All @@ -880,24 +880,24 @@ inline auto pathfinder_lbfgs_single(
lp_ratio.tail(new_lp_ratio.size()) = new_lp_ratio.array();
const auto total_size = elbo_draws.cols() + new_draws.cols();
constrained_draws_mat
= Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic>(names.size(),
= Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>(names.size(),
total_size);
Eigen::VectorXd unconstrained_col;
Eigen::VectorXd approx_samples_constrained_col;
for (Eigen::Index i = 0; i < elbo_draws.cols(); ++i) {
constrained_draws_mat.col(i).head(2) = elbo_lp_mat.row(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);
approx_samples_constrained_col).matrix();
}
for (Eigen::Index i = elbo_draws.cols(), j = 0; i < total_size;
++i, ++j) {
constrained_draws_mat.col(i).head(2) = lp_draws.row(j);
constrained_draws_mat.col(i).head(2) = lp_draws.row(j).matrix();
unconstrained_col = new_draws.col(j);
constrained_draws_mat.col(i).tail(num_unconstrained_params)
= constrain_fun(rng, unconstrained_col,
approx_samples_constrained_col);
approx_samples_constrained_col).matrix();
}
} catch (const std::exception& e) {
std::string err_msg = e.what();
Expand All @@ -908,31 +908,31 @@ inline auto pathfinder_lbfgs_single(
+ "Returning the approximate samples used for ELBO calculation: "
+ err_msg);
constrained_draws_mat
= Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic>(
= Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>(
names.size(), elbo_draws.cols());
Eigen::VectorXd approx_samples_constrained_col;
Eigen::VectorXd unconstrained_col;
for (Eigen::Index i = 0; i < elbo_draws.cols(); ++i) {
constrained_draws_mat.col(i).head(2) = elbo_lp_mat.row(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);
approx_samples_constrained_col).matrix();
}
lp_ratio = std::move(elbo_best.lp_ratio);
}
} else {
constrained_draws_mat
= Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic>(
= Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>(
names.size(), elbo_draws.cols());
Eigen::VectorXd approx_samples_constrained_col;
Eigen::VectorXd unconstrained_col;
for (Eigen::Index i = 0; i < elbo_draws.cols(); ++i) {
constrained_draws_mat.col(i).head(2) = elbo_lp_mat.row(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);
approx_samples_constrained_col).matrix();
}
lp_ratio = std::move(elbo_best.lp_ratio);
}
Expand Down

0 comments on commit 1e776fa

Please sign in to comment.