Exemplo n.º 1
0
  void SetUp() {
    output.str("");
    error.str("");

    model_output.str("");
    sample_output.str("");
    diagnostic_output.str("");
    message_output.str("");
    writer_output.str("");

    sampler = new mock_sampler(&output, &error);

    std::fstream empty_data_stream(std::string("").c_str());
    stan::io::dump empty_data_context(empty_data_stream);
    empty_data_stream.close();
    
    model = new stan_model(empty_data_context, &model_output);
    
    stan::interface::recorder::csv sample_recorder(&sample_output, "# ");
    stan::interface::recorder::csv diagnostic_recorder(&diagnostic_output, "# ");
    stan::interface::recorder::messages message_recorder(&message_output, "# ");

    writer = new stan::io::mcmc_writer<stan_model,
                                       stan::interface::recorder::csv,
                                       stan::interface::recorder::csv,
                                       stan::interface::recorder::messages>
      (sample_recorder, diagnostic_recorder, message_recorder, &writer_output);

    base_rng.seed(123456);

    q = Eigen::VectorXd(0,1);
    log_prob = 0;
    stat = 0;
  }
Exemplo n.º 2
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());
}
Exemplo n.º 3
0
TEST(StanIoMcmcWriter, write_sample_params) {
  
  // 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_sample_params<rng_t>(base_rng, sample, sampler, model);
  
  std::string line;
  std::getline(sample_stream, line);
  
  std::stringstream expected_stream;
  expected_stream << log_prob << ",";
  expected_stream << accept_stat << ",";
  expected_stream << sampler.get_current_stepsize() << ",";
  expected_stream << 0 << ",";
  expected_stream << 0 << ",";
  expected_stream << 0 << ",";
  expected_stream << real(0) << ",";
  expected_stream << real(1);
  
  std::string expected_line;
  std::getline(expected_stream, expected_line);
  
  EXPECT_EQ(expected_line, line);
  EXPECT_EQ("", message_stream.str());
  EXPECT_EQ("", output.str());
}
Exemplo n.º 4
0
TEST(StanIoMcmcWriter, write_timing) {
  
  // 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);
  
  double warm = 0.193933;
  double sampling = 0.483830;

  writer.write_timing(warm, sampling, sample_recorder);

  std::stringstream expected_stream;
  expected_stream << std::endl;
  expected_stream << "#  Elapsed Time: " << warm << " seconds (Warm-up)" << std::endl;
  expected_stream << "#                " << sampling << " seconds (Sampling)" << std::endl;
  expected_stream << "#                " << warm + sampling << " seconds (Total)" << std::endl;
  expected_stream << std::endl;
  
  std::string line;
  std::string expected_line;

  // Line 1
  std::getline(expected_stream, expected_line);
  
  std::getline(sample_stream, line);
  EXPECT_EQ(expected_line, line);
  
  std::getline(diagnostic_stream, line);
  EXPECT_EQ(expected_line, line);
  
  // Line 2
  std::getline(expected_stream, expected_line);
  
  std::getline(sample_stream, line);
  EXPECT_EQ(expected_line, line);
  
  std::getline(diagnostic_stream, line);
  EXPECT_EQ(expected_line, line);
  
  // Line 3
  std::getline(expected_stream, expected_line);
  
  std::getline(sample_stream, line);
  EXPECT_EQ(expected_line, line);
  
  std::getline(diagnostic_stream, line);
  EXPECT_EQ(expected_line, line);
  
  // Line 4
  std::getline(expected_stream, expected_line);
  
  std::getline(sample_stream, line);
  EXPECT_EQ(expected_line, line);
  
  std::getline(diagnostic_stream, line);
  EXPECT_EQ(expected_line, line);
  
  // Line 5
  std::getline(expected_stream, expected_line);
  
  std::getline(sample_stream, line);
  EXPECT_EQ(expected_line, line);
  
  std::getline(diagnostic_stream, line);
  EXPECT_EQ(expected_line, line);
  
  EXPECT_EQ("", message_stream.str());
  EXPECT_EQ("", output.str());
}
Exemplo n.º 5
0
TEST(StanIoMcmcWriter, write_adapt_finish) {
  
  // 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_adapt_finish(&sampler);
  
  std::stringstream expected_stream;
  expected_stream << "# Adaptation terminated" << std::endl;
  expected_stream << "# Step size = " << sampler.get_current_stepsize() << std::endl;
  expected_stream << "# Diagonal elements of inverse mass matrix:" << std::endl;
  expected_stream << "# " << sampler.z().mInv(0) << ", " << sampler.z().mInv(1) << std::endl;
  
  std::string line;
  std::string expected_line;
  
  // Line 1
  std::getline(expected_stream, expected_line);
  
  std::getline(sample_stream, line);
  EXPECT_EQ(expected_line, line);
  
  std::getline(diagnostic_stream, line);
  EXPECT_EQ(expected_line, line);
  
  // Line 2
  std::getline(expected_stream, expected_line);
  
  std::getline(sample_stream, line);
  EXPECT_EQ(expected_line, line);
  
  std::getline(diagnostic_stream, line);
  EXPECT_EQ(expected_line, line);
  
  // Line 3
  std::getline(expected_stream, expected_line);
  
  std::getline(sample_stream, line);
  EXPECT_EQ(expected_line, line);
  
  std::getline(diagnostic_stream, line);
  EXPECT_EQ(expected_line, line);
  
  // Line 4
  std::getline(expected_stream, expected_line);
  
  std::getline(sample_stream, line);
  EXPECT_EQ(expected_line, line);
  
  std::getline(diagnostic_stream, line);
  EXPECT_EQ(expected_line, line);
  
  EXPECT_EQ("", message_stream.str());
  EXPECT_EQ("", output.str());
}