示例#1
0
TEST(McmcNutsBaseNuts, transition) {

  rng_t base_rng(0);

  int model_size = 1;
  double init_momentum = 1.5;

  stan::mcmc::ps_point z_init(model_size);
  z_init.q(0) = 0;
  z_init.p(0) = init_momentum;

  stan::mcmc::mock_model model(model_size);
  stan::mcmc::mock_nuts sampler(model, base_rng);

  sampler.set_nominal_stepsize(1);
  sampler.set_stepsize_jitter(0);
  sampler.sample_stepsize();
  sampler.z() = z_init;

  std::stringstream output_stream;
  stan::interface_callbacks::writer::stream_writer writer(output_stream);
  std::stringstream error_stream;
  stan::interface_callbacks::writer::stream_writer error_writer(error_stream);

  stan::mcmc::sample init_sample(z_init.q, 0, 0);

  stan::mcmc::sample s = sampler.transition(init_sample, writer, error_writer);

  EXPECT_EQ(31.5, s.cont_params()(0));
  EXPECT_EQ(0, s.log_prob());
  EXPECT_EQ(1, s.accept_stat());
  EXPECT_EQ("", output_stream.str());
  EXPECT_EQ("", error_stream.str());
}
示例#2
0
TEST(McmcBaseStaticHMC, set_nominal_stepsize_and_T) {

    rng_t base_rng(0);

    std::vector<double> q(5, 1.0);
    std::vector<int> r(2, 2);

    stan::mcmc::mock_model model(q.size());

    stan::mcmc::mock_static_hmc sampler(model, base_rng, &std::cout, &std::cerr);

    double old_epsilon = 1.0;
    double old_T = 3.0;

    sampler.set_nominal_stepsize_and_T(old_epsilon, old_T);
    EXPECT_EQ(old_epsilon, sampler.get_nominal_stepsize());
    EXPECT_EQ(old_T, sampler.get_T());
    EXPECT_EQ(true, sampler.get_L() > 0);

    sampler.set_nominal_stepsize_and_T(-0.1, 5.0);
    EXPECT_EQ(old_epsilon, sampler.get_nominal_stepsize());
    EXPECT_EQ(old_T, sampler.get_T());

    sampler.set_nominal_stepsize_and_T(5.0, -0.1);
    EXPECT_EQ(old_epsilon, sampler.get_nominal_stepsize());
    EXPECT_EQ(old_T, sampler.get_T());

}
示例#3
0
TEST(McmcDenseEMetric, streams) {
  stan::test::capture_std_streams();
  
  rng_t base_rng(0);
  
  Eigen::VectorXd q(2);
  q(0) = 5;
  q(1) = 1;

  
  stan::mcmc::mock_model model(q.size());

  // typedef to use within Google Test macros
  typedef stan::mcmc::dense_e_metric<stan::mcmc::mock_model, rng_t> dense_e;
  
  EXPECT_NO_THROW(dense_e metric(model, 0));

  std::stringstream metric_output;
  EXPECT_NO_THROW(dense_e metric(model, &metric_output));
  EXPECT_EQ("", metric_output.str());

  stan::test::reset_std_streams();
  EXPECT_EQ("", stan::test::cout_ss.str());
  EXPECT_EQ("", stan::test::cerr_ss.str());
}
示例#4
0
TEST(Services, do_bfgs_optimize__lbfgs) {
  std::vector<double> cont_vector(2);
  cont_vector[0] = -1; cont_vector[1] = 1;
  std::vector<int> disc_vector;

  static const std::string DATA("");
  std::stringstream data_stream(DATA);
  stan::io::dump dummy_context(data_stream);
  Model model(dummy_context);

  typedef stan::optimization::BFGSLineSearch<Model,stan::optimization::LBFGSUpdate<> > Optimizer_LBFGS;
  Optimizer_LBFGS lbfgs(model, cont_vector, disc_vector, &std::cout);


  double lp = 0;
  bool save_iterations = true;
  int refresh = 0;
  int return_code;
  unsigned int random_seed = 0;
  rng_t base_rng(random_seed);

  std::fstream* output_stream = 0;
  mock_callback callback;

  return_code = stan::services::optimization::do_bfgs_optimize(model, lbfgs, base_rng,
                                                               lp, cont_vector, disc_vector,
                                                               output_stream, &std::cout,
                                                               save_iterations, refresh,
                                                               callback);
  EXPECT_FLOAT_EQ(return_code, 0);
  EXPECT_EQ(35, callback.n);
}
示例#5
0
TEST(McmcDerivedNuts, compute_criterion_dense_e) {
  
  rng_t base_rng(0);
  
  int model_size = 1;
  
  stan::mcmc::ps_point start(model_size);
  stan::mcmc::dense_e_point finish(model_size);
  Eigen::VectorXd rho(model_size);
  
  stan::mcmc::mock_model model(model_size);
  stan::mcmc::dense_e_nuts<stan::mcmc::mock_model, rng_t> sampler(model, base_rng, 0, 0);
  
  start.q(0) = 1;
  start.p(0) = 1;
  
  finish.q(0) = 2;
  finish.p(0) = 1;
  
  rho = start.p + finish.p;
  
  EXPECT_EQ(true, sampler.compute_criterion(start, finish, rho));
  
  start.q(0) = 1;
  start.p(0) = 1;
  
  finish.q(0) = 2;
  finish.p(0) = -1;
  
  rho = start.p + finish.p;
  
  EXPECT_FALSE(sampler.compute_criterion(start, finish, rho));
  
}
示例#6
0
TEST(McmcStaticBaseStaticHMC, set_nominal_stepsize_and_L) {

  rng_t base_rng(0);

  std::vector<double> q(5, 1.0);
  std::vector<int> r(2, 2);

  stan::mcmc::mock_model model(q.size());

  stan::mcmc::mock_static_hmc sampler(model, base_rng);

  double old_epsilon = 1.0;
  int old_L = 10;

  sampler.set_nominal_stepsize_and_L(old_epsilon, old_L);
  EXPECT_EQ(old_epsilon, sampler.get_nominal_stepsize());
  EXPECT_EQ(old_L, sampler.get_L());
  EXPECT_EQ(true, sampler.get_T() > 0);

  sampler.set_nominal_stepsize_and_L(-0.1, 5);
  EXPECT_EQ(old_epsilon, sampler.get_nominal_stepsize());
  EXPECT_EQ(old_L, sampler.get_L());

  sampler.set_nominal_stepsize_and_T(5.0, -1);
  EXPECT_EQ(old_epsilon, sampler.get_nominal_stepsize());
  EXPECT_EQ(old_L, sampler.get_L());
}
示例#7
0
TEST(McmcUnitEMetric, sample_p) {
  
  rng_t base_rng(0);
  
  std::vector<double> q(5, 1.0);
  std::vector<int> r(2, 2);
  
  stan::mcmc::mock_model model(q.size());
  
  stan::mcmc::unit_e_metric<stan::mcmc::mock_model, rng_t> metric(model, &std::cout);
  stan::mcmc::unit_e_point z(q.size(), r.size());
  
  int n_samples = 1000;
  double m = 0;
  double m2 = 0;
  
  for (int i = 0; i < n_samples; ++i) {
    metric.sample_p(z, base_rng);
    double T = metric.T(z);
    
    double delta = T - m;
    m += delta / static_cast<double>(i + 1);
    m2 += delta * (T - m);
  }
  
  double var = m2 / (n_samples + 1.0);
  
  // Mean within 5sigma of expected value (d / 2)
  EXPECT_EQ(true, fabs(m   - 0.5 * q.size()) < 5.0 * sqrt(var));
  
  // Variance within 10% of expected value (d / 2)
  EXPECT_EQ(true, fabs(var - 0.5 * q.size()) < 0.1 * q.size());
  
}
示例#8
0
TEST(advi_test, hier_logistic_cp_constraint_meanfield) {
  // Create mock data_var_context
  std::fstream data_stream("src/test/test-models/good/variational/hier_logistic.data.R",
                           std::fstream::in);
  stan::io::dump data_var_context(data_stream);
  data_stream.close();

  std::stringstream output;
  output.clear();

  // Instantiate model
  Model_cp my_model(data_var_context);

  // RNG
  rng_t base_rng(0);

  // Dummy input
  Eigen::VectorXd cont_params = Eigen::VectorXd::Zero(my_model.num_params_r());

  // ADVI
  stan::variational::advi<Model_cp, stan::variational::normal_meanfield, rng_t> test_advi(my_model,
                                                     cont_params,
                                                     base_rng,
                                                     10,
                                                     100,
                                                     100,
                                                     1);

  stan::interface_callbacks::writer::noop_writer writer;
  
  test_advi.run(0.01, false, 50, 1, 2e4,
                writer, writer, writer);
}
示例#9
0
TEST(McmcNuts, instantiaton_test) {
  rng_t base_rng(4839294);

  std::stringstream output;
  stan::callbacks::stream_writer writer(output);
  std::stringstream error_stream;
  stan::callbacks::stream_writer error_writer(error_stream);

  std::fstream empty_stream("", std::fstream::in);
  stan::io::dump data_var_context(empty_stream);
  gauss3D_model_namespace::gauss3D_model model(data_var_context);

  stan::mcmc::unit_e_nuts<gauss3D_model_namespace::gauss3D_model, rng_t>
    unit_e_sampler(model, base_rng);
  
  stan::mcmc::diag_e_nuts<gauss3D_model_namespace::gauss3D_model, rng_t>
    diag_e_sampler(model, base_rng);
  
  stan::mcmc::dense_e_nuts<gauss3D_model_namespace::gauss3D_model, rng_t>
    dense_e_sampler(model, base_rng);
  
  stan::mcmc::adapt_unit_e_nuts<gauss3D_model_namespace::gauss3D_model, rng_t>
    adapt_unit_e_sampler(model, base_rng);
  
  stan::mcmc::adapt_diag_e_nuts<gauss3D_model_namespace::gauss3D_model, rng_t>
    adapt_diag_e_sampler(model, base_rng);
  
  stan::mcmc::adapt_dense_e_nuts<gauss3D_model_namespace::gauss3D_model, rng_t>
    adapt_dense_e_sampler(model, base_rng);
}
示例#10
0
TEST(McmcDiagEMetric, sample_p) {

  rng_t base_rng(0);

  Eigen::VectorXd q(2);
  q(0) = 5;
  q(1) = 1;

  stan::mcmc::mock_model model(q.size());
  stan::mcmc::diag_e_metric<stan::mcmc::mock_model, rng_t> metric(model);
  stan::mcmc::diag_e_point z(q.size());

  int n_samples = 1000;
  double m = 0;
  double m2 = 0;

  for (int i = 0; i < n_samples; ++i) {
    metric.sample_p(z, base_rng);
    double tau = metric.tau(z);

    double delta = tau - m;
    m += delta / static_cast<double>(i + 1);
    m2 += delta * (tau - m);
  }

  double var = m2 / (n_samples + 1.0);

  // Mean within 5sigma of expected value (d / 2)
  EXPECT_TRUE(std::fabs(m   - 0.5 * q.size()) < 5.0 * sqrt(var));

  // Variance within 10% of expected value (d / 2)
  EXPECT_TRUE(std::fabs(var - 0.5 * q.size()) < 0.1 * q.size());
}
示例#11
0
TEST(McmcBaseStaticHMC, set_nominal_stepsize) {
  
  rng_t base_rng(0);
  
  std::vector<double> q(5, 1.0);
  std::vector<int> r(2, 2);
  
  stan::mcmc::mock_model model(q.size());

  std::stringstream output, error;
  
  stan::mcmc::mock_static_hmc sampler(model, base_rng, &output, &error);
  
  double old_epsilon = 1.0;
  
  sampler.set_nominal_stepsize(old_epsilon);
  EXPECT_EQ(old_epsilon, sampler.get_nominal_stepsize());
  EXPECT_EQ(true, sampler.get_L() > 0);
  
  sampler.set_nominal_stepsize(-0.1);
  EXPECT_EQ(old_epsilon, sampler.get_nominal_stepsize());
  
  EXPECT_EQ("", output.str());
  EXPECT_EQ("", error.str());
}
示例#12
0
TEST(McmcNutsBaseNuts, build_tree_test) {

  rng_t base_rng(0);

  int model_size = 1;
  double init_momentum = 1.5;

  stan::mcmc::ps_point z_init(model_size);
  z_init.q(0) = 0;
  z_init.p(0) = init_momentum;

  stan::mcmc::ps_point z_propose(model_size);

  Eigen::VectorXd p_sharp_left = Eigen::VectorXd::Zero(model_size);
  Eigen::VectorXd p_sharp_right = Eigen::VectorXd::Zero(model_size);
  Eigen::VectorXd rho = z_init.p;
  double log_sum_weight = -std::numeric_limits<double>::infinity();

  double H0 = -0.1;
  int n_leapfrog = 0;
  double sum_metro_prob = 0;

  stan::mcmc::mock_model model(model_size);
  stan::mcmc::mock_nuts sampler(model, base_rng);

  sampler.set_nominal_stepsize(1);
  sampler.set_stepsize_jitter(0);
  sampler.sample_stepsize();
  sampler.z() = z_init;

  std::stringstream debug, info, warn, error, fatal;
  stan::callbacks::stream_logger logger(debug, info, warn, error, fatal);

  bool valid_subtree = sampler.build_tree(3, z_propose,
                                          p_sharp_left, p_sharp_right, rho,
                                          H0, 1, n_leapfrog, log_sum_weight,
                                          sum_metro_prob, logger);

  EXPECT_TRUE(valid_subtree);

  EXPECT_EQ(init_momentum * (n_leapfrog + 1), rho(0));
  EXPECT_EQ(1, p_sharp_left(0));
  EXPECT_EQ(1, p_sharp_right(0));

  EXPECT_EQ(8 * init_momentum, sampler.z().q(0));
  EXPECT_EQ(init_momentum, sampler.z().p(0));

  EXPECT_EQ(8, n_leapfrog);
  EXPECT_FLOAT_EQ(H0 + std::log(n_leapfrog), log_sum_weight);
  EXPECT_FLOAT_EQ(std::exp(H0) * n_leapfrog, sum_metro_prob);

  EXPECT_EQ("", debug.str());
  EXPECT_EQ("", info.str());
  EXPECT_EQ("", warn.str());
  EXPECT_EQ("", error.str());
  EXPECT_EQ("", fatal.str());
}
示例#13
0
TEST(StanIoMcmcWriter, write_diagnostic_names) {
  
  // Model
  std::fstream data_stream("", std::fstream::in);
  stan::io::dump data_var_context(data_stream);
  data_stream.close();
  
  std::stringstream output;
  io_example_model_namespace::io_example_model model(data_var_context, &output);
  
  // Sample
  Eigen::VectorXd real(2);
  real(0) = 1.43;
  real(1) = 2.71;
  
  double log_prob = 3.14;
  double accept_stat = 0.84;
  
  stan::mcmc::sample sample(real, log_prob, accept_stat);
  
  // Sampler
  typedef boost::ecuyer1988 rng_t;
  rng_t base_rng(0);
  
  stan::mcmc::adapt_diag_e_nuts<io_example_model_namespace::io_example_model, rng_t>
    sampler(model, base_rng, 0, 0);
  sampler.seed(real);
  
  // Writer
  std::stringstream sample_stream;
  std::stringstream diagnostic_stream;
  std::stringstream message_stream;
  
  stan::interface::recorder::csv sample_recorder(&sample_stream, "# ");
  stan::interface::recorder::csv diagnostic_recorder(&diagnostic_stream, "# ");
  stan::interface::recorder::messages message_recorder(&message_stream, "# ");

  stan::io::mcmc_writer<io_example_model_namespace::io_example_model,
                        stan::interface::recorder::csv,
                        stan::interface::recorder::csv,
                        stan::interface::recorder::messages> 
    writer(sample_recorder, diagnostic_recorder, message_recorder);
  
  writer.write_diagnostic_names(sample, &sampler, model);
  
  std::string line;
  std::getline(diagnostic_stream, line);
  
  // FIXME: make this work, too
  EXPECT_EQ("lp__,accept_stat__,stepsize__,treedepth__,n_leapfrog__,n_divergent__,mu1,mu2,p_mu1,p_mu2,g_mu1,g_mu2", line);
  
  EXPECT_EQ("", message_stream.str());
  EXPECT_EQ("", output.str());
}
示例#14
0
TEST(McmcBaseNuts, slice_criterion) {
  
  rng_t base_rng(0);
  
  int model_size = 1;
  double init_momentum = 1.5;
  
  Eigen::VectorXd rho = Eigen::VectorXd::Zero(model_size);
  
  stan::mcmc::ps_point z_init(model_size);
  z_init.q(0) = 0;
  z_init.p(0) = init_momentum;
  
  stan::mcmc::ps_point z_propose(model_size);
  
  stan::mcmc::nuts_util util;
  util.log_u = 0;
  util.H0 = 0;
  util.sign = 1;
  util.n_tree = 0;
  util.sum_prob = 0;
  
  std::stringstream output, error;
  stan::mcmc::mock_model model(model_size);
  stan::mcmc::divergent_nuts sampler(model, base_rng, &output, &error);
  
  sampler.set_nominal_stepsize(1);
  sampler.set_stepsize_jitter(0);
  sampler.sample_stepsize();
  sampler.z() = z_init;
  
  int n_valid = 0;
  
  sampler.z().V = -750;
  n_valid = sampler.build_tree(0, rho, &z_init, z_propose, util);
  
  EXPECT_EQ(1, n_valid);
  EXPECT_EQ(0, sampler.n_divergent_);
  
  sampler.z().V = -250;
  n_valid = sampler.build_tree(0, rho, &z_init, z_propose, util);
  
  EXPECT_EQ(0, n_valid);
  EXPECT_EQ(0, sampler.n_divergent_);
  
  sampler.z().V = 750;
  n_valid = sampler.build_tree(0, rho, &z_init, z_propose, util);
  
  EXPECT_EQ(0, n_valid);
  EXPECT_EQ(1, sampler.n_divergent_);

  EXPECT_EQ("", output.str());
  EXPECT_EQ("", error.str());  
}
示例#15
0
TEST(McmcNutsBaseNuts, build_tree) {

  rng_t base_rng(0);

  int model_size = 1;
  double init_momentum = 1.5;

  stan::mcmc::ps_point z_init(model_size);
  z_init.q(0) = 0;
  z_init.p(0) = init_momentum;

  stan::mcmc::ps_point z_propose(model_size);

  Eigen::VectorXd rho = z_init.p;
  double log_sum_weight = -std::numeric_limits<double>::infinity();

  double H0 = -0.1;
  int n_leapfrog = 0;
  double sum_metro_prob = 0;

  stan::mcmc::mock_model model(model_size);
  stan::mcmc::mock_nuts sampler(model, base_rng);

  sampler.set_nominal_stepsize(1);
  sampler.set_stepsize_jitter(0);
  sampler.sample_stepsize();
  sampler.z() = z_init;

  std::stringstream output;
  stan::interface_callbacks::writer::stream_writer writer(output);
  std::stringstream error_stream;
  stan::interface_callbacks::writer::stream_writer error_writer(error_stream);


  bool valid_subtree = sampler.build_tree(3, rho, z_propose,
                                          H0, 1, n_leapfrog, log_sum_weight,
                                          sum_metro_prob, writer, error_writer);

  EXPECT_TRUE(valid_subtree);

  EXPECT_EQ(init_momentum * (n_leapfrog + 1), rho(0));

  EXPECT_EQ(8 * init_momentum, sampler.z().q(0));
  EXPECT_EQ(init_momentum, sampler.z().p(0));

  EXPECT_EQ(8, n_leapfrog);
  EXPECT_FLOAT_EQ(H0 + std::log(n_leapfrog), log_sum_weight);
  EXPECT_FLOAT_EQ(std::exp(H0) * n_leapfrog, sum_metro_prob);

  EXPECT_EQ("", output.str());
  EXPECT_EQ("", error_stream.str());
}
示例#16
0
TEST(McmcNutsBaseNuts, set_max_delta_test) {
  rng_t base_rng(0);

  Eigen::VectorXd q(2);
  q(0) = 5;
  q(1) = 1;

  stan::mcmc::mock_model model(q.size());
  stan::mcmc::mock_nuts sampler(model, base_rng);

  double old_max_delta = 10;
  sampler.set_max_delta(old_max_delta);
  EXPECT_EQ(old_max_delta, sampler.get_max_delta());
}
示例#17
0
TEST(McmcHmcIntegratorsImplLeapfrog, unit_e_energy_conservation) {
  rng_t base_rng(0);

  std::fstream data_stream(std::string("").c_str(), std::fstream::in);
  stan::io::dump data_var_context(data_stream);
  data_stream.close();

  std::stringstream model_output;
  std::stringstream debug, info, warn, error, fatal;
  stan::callbacks::stream_logger logger(debug, info, warn, error, fatal);

  gauss_model_namespace::gauss_model model(data_var_context, &model_output);

  stan::mcmc::impl_leapfrog<
    stan::mcmc::unit_e_metric<gauss_model_namespace::gauss_model, rng_t> >
    integrator;

  stan::mcmc::unit_e_metric<gauss_model_namespace::gauss_model, rng_t> metric(model);

  stan::mcmc::unit_e_point z(1);
  z.q(0) = 1;
  z.p(0) = 1;

  metric.init(z, logger);
  double H0 = metric.H(z);
  double aveDeltaH = 0;

  double epsilon = 1e-3;
  double tau = 6.28318530717959;
  size_t L = tau / epsilon;

  for (size_t n = 0; n < L; ++n) {
    integrator.evolve(z, metric, epsilon, logger);

    double deltaH = metric.H(z) - H0;
    aveDeltaH += (deltaH - aveDeltaH) / double(n + 1);
  }

  // Average error in Hamiltonian should be O(epsilon^{2})
  // in general, smaller for the gauss_model in this case due to cancellations
  EXPECT_NEAR(aveDeltaH, 0, epsilon * epsilon);

  EXPECT_EQ("", model_output.str());
  EXPECT_EQ("", debug.str());
  EXPECT_EQ("", info.str());
  EXPECT_EQ("", warn.str());
  EXPECT_EQ("", error.str());
  EXPECT_EQ("", fatal.str());
}
示例#18
0
TEST(McmcBaseNuts, build_tree) {
  
  rng_t base_rng(0);
  
  int model_size = 1;
  double init_momentum = 1.5;
  
  Eigen::VectorXd rho = Eigen::VectorXd::Zero(model_size);
  
  stan::mcmc::ps_point z_init(model_size);
  z_init.q(0) = 0;
  z_init.p(0) = init_momentum;
  
  stan::mcmc::ps_point z_propose(model_size);
  
  stan::mcmc::nuts_util util;
  util.log_u = -1;
  util.H0 = -0.1;
  util.sign = 1;
  util.n_tree = 0;
  util.sum_prob = 0;
  
  std::stringstream output, error;
  stan::mcmc::mock_model model(model_size);
  stan::mcmc::mock_nuts sampler(model, base_rng, &output, &error);
  
  sampler.set_nominal_stepsize(1);
  sampler.set_stepsize_jitter(0);
  sampler.sample_stepsize();
  sampler.z() = z_init;
  
  int n_valid = sampler.build_tree(3, rho, &z_init, z_propose, util);
  
  EXPECT_EQ(8, n_valid);
  
  EXPECT_EQ(8, util.n_tree);
  EXPECT_FLOAT_EQ(std::exp(util.H0) * util.n_tree, util.sum_prob);
  
  EXPECT_EQ(init_momentum * util.n_tree, rho(0));
  
  EXPECT_EQ(init_momentum, z_init.q(0));
  EXPECT_EQ(init_momentum, z_init.p(0));
  
  EXPECT_EQ(8 * init_momentum, sampler.z().q(0));
  EXPECT_EQ(init_momentum, sampler.z().p(0));

  EXPECT_EQ("", output.str());
  EXPECT_EQ("", error.str());  
}
示例#19
0
TEST(McmcBaseHMC, point_construction) {
  
  rng_t base_rng(0);
  
  std::vector<double> q(5, 1.0);
  std::vector<int> r(2, 2);
  
  stan::mcmc::mock_model model(q.size());
  
  stan::mcmc::mock_hmc sampler(model, base_rng, &std::cout, &std::cerr);

  EXPECT_EQ(q.size(), sampler.z().q.size());
  EXPECT_EQ(q.size(), sampler.z().g.size());
  
}
示例#20
0
TEST(McmcNutsBaseNuts, rho_aggregation_test) {

  rng_t base_rng(0);

  int model_size = 1;
  double init_momentum = 1.5;

  stan::mcmc::ps_point z_init(model_size);
  z_init.q(0) = 0;
  z_init.p(0) = init_momentum;

  stan::mcmc::ps_point z_propose(model_size);

  Eigen::VectorXd p_sharp_left = Eigen::VectorXd::Zero(model_size);
  Eigen::VectorXd p_sharp_right = Eigen::VectorXd::Zero(model_size);
  Eigen::VectorXd rho = z_init.p;
  double log_sum_weight = -std::numeric_limits<double>::infinity();

  double H0 = -0.1;
  int n_leapfrog = 0;
  double sum_metro_prob = 0;

  stan::mcmc::mock_model model(model_size);
  stan::mcmc::rho_inspector_mock_nuts sampler(model, base_rng);

  sampler.set_nominal_stepsize(1);
  sampler.set_stepsize_jitter(0);
  sampler.sample_stepsize();
  sampler.z() = z_init;

  std::stringstream debug, info, warn, error, fatal;
  stan::callbacks::stream_logger logger(debug, info, warn, error, fatal);

  sampler.build_tree(3, z_propose,
                     p_sharp_left, p_sharp_right, rho,
                     H0, 1, n_leapfrog, log_sum_weight,
                     sum_metro_prob, logger);

  EXPECT_EQ(7, sampler.rho_values.size());
  EXPECT_EQ(2 * init_momentum, sampler.rho_values.at(0));
  EXPECT_EQ(2 * init_momentum, sampler.rho_values.at(1));
  EXPECT_EQ(4 * init_momentum, sampler.rho_values.at(2));
  EXPECT_EQ(2 * init_momentum, sampler.rho_values.at(3));
  EXPECT_EQ(2 * init_momentum, sampler.rho_values.at(4));
  EXPECT_EQ(4 * init_momentum, sampler.rho_values.at(5));
  EXPECT_EQ(8 * init_momentum, sampler.rho_values.at(6));
}
示例#21
0
TEST(McmcUnitENuts, transition_test) {
  rng_t base_rng(4839294);

  stan::mcmc::unit_e_point z_init(3);
  z_init.q(0) = 1;
  z_init.q(1) = -1;
  z_init.q(2) = 1;
  z_init.p(0) = -1;
  z_init.p(1) = 1;
  z_init.p(2) = -1;

  std::stringstream output_stream;
  stan::interface_callbacks::writer::stream_writer writer(output_stream);
  std::stringstream error_stream;
  stan::interface_callbacks::writer::stream_writer error_writer(error_stream);

  std::fstream empty_stream("", std::fstream::in);
  stan::io::dump data_var_context(empty_stream);
  gauss3D_model_namespace::gauss3D_model model(data_var_context);

  stan::mcmc::unit_e_nuts<gauss3D_model_namespace::gauss3D_model, rng_t>
    sampler(model, base_rng);

  sampler.z() = z_init;
  sampler.init_hamiltonian(writer, error_writer);
  sampler.set_nominal_stepsize(0.1);
  sampler.set_stepsize_jitter(0);
  sampler.sample_stepsize();

  stan::mcmc::sample init_sample(z_init.q, 0, 0);

  stan::mcmc::sample s = sampler.transition(init_sample, writer, error_writer);

  EXPECT_EQ(4, sampler.depth_);
  EXPECT_EQ((2 << 3) - 1, sampler.n_leapfrog_);
  EXPECT_FALSE(sampler.divergent_);

  EXPECT_FLOAT_EQ(1.8718261, s.cont_params()(0));
  EXPECT_FLOAT_EQ(-0.74208695, s.cont_params()(1));
  EXPECT_FLOAT_EQ( 1.5202962, s.cont_params()(2));
  EXPECT_FLOAT_EQ(-3.1828632, s.log_prob());
  EXPECT_FLOAT_EQ(0.99629009, s.accept_stat());
  EXPECT_EQ("", output_stream.str());
  EXPECT_EQ("", error_stream.str());
}
示例#22
0
TEST(McmcDenseEMetric, sample_p) {
  rng_t base_rng(0);

  Eigen::Matrix2d  m(2,2);
  m(0, 0) = 3.0;
  m(1, 0) = -2.0;
  m(0, 1) = -2.0;
  m(1, 1) = 4.0;

  Eigen::Matrix2d  m_inv = m.inverse();

  stan::mcmc::mock_model model(2);

  stan::mcmc::dense_e_metric<stan::mcmc::mock_model, rng_t> metric(model);
  stan::mcmc::dense_e_point z(2);
  z.set_metric(m_inv);

  int n_samples = 1000;

  Eigen::Matrix2d sample_cov(2,2);
  sample_cov(0, 0) = 0.0;
  sample_cov(0, 1) = 0.0;
  sample_cov(1, 0) = 0.0;
  sample_cov(1, 1) = 0.0;

  for (int i = 0; i < n_samples; ++i) {
    metric.sample_p(z, base_rng);
    sample_cov(0, 0) += z.p[0] * z.p[0] / n_samples;
    sample_cov(0, 1) += z.p[0] * z.p[1] / n_samples;
    sample_cov(1, 0) += z.p[1] * z.p[0] / n_samples;
    sample_cov(1, 1) += z.p[1] * z.p[1] / n_samples;
  }

  Eigen::Matrix2d var(2,2);
  var(0, 0) = 2 * m(0, 0);
  var(1, 0) = m(1, 0) * m(1, 0) + m(1, 1) * m(0, 0);
  var(0, 1) = m(0, 1) * m(0, 1) + m(1, 1) * m(0, 0);
  var(1, 1) = 2 * m(1, 1);

  // Covariance matrix within 5sigma of expected value (comes from a Wishart distribution)
  EXPECT_TRUE(std::fabs(m(0, 0)   - sample_cov(0, 0)) < 5.0 * sqrt(var(0, 0) / n_samples));
  EXPECT_TRUE(std::fabs(m(1, 0)   - sample_cov(1, 0)) < 5.0 * sqrt(var(1, 0) / n_samples));
  EXPECT_TRUE(std::fabs(m(0, 1)   - sample_cov(0, 1)) < 5.0 * sqrt(var(0, 1) / n_samples));
  EXPECT_TRUE(std::fabs(m(1, 1)   - sample_cov(1, 1)) < 5.0 * sqrt(var(1, 1) / n_samples));
}
示例#23
0
TEST(McmcNutsBaseNuts, set_max_depth) {

  rng_t base_rng(0);

  Eigen::VectorXd q(2);
  q(0) = 5;
  q(1) = 1;

  stan::mcmc::mock_model model(q.size());
  stan::mcmc::mock_nuts sampler(model, base_rng);

  int old_max_depth = 1;
  sampler.set_max_depth(old_max_depth);
  EXPECT_EQ(old_max_depth, sampler.get_max_depth());

  sampler.set_max_depth(-1);
  EXPECT_EQ(old_max_depth, sampler.get_max_depth());
}
示例#24
0
TEST(McmcBaseNuts, set_max_delta) {  
  rng_t base_rng(0);
  
  Eigen::VectorXd q(2);
  q(0) = 5;
  q(1) = 1;
  
  std::stringstream output, error;
  stan::mcmc::mock_model model(q.size());
  stan::mcmc::mock_nuts sampler(model, base_rng, &output, &error);
  
  double old_max_delta = 10;
  sampler.set_max_delta(old_max_delta);
  EXPECT_EQ(old_max_delta, sampler.get_max_delta());

  EXPECT_EQ("", output.str());
  EXPECT_EQ("", error.str());  
}
示例#25
0
TEST(McmcBaseHMC, seed) {
  
  rng_t base_rng(0);
  
  std::vector<double> q(5, 1.0);
  std::vector<int> r;
  
  stan::mcmc::mock_model model(q.size());
  
  stan::mcmc::mock_hmc sampler(model, base_rng, &std::cout, &std::cerr);
  
  std::vector<double> q_seed(q.size(), -1.0);
  
  sampler.seed(q_seed, r);
  
  for (int i = 0; i < q.size(); ++i)
    EXPECT_EQ(q_seed.at(i), sampler.z().q.at(i));
  
}
示例#26
0
TEST(McmcBaseHMC, set_nominal_stepsize) {
  
  rng_t base_rng(0);
  
  std::vector<double> q(5, 1.0);
  std::vector<int> r(2, 2);
  
  stan::mcmc::mock_model model(q.size());
  
  stan::mcmc::mock_hmc sampler(model, base_rng, &std::cout, &std::cerr);
  
  double old_epsilon = 1.0;
  sampler.set_nominal_stepsize(old_epsilon);
  EXPECT_EQ(old_epsilon, sampler.get_nominal_stepsize());
  
  sampler.set_nominal_stepsize(-0.1);
  EXPECT_EQ(old_epsilon, sampler.get_nominal_stepsize());
  
}
示例#27
0
TEST(McmcHmcIntegratorsExplLeapfrog, energy_conservation) {
  
  rng_t base_rng(0);
  
  std::fstream data_stream(std::string("").c_str(), std::fstream::in);
  stan::io::dump data_var_context(data_stream);
  data_stream.close();
  
  gauss_namespace::gauss model(data_var_context, &std::cout);
  
  stan::mcmc::expl_leapfrog<
  stan::mcmc::unit_e_metric<gauss_namespace::gauss, rng_t>,
  stan::mcmc::unit_e_point> integrator;
  
  stan::mcmc::unit_e_metric<gauss_namespace::gauss, rng_t> metric(model, &std::cout);
  
  stan::mcmc::unit_e_point z(1);
  z.q(0) = 1;
  z.p(0) = 1;
  
  metric.update(z);
  double H0 = metric.H(z);
  double aveDeltaH = 0;
  
  double epsilon = 1e-3;
  double tau = 6.28318530717959;
  size_t L = tau / epsilon;
  
  for (size_t n = 0; n < L; ++n) {
    
    integrator.evolve(z, metric, epsilon);
    
    double deltaH = metric.H(z) - H0;
    aveDeltaH += (deltaH - aveDeltaH) / double(n + 1);
    
  }

  // Average error in Hamiltonian should be O(epsilon^{2})
  // in general, smaller for the gaussian case due to cancellations
  EXPECT_NEAR(aveDeltaH, 0, epsilon * epsilon);
  
}
示例#28
0
TEST(McmcStaticBaseStaticHMC, set_T) {

  rng_t base_rng(0);

  std::vector<double> q(5, 1.0);
  std::vector<int> r(2, 2);

  stan::mcmc::mock_model model(q.size());

  stan::mcmc::mock_static_hmc sampler(model, base_rng);

  double old_T = 3.0;

  sampler.set_T(old_T);
  EXPECT_EQ(old_T, sampler.get_T());
  EXPECT_EQ(true, sampler.get_L() > 0);

  sampler.set_T(-0.1);
  EXPECT_EQ(old_T, sampler.get_T());
}
示例#29
0
TEST(McmcNutsBaseNuts, transition) {

  rng_t base_rng(0);

  int model_size = 1;
  double init_momentum = 1.5;

  stan::mcmc::ps_point z_init(model_size);
  z_init.q(0) = 0;
  z_init.p(0) = init_momentum;

  stan::mcmc::mock_model model(model_size);
  stan::mcmc::mock_nuts sampler(model, base_rng);

  sampler.set_nominal_stepsize(1);
  sampler.set_stepsize_jitter(0);
  sampler.sample_stepsize();
  sampler.z() = z_init;

  std::stringstream debug, info, warn, error, fatal;
  stan::callbacks::stream_logger logger(debug, info, warn, error, fatal);

  stan::mcmc::sample init_sample(z_init.q, 0, 0);

  // Transition will expand trajectory until max_depth is hit
  stan::mcmc::sample s = sampler.transition(init_sample, logger);

  EXPECT_EQ(sampler.get_max_depth(), sampler.depth_);
  EXPECT_EQ((2 << (sampler.get_max_depth() - 1)) - 1, sampler.n_leapfrog_);
  EXPECT_FALSE(sampler.divergent_);

  EXPECT_EQ(21 * init_momentum, s.cont_params()(0));
  EXPECT_EQ(0, s.log_prob());
  EXPECT_EQ(1, s.accept_stat());
  EXPECT_EQ("", debug.str());
  EXPECT_EQ("", info.str());
  EXPECT_EQ("", warn.str());
  EXPECT_EQ("", error.str());
  EXPECT_EQ("", fatal.str());
}
示例#30
0
TEST(McmcNutsDerivedNuts, compute_criterion_softabs) {

  rng_t base_rng(0);

  int model_size = 1;

  stan::mcmc::softabs_point start(model_size);
  stan::mcmc::softabs_point finish(model_size);
  Eigen::VectorXd p_sharp_start(model_size);
  Eigen::VectorXd p_sharp_finish(model_size);
  Eigen::VectorXd rho(model_size);

  stan::mcmc::mock_model model(model_size);
  stan::mcmc::softabs_nuts<stan::mcmc::mock_model, rng_t>
    sampler(model, base_rng);

  start.q(0) = 1;
  start.p(0) = 1;

  finish.q(0) = 2;
  finish.p(0) = 1;

  p_sharp_start = start.p;
  p_sharp_finish = finish.p;
  rho = start.p + finish.p;

  EXPECT_TRUE(sampler.compute_criterion(p_sharp_start, p_sharp_finish, rho));

  start.q(0) = 1;
  start.p(0) = 1;

  finish.q(0) = 2;
  finish.p(0) = -1;

  p_sharp_start = start.p;
  p_sharp_finish = finish.p;
  rho = start.p + finish.p;

  EXPECT_FALSE(sampler.compute_criterion(p_sharp_start, p_sharp_finish, rho));
}