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)) {
An abstract base class for adaptive samplers.
void nfevals_plus_eq(int n)
Add the specified number of evaluations to the number of function evaluations.
Hamiltonian Monte Carlo (HMC) sampler.
virtual void set_params_r(const std::vector< double > &x)
Set the model real parameters to the specified values and update gradients and log probability to mat...
virtual void set_params(const std::vector< double > &x, const std::vector< int > &z)
Set the model real and integer parameters to the specified values.
virtual ~hmc()
Destructor.
virtual sample next_impl()
Return the next sample.
hmc(stan::model::prob_grad &model, double epsilon, int L, unsigned int random_seed=static_cast< unsigned int >(std::time(0)))
Constructs a Hamiltonian Monte Carlo (HMC) sampler for the specified model, using the specified step ...
virtual void set_params_i(const std::vector< int > &z)
Set the model real parameters to the specified values and update gradients and log probability to mat...
Representation of a MCMC sample.
The prob_grad class represents densities with fixed numbers of discrete and scalar parameters and the...
virtual void init(std::vector< double > ¶ms_r, std::vector< int > ¶ms_i)
virtual size_t num_params_r()
virtual size_t num_params_i()
virtual double log_prob_star(size_t idx, int val, std::vector< double > ¶ms_r, std::vector< int > ¶ms_i, std::ostream *output_stream=0)
virtual double grad_log_prob(std::vector< double > ¶ms_r, std::vector< int > ¶ms_i, std::vector< double > &gradient, std::ostream *output_stream=0)=0
int param_range_i_lower(size_t idx)
int param_range_i_upper(size_t idx)
Reimplementing boost functionality.
var exp(const var &a)
Return the exponentiation of the specified variable (cmath).
double epsilon()
Return minimum positive number representable.
double dot_self(const Eigen::Matrix< double, R, C > &v)
Returns the dot product of the specified vector with itself.
double leapfrog(stan::model::prob_grad &model, std::vector< int > z, std::vector< double > &x, std::vector< double > &m, std::vector< double > &g, double epsilon, std::ostream *error_msgs=0, std::ostream *output_msgs=0)
Computes the log probability for a single leapfrog step in Hamiltonian Monte Carlo.
int sample_unnorm_log(std::vector< double > probs, boost::uniform_01< boost::mt19937 & > &rand_uniform_01)
Probability, optimization and sampling library.