hops
MarkovChainAdapter.hpp
Go to the documentation of this file.
1 #ifndef HOPS_MARKOVCHAINADAPTER_HPP
2 #define HOPS_MARKOVCHAINADAPTER_HPP
3 
4 #include "MarkovChain.hpp"
5 #include "../Utility/Data.hpp"
6 
14 
15 
16 namespace hops {
17  template<typename MarkovChainImpl>
18  class MarkovChainAdapter : public MarkovChain, public MarkovChainImpl {
19  public:
20  explicit MarkovChainAdapter(const MarkovChainImpl &markovChainImpl) : MarkovChainImpl(markovChainImpl) {}
21 
22  void draw(RandomNumberGenerator &randomNumberGenerator, long numberOfSamples) override {
23  assert(numberOfSamples > 0);
24  for (long i = 0; i < numberOfSamples; ++i) {
25  MarkovChainImpl::draw(randomNumberGenerator);
26  MarkovChainImpl::storeRecord();
27  }
28  }
29 
30  void draw(RandomNumberGenerator &randomNumberGenerator, long numberOfSamples, long thinning) override {
31  assert(numberOfSamples > 0);
32  assert(thinning > 0);
33  for (long i = 0; i < numberOfSamples; ++i) {
34  for (long j = 0; j < thinning; ++j) {
35  MarkovChainImpl::draw(randomNumberGenerator);
36  }
37  MarkovChainImpl::storeRecord();
38  }
39  }
40 
41  void writeHistory(FileWriter *const fileWriter) override {
42  MarkovChainImpl::writeRecordsToFile(fileWriter);
43  }
44 
45  void installDataObject(ChainData &chainData) override {
46  MarkovChainImpl::installDataObject(chainData);
47  }
48 
49  const std::vector<Eigen::VectorXd> &getStateRecords() override {
50  return MarkovChainImpl::getStateRecords();
51  }
52 
53  void reserveStateRecords(long numberOfSamples) override {
54  return MarkovChainImpl::reserveStateRecords(numberOfSamples);
55  }
56 
57  void clearHistory() override {
58  MarkovChainImpl::clearRecords();
59  }
60 
61  std::string getName() override {
62  return MarkovChainImpl::getName();
63  }
64 
65  double getAcceptanceRate() override {
66  return MarkovChainImpl::getAcceptanceRate();
67  }
68 
69  void setAttribute(MarkovChainAttribute markovChainAttribute, double value) override {
70  switch (markovChainAttribute) {
73  MarkovChainImpl::setFisherWeight(value);
74  break;
75  }
76  throw std::runtime_error("FISHER_WEIGHT attribute does not exist.");
77  }
80  MarkovChainImpl::setStepSize(value);
81  break;
82  }
83  throw std::runtime_error("STEP_SIZE attribute does not exist.");
84  }
87  MarkovChainImpl::setColdness(value);
88  break;
89  }
90  throw std::runtime_error("PARALLEL_TEMPERING_COLDNESS attribute does not exist.");
91 
92  }
95  MarkovChainImpl::setExchangeAttemptProbability(value);
96  break;
97  }
98  throw std::runtime_error("PARALLEL_TEMPERING_EXCHANGE_PROBABILITY attribute does not exist.");
99  }
100  default: {
101  throw std::runtime_error("Attribute does not exist.");
102  }
103  }
104  }
105 
106  void setState(Eigen::Matrix<double, -1, 1, 0, -1, 1> state) override {
107  MarkovChainImpl::setState(state);
108  }
109 
110  double getAttribute(MarkovChainAttribute markovChainAttribute) override {
111  switch (markovChainAttribute) {
114  return MarkovChainImpl::getColdness();
115  }
116  throw std::runtime_error("PARALLEL_TEMPERING_COLDNESS attribute does not exist.");
117 
118  }
121  return MarkovChainImpl::getExchangeAttemptProbability();
122  }
123  throw std::runtime_error("PARALLEL_TEMPERING_EXCHANGE_PROBABILITY attribute does not exist.");
124  }
126  return MarkovChainImpl::computeLogAcceptanceProbability();
127  }
130  return MarkovChainImpl::getStepSize();
131  }
132  throw std::runtime_error("STEP_SIZE attribute does not exist.");
133  }
134  default: {
135  throw std::runtime_error("Attribute does not exist.");
136  }
137  }
138  }
139  };
140 }
141 
142 #endif //HOPS_MARKOVCHAINADAPTER_HPP
hops::IsGetColdnessAvailable
Definition: IsGetColdnessAvailable.hpp:9
hops::MarkovChainAdapter::setAttribute
void setAttribute(MarkovChainAttribute markovChainAttribute, double value) override
Definition: MarkovChainAdapter.hpp:69
IsSetFisherWeightAvailable.hpp
hops::MarkovChainAdapter
Definition: MarkovChainAdapter.hpp:18
IsSetColdnessAvailable.hpp
hops::MarkovChainAttribute::PREVIOUS_STEP_ACCEPTANCE_PROBABILITY
@ PREVIOUS_STEP_ACCEPTANCE_PROBABILITY
IsGetExchangeAttemptProbabilityAvailable.hpp
hops::MarkovChainAdapter::setState
void setState(Eigen::Matrix< double, -1, 1, 0, -1, 1 > state) override
Definition: MarkovChainAdapter.hpp:106
pcg_detail::engine
Definition: pcg_random.hpp:364
hops::MarkovChainAdapter::draw
void draw(RandomNumberGenerator &randomNumberGenerator, long numberOfSamples) override
Updates internal state of the chain and stores numberOfSamples samples in memory.
Definition: MarkovChainAdapter.hpp:22
hops::MarkovChainAdapter::draw
void draw(RandomNumberGenerator &randomNumberGenerator, long numberOfSamples, long thinning) override
Updates internal state of the chain and stores numberOfSamples samples in memory.
Definition: MarkovChainAdapter.hpp:30
hops::IsGetExchangeAttemptProbabilityAvailable
Definition: IsGetExchangeAttemptProbabilityAvailable.hpp:9
hops::MarkovChainAdapter::getAcceptanceRate
double getAcceptanceRate() override
Definition: MarkovChainAdapter.hpp:65
hops::IsSetFisherWeightAvailable
Definition: IsSetFisherWeightAvailable.hpp:7
hops::MarkovChainAdapter::clearHistory
void clearHistory() override
Deletes all stored chain history.
Definition: MarkovChainAdapter.hpp:57
hops::MarkovChainAdapter::getName
std::string getName() override
Definition: MarkovChainAdapter.hpp:61
hops::ChainData
Definition: ChainData.hpp:12
hops::MarkovChainAttribute::FISHER_WEIGHT
@ FISHER_WEIGHT
hops::IsGetStepSizeAvailable
Definition: IsGetStepSizeAvailable.hpp:9
hops::IsSetExchangeAttemptProbabilityAvailable
Definition: IsSetExchangeAttemptProbabilityAvailable.hpp:8
hops::MarkovChainAdapter::MarkovChainAdapter
MarkovChainAdapter(const MarkovChainImpl &markovChainImpl)
Definition: MarkovChainAdapter.hpp:20
hops::MarkovChainAttribute::PARALLEL_TEMPERING_EXCHANGE_PROBABILITY
@ PARALLEL_TEMPERING_EXCHANGE_PROBABILITY
hops::FileWriter
Definition: FileWriter.hpp:9
hops
Definition: CsvReader.hpp:8
IsGetColdnessAvailable.hpp
hops::MarkovChainInterface
Definition: MarkovChain.hpp:17
MarkovChain.hpp
hops::MarkovChainAttribute::PARALLEL_TEMPERING_COLDNESS
@ PARALLEL_TEMPERING_COLDNESS
IsGetStepSizeAvailable.hpp
hops::IsSetStepSizeAvailable
Definition: IsSetStepSizeAvailable.hpp:8
hops::MarkovChainAttribute
MarkovChainAttribute
Definition: MarkovChainAttribute.hpp:5
string
NAME string(REPLACE ".cpp" "_bin" example_name ${example_filename}) if($
Definition: hops/Third-party/HighFive/src/examples/CMakeLists.txt:6
IsSetExchangeAttemptProbabilityAvailable.hpp
hops::IsSetColdnessAvailable
Definition: IsSetColdnessAvailable.hpp:8
hops::MarkovChainAdapter::reserveStateRecords
void reserveStateRecords(long numberOfSamples) override
Definition: MarkovChainAdapter.hpp:53
hops::MarkovChainAttribute::STEP_SIZE
@ STEP_SIZE
hops::MarkovChainAdapter::getStateRecords
const std::vector< Eigen::VectorXd > & getStateRecords() override
Definition: MarkovChainAdapter.hpp:49
hops::MarkovChainAdapter::getAttribute
double getAttribute(MarkovChainAttribute markovChainAttribute) override
Definition: MarkovChainAdapter.hpp:110
IsSetStepSizeAvailable.hpp
hops::MarkovChainAdapter::installDataObject
void installDataObject(ChainData &chainData) override
Definition: MarkovChainAdapter.hpp:45
hops::MarkovChainAdapter::writeHistory
void writeHistory(FileWriter *const fileWriter) override
Writes all stored chain history using the fileWriter.
Definition: MarkovChainAdapter.hpp:41