hmc_static_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 <vector>
9 #include <gtest/gtest.h>
10 
11 /**
12  * Use 3-param model test-models/good/mcmc/hmc/common/gauss3D
13  * fix seed 12345, test against specified inv Euclidean metric
14  * Tests crafted by running samplers with test config
15  * to capture resulting inverse Euclidean metric values.
16  */
17 
18 class ServicesSampleHmcStaticDenseEMassMatrix : public testing::Test {
19 public:
21  : model(context, &model_log) {}
22 
23  std::stringstream model_log;
27  stan_model model;
28 };
29 
31  unsigned int random_seed = 12345;
32  unsigned int chain = 1;
33  double init_radius = 2;
34  int num_warmup = 0;
35  int num_samples = 2;
36  int num_thin = 1;
37  bool save_warmup = false;
38  int refresh = 0;
39  double stepsize = 1;
40  double stepsize_jitter = 0;
41  int int_time = 8;
43  EXPECT_EQ(interrupt.call_count(), 0);
44 
45  int return_code =
47  context,
48  random_seed,
49  chain,
50  init_radius,
51  num_warmup,
52  num_samples,
53  num_thin,
54  save_warmup,
55  refresh,
56  stepsize,
57  stepsize_jitter,
58  int_time,
59  interrupt,
60  logger,
61  init,
62  parameter,
63  diagnostic);
64  EXPECT_EQ(0, return_code);
65 
66  stan::io::dump dmp =
68  stan::io::var_context& inv_metric = dmp;
69  std::vector<double> dense_vals
70  = inv_metric.vals_r("inv_metric");
71  // check returned Euclidean metric
72  stan::test::unit::check_adaptation(3, dense_vals, parameter, 0.2);
73 }
74 
76  unsigned int random_seed = 12345;
77  unsigned int chain = 1;
78  double init_radius = 2;
79  int num_warmup = 250;
80  int num_samples = 2;
81  int num_thin = 1;
82  bool save_warmup = false;
83  int refresh = 0;
84  double stepsize = 1;
85  double stepsize_jitter = 0;
86  int int_time = 8;
87  double delta = .8;
88  double gamma = .05;
89  double kappa = .75;
90  double t0 = 10;
91  unsigned int init_buffer = 75;
92  unsigned int term_buffer = 50;
93  unsigned int window = 25;
95  EXPECT_EQ(interrupt.call_count(), 0);
96 
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  int_time,
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 int_time = 8;
139  EXPECT_EQ(interrupt.call_count(), 0);
140 
141  std::string txt =
142  "inv_metric <- structure(c("
143  " 0.926739, 0.0734898, -0.12395, "
144  " 0.0734898, 0.876038, -0.051543, "
145  " -0.12395, -0.051543, 0.8274 "
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  int_time,
166  interrupt,
167  logger,
168  init,
169  parameter,
170  diagnostic);
171 
172  EXPECT_EQ(0, return_code);
173 
174  std::vector<double> dense_vals(3);
175  dense_vals = inv_metric.vals_r("inv_metric");
176  stan::test::unit::check_adaptation(3, dense_vals, parameter, 0.2);
177 }
178 
180  unsigned int random_seed = 12345;
181  unsigned int chain = 1;
182  double init_radius = 2;
183  int num_warmup = 0;
184  int num_samples = 2;
185  int num_thin = 1;
186  bool save_warmup = false;
187  int refresh = 0;
188  double stepsize = 1;
189  double stepsize_jitter = 0;
190  int int_time = 8;
191  double delta = .8;
192  double gamma = .05;
193  double kappa = .75;
194  double t0 = 10;
195  unsigned int init_buffer = 75;
196  unsigned int term_buffer = 50;
197  unsigned int window = 25;
199  EXPECT_EQ(interrupt.call_count(), 0);
200 
201  std::string txt =
202  "inv_metric <- structure(c("
203  " 0.926739, 0.0734898, -0.12395, "
204  " 0.0734898, 0.876038, -0.051543, "
205  " -0.12395, -0.051543, 0.8274 "
206  "), .Dim = c(3,3))";
207  std::stringstream in(txt);
208  stan::io::dump dump(in);
209  stan::io::var_context& inv_metric = dump;
210 
211  int return_code =
213  context,
214  inv_metric,
215  random_seed,
216  chain,
217  init_radius,
218  num_warmup,
219  num_samples,
220  num_thin,
221  save_warmup,
222  refresh,
223  stepsize,
224  stepsize_jitter,
225  int_time,
226  delta,
227  gamma,
228  kappa,
229  t0,
230  init_buffer,
231  term_buffer,
232  window,
233  interrupt,
234  logger,
235  init,
236  parameter,
237  diagnostic);
238 
239  EXPECT_EQ(0, return_code);
240 
241  std::vector<double> dense_vals(9);
242  dense_vals = inv_metric.vals_r("inv_metric");
243  stan::test::unit::check_adaptation(3, 3, dense_vals, parameter, 0.2);
244 }
double delta
Definition: runWimpSim.h:98
stan::io::dump create_unit_e_dense_inv_metric(size_t num_params)
virtual std::vector< double > vals_r(const std::string &name) const =0
int hmc_static_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, double int_time, 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)
chain
Check that an output directory exists.
const XML_Char * context
Definition: expat.h:434
TEST_F(ServicesSampleHmcStaticDenseEMassMatrix, unit_e_no_adapt)
ifstream in
Definition: comparison.C:7
int hmc_static_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, double int_time, callbacks::interrupt &interrupt, callbacks::logger &logger, callbacks::writer &init_writer, callbacks::writer &sample_writer, callbacks::writer &diagnostic_writer)
stan::test::unit::instrumented_writer diagnostic
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