hops
DNest4Adapter.hpp
Go to the documentation of this file.
1 #ifndef HOPS_DNEST4ADAPTER_HPP
2 #define HOPS_DNEST4ADAPTER_HPP
3 
4 #include <dnest4/DNest4.h>
5 
6 namespace hops {
7 
11  template<typename Environment>
13  public:
15  }
16 
21  void from_prior(DNest4::RNG &rng);
22 
28  double perturb(DNest4::RNG &rng);
29 
30  [[nodiscard]] double log_likelihood() const;
31 
36  void print(std::ostream &out) const;
37 
42  [[nodiscard]] std::string description() const;
43 
44  private:
45  Environment environment;
46  typename Environment::StateType state;
47  };
48 
49  template<typename ModelImplType>
51  if(!environment.isRngInitialized()) {
52  int seed = rng.rand_int(std::numeric_limits<int>::max()-1);
53  environment.seedRng(seed);
54  }
55  ModelImplType::getSampler()->draw(ModelImplType::getRandomNumberGenerator());
56  this->state = ModelImplType::getSampler()->getState();
57  }
58 
59  template<typename ModelImplType>
60  double DNest4Adapter<ModelImplType>::perturb(DNest4::RNG &rng) {
61  if(!ModelImplType::isRngInitialized()) {
62  int seed = rng.rand_int(std::numeric_limits<int>::max()-1);
63  ModelImplType::seedRng(seed);
64  }
65  ModelImplType::getSampler()->draw(ModelImplType::getRandomNumberGenerator());
66  this->state = ModelImplType::getSampler()->getState();
67  return ModelImplType::getSampler()->computeLogAcceptanceProbability();
68  }
69 
70  template<typename ModelImplType>
72  return -ModelImplType::getModel()->computeNegativeLogLikelihood(this->state);
73  }
74 
75  template<typename ModelImplType>
76  void DNest4Adapter<ModelImplType>::print(std::ostream &out) const {
77  for (long i = 0; i < this->state.rows(); i++)
78  out << this->state(i) << " ";
79  }
80 
81  template<typename ModelImplType>
83  TODO if implements getParameterNames() then return those as string
84  model->getPara
85  std::string description;
86  for (long i = 0; i < state.rows(); ++i) {
87  description += "dim " + std::to_string(i) + " ,";
88  }
89  description.pop_back();
90  return description;
91 
92  environment = Environment::getInstance();
93  this->state = environment.getSampler()->getState();
94  }
95 }
96 
97 
98 #endif //HOPS_DNEST4ADAPTER_HPP
hops::DNest4Adapter::log_likelihood
double log_likelihood() const
Definition: DNest4Adapter.hpp:71
hops::DNest4Adapter::description
std::string description() const
Definition: DNest4Adapter.hpp:82
hops::DNest4Adapter::from_prior
void from_prior(DNest4::RNG &rng)
Definition: DNest4Adapter.hpp:50
hops::DNest4Adapter::DNest4Adapter
DNest4Adapter()
Definition: DNest4Adapter.hpp:14
hops::DNest4Adapter::perturb
double perturb(DNest4::RNG &rng)
Definition: DNest4Adapter.hpp:60
hops
Definition: CsvReader.hpp:8
string
NAME string(REPLACE ".cpp" "_bin" example_name ${example_filename}) if($
Definition: hops/Third-party/HighFive/src/examples/CMakeLists.txt:6
hops::DNest4Adapter::print
void print(std::ostream &out) const
Definition: DNest4Adapter.hpp:76
hops::DNest4Adapter
Definition: DNest4Adapter.hpp:12