hops
MetropolisHastingsFilter.hpp
Go to the documentation of this file.
1 #ifndef HOPS_METROPOLISHASTINGSFILTER_HPP
2 #define HOPS_METROPOLISHASTINGSFILTER_HPP
3 
4 #include <random>
5 
10 
11 
12 namespace hops {
13  template<typename MarkovChainProposer>
14  class MetropolisHastingsFilter : public MarkovChainProposer {
15  public:
16  explicit MetropolisHastingsFilter(const MarkovChainProposer &markovChainImpl) : MarkovChainProposer(
17  markovChainImpl) {}
18 
19  void draw(RandomNumberGenerator &randomNumberGenerator);
20 
21  double getAcceptanceRate();
22 
23  void clearRecords() {
24  numberOfProposals = 0;
25  numberOfAcceptedProposals = 0;
27  MarkovChainProposer::clearRecords();
28  }
29  }
30 
31  private:
32  long numberOfAcceptedProposals = 0;
33  long numberOfProposals = 0;
34  std::uniform_real_distribution<double> uniformRealDistribution;
35  };
36 
37  template<typename MarkovChainProposer>
39  MarkovChainProposer::propose(randomNumberGenerator);
40  numberOfProposals++;
41  double acceptanceChance = std::log(uniformRealDistribution(randomNumberGenerator));
42  double acceptanceProbability = MarkovChainProposer::computeLogAcceptanceProbability();
44  MarkovChainProposer::addMessage("interior(");
45  MarkovChainProposer::addMessage(std::isfinite(acceptanceProbability) ? "true" : "false");
46  MarkovChainProposer::addMessage(")");
47  MarkovChainProposer::addMessage(" alpha(");
48  MarkovChainProposer::addMessage(std::to_string(std::exp(acceptanceProbability)));
49  MarkovChainProposer::addMessage(") action(");
50  }
51  if (acceptanceChance < acceptanceProbability) {
52  MarkovChainProposer::acceptProposal();
53  numberOfAcceptedProposals++;
55  MarkovChainProposer::addMessage("accept) | ");
56  }
57  }
58  else {
60  MarkovChainProposer::addMessage("reject) | ");
61  }
62  }
63  }
64 
65  template<typename MarkovChainProposer>
67  return numberOfProposals != 0 ? static_cast<double>(numberOfAcceptedProposals) / numberOfProposals :
68  0;
69  }
70 }
71 
72 #endif //HOPS_METROPOLISHASTINGSFILTER_HPP
hops::MetropolisHastingsFilter::MetropolisHastingsFilter
MetropolisHastingsFilter(const MarkovChainProposer &markovChainImpl)
Definition: MetropolisHastingsFilter.hpp:16
hops::MetropolisHastingsFilter::getAcceptanceRate
double getAcceptanceRate()
Definition: MetropolisHastingsFilter.hpp:66
hops::IsClearRecordsAvailable
Definition: IsClearRecordsAvailable.hpp:8
RandomNumberGenerator.hpp
IsAddMessageAvailabe.hpp
IsClearRecordsAvailable.hpp
pcg_detail::engine
Definition: pcg_random.hpp:364
hops::MetropolisHastingsFilter::clearRecords
void clearRecords()
Definition: MetropolisHastingsFilter.hpp:23
hops::IsAddMessageAvailable
Definition: IsAddMessageAvailabe.hpp:9
hops::MetropolisHastingsFilter::draw
void draw(RandomNumberGenerator &randomNumberGenerator)
Definition: MetropolisHastingsFilter.hpp:38
hops
Definition: CsvReader.hpp:8
FileWriter.hpp
hops::MetropolisHastingsFilter
Definition: MetropolisHastingsFilter.hpp:14