hops
GaussianProposal.hpp
Go to the documentation of this file.
1 #ifndef HOPS_GAUSSIANPROPOSAL_HPP
2 #define HOPS_GAUSSIANPROPOSAL_HPP
3 
4 #include "../IsSetStepSizeAvailable.hpp"
5 #include "../../RandomNumberGenerator/RandomNumberGenerator.hpp"
6 #include "../../FileWriter/CsvWriter.hpp"
7 #include <random>
8 
9 namespace hops {
10  template<typename MatrixType, typename VectorType>
12  public:
14 
22  GaussianProposal(MatrixType A, VectorType b, StateType currentState, typename MatrixType::Scalar stepSize = 1);
23 
24  void propose(RandomNumberGenerator &randomNumberGenerator);
25 
26  void acceptProposal();
27 
28  StateType getState() const;
29 
30  StateType getProposal() const;
31 
32  void setState(StateType newState);
33 
34  void setStepSize(typename MatrixType::Scalar stepSize);
35 
36  typename MatrixType::Scalar getStepSize() const;
37 
38  [[nodiscard]] typename MatrixType::Scalar computeLogAcceptanceProbability() {
39  bool isProposalInteriorPoint = ((b - A * m_proposal).array() >= 0).all();
40  if (!isProposalInteriorPoint) {
41  return -std::numeric_limits<typename MatrixType::Scalar>::infinity();
42  }
43  return 0;
44  }
45 
47 
48  private:
49  MatrixType A;
50  VectorType b;
51  StateType state;
52  StateType m_proposal;
53 
54  typename MatrixType::Scalar stepSize;
55 
56  std::normal_distribution<typename MatrixType::Scalar> normal;
57  };
58 
59  template<typename MatrixType, typename VectorType>
61  VectorType b_,
62  VectorType currentState_,
63  typename MatrixType::Scalar stepSize_) :
64  A(std::move(A_)),
65  b(std::move(b_)),
66  state(std::move(currentState_)),
67  m_proposal(this->state),
68  stepSize(stepSize_) {
69  normal = std::normal_distribution<typename MatrixType::Scalar>(0, stepSize);
70  }
71 
72  template<typename MatrixType, typename VectorType>
74  RandomNumberGenerator &randomNumberGenerator) {
75  for (long i = 0; i < m_proposal.rows(); ++i) {
76  m_proposal(i) = normal(randomNumberGenerator);
77  }
78 
79  m_proposal.noalias() += state;
80  }
81 
82  template<typename MatrixType, typename VectorType>
83  void
85  state.swap(m_proposal);
86  }
87 
88  template<typename MatrixType, typename VectorType>
91  return state;
92  }
93 
94  template<typename MatrixType, typename VectorType>
97  return m_proposal;
98  }
99 
100  template<typename MatrixType, typename VectorType>
102  GaussianProposal::state = std::move(newState);
103  }
104 
105  template<typename MatrixType, typename VectorType>
107  typename MatrixType::Scalar newStepSize) {
108  stepSize = newStepSize;
109  normal = std::normal_distribution<typename MatrixType::Scalar>(0, stepSize);
110  }
111 
112  template<typename MatrixType, typename VectorType>
113  typename MatrixType::Scalar
115  return stepSize;
116  }
117 
118  template<typename MatrixType, typename VectorType>
120  return "Gaussian";
121  }
122 }
123 
124 #endif //HOPS_GAUSSIANPROPOSAL_HPP
hops::GaussianProposal::getStepSize
MatrixType::Scalar getStepSize() const
Definition: GaussianProposal.hpp:114
hops::GaussianProposal::GaussianProposal
GaussianProposal(MatrixType A, VectorType b, StateType currentState, typename MatrixType::Scalar stepSize=1)
Constructs classical Gaussian random walk m_proposal mechanism on polytope defined as Ax<b.
Definition: GaussianProposal.hpp:60
hops::MatrixType
Eigen::MatrixXd MatrixType
Definition: MatrixType.hpp:7
pcg_detail::engine
Definition: pcg_random.hpp:364
hops::GaussianProposal::propose
void propose(RandomNumberGenerator &randomNumberGenerator)
Definition: GaussianProposal.hpp:73
hops::GaussianProposal::getProposal
StateType getProposal() const
Definition: GaussianProposal.hpp:96
hops::GaussianProposal::getState
StateType getState() const
Definition: GaussianProposal.hpp:90
hops::GaussianProposal::computeLogAcceptanceProbability
MatrixType::Scalar computeLogAcceptanceProbability()
Definition: GaussianProposal.hpp:38
hops::GaussianProposal
Definition: GaussianProposal.hpp:11
hops::GaussianProposal::StateType
VectorType StateType
Definition: GaussianProposal.hpp:13
hops::GaussianProposal::acceptProposal
void acceptProposal()
Definition: GaussianProposal.hpp:84
hops
Definition: CsvReader.hpp:8
hops::GaussianProposal::getName
std::string getName()
Definition: GaussianProposal.hpp:119
hops::GaussianProposal::setState
void setState(StateType newState)
Definition: GaussianProposal.hpp:101
string
NAME string(REPLACE ".cpp" "_bin" example_name ${example_filename}) if($
Definition: hops/Third-party/HighFive/src/examples/CMakeLists.txt:6
hops::VectorType
Eigen::VectorXd VectorType
Definition: VectorType.hpp:7
hops::GaussianProposal::setStepSize
void setStepSize(typename MatrixType::Scalar stepSize)
Definition: GaussianProposal.hpp:106