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?");
116 const std::vector<double>& params_r,
117 const std::vector<int>& params_i,
119 double epsilon_pm = 0.0,
120 bool epsilon_adapt =
true,
121 double delta = 0.651,
123 BaseRNG rand_int = BaseRNG(std::time(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())
142 throw std::invalid_argument(
"hmc_base ctor: double params must match in size");
145 throw std::invalid_argument(
"hmc_base ctor: int params must match in size");
165 const std::vector<int>& z) {
166 if (x.size() != this->
_x.size())
167 throw std::invalid_argument(
"hmc_base::set_params double params must match in size");
168 if (z.size() != this->
_z.size())
169 throw std::invalid_argument(
"hmc_base::set_params int params must match in size");
188 throw std::invalid_argument(
"x.size() must match num model params.");
207 throw std::invalid_argument(
"z.size() must match num params");
227 std::vector<double> result;