hmc_nuts_unit_e_adapt_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 ServicesSampleHmcNutsUnitEAdapt : 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;
32  double delta = .1;
33  double gamma = .1;
34  double kappa = .1;
35  double t0 = .1;
37  EXPECT_EQ(interrupt.call_count(), 0);
38 
40  model, context, random_seed, chain, init_radius,
41  num_warmup, num_samples, num_thin, save_warmup, refresh,
42  stepsize, stepsize_jitter, max_depth, delta, gamma, kappa, t0,
43  interrupt, logger, init,
45 
46  EXPECT_EQ(0, return_code);
47 
48  int num_output_lines = (num_warmup+num_samples)/num_thin;
49  EXPECT_EQ(num_warmup+num_samples, interrupt.call_count());
50  EXPECT_EQ(1, parameter.call_count("vector_string"));
51  EXPECT_EQ(num_output_lines, parameter.call_count("vector_double"));
52  EXPECT_EQ(1, diagnostic.call_count("vector_string"));
53  EXPECT_EQ(num_output_lines, diagnostic.call_count("vector_double"));
54 }
55 
57  unsigned int random_seed = 0;
58  unsigned int chain = 1;
59  double init_radius = 0;
60  int num_warmup = 200;
61  int num_samples = 400;
62  int num_thin = 5;
63  bool save_warmup = true;
64  int refresh = 0;
65  double stepsize = 0.1;
66  double stepsize_jitter = 0;
67  int max_depth = 8;
68  double delta = .1;
69  double gamma = .1;
70  double kappa = .1;
71  double t0 = .1;
73  EXPECT_EQ(interrupt.call_count(), 0);
74 
76  model, context, random_seed, chain, init_radius,
77  num_warmup, num_samples, num_thin, save_warmup, refresh,
78  stepsize, stepsize_jitter, max_depth, delta, gamma, kappa, t0,
79  interrupt, logger, init,
81 
82  std::vector<std::vector<std::string> > parameter_names;
83  parameter_names = parameter.vector_string_values();
84  std::vector<std::vector<double> > parameter_values;
85  parameter_values = parameter.vector_double_values();
86  std::vector<std::vector<std::string> > diagnostic_names;
87  diagnostic_names = diagnostic.vector_string_values();
88  std::vector<std::vector<double> > diagnostic_values;
89  diagnostic_values = diagnostic.vector_double_values();
90 
91  // Expectations of parameter parameter names.
92  ASSERT_EQ(9, parameter_names[0].size());
93  EXPECT_EQ("lp__", parameter_names[0][0]);
94  EXPECT_EQ("accept_stat__", parameter_names[0][1]);
95  EXPECT_EQ("stepsize__", parameter_names[0][2]);
96  EXPECT_EQ("treedepth__", parameter_names[0][3]);
97  EXPECT_EQ("n_leapfrog__", parameter_names[0][4]);
98  EXPECT_EQ("divergent__", parameter_names[0][5]);
99  EXPECT_EQ("energy__", parameter_names[0][6]);
100  EXPECT_EQ("x", parameter_names[0][7]);
101  EXPECT_EQ("y", parameter_names[0][8]);
102 
103  // Expect one name per parameter value.
104  EXPECT_EQ(parameter_names[0].size(), parameter_values[0].size());
105  EXPECT_EQ(diagnostic_names[0].size(), diagnostic_values[0].size());
106 
107  EXPECT_EQ((num_warmup+num_samples)/num_thin, parameter_values.size());
108 
109  // Expect one call to set parameter names, and one set of output per
110  // iteration.
111  EXPECT_EQ("lp__", diagnostic_names[0][0]);
112  EXPECT_EQ("accept_stat__", diagnostic_names[0][1]);
113 }
114 
116  unsigned int random_seed = 0;
117  unsigned int chain = 1;
118  double init_radius = 0;
119  int num_warmup = 200;
120  int num_samples = 400;
121  int num_thin = 5;
122  bool save_warmup = true;
123  int refresh = 0;
124  double stepsize = 0.1;
125  double stepsize_jitter = 0;
126  int max_depth = 8;
127  double delta = .1;
128  double gamma = .1;
129  double kappa = .1;
130  double t0 = .1;
132  EXPECT_EQ(interrupt.call_count(), 0);
133 
135  model, context, random_seed, chain, init_radius,
136  num_warmup, num_samples, num_thin, save_warmup, refresh,
137  stepsize, stepsize_jitter, max_depth, delta, gamma, kappa, t0,
138  interrupt, logger, init,
140 
141  std::vector<std::vector<std::string> > parameter_names;
142  parameter_names = parameter.vector_string_values();
143  std::vector<std::vector<double> > parameter_values;
144  parameter_values = parameter.vector_double_values();
145  std::vector<std::vector<std::string> > diagnostic_names;
146  diagnostic_names = diagnostic.vector_string_values();
147  std::vector<std::vector<double> > diagnostic_values;
148  diagnostic_values = diagnostic.vector_double_values();
149 
150  EXPECT_EQ(return_code, 0);
151 }
152 
154  unsigned int random_seed = 0;
155  unsigned int chain = 1;
156  double init_radius = 0;
157  int num_warmup = 200;
158  int num_samples = 400;
159  int num_thin = 5;
160  bool save_warmup = true;
161  int refresh = 0;
162  double stepsize = 0.1;
163  double stepsize_jitter = 0;
164  int max_depth = 8;
165  double delta = .1;
166  double gamma = .1;
167  double kappa = .1;
168  double t0 = .1;
170  EXPECT_EQ(interrupt.call_count(), 0);
171 
172 
174  model, context, random_seed, chain, init_radius,
175  num_warmup, num_samples, num_thin, save_warmup, refresh,
176  stepsize, stepsize_jitter, max_depth, delta, gamma, kappa, t0,
177  interrupt, logger, init,
179 
180  std::vector<std::string> init_values;
181  init_values = init.string_values();
182 
183  EXPECT_EQ(0, init_values.size());
184 
185  EXPECT_EQ(1, logger.find_info("Elapsed Time:"));
186  EXPECT_EQ(1, logger.find_info("seconds (Warm-up)"));
187  EXPECT_EQ(1, logger.find_info("seconds (Sampling)"));
188  EXPECT_EQ(1, logger.find_info("seconds (Total)"));
189  EXPECT_EQ(0, logger.call_count_error());
190 }
stan::test::unit::instrumented_writer init
std::vector< std::vector< std::string > > vector_string_values()
stan::test::unit::instrumented_writer parameter
double delta
Definition: runWimpSim.h:98
int hmc_nuts_unit_e_adapt(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, double delta, double gamma, double kappa, double t0, callbacks::interrupt &interrupt, callbacks::logger &logger, callbacks::writer &init_writer, callbacks::writer &sample_writer, callbacks::writer &diagnostic_writer)
stan::test::unit::instrumented_logger logger
chain
Check that an output directory exists.
const XML_Char * context
Definition: expat.h:434
TEST_F(ServicesSampleHmcNutsUnitEAdapt, call_count)
stan::test::unit::instrumented_writer diagnostic
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