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

Prevent pathfinder when 0 parameters, warn if too many PSIS requested #1221

Merged
merged 2 commits into from
Nov 9, 2023
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
12 changes: 12 additions & 0 deletions src/cmdstan/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,18 @@ int command(int argc, const char *argv[]) {
int num_draws = get_arg_val<int_argument>(*pathfinder_arg, "num_draws");
int num_psis_draws
= get_arg_val<int_argument>(*pathfinder_arg, "num_psis_draws");

if (num_psis_draws > num_draws * num_chains) {
logger.warn(
"Warning: Number of PSIS draws is larger than the total number of "
"draws returned by the single Pathfinders. This is likely "
"unintentional and leads to re-sampling from the same draws.");
}
if (model.num_params_r() == 0) {
throw std::invalid_argument(
"Model has 0 parameters, cannot run Pathfinder.");
}

if (num_chains == 1) {
save_single_paths = save_single_paths || !diagnostic_file.empty();
return_code = stan::services::pathfinder::pathfinder_lbfgs_single<
Expand Down
20 changes: 20 additions & 0 deletions src/test/interface/pathfinder_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class CmdStan : public testing::Test {
eight_schools_model = {"src", "test", "test-models", "eight_schools"};
eight_schools_data
= {"src", "test", "test-models", "eight_schools.data.json"};
empty_model = {"src", "test", "test-models", "empty"};
arg_output = {"test", "output"};
arg_diags = {"test", "diagnostics"};
output_csv = {"test", "output.csv"};
Expand All @@ -38,6 +39,7 @@ class CmdStan : public testing::Test {
std::vector<std::string> multi_normal_model;
std::vector<std::string> eight_schools_model;
std::vector<std::string> eight_schools_data;
std::vector<std::string> empty_model;
std::vector<std::string> arg_output;
std::vector<std::string> arg_diags;
std::vector<std::string> output_csv;
Expand Down Expand Up @@ -241,3 +243,21 @@ TEST_F(CmdStan, pathfinder_lbfgs_iterations) {
EXPECT_EQ(1, count_matches("\"3\":{\"iter\":3,", output));
EXPECT_EQ(0, count_matches("\"4\":{\"iter\":4,", output));
}

TEST_F(CmdStan, pathfinder_empty_model) {
std::stringstream ss;
ss << convert_model_path(empty_model) << " method=pathfinder";
run_command_output out = run_command(ss.str());
ASSERT_TRUE(out.hasError);
EXPECT_EQ(1, count_matches("Model has 0 parameters", out.output));
}

TEST_F(CmdStan, pathfinder_too_many_PSIS_draws) {
std::stringstream ss;
ss << convert_model_path(multi_normal_model) << " method=pathfinder"
<< " num_paths=1 num_draws=10 num_psis_draws=11";
run_command_output out = run_command(ss.str());
ASSERT_FALSE(out.hasError);
EXPECT_EQ(
1, count_matches("Warning: Number of PSIS draws is larger", out.output));
}