Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/stan-dev/stan into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
mitzimorris committed Nov 27, 2024
2 parents a7f38f2 + 595ae41 commit 9774824
Show file tree
Hide file tree
Showing 10 changed files with 306 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/stan_math
Submodule stan_math updated 7632 files
1 change: 0 additions & 1 deletion src/stan/analyze/mcmc/mcse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace analyze {
* @return mcse
*/
inline double mcse_mean(const Eigen::MatrixXd& chains) {
const Eigen::Index num_draws = chains.rows();
if (chains.rows() < 4 || !is_finite_and_varies(chains))
return std::numeric_limits<double>::quiet_NaN();

Expand Down
4 changes: 4 additions & 0 deletions src/stan/io/stan_csv_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ class stan_csv_reader {

int rows = lines - 3;
int cols = std::count(line.begin(), line.end(), ',') + 1;
if (cols == 1) {
// model has no parameters
return;
}
adaptation.metric.resize(rows, cols);
char comment; // Buffer for comment indicator, #

Expand Down
47 changes: 40 additions & 7 deletions src/stan/services/util/initialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,19 @@ std::vector<double> initialize(Model& model, const InitContext& init, RNG& rng,
model.transform_inits(context, disc_vector, unconstrained, &msg);
}
} catch (std::domain_error& e) {
if (msg.str().length() > 0)
if (msg.str().length() > 0) {
logger.info(msg);
}
logger.warn("Rejecting initial value:");
logger.warn(
" Error evaluating the log probability"
" at the initial value.");
logger.warn(e.what());
continue;
} catch (std::exception& e) {
if (msg.str().length() > 0)
if (msg.str().length() > 0) {
logger.info(msg);
}
logger.error(
"Unrecoverable error evaluating the log probability"
" at the initial value.");
Expand All @@ -127,8 +129,9 @@ std::vector<double> initialize(Model& model, const InitContext& init, RNG& rng,
// the parameters.
log_prob = model.template log_prob<false, Jacobian>(unconstrained,
disc_vector, &msg);
if (msg.str().length() > 0)
if (msg.str().length() > 0) {
logger.info(msg);
}
} catch (std::domain_error& e) {
if (msg.str().length() > 0)
logger.info(msg);
Expand All @@ -139,8 +142,9 @@ std::vector<double> initialize(Model& model, const InitContext& init, RNG& rng,
logger.warn(e.what());
continue;
} catch (std::exception& e) {
if (msg.str().length() > 0)
if (msg.str().length() > 0) {
logger.info(msg);
}
logger.error(
"Unrecoverable error evaluating the log probability"
" at the initial value.");
Expand All @@ -165,8 +169,9 @@ std::vector<double> initialize(Model& model, const InitContext& init, RNG& rng,
log_prob = stan::model::log_prob_grad<true, Jacobian>(
model, unconstrained, disc_vector, gradient, &log_prob_msg);
} catch (const std::exception& e) {
if (log_prob_msg.str().length() > 0)
if (log_prob_msg.str().length() > 0) {
logger.info(log_prob_msg);
}
logger.error(e.what());
throw;
}
Expand Down Expand Up @@ -210,8 +215,36 @@ std::vector<double> initialize(Model& model, const InitContext& init, RNG& rng,
return unconstrained;
}
}

if (!is_initialized_with_zero) {
if (is_fully_initialized) {
logger.info("");
logger.error("User-specified initialization failed.");
logger.error(
" Try specifying new initial values,"
" using partially specialized initialization,"
" reducing the range of constrained values,"
" or reparameterizing the model.");
} else if (any_initialized) {
logger.info("");
std::stringstream msg;
msg << "Partial user-specified initialization failed. "
"Initialization of non user specified parameters "
"between (-"
<< init_radius << ", " << init_radius << ") failed after"
<< " " << MAX_INIT_TRIES << " attempts. ";
logger.error(msg);
logger.error(
" Try specifying full initial values,"
" reducing the range of constrained values,"
" or reparameterizing the model.");
} else if (is_initialized_with_zero) {
logger.info("");
logger.error("Initial values of 0 failed to initialize.");
logger.error(
" Try specifying new initial values,"
" using partially specialized initialization,"
" reducing the range of constrained values,"
" or reparameterizing the model.");
} else {
logger.info("");
std::stringstream msg;
msg << "Initialization between (-" << init_radius << ", " << init_radius
Expand Down
6 changes: 6 additions & 0 deletions src/test/test-models/good/services/test_fail.stan
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters {
array[2] real<lower=-10, upper=10> y;
}
model {
reject("");
}
13 changes: 13 additions & 0 deletions src/test/unit/io/stan_csv_reader_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class StanIoStanCsvReader : public testing::Test {
"src/test/unit/io/test_csv_files/bernoulli_warmup.csv");
fixed_param_stream.open(
"src/test/unit/io/test_csv_files/fixed_param_output.csv");
no_params_stream.open(
"src/test/unit/io/test_csv_files/no_parameters_hmc.csv");
}

void TearDown() {
Expand All @@ -46,6 +48,7 @@ class StanIoStanCsvReader : public testing::Test {
bernoulli_thin_stream.close();
bernoulli_warmup_stream.close();
fixed_param_stream.close();
no_params_stream.close();
}

std::ifstream blocker0_stream, epil0_stream;
Expand All @@ -58,6 +61,7 @@ class StanIoStanCsvReader : public testing::Test {
std::ifstream bernoulli_thin_stream;
std::ifstream bernoulli_warmup_stream;
std::ifstream fixed_param_stream;
std::ifstream no_params_stream;
};

TEST_F(StanIoStanCsvReader, read_metadata1) {
Expand Down Expand Up @@ -570,6 +574,14 @@ TEST_F(StanIoStanCsvReader, fixed_param) {
ASSERT_EQ(10, fixed_param.samples.rows());
}

TEST_F(StanIoStanCsvReader, no_parameters) {
stan::io::stan_csv no_parameters_hmc;
std::stringstream out;
no_parameters_hmc = stan::io::stan_csv_reader::parse(no_params_stream, &out);
ASSERT_EQ(100, no_parameters_hmc.samples.rows());
ASSERT_EQ(0, no_parameters_hmc.adaptation.metric.size());
}

TEST_F(StanIoStanCsvReader, no_samples) {
std::ifstream no_samples_stream;
no_samples_stream.open(
Expand All @@ -590,4 +602,5 @@ TEST_F(StanIoStanCsvReader, variational) {
= stan::io::stan_csv_reader::parse(variational_stream, &out);
variational_stream.close();
ASSERT_EQ(1000, variational.metadata.num_samples);
ASSERT_EQ(0, variational.adaptation.metric.size());
}
157 changes: 157 additions & 0 deletions src/test/unit/io/test_csv_files/no_parameters_hmc.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# stan_version_major = 2
# stan_version_minor = 35
# stan_version_patch = 0
# model = sim_model
# start_datetime = 2024-11-25 16:05:51 UTC
# method = sample (Default)
# sample
# num_samples = 100
# num_warmup = 100
# save_warmup = false (Default)
# thin = 1 (Default)
# adapt
# engaged = true (Default)
# gamma = 0.05 (Default)
# delta = 0.8 (Default)
# kappa = 0.75 (Default)
# t0 = 10 (Default)
# init_buffer = 75 (Default)
# term_buffer = 50 (Default)
# window = 25 (Default)
# save_metric = false (Default)
# algorithm = hmc (Default)
# hmc
# engine = nuts (Default)
# nuts
# max_depth = 10 (Default)
# metric = diag_e (Default)
# metric_file = (Default)
# stepsize = 1 (Default)
# stepsize_jitter = 0 (Default)
# num_chains = 1 (Default)
# id = 1 (Default)
# data
# file = (Default)
# init = 2 (Default)
# random
# seed = 1799096017 (Default)
# output
# file = output.csv (Default)
# diagnostic_file = (Default)
# refresh = 100 (Default)
# sig_figs = -1 (Default)
# profile_file = profile.csv (Default)
# save_cmdstan_config = false (Default)
# num_threads = 1 (Default)
# stanc_version = %%NAME%%3 %%VERSION%%
# stancflags =
lp__,accept_stat__,stepsize__,treedepth__,n_leapfrog__,divergent__,energy__,x
# Adaptation terminated
# Step size = 1.35996e+14
# Diagonal elements of inverse mass matrix:
#
0,1,1.35996e+14,1,1,0,0,8.57152
0,1,1.35996e+14,1,1,0,0,-4.56101
0,1,1.35996e+14,1,1,0,0,9.30019
0,1,1.35996e+14,1,1,0,0,-14.6254
0,1,1.35996e+14,1,1,0,0,8.30123
0,1,1.35996e+14,1,1,0,0,19.2067
0,1,1.35996e+14,1,1,0,0,12.6027
0,1,1.35996e+14,1,1,0,0,-5.70173
0,1,1.35996e+14,1,1,0,0,3.04002
0,1,1.35996e+14,1,1,0,0,-8.28215
0,1,1.35996e+14,1,1,0,0,3.20386
0,1,1.35996e+14,1,1,0,0,-6.45707
0,1,1.35996e+14,1,1,0,0,-5.29269
0,1,1.35996e+14,1,1,0,0,5.1527
0,1,1.35996e+14,1,1,0,0,-3.69775
0,1,1.35996e+14,1,1,0,0,-1.77925
0,1,1.35996e+14,1,1,0,0,-13.7562
0,1,1.35996e+14,1,1,0,0,-0.789554
0,1,1.35996e+14,1,1,0,0,-14.0911
0,1,1.35996e+14,1,1,0,0,-4.71911
0,1,1.35996e+14,1,1,0,0,-12.7874
0,1,1.35996e+14,1,1,0,0,15.029
0,1,1.35996e+14,1,1,0,0,-6.30709
0,1,1.35996e+14,1,1,0,0,3.50805
0,1,1.35996e+14,1,1,0,0,9.01495
0,1,1.35996e+14,1,1,0,0,5.9809
0,1,1.35996e+14,1,1,0,0,11.02
0,1,1.35996e+14,1,1,0,0,9.68621
0,1,1.35996e+14,1,1,0,0,-4.20659
0,1,1.35996e+14,1,1,0,0,-7.71128
0,1,1.35996e+14,1,1,0,0,-13.6125
0,1,1.35996e+14,1,1,0,0,0.541567
0,1,1.35996e+14,1,1,0,0,-6.96969
0,1,1.35996e+14,1,1,0,0,2.49902
0,1,1.35996e+14,1,1,0,0,8.31074
0,1,1.35996e+14,1,1,0,0,13.4856
0,1,1.35996e+14,1,1,0,0,-4.60591
0,1,1.35996e+14,1,1,0,0,6.84733
0,1,1.35996e+14,1,1,0,0,-16.8676
0,1,1.35996e+14,1,1,0,0,4.43181
0,1,1.35996e+14,1,1,0,0,-10.6785
0,1,1.35996e+14,1,1,0,0,-5.56113
0,1,1.35996e+14,1,1,0,0,-0.95401
0,1,1.35996e+14,1,1,0,0,11.2198
0,1,1.35996e+14,1,1,0,0,3.43417
0,1,1.35996e+14,1,1,0,0,-11.2942
0,1,1.35996e+14,1,1,0,0,-14.3029
0,1,1.35996e+14,1,1,0,0,3.69492
0,1,1.35996e+14,1,1,0,0,0.319324
0,1,1.35996e+14,1,1,0,0,-5.95097
0,1,1.35996e+14,1,1,0,0,5.99333
0,1,1.35996e+14,1,1,0,0,-6.59629
0,1,1.35996e+14,1,1,0,0,14.1795
0,1,1.35996e+14,1,1,0,0,-7.58818
0,1,1.35996e+14,1,1,0,0,4.89377
0,1,1.35996e+14,1,1,0,0,4.63195
0,1,1.35996e+14,1,1,0,0,-4.62905
0,1,1.35996e+14,1,1,0,0,-11.4145
0,1,1.35996e+14,1,1,0,0,4.03017
0,1,1.35996e+14,1,1,0,0,-10.0459
0,1,1.35996e+14,1,1,0,0,-11.8674
0,1,1.35996e+14,1,1,0,0,-0.161997
0,1,1.35996e+14,1,1,0,0,-5.75037
0,1,1.35996e+14,1,1,0,0,-13.3027
0,1,1.35996e+14,1,1,0,0,4.86817
0,1,1.35996e+14,1,1,0,0,11.1937
0,1,1.35996e+14,1,1,0,0,13.918
0,1,1.35996e+14,1,1,0,0,-12.2423
0,1,1.35996e+14,1,1,0,0,22.3588
0,1,1.35996e+14,1,1,0,0,-8.30628
0,1,1.35996e+14,1,1,0,0,-6.87127
0,1,1.35996e+14,1,1,0,0,12.721
0,1,1.35996e+14,1,1,0,0,-7.86135
0,1,1.35996e+14,1,1,0,0,-8.56196
0,1,1.35996e+14,1,1,0,0,-4.04709
0,1,1.35996e+14,1,1,0,0,-21.6766
0,1,1.35996e+14,1,1,0,0,-19.6485
0,1,1.35996e+14,1,1,0,0,1.99421
0,1,1.35996e+14,1,1,0,0,11.2645
0,1,1.35996e+14,1,1,0,0,-9.35154
0,1,1.35996e+14,1,1,0,0,-3.37081
0,1,1.35996e+14,1,1,0,0,2.46874
0,1,1.35996e+14,1,1,0,0,7.28248
0,1,1.35996e+14,1,1,0,0,19.3846
0,1,1.35996e+14,1,1,0,0,-4.92502
0,1,1.35996e+14,1,1,0,0,-11.4687
0,1,1.35996e+14,1,1,0,0,3.12818
0,1,1.35996e+14,1,1,0,0,-5.79636
0,1,1.35996e+14,1,1,0,0,12.2196
0,1,1.35996e+14,1,1,0,0,5.59427
0,1,1.35996e+14,1,1,0,0,-22.9084
0,1,1.35996e+14,1,1,0,0,2.70951
0,1,1.35996e+14,1,1,0,0,-0.604509
0,1,1.35996e+14,1,1,0,0,1.06201
0,1,1.35996e+14,1,1,0,0,-3.10986
0,1,1.35996e+14,1,1,0,0,6.57804
0,1,1.35996e+14,1,1,0,0,9.0426
0,1,1.35996e+14,1,1,0,0,3.30854
0,1,1.35996e+14,1,1,0,0,8.85378
0,1,1.35996e+14,1,1,0,0,-1.71584
#
# Elapsed Time: 0 seconds (Warm-up)
# 0 seconds (Sampling)
# 0 seconds (Total)
#
17 changes: 17 additions & 0 deletions src/test/unit/services/instrumented_callbacks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,23 @@ class instrumented_logger : public stan::callbacks::logger {
return count;
}

public:
std::vector<std::string> return_all_logs() {
std::vector<std::string> all_logs;
all_logs.reserve(debug_.size() + info_.size() + warn_.size() + error_.size()
+ fatal_.size() + 5);
all_logs.emplace_back("DEBUG");
all_logs.insert(all_logs.end(), debug_.begin(), debug_.end());
all_logs.emplace_back("INFO");
all_logs.insert(all_logs.end(), info_.begin(), info_.end());
all_logs.emplace_back("WARN");
all_logs.insert(all_logs.end(), warn_.begin(), warn_.end());
all_logs.emplace_back("ERROR");
all_logs.insert(all_logs.end(), error_.begin(), error_.end());
all_logs.emplace_back("FATAL");
all_logs.insert(all_logs.end(), fatal_.begin(), fatal_.end());
return all_logs;
}
std::vector<std::string> debug_;
std::vector<std::string> info_;
std::vector<std::string> warn_;
Expand Down
Loading

0 comments on commit 9774824

Please sign in to comment.