1 #ifndef __STAN__MCMC__ADAPTIVE_CDHMC_H__
2 #define __STAN__MCMC__ADAPTIVE_CDHMC_H__
7 #include <boost/random/normal_distribution.hpp>
8 #include <boost/random/mersenne_twister.hpp>
9 #include <boost/random/variate_generator.hpp>
10 #include <boost/random/uniform_01.hpp>
47 std::vector<double> _x;
51 std::vector<double> _g;
64 boost::mt19937 _rand_int;
65 boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > _rand_unit_norm;
66 boost::uniform_01<boost::mt19937&> _rand_uniform_01;
71 inline static double da_gamma() {
return 0.05; }
84 _L = lround(_epsilonL / epsilon);
111 double delta = 0.651,
113 unsigned int random_seed = static_cast<unsigned int>(std::time(0)))
116 _x(model.num_params_r()),
117 _z(model.num_params_i()),
118 _g(model.num_params_r()),
123 _rand_int(random_seed),
124 _rand_unit_norm(_rand_int,
125 boost::normal_distribution<>()),
126 _rand_uniform_01(_rand_int),
128 _da(da_gamma(), std::vector<double>(1, 0)) {
134 _da.
setx0(std::vector<double>(1,
log(_epsilon)));
154 const std::vector<int>& z) {
155 if (x.size() != _x.size())
156 throw std::invalid_argument(
"adaptive_cdhmc::set_params: double params must match in size");
157 if (z.size() != _z.size())
158 throw std::invalid_argument(
"adaptive_cdhmc::set_params: int params must match in size");
177 throw std::invalid_argument(
"x.size() must match the number of parameters of the model.");
195 throw std::invalid_argument(
"z.size() must match the number of parameters of the model.");
206 std::vector<double> x = _x;
208 for (
size_t i = 0; i < m.size(); ++i)
209 m[i] = _rand_unit_norm();
210 std::vector<double> g = _g;
211 double lastlogp = _logp;
212 double logp =
leapfrog(_model, _z, x, m, g, _epsilon);
213 double H = logp - lastlogp;
214 int direction = H >
log(0.5) ? 1 : -1;
220 for (
size_t i = 0; i < m.size(); ++i)
221 m[i] = _rand_unit_norm();
222 logp =
leapfrog(_model, _z, x, m, g, _epsilon);
226 if ((direction == 1) && (H <
log(0.5)))
228 else if ((direction == -1) && (H >
log(0.5)))
231 _epsilon = direction == 1 ? 2 * _epsilon : 0.5 * _epsilon;
243 std::vector<double> probs;
255 for (
size_t i = 0; i < m.size(); ++i)
256 m[i] = _rand_unit_norm();
259 std::vector<double> g_new(_g);
260 std::vector<double> x_new(_x);
261 double logp_new = -1e100;
262 for (
unsigned int l = 0; l < _L; ++l)
263 logp_new =
leapfrog(_model, _z, x_new, m, g_new, _epsilon);
267 double dH = H_new - H;
268 if (_rand_uniform_01() <
exp(dH)) {
276 if (adapt_stat != adapt_stat)
279 double adapt_g = adapt_stat - _delta;
280 std::vector<double> gvec(1, -adapt_g);
281 std::vector<double> result;
285 std::vector<double> result;
288 double avg_eta = 1.0 /
n_steps();
304 std::vector<double> result;
315 params.assign(1, _epsilon);