hmc_nuts_unit_e_test.cpp
Go to the documentation of this file.
2 #include <gtest/gtest.h>
4 #include <test/test-models/good/optimization/rosenbrock.hpp>
6 #include <iostream>
7 
8 class ServicesSampleHmcNutsUnitE : public testing::Test {
9 public:
11  : model(context, &model_log) {}
12 
13  std::stringstream model_log;
17  stan_model model;
18 };
19 
21  unsigned int random_seed = 0;
22  unsigned int chain = 1;
23  double init_radius = 0;
24  int num_warmup = 200;
25  int num_samples = 400;
26  int num_thin = 5;
27  bool save_warmup = true;
28  int refresh = 0;
29  double stepsize = 0.1;
30  double stepsize_jitter = 0;
31  int max_depth = 8;
33  EXPECT_EQ(interrupt.call_count(), 0);
34 
36  model, context, random_seed, chain, init_radius,
37  num_warmup, num_samples, num_thin, save_warmup, refresh,
38  stepsize, stepsize_jitter, max_depth,
39  interrupt, logger, init,
41 
42  EXPECT_EQ(0, return_code);
43 
44  int num_output_lines = (num_warmup+num_samples)/num_thin;
45  EXPECT_EQ(num_warmup+num_samples, interrupt.call_count());
46  EXPECT_EQ(1, parameter.call_count("vector_string"));
47  EXPECT_EQ(num_output_lines, parameter.call_count("vector_double"));
48  EXPECT_EQ(1, diagnostic.call_count("vector_string"));
49  EXPECT_EQ(num_output_lines, diagnostic.call_count("vector_double"));
50 }
51 
52 TEST_F(ServicesSampleHmcNutsUnitE, parameter_checks) {
53  unsigned int random_seed = 0;
54  unsigned int chain = 1;
55  double init_radius = 0;
56  int num_warmup = 200;
57  int num_samples = 400;
58  int num_thin = 5;
59  bool save_warmup = true;
60  int refresh = 0;
61  double stepsize = 0.1;
62  double stepsize_jitter = 0;
63  int max_depth = 8;
65  EXPECT_EQ(interrupt.call_count(), 0);
66 
68  model, context, random_seed, chain, init_radius,
69  num_warmup, num_samples, num_thin, save_warmup, refresh,
70  stepsize, stepsize_jitter, max_depth,
71  interrupt, logger, init,
73 
74  std::vector<std::vector<std::string> > parameter_names;
75  parameter_names = parameter.vector_string_values();
76  std::vector<std::vector<double> > parameter_values;
77  parameter_values = parameter.vector_double_values();
78  std::vector<std::vector<std::string> > diagnostic_names;
79  diagnostic_names = diagnostic.vector_string_values();
80  std::vector<std::vector<double> > diagnostic_values;
81  diagnostic_values = diagnostic.vector_double_values();
82 
83  // Expectations of parameter parameter names.
84  ASSERT_EQ(9, parameter_names[0].size());
85  EXPECT_EQ("lp__", parameter_names[0][0]);
86  EXPECT_EQ("accept_stat__", parameter_names[0][1]);
87  EXPECT_EQ("stepsize__", parameter_names[0][2]);
88  EXPECT_EQ("treedepth__", parameter_names[0][3]);
89  EXPECT_EQ("n_leapfrog__", parameter_names[0][4]);
90  EXPECT_EQ("divergent__", parameter_names[0][5]);
91  EXPECT_EQ("energy__", parameter_names[0][6]);
92  EXPECT_EQ("x", parameter_names[0][7]);
93  EXPECT_EQ("y", parameter_names[0][8]);
94 
95  // Expect one name per parameter value.
96  EXPECT_EQ(parameter_names[0].size(), parameter_values[0].size());
97  EXPECT_EQ(diagnostic_names[0].size(), diagnostic_values[0].size());
98 
99  EXPECT_EQ((num_warmup+num_samples)/num_thin, parameter_values.size());
100 
101  // Expect one call to set parameter names, and one set of output per
102  // iteration.
103  EXPECT_EQ("lp__", diagnostic_names[0][0]);
104  EXPECT_EQ("accept_stat__", diagnostic_names[0][1]);
105 }
106 
108  unsigned int random_seed = 0;
109  unsigned int chain = 1;
110  double init_radius = 0;
111  int num_warmup = 200;
112  int num_samples = 400;
113  int num_thin = 5;
114  bool save_warmup = true;
115  int refresh = 0;
116  double stepsize = 0.1;
117  double stepsize_jitter = 0;
118  int max_depth = 8;
120  EXPECT_EQ(interrupt.call_count(), 0);
121 
123  model, context, random_seed, chain, init_radius,
124  num_warmup, num_samples, num_thin, save_warmup, refresh,
125  stepsize, stepsize_jitter, max_depth,
126  interrupt, logger, init,
128 
129  std::vector<std::vector<std::string> > parameter_names;
130  parameter_names = parameter.vector_string_values();
131  std::vector<std::vector<double> > parameter_values;
132  parameter_values = parameter.vector_double_values();
133  std::vector<std::vector<std::string> > diagnostic_names;
134  diagnostic_names = diagnostic.vector_string_values();
135  std::vector<std::vector<double> > diagnostic_values;
136  diagnostic_values = diagnostic.vector_double_values();
137 
138  EXPECT_EQ(return_code, 0);
139 }
140 
141 TEST_F(ServicesSampleHmcNutsUnitE, output_regression) {
142  unsigned int random_seed = 0;
143  unsigned int chain = 1;
144  double init_radius = 0;
145  int num_warmup = 200;
146  int num_samples = 400;
147  int num_thin = 5;
148  bool save_warmup = true;
149  int refresh = 0;
150  double stepsize = 0.1;
151  double stepsize_jitter = 0;
152  int max_depth = 8;
154  EXPECT_EQ(interrupt.call_count(), 0);
155 
156 
158  model, context, random_seed, chain, init_radius,
159  num_warmup, num_samples, num_thin, save_warmup, refresh,
160  stepsize, stepsize_jitter, max_depth,
161  interrupt, logger, init,
163 
164  std::vector<std::string> init_values;
165  init_values = init.string_values();
166 
167  EXPECT_EQ(0, init_values.size());
168 
169  EXPECT_EQ(1, logger.find_info("Elapsed Time:"));
170  EXPECT_EQ(1, logger.find_info("seconds (Warm-up)"));
171  EXPECT_EQ(1, logger.find_info("seconds (Sampling)"));
172  EXPECT_EQ(1, logger.find_info("seconds (Total)"));
173  EXPECT_EQ(0, logger.call_count_error());
174 }
int hmc_nuts_unit_e(Model &model, stan::io::var_context &init, unsigned int random_seed, unsigned int chain, double init_radius, int num_warmup, int num_samples, int num_thin, bool save_warmup, int refresh, double stepsize, double stepsize_jitter, int max_depth, callbacks::interrupt &interrupt, callbacks::logger &logger, callbacks::writer &init_writer, callbacks::writer &sample_writer, callbacks::writer &diagnostic_writer)
std::vector< std::vector< std::string > > vector_string_values()
stan::test::unit::instrumented_writer parameter
stan::test::unit::instrumented_writer diagnostic
stan::test::unit::instrumented_writer init
chain
Check that an output directory exists.
TEST_F(ServicesSampleHmcNutsUnitE, call_count)
stan::test::unit::instrumented_logger logger
const XML_Char * context
Definition: expat.h:434
stan::io::empty_var_context context
std::vector< std::vector< double > > vector_double_values()
unsigned int find_info(const std::string &msg)
const XML_Char XML_Content * model
Definition: expat.h:151
void refresh()
Definition: show_event.C:21