Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement calculate_lp, diagnostic args for laplace #1246

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/cmdstan/arguments/arg_laplace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ class arg_laplace : public categorical_argument {
""));
_subarguments.push_back(
new arg_single_bool("jacobian",
"When true, include change-of-variables adjustment"
" for constraining parameter transforms",
"When true, include change-of-variables adjustment "
"for constraining parameter transforms.",
true));
_subarguments.push_back(new arg_single_int_nonneg(
"draws", "Number of draws from the laplace approximation", 1000));
_subarguments.push_back(new arg_single_bool(
"calculate_lp",
"If true, calculate the log probability of the model at each draw.",
true));
}
};

Expand Down
24 changes: 17 additions & 7 deletions src/cmdstan/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,20 @@ int command(int argc, const char *argv[]) {
init_filestream_writers(sample_writers, num_chains, id, output_file, "",
".csv", sig_figs, "# ");
if (!diagnostic_file.empty()) {
init_filestream_writers(diagnostic_csv_writers, num_chains, id,
diagnostic_file, "", ".csv", sig_figs, "# ");
if (user_method->arg("laplace")) {
init_filestream_writers(diagnostic_json_writers, num_chains, id,
diagnostic_file, "", ".json", sig_figs);
init_null_writers(diagnostic_csv_writers, num_chains);

} else {
init_filestream_writers(diagnostic_csv_writers, num_chains, id,
diagnostic_file, "", ".csv", sig_figs, "# ");
init_null_writers(diagnostic_json_writers, num_chains);
}
} else {
init_null_writers(diagnostic_csv_writers, num_chains);
init_null_writers(diagnostic_json_writers, num_chains);
}
init_null_writers(diagnostic_json_writers, num_chains);
}
if (user_method->arg("sample")
&& get_arg_val<bool_argument>(parser, "method", "sample", "adapt",
Expand Down Expand Up @@ -383,15 +391,17 @@ int command(int argc, const char *argv[]) {
}
Eigen::VectorXd theta_hat = get_laplace_mode(fname, model);
bool jacobian = get_arg_val<bool_argument>(*laplace_arg, "jacobian");
bool calculate_lp
= get_arg_val<bool_argument>(*laplace_arg, "calculate_lp");
int draws = get_arg_val<int_argument>(*laplace_arg, "draws");
if (jacobian) {
return_code = stan::services::laplace_sample<true>(
model, theta_hat, draws, random_seed, refresh, interrupt, logger,
sample_writers[0]);
model, theta_hat, draws, calculate_lp, random_seed, refresh,
interrupt, logger, sample_writers[0], diagnostic_json_writers[0]);
} else {
return_code = stan::services::laplace_sample<false>(
model, theta_hat, draws, random_seed, refresh, interrupt, logger,
sample_writers[0]);
model, theta_hat, draws, calculate_lp, random_seed, refresh,
interrupt, logger, sample_writers[0], diagnostic_json_writers[0]);
}
// ---- laplace end ---- //
} else if (user_method->arg("log_prob")) {
Expand Down