generate_transitions_test.cpp
Go to the documentation of this file.
5 #include <gtest/gtest.h>
7 #include <test/test-models/good/optimization/rosenbrock.hpp>
9 #include <iostream>
10 #include <exception>
11 
12 class ServicesSamplesGenerateTransitions : public testing::Test {
13 public:
15  : model(context, &model_log) {}
16 
17  std::stringstream model_log;
22  stan_model model;
23 };
24 
25 
27  unsigned int seed = 0;
28  unsigned int chain = 1;
29  double init_radius = 0;
30  int refresh = 0;
31  int num_iterations = 10;
33  EXPECT_EQ(interrupt.call_count(), 0);
34 
35  boost::ecuyer1988 rng = stan::services::util::create_rng(seed, chain);
36 
37  std::vector<int> disc_vector;
38  std::vector<double> cont_vector
39  = stan::services::util::initialize(model, context, rng, init_radius,
40  false,
42 
45  writer(parameter, diagnostic, logger);
46  Eigen::VectorXd cont_params(cont_vector.size());
47  for (size_t i = 0; i < cont_vector.size(); i++)
48  cont_params[i] = cont_vector[i];
49  stan::mcmc::sample s(cont_params, 0, 0);
50 
51  writer.write_sample_names(s, sampler, model);
52  writer.write_diagnostic_names(s, sampler, model);
53 
55  sampler, num_iterations, 0, 20, 1, refresh, true, false, writer,
56  s, model, rng, interrupt, logger);
57 
58  std::vector<std::vector<std::string> > parameter_names;
59  parameter_names = parameter.vector_string_values();
60  std::vector<std::vector<double> > parameter_values;
61  parameter_values = parameter.vector_double_values();
62  std::vector<std::vector<std::string> > diagnostic_names;
63  diagnostic_names = diagnostic.vector_string_values();
64  std::vector<std::vector<double> > diagnostic_values;
65  diagnostic_values = diagnostic.vector_double_values();
66 
67  // Expect interrupt to be called once per iteration.
68  EXPECT_EQ(interrupt.call_count(), num_iterations);
69 
70  // Expect no messages and no init messages
71  EXPECT_EQ(logger.call_count(), 0);
72  EXPECT_EQ(init.call_count(), 0);
73 
74  // Expect on call to set parameter names, and one set of output per
75  // iteration.
76  EXPECT_EQ(parameter.call_count("vector_string"), 1);
77  EXPECT_EQ(parameter.call_count("vector_double"), num_iterations);
78 
79 
80  // Expect one call to set parameter names, and one set of output per
81  // iteration, not sure where the "+1" is coming from yet...
82  EXPECT_EQ(diagnostic.call_count("vector_string"), 1);
83  EXPECT_EQ(diagnostic.call_count("vector_double"), num_iterations + 1);
84 
85 
86 }
87 
88 
90  unsigned int seed = 0;
91  unsigned int chain = 1;
92  double init_radius = 0;
93  int refresh = 0;
94  int num_iterations = 10;
96  EXPECT_EQ(interrupt.call_count(), 0);
97 
98  boost::ecuyer1988 rng = stan::services::util::create_rng(seed, chain);
99 
100  std::vector<int> disc_vector;
101  std::vector<double> cont_vector
102  = stan::services::util::initialize(model, context, rng, init_radius,
103  false,
104  logger, diagnostic);
105 
108  writer(parameter, diagnostic, logger);
109  Eigen::VectorXd cont_params(cont_vector.size());
110  for (size_t i = 0; i < cont_vector.size(); i++)
111  cont_params[i] = cont_vector[i];
112  stan::mcmc::sample s(cont_params, 0, 0);
113 
114  writer.write_sample_names(s, sampler, model);
115  writer.write_diagnostic_names(s, sampler, model);
116 
118  sampler, num_iterations, 0, 20, 1, refresh, true, false, writer,
119  s, model, rng, interrupt, logger);
120 
121  std::vector<std::vector<std::string> > parameter_names;
122  parameter_names = parameter.vector_string_values();
123  std::vector<std::vector<double> > parameter_values;
124  parameter_values = parameter.vector_double_values();
125  std::vector<std::vector<std::string> > diagnostic_names;
126  diagnostic_names = diagnostic.vector_string_values();
127  std::vector<std::vector<double> > diagnostic_values;
128  diagnostic_values = diagnostic.vector_double_values();
129 
130  // First parameters are log density and acceptance prob.
131  EXPECT_EQ(parameter_names[0][0], "lp__");
132  EXPECT_EQ(parameter_names[0][1], "accept_stat__");
133 
134  // First diagnostic parameters are log density and acceptance
135  // prob.
136  EXPECT_EQ(diagnostic_names[0][0], "lp__");
137  EXPECT_EQ(diagnostic_names[0][1], "accept_stat__");
138 
139  // Expect one parameter name per parameter value.
140  EXPECT_EQ(parameter_names[0].size(), parameter_values[0].size());
141  EXPECT_EQ(diagnostic_names[0].size(), diagnostic_values[0].size());
142 
143 }
std::vector< std::vector< std::string > > vector_string_values()
TEST_F(ServicesSamplesGenerateTransitions, call_counting)
stan::test::unit::instrumented_writer diagnostic
const XML_Char * s
Definition: expat.h:262
unsigned int seed
Definition: runWimpSim.h:102
stan::test::unit::instrumented_logger logger
void write_sample_names(stan::mcmc::sample &sample, stan::mcmc::base_mcmc &sampler, Model &model)
Definition: mcmc_writer.hpp:67
stan::test::unit::instrumented_writer parameter
chain
Check that an output directory exists.
void write_diagnostic_names(stan::mcmc::sample sample, stan::mcmc::base_mcmc &sampler, Model &model)
std::vector< double > initialize(Model &model, stan::io::var_context &init, RNG &rng, double init_radius, bool print_timing, stan::callbacks::logger &logger, stan::callbacks::writer &init_writer)
Definition: initialize.hpp:68
const XML_Char * context
Definition: expat.h:434
stan::test::unit::instrumented_writer init
std::vector< std::vector< double > > vector_double_values()
const XML_Char XML_Content * model
Definition: expat.h:151
void generate_transitions(stan::mcmc::base_mcmc &sampler, int num_iterations, int start, int finish, int num_thin, int refresh, bool save, bool warmup, util::mcmc_writer &mcmc_writer, stan::mcmc::sample &init_s, Model &model, RNG &base_rng, callbacks::interrupt &callback, callbacks::logger &logger)
boost::ecuyer1988 create_rng(unsigned int seed, unsigned int chain)
Definition: create_rng.hpp:25
void refresh()
Definition: show_event.C:21