hmc_nuts_dense_inv_metric_test.cpp
Go to the documentation of this file.
4 #include <test/test-models/good/mcmc/hmc/common/gauss3D.hpp>
7 #include <iostream>
8 #include <gtest/gtest.h>
9 
10 /**
11  * Use 3-param model test-models/good/mcmc/hmc/common/gauss3D
12  * fix seed 12345, test against specified inv Euclidean metric
13  * Tests crafted by running samplers with test config
14  * to capture resulting inverse Euclidean metric values.
15  */
16 
17 class ServicesSampleHmcNutsDenseEMassMatrix : public testing::Test {
18 public:
20  : model(context, &model_log) {}
21 
22  std::stringstream model_log;
26  stan_model model;
27 };
28 
30  unsigned int random_seed = 12345;
31  unsigned int chain = 1;
32  double init_radius = 2;
33  int num_warmup = 0;
34  int num_samples = 2;
35  int num_thin = 1;
36  bool save_warmup = false;
37  int refresh = 0;
38  double stepsize = 1;
39  double stepsize_jitter = 0;
40  int max_depth = 10;
42  EXPECT_EQ(interrupt.call_count(), 0);
43 
44  int return_code =
46  context,
47  random_seed,
48  chain,
49  init_radius,
50  num_warmup,
51  num_samples,
52  num_thin,
53  save_warmup,
54  refresh,
55  stepsize,
56  stepsize_jitter,
57  max_depth,
58  interrupt,
59  logger,
60  init,
61  parameter,
62  diagnostic);
63  EXPECT_EQ(0, return_code);
64 
65  stan::io::dump dmp =
67  stan::io::var_context& inv_metric = dmp;
68  std::vector<double> dense_vals
69  = inv_metric.vals_r("inv_metric");
70  // check returned Euclidean metric
71  stan::test::unit::check_adaptation(3, 3, dense_vals, parameter, 0.1);
72 }
73 
75  unsigned int random_seed = 12345;
76  unsigned int chain = 1;
77  double init_radius = 2;
78  int num_warmup = 250;
79  int num_samples = 0;
80  int num_thin = 1;
81  bool save_warmup = false;
82  int refresh = 0;
83  double stepsize = 1;
84  double stepsize_jitter = 0;
85  int max_depth = 10;
86  double delta = .8;
87  double gamma = .05;
88  double kappa = .75;
89  double t0 = 10;
90  unsigned int init_buffer = 75;
91  unsigned int term_buffer = 50;
92  unsigned int window = 25;
94  EXPECT_EQ(interrupt.call_count(), 0);
95 
96  // run sampler with ident Euclidean metric
97  int return_code =
99  context,
100  random_seed,
101  chain,
102  init_radius,
103  num_warmup,
104  num_samples,
105  num_thin,
106  save_warmup,
107  refresh,
108  stepsize,
109  stepsize_jitter,
110  max_depth,
111  delta,
112  gamma,
113  kappa,
114  t0,
115  init_buffer,
116  term_buffer,
117  window,
118  interrupt,
119  logger,
120  init,
121  parameter,
122  diagnostic);
123  EXPECT_EQ(0, return_code);
124 }
125 
127  unsigned int random_seed = 12345;
128  unsigned int chain = 1;
129  double init_radius = 2;
130  int num_warmup = 0;
131  int num_samples = 2;
132  int num_thin = 1;
133  bool save_warmup = false;
134  int refresh = 0;
135  double stepsize = 1;
136  double stepsize_jitter = 0;
137  int max_depth = 10;
139  EXPECT_EQ(interrupt.call_count(), 0);
140 
141  std::string txt =
142  "inv_metric <- structure(c("
143  "0.640211, 0.156096, -0.374048, "
144  "0.156096, 1.41239, -0.0412753, "
145  "-0.374048, -0.0412753, 1.29567 "
146  "), .Dim = c(3,3))";
147  std::stringstream in(txt);
148  stan::io::dump dump(in);
149  stan::io::var_context& inv_metric = dump;
150 
151  int return_code =
153  context,
154  inv_metric,
155  random_seed,
156  chain,
157  init_radius,
158  num_warmup,
159  num_samples,
160  num_thin,
161  save_warmup,
162  refresh,
163  stepsize,
164  stepsize_jitter,
165  max_depth,
166  interrupt,
167  logger,
168  init,
169  parameter,
170  diagnostic);
171  EXPECT_EQ(0, return_code);
172 
173  std::vector<double> dense_vals(9);
174  dense_vals = inv_metric.vals_r("inv_metric");
175  stan::test::unit::check_adaptation(3, 3, dense_vals, parameter, 0.4);
176 }
177 
179  unsigned int random_seed = 12345;
180  unsigned int chain = 1;
181  double init_radius = 2;
182  int num_warmup = 0;
183  int num_samples = 2;
184  int num_thin = 1;
185  bool save_warmup = false;
186  int refresh = 0;
187  double stepsize = 1;
188  double stepsize_jitter = 0;
189  int max_depth = 10;
190  double delta = .8;
191  double gamma = .05;
192  double kappa = .75;
193  double t0 = 10;
194  unsigned int init_buffer = 75;
195  unsigned int term_buffer = 50;
196  unsigned int window = 25;
198  EXPECT_EQ(interrupt.call_count(), 0);
199 
200  std::string txt =
201  "inv_metric <- structure(c("
202  "0.640211, 0.156096, -0.374048, "
203  "0.156096, 1.41239, -0.0412753, "
204  "-0.374048, -0.0412753, 1.29567 "
205  "), .Dim = c(3,3))";
206  std::stringstream in(txt);
207  stan::io::dump dump(in);
208  stan::io::var_context& inv_metric = dump;
209 
210  int return_code =
212  context,
213  inv_metric,
214  random_seed,
215  chain,
216  init_radius,
217  num_warmup,
218  num_samples,
219  num_thin,
220  save_warmup,
221  refresh,
222  stepsize,
223  stepsize_jitter,
224  max_depth,
225  delta,
226  gamma,
227  kappa,
228  t0,
229  init_buffer,
230  term_buffer,
231  window,
232  interrupt,
233  logger,
234  init,
235  parameter,
236  diagnostic);
237 
238  EXPECT_EQ(0, return_code);
239 
240  std::vector<double> dense_vals(9);
241  dense_vals = inv_metric.vals_r("inv_metric");
242  stan::test::unit::check_adaptation(3, 3, dense_vals, parameter, 0.4);
243 }
stan::test::unit::instrumented_writer diagnostic
double delta
Definition: runWimpSim.h:98
stan::io::dump create_unit_e_dense_inv_metric(size_t num_params)
stan::test::unit::instrumented_writer parameter
virtual std::vector< double > vals_r(const std::string &name) const =0
TEST_F(ServicesSampleHmcNutsDenseEMassMatrix, ident_no_adapt)
stan::test::unit::instrumented_writer init
int hmc_nuts_dense_e(Model &model, stan::io::var_context &init, stan::io::var_context &init_inv_metric, 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)
stan::test::unit::instrumented_logger logger
chain
Check that an output directory exists.
const XML_Char * context
Definition: expat.h:434
int hmc_nuts_dense_e_adapt(Model &model, stan::io::var_context &init, stan::io::var_context &init_inv_metric, 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, unsigned int init_buffer, unsigned int term_buffer, unsigned int window, callbacks::interrupt &interrupt, callbacks::logger &logger, callbacks::writer &init_writer, callbacks::writer &sample_writer, callbacks::writer &diagnostic_writer)
ifstream in
Definition: comparison.C:7
void check_adaptation(const size_t &num_params, const std::vector< double > &param_vals, stan::test::unit::instrumented_writer &report, const double &err_margin)
const XML_Char XML_Content * model
Definition: expat.h:151
void refresh()
Definition: show_event.C:21
enum BeamMode string