Skip to content
Open
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
10 changes: 6 additions & 4 deletions src/stan/mcmc/covar_adaptation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ class covar_adaptation : public windowed_adaptation {

estimator_.sample_covariance(covar);

double n = static_cast<double>(estimator_.num_samples());
covar = (n / (n + 5.0)) * covar
+ 1e-3 * (5.0 / (n + 5.0))
* Eigen::MatrixXd::Identity(covar.rows(), covar.cols());
if (estimator_.num_samples() > 1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this behavior probably won't be obvious I think we need to wire a message to the logger. Both here and in var_adaptation can you add a std::string& msg argument which passes the message "Warning: inverse metric is not updated for window sizes less than 2". Then we can pass that to the logger in all of the transition implementations.

double n = static_cast<double>(estimator_.num_samples());
covar = (n / (n + 5.0)) * covar
+ 1e-3 * (5.0 / (n + 5.0))
* Eigen::MatrixXd::Identity(covar.rows(), covar.cols());
}

estimator_.restart();

Expand Down
8 changes: 5 additions & 3 deletions src/stan/mcmc/var_adaptation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ class var_adaptation : public windowed_adaptation {

estimator_.sample_variance(var);

double n = static_cast<double>(estimator_.num_samples());
var = (n / (n + 5.0)) * var
+ 1e-3 * (5.0 / (n + 5.0)) * Eigen::VectorXd::Ones(var.size());
if (estimator_.num_samples() > 1) {
double n = static_cast<double>(estimator_.num_samples());
var = (n / (n + 5.0)) * var
+ 1e-3 * (5.0 / (n + 5.0)) * Eigen::VectorXd::Ones(var.size());
}

estimator_.restart();

Expand Down
29 changes: 29 additions & 0 deletions src/test/unit/mcmc/covar_adaptation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,32 @@ TEST(McmcCovarAdaptation, learn_covariance) {
}
EXPECT_EQ(0, logger.call_count());
}

TEST(McmcCovarAdaptation, learn_covariance_one_sample) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corresponding test in var_adaptation_test.cpp?

stan::test::unit::instrumented_logger logger;

const int n = 10;
Eigen::VectorXd q = Eigen::VectorXd::Zero(n);
Eigen::MatrixXd covar(Eigen::MatrixXd::Identity(n, n));

const int n_learn = 1;

Eigen::MatrixXd target_covar(Eigen::MatrixXd::Identity(n, n));

stan::mcmc::covar_adaptation adapter(n);
adapter.set_window_params(50, 0, 0, n_learn, logger);

bool update = false;

for (int i = 0; i < n_learn; ++i)
update = adapter.learn_covariance(covar, q);

EXPECT_TRUE(update);

for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
EXPECT_EQ(target_covar(i, j), covar(i, j));
}
}
EXPECT_EQ(0, logger.call_count());
}