1 #ifndef __STAN__MCMC__HMC_BASE_H__
2 #define __STAN__MCMC__HMC_BASE_H__
6 #include <boost/random/normal_distribution.hpp>
7 #include <boost/random/mersenne_twister.hpp>
8 #include <boost/random/variate_generator.hpp>
9 #include <boost/random/uniform_01.hpp>
21 template <
class BaseRNG = boost::mt19937>
40 boost::variate_generator<BaseRNG&,
47 std::vector<double>
_x;
49 std::vector<double>
_g;
58 std::vector<double> x = this->
_x;
60 for (
size_t i = 0; i < m.size(); ++i)
62 std::vector<double> g = this->
_g;
63 double lastlogp = this->
_logp;
65 double H = logp - lastlogp;
66 int direction = H >
log(0.5) ? 1 : -1;
70 for (
size_t i = 0; i < m.size(); ++i)
74 if ((direction == 1) && !(H >
log(0.5)))
76 else if ((direction == -1) && !(H <
log(0.5)))
83 if (this->_epsilon > 1e300)
84 throw std::runtime_error(
"Posterior is improper. Please check your model.");
85 if (this->_epsilon == 0)
86 throw std::runtime_error(
"No acceptably small step size could be found. Perhaps the posterior is not continuous?");
117 double epsilon_pm = 0.0,
118 bool epsilon_adapt =
true,
119 double delta = 0.651,
121 BaseRNG rand_int = BaseRNG(std::time(0)),
122 const std::vector<double>* params_r = 0,
123 const std::vector<int>* params_i = 0)
132 _da(gamma, std::vector<double>(1, 0.0)),
136 _x(model.num_params_r()),
137 _z(model.num_params_i()),
138 _g(model.num_params_r())
143 throw std::invalid_argument(
"hmc_base ctor: double params must match in size");
148 throw std::invalid_argument(
"hmc_base ctor: int params must match in size");
169 const std::vector<int>& z) {
170 if (x.size() != this->
_x.size())
171 throw std::invalid_argument(
"hmc_base::set_params double params must match in size");
172 if (z.size() != this->
_z.size())
173 throw std::invalid_argument(
"hmc_base::set_params int params must match in size");
192 throw std::invalid_argument(
"x.size() must match num model params.");
211 throw std::invalid_argument(
"z.size() must match num params");
231 std::vector<double> result;