Stan  2.10.0
probability, sampling & optimization
base_static_hmc.hpp
Go to the documentation of this file.
1 #ifndef STAN_MCMC_HMC_STATIC_BASE_STATIC_HMC_HPP
2 #define STAN_MCMC_HMC_STATIC_BASE_STATIC_HMC_HPP
3 
7 #include <boost/math/special_functions/fpclassify.hpp>
8 #include <cmath>
9 #include <limits>
10 #include <string>
11 #include <vector>
12 
13 namespace stan {
14  namespace mcmc {
19  template <class Model,
20  template<class, class> class Hamiltonian,
21  template<class> class Integrator,
22  class BaseRNG>
24  : public base_hmc<Model, Hamiltonian, Integrator, BaseRNG> {
25  public:
26  base_static_hmc(const Model& model, BaseRNG& rng)
27  : base_hmc<Model, Hamiltonian, Integrator, BaseRNG>(model, rng),
28  T_(1), energy_(0) {
29  update_L_();
30  }
31 
33 
34  sample
35  transition(sample& init_sample,
38  this->sample_stepsize();
39 
40  this->seed(init_sample.cont_params());
41 
42  this->hamiltonian_.sample_p(this->z_, this->rand_int_);
43  this->hamiltonian_.init(this->z_, info_writer, error_writer);
44 
45  ps_point z_init(this->z_);
46 
47  double H0 = this->hamiltonian_.H(this->z_);
48 
49  for (int i = 0; i < L_; ++i)
50  this->integrator_.evolve(this->z_, this->hamiltonian_,
51  this->epsilon_,
52  info_writer, error_writer);
53 
54  double h = this->hamiltonian_.H(this->z_);
55  if (boost::math::isnan(h)) h = std::numeric_limits<double>::infinity();
56 
57  double acceptProb = std::exp(H0 - h);
58 
59  if (acceptProb < 1 && this->rand_uniform_() > acceptProb)
60  this->z_.ps_point::operator=(z_init);
61 
62  acceptProb = acceptProb > 1 ? 1 : acceptProb;
63 
64  this->energy_ = this->hamiltonian_.H(this->z_);
65  return sample(this->z_.q, - this->hamiltonian_.V(this->z_), acceptProb);
66  }
67 
68  void get_sampler_param_names(std::vector<std::string>& names) {
69  names.push_back("stepsize__");
70  names.push_back("int_time__");
71  names.push_back("energy__");
72  }
73 
74  void get_sampler_params(std::vector<double>& values) {
75  values.push_back(this->epsilon_);
76  values.push_back(this->T_);
77  values.push_back(this->energy_);
78  }
79 
80  void set_nominal_stepsize_and_T(const double e, const double t) {
81  if (e > 0 && t > 0) {
82  this->nom_epsilon_ = e;
83  T_ = t;
84  update_L_();
85  }
86  }
87 
88  void set_nominal_stepsize_and_L(const double e, const int l) {
89  if (e > 0 && l > 0) {
90  this->nom_epsilon_ = e;
91  L_ = l;
92  T_ = this->nom_epsilon_ * L_; }
93  }
94 
95  void set_T(const double t) {
96  if (t > 0) {
97  T_ = t;
98  update_L_();
99  }
100  }
101 
102  void set_nominal_stepsize(const double e) {
103  if (e > 0) {
104  this->nom_epsilon_ = e;
105  update_L_();
106  }
107  }
108 
109  double get_T() {
110  return this->T_;
111  }
112 
113  int get_L() {
114  return this->L_;
115  }
116 
117  protected:
118  double T_;
119  int L_;
120  double energy_;
121 
122  void update_L_() {
123  L_ = static_cast<int>(T_ / this->nom_epsilon_);
124  L_ = L_ < 1 ? 1 : L_;
125  }
126  };
127 
128  } // mcmc
129 } // stan
130 #endif
Hamiltonian< Model, BaseRNG >::PointType z_
Definition: base_hmc.hpp:164
void sample(stan::mcmc::base_mcmc *sampler, int num_warmup, int num_samples, int num_thin, int refresh, bool save, stan::services::sample::mcmc_writer< Model, SampleRecorder, DiagnosticRecorder, MessageRecorder > &mcmc_writer, stan::mcmc::sample &init_s, Model &model, RNG &base_rng, const std::string &prefix, const std::string &suffix, std::ostream &o, StartTransitionCallback &callback, interface_callbacks::writer::base_writer &info_writer, interface_callbacks::writer::base_writer &error_writer)
Definition: sample.hpp:17
Probability, optimization and sampling library.
void set_nominal_stepsize_and_T(const double e, const double t)
void set_nominal_stepsize_and_L(const double e, const int l)
void set_nominal_stepsize(const double e)
Point in a generic phase space.
Definition: ps_point.hpp:17
double cont_params(int k) const
Definition: sample.hpp:25
base_writer is an abstract base class defining the interface for Stan writer callbacks.
Definition: base_writer.hpp:20
Hamiltonian Monte Carlo implementation using the endpoint of trajectories with a static integration t...
void get_sampler_param_names(std::vector< std::string > &names)
void seed(const Eigen::VectorXd &q)
Definition: base_hmc.hpp:53
void get_sampler_params(std::vector< double > &values)
boost::uniform_01< BaseRNG & > rand_uniform_
Definition: base_hmc.hpp:171
base_static_hmc(const Model &model, BaseRNG &rng)
Hamiltonian< Model, BaseRNG > hamiltonian_
Definition: base_hmc.hpp:166
sample transition(sample &init_sample, interface_callbacks::writer::base_writer &info_writer, interface_callbacks::writer::base_writer &error_writer)
Integrator< Hamiltonian< Model, BaseRNG > > integrator_
Definition: base_hmc.hpp:165
void set_T(const double t)

     [ Stan Home Page ] © 2011–2016, Stan Development Team.