Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
base_hamiltonian.hpp
Go to the documentation of this file.
1 #ifndef STAN__MCMC__BASE__HAMILTONIAN__BETA
2 #define STAN__MCMC__BASE__HAMILTONIAN__BETA
3 
4 #include <stdexcept>
5 #include <fstream>
6 #include <iostream>
7 #include <vector>
8 
10 #include <stan/model/util.hpp>
11 
12 namespace stan {
13 
14  namespace mcmc {
15 
16  template <typename M, typename P, typename BaseRNG>
18 
19  public:
20 
21  base_hamiltonian(M& m, std::ostream* e): model_(m), err_stream_(e) {};
23 
24  virtual double T(P& z) = 0;
25  double V(P& z) { return z.V; }
26 
27  virtual double tau(P& z) = 0;
28  virtual double phi(P& z) = 0;
29 
30  double H(P& z) { return T(z) + V(z); }
31 
32  // tau = 0.5 p_{i} p_{j} Lambda^{ij} (q)
33  virtual const Eigen::VectorXd dtau_dq(P& z) = 0;
34  virtual const Eigen::VectorXd dtau_dp(P& z) = 0;
35 
36  // phi = 0.5 * log | Lambda (q) | + V(q)
37  virtual const Eigen::VectorXd dphi_dq(P& z) = 0;
38 
39  virtual void sample_p(P& z, BaseRNG& rng) = 0;
40 
41  virtual void init(P& z) { this->update(z); }
42 
43  virtual void update(P& z) {
44 
45  try {
46  stan::model::gradient(model_, z.q, z.V, z.g, err_stream_);
47  z.V *= -1;
48  } catch (const std::exception& e) {
49  this->write_error_msg_(err_stream_, e);
50  z.V = std::numeric_limits<double>::infinity();
51  }
52 
53  z.g *= -1;
54 
55  }
56 
57  protected:
58 
59  M& model_;
60 
61  std::ostream* err_stream_;
62 
63  void write_error_msg_(std::ostream* error_msgs,
64  const std::exception& e) {
65 
66  if (!error_msgs) return;
67 
68  *error_msgs << std::endl
69  << "Informational Message: The current Metropolis proposal is about to be "
70  << "rejected because of the following issue:"
71  << std::endl
72  << e.what() << std::endl
73  << "If this warning occurs sporadically, such as for highly constrained "
74  << "variable types like covariance matrices, then the sampler is fine,"
75  << std::endl
76  << "but if this warning occurs often then your model may be either severely "
77  << "ill-conditioned or misspecified."
78  << std::endl;
79 
80  }
81 
82  };
83 
84  } // mcmc
85 
86 } // stan
87 
88 
89 #endif
virtual void sample_p(P &z, BaseRNG &rng)=0
virtual const Eigen::VectorXd dphi_dq(P &z)=0
virtual double T(P &z)=0
void gradient(const M &model, const Eigen::Matrix< double, Eigen::Dynamic, 1 > &x, double &f, Eigen::Matrix< double, Eigen::Dynamic, 1 > &grad_f, std::ostream *msgs=0)
Definition: util.hpp:405
virtual const Eigen::VectorXd dtau_dp(P &z)=0
void write_error_msg_(std::ostream *error_msgs, const std::exception &e)
virtual double tau(P &z)=0
base_hamiltonian(M &m, std::ostream *e)
virtual double phi(P &z)=0
virtual const Eigen::VectorXd dtau_dq(P &z)=0
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:86

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