1 #ifndef __STAN__MCMC__HMC_HPP__
2 #define __STAN__MCMC__HMC_HPP__
8 #include <boost/random/mersenne_twister.hpp>
9 #include <boost/random/normal_distribution.hpp>
10 #include <boost/random/uniform_01.hpp>
11 #include <boost/random/variate_generator.hpp>
38 std::vector<double> _x;
40 std::vector<double> _g;
46 boost::mt19937 _rand_int;
47 boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > _rand_unit_norm;
48 boost::uniform_01<boost::mt19937&> _rand_uniform_01;
71 unsigned int random_seed = static_cast<unsigned int>(std::time(0)))
74 _x(model.num_params_r()),
75 _z(model.num_params_i()),
76 _g(model.num_params_r()),
81 _rand_int(random_seed),
82 _rand_unit_norm(_rand_int,
83 boost::normal_distribution<>()),
84 _rand_uniform_01(_rand_int) {
111 const std::vector<int>& z) {
112 if (x.size() != _x.size() || z.size() != _z.size())
113 throw std::invalid_argument(
"x.size() or z.size() mismatch");
131 throw std::invalid_argument (
"x.size() must match the number of parameters of the model.");
149 throw std::invalid_argument (
"z.size() must match the number of parameters of the model.");
161 std::vector<double> probs;
173 for (
size_t i = 0; i < m.size(); ++i)
174 m[i] = _rand_unit_norm();
177 std::vector<double> g_new(_g);
178 std::vector<double> x_new(_x);
181 double logp_new = -1e100;
182 for (
unsigned int l = 0; l < _L; ++l)
183 logp_new =
leapfrog(_model, _z, x_new, m, g_new, _epsilon);
187 double dH = H_new - H;
188 if (_rand_uniform_01() <
exp(dH)) {