hmc_nuts_diag_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 ServicesSampleHmcNutsDiagEMassMatrix : 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> diag_vals
69  = inv_metric.vals_r("inv_metric");
71 }
72 
74  unsigned int random_seed = 12345;
75  unsigned int chain = 1;
76  double init_radius = 2;
77  int num_warmup = 250;
78  int num_samples = 0;
79  int num_thin = 1;
80  bool save_warmup = false;
81  int refresh = 0;
82  double stepsize = 1;
83  double stepsize_jitter = 0;
84  int max_depth = 10;
85  double delta = .8;
86  double gamma = .05;
87  double kappa = .75;
88  double t0 = 10;
89  unsigned int init_buffer = 75;
90  unsigned int term_buffer = 50;
91  unsigned int window = 25;
93  EXPECT_EQ(interrupt.call_count(), 0);
94 
95  // run sampler with unit_e Euclidean metric, 250 warmups
96  int return_code =
98  context,
99  random_seed,
100  chain,
101  init_radius,
102  num_warmup,
103  num_samples,
104  num_thin,
105  save_warmup,
106  refresh,
107  stepsize,
108  stepsize_jitter,
109  max_depth,
110  delta,
111  gamma,
112  kappa,
113  t0,
114  init_buffer,
115  term_buffer,
116  window,
117  interrupt,
118  logger,
119  init,
120  parameter,
121  diagnostic);
122  EXPECT_EQ(0, return_code);
123 }
124 
126  unsigned int random_seed = 12345;
127  unsigned int chain = 1;
128  double init_radius = 2;
129  int num_warmup = 0;
130  int num_samples = 2;
131  int num_thin = 1;
132  bool save_warmup = false;
133  int refresh = 0;
134  double stepsize = 1;
135  double stepsize_jitter = 0;
136  int max_depth = 10;
138  EXPECT_EQ(interrupt.call_count(), 0);
139 
140  std::string txt =
141  "inv_metric <- structure(c(0.787405, 0.884987, 1.19869),.Dim=c(3))";
142  std::stringstream in(txt);
143  stan::io::dump dump(in);
144  stan::io::var_context& inv_metric = dump;
145 
146  int return_code =
148  context,
149  inv_metric,
150  random_seed,
151  chain,
152  init_radius,
153  num_warmup,
154  num_samples,
155  num_thin,
156  save_warmup,
157  refresh,
158  stepsize,
159  stepsize_jitter,
160  max_depth,
161  interrupt,
162  logger,
163  init,
164  parameter,
165  diagnostic);
166 
167  EXPECT_EQ(0, return_code);
168 
169  // check results against initial vals (stan::io::var_context inv_metric)
170  std::vector<double> diag_vals(3);
171  diag_vals = inv_metric.vals_r("inv_metric");
172  stan::test::unit::check_adaptation(3, diag_vals, parameter, 0.2);
173 }
174 
176  // run adaptive sampler with 0 warmups
177  unsigned int random_seed = 12345;
178  unsigned int chain = 1;
179  double init_radius = 2;
180  int num_warmup = 0;
181  int num_samples = 2;
182  int num_thin = 1;
183  bool save_warmup = false;
184  int refresh = 0;
185  double stepsize = 1;
186  double stepsize_jitter = 0;
187  int max_depth = 10;
188  double delta = .8;
189  double gamma = .05;
190  double kappa = .75;
191  double t0 = 10;
192  unsigned int init_buffer = 75;
193  unsigned int term_buffer = 50;
194  unsigned int window = 25;
196  EXPECT_EQ(interrupt.call_count(), 0);
197 
198  std::string txt =
199  "inv_metric <- structure(c(0.787405, 0.884987, 1.19869),.Dim=c(3))";
200  std::stringstream in(txt);
201  stan::io::dump dump(in);
202  stan::io::var_context& inv_metric = dump;
203 
204  int return_code =
206  context,
207  inv_metric,
208  random_seed,
209  chain,
210  init_radius,
211  num_warmup,
212  num_samples,
213  num_thin,
214  save_warmup,
215  refresh,
216  stepsize,
217  stepsize_jitter,
218  max_depth,
219  delta,
220  gamma,
221  kappa,
222  t0,
223  init_buffer,
224  term_buffer,
225  window,
226  interrupt,
227  logger,
228  init,
229  parameter,
230  diagnostic);
231 
232  EXPECT_EQ(0, return_code);
233 
234  // check results against initial vals (stan::io::var_context inv_metric)
235  std::vector<double> diag_vals(3);
236  diag_vals = inv_metric.vals_r("inv_metric");
237  stan::test::unit::check_adaptation(3, diag_vals, parameter, 0.2);
238 }
stan::test::unit::instrumented_writer diagnostic
double delta
Definition: runWimpSim.h:98
int hmc_nuts_diag_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_writer parameter
virtual std::vector< double > vals_r(const std::string &name) const =0
stan::io::dump create_unit_e_diag_inv_metric(size_t num_params)
stan::test::unit::instrumented_writer init
chain
Check that an output directory exists.
TEST_F(ServicesSampleHmcNutsDiagEMassMatrix, unit_e_no_adapt)
const XML_Char * context
Definition: expat.h:434
ifstream in
Definition: comparison.C:7
int hmc_nuts_diag_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)
void check_adaptation(const size_t &num_params, const std::vector< double > &param_vals, stan::test::unit::instrumented_writer &report, const double &err_margin)
stan::test::unit::instrumented_logger logger
const XML_Char XML_Content * model
Definition: expat.h:151
void refresh()
Definition: show_event.C:21
enum BeamMode string