MUQ  0.4.3
SingleChainMCMC.h
Go to the documentation of this file.
1 #ifndef SINGLECHAINMCMC_H
2 #define SINGLECHAINMCMC_H
3 
8 
9 #include <vector>
10 
11 #include <boost/property_tree/ptree.hpp>
12 
13 namespace muq{
14  namespace SamplingAlgorithms{
15 
29  {
30 
31  public:
32 
33 #if MUQ_HAS_PARCER
34  SingleChainMCMC(boost::property_tree::ptree pt,
35  std::shared_ptr<parcer::Communicator> const& comm,
36  std::vector<std::shared_ptr<TransitionKernel> > const& kernelsIn);
37 
38  SingleChainMCMC(boost::property_tree::ptree pt,
39  std::shared_ptr<AbstractSamplingProblem> const& problem,
40  std::shared_ptr<parcer::Communicator> const& comm);
41 #endif
42 
43  SingleChainMCMC(boost::property_tree::ptree pt,
44  std::shared_ptr<AbstractSamplingProblem> const& problem);
45 
46  SingleChainMCMC(boost::property_tree::ptree pt,
47  std::vector<std::shared_ptr<TransitionKernel> > const& kernels);
48 
49  SingleChainMCMC(boost::property_tree::ptree pt,
50  std::shared_ptr<TransitionKernel> const& kernel) : SingleChainMCMC(pt, std::vector<std::shared_ptr<TransitionKernel>>(1,kernel)){};
51 
52  virtual ~SingleChainMCMC() = default;
53 
55 
59  virtual void SetState(std::shared_ptr<SamplingState> const& x0);
60  virtual void SetState(std::vector<Eigen::VectorXd> const& x0);
61 
62  template<typename... Args>
63  inline void SetState(Args const&... args) {
64  std::vector<Eigen::VectorXd> vec;
65  SetStateRecurse(vec, args...);
66  }
67 
68  virtual std::vector<std::shared_ptr<TransitionKernel>>& Kernels(){return kernels;};
69 
70  template<typename... Args>
71  inline std::shared_ptr<MarkovChain> Run(Args const&... args) {
72  std::vector<Eigen::VectorXd> vec;
73  return RunRecurse(vec, args...);
74  }
75 
76  virtual std::shared_ptr<MarkovChain> Run(std::vector<Eigen::VectorXd> const& x0);
77 
78  virtual double TotalTime() { return totalTime; }
79 
103  virtual void AddNumSamps(unsigned int numNewSamps){numSamps+=numNewSamps;};
104 
110  virtual unsigned int NumSamps() const{return numSamps;};
111 
112  void PrintStatus(std::string prefix) const;
113 
115  virtual std::shared_ptr<MarkovChain> GetSamples() const;
116 
118  std::shared_ptr<MarkovChain> GetQOIs() const;
119 
120 #if MUQ_HAS_PARCER
121  std::shared_ptr<parcer::Communicator> GetCommunicator() const;
122 #endif
123 
124  protected:
125 
126  virtual void Sample();
127 
128  std::shared_ptr<SamplingState> SaveSamples(std::vector<std::shared_ptr<SamplingState> > const& newStates,
129  std::shared_ptr<SamplingState> & lastSavedState,
130  unsigned int & sampNum) const;
131 
132  bool ShouldSave(unsigned int const sampNum) const;
133 
134  void PrintStatus(unsigned int currInd) const{PrintStatus("",currInd);};
135  void PrintStatus(std::string prefix, unsigned int currInd) const;
136 
137  // Samples and quantities of interest will be stored in the MarkovChain class
138  std::shared_ptr<MarkovChain> samples;
139  std::shared_ptr<MarkovChain> QOIs;
140 
141  std::shared_ptr<SaveSchedulerBase> scheduler;
142  std::shared_ptr<SaveSchedulerBase> schedulerQOI;
143 
144  unsigned int numSamps;
145  unsigned int burnIn;
146  unsigned int printLevel;
147 
148  // A vector of transition kernels: One for each block
149  std::vector<std::shared_ptr<TransitionKernel>> kernels;
150 
151  private:
152 
153 #if MUQ_HAS_PARCER
154  std::shared_ptr<parcer::Communicator> comm = nullptr;
155 #endif
156 
157  template<typename... Args>
158  inline std::shared_ptr<MarkovChain> RunRecurse(std::vector<Eigen::VectorXd>& vec, Eigen::VectorXd const& ith, Args const&... args) {
159  vec.push_back(ith);
160  return RunRecurse(vec, args...);
161  }
162 
163  inline std::shared_ptr<MarkovChain> RunRecurse(std::vector<Eigen::VectorXd>& vec, Eigen::VectorXd const& last) {
164  vec.push_back(last);
165  return Run(vec);
166  }
167 
168  inline std::shared_ptr<MarkovChain> RunRecurse(std::vector<Eigen::VectorXd>& vec) {
169  return Run(vec);
170  }
171 
172  template<typename... Args>
173  inline void SetStateRecurse(std::vector<Eigen::VectorXd>& vec, Eigen::VectorXd const& it, Args const&... args) {
174  vec.push_back(it);
175  SetStateRecurse(vec, args...);
176  }
177 
178  inline void SetStateRecurse(std::vector<Eigen::VectorXd>& vec, Eigen::VectorXd const& last) {
179  vec.push_back(last);
180  SetState(vec);
181  }
182 
183  unsigned int sampNum = 1;
184  std::shared_ptr<SamplingState> prevState = nullptr;
185  std::shared_ptr<SamplingState> lastSavedState = nullptr;
186  double totalTime = 0.0;
187 
188  void Setup(boost::property_tree::ptree pt,
189  std::vector<std::shared_ptr<TransitionKernel>> const& kernelsIn);
190 
191  void Setup(boost::property_tree::ptree pt, std::shared_ptr<AbstractSamplingProblem> const& problem);
192 
193  }; // class SingleChainMCMC
194  } // namespace SamplingAlgorithms
195 } // namespace muq
196 
197 #endif // #ifndef SINGLECHAINMCMC_H
Defines an MCMC sampler with a single chain.
bool ShouldSave(unsigned int const sampNum) const
SingleChainMCMC(boost::property_tree::ptree pt, std::shared_ptr< AbstractSamplingProblem > const &problem)
void SetState(Args const &... args)
virtual std::shared_ptr< MarkovChain > GetSamples() const
void SetStateRecurse(std::vector< Eigen::VectorXd > &vec, Eigen::VectorXd const &last)
std::shared_ptr< SamplingState > SaveSamples(std::vector< std::shared_ptr< SamplingState > > const &newStates, std::shared_ptr< SamplingState > &lastSavedState, unsigned int &sampNum) const
void Setup(boost::property_tree::ptree pt, std::shared_ptr< AbstractSamplingProblem > const &problem)
SingleChainMCMC(boost::property_tree::ptree pt, std::shared_ptr< parcer::Communicator > const &comm, std::vector< std::shared_ptr< TransitionKernel > > const &kernelsIn)
std::shared_ptr< parcer::Communicator > comm
std::shared_ptr< SamplingState > prevState
std::shared_ptr< SaveSchedulerBase > schedulerQOI
std::shared_ptr< MarkovChain > samples
void PrintStatus(unsigned int currInd) const
std::shared_ptr< MarkovChain > RunRecurse(std::vector< Eigen::VectorXd > &vec, Eigen::VectorXd const &last)
void Setup(boost::property_tree::ptree pt, std::vector< std::shared_ptr< TransitionKernel >> const &kernelsIn)
std::vector< std::shared_ptr< TransitionKernel > > kernels
virtual unsigned int NumSamps() const
std::shared_ptr< MarkovChain > Run(Args const &... args)
virtual void SetState(std::shared_ptr< SamplingState > const &x0)
Set the state of the MCMC chain.
std::shared_ptr< SaveSchedulerBase > scheduler
std::shared_ptr< parcer::Communicator > GetCommunicator() const
virtual std::vector< std::shared_ptr< TransitionKernel > > & Kernels()
SingleChainMCMC(boost::property_tree::ptree pt, std::shared_ptr< TransitionKernel > const &kernel)
std::shared_ptr< MarkovChain > QOIs
void PrintStatus(std::string prefix) const
virtual void AddNumSamps(unsigned int numNewSamps)
std::shared_ptr< MarkovChain > GetQOIs() const
SingleChainMCMC(boost::property_tree::ptree pt, std::shared_ptr< AbstractSamplingProblem > const &problem, std::shared_ptr< parcer::Communicator > const &comm)
std::shared_ptr< MarkovChain > RunRecurse(std::vector< Eigen::VectorXd > &vec, Eigen::VectorXd const &ith, Args const &... args)
std::shared_ptr< MarkovChain > RunRecurse(std::vector< Eigen::VectorXd > &vec)
std::shared_ptr< SamplingState > lastSavedState
void SetStateRecurse(std::vector< Eigen::VectorXd > &vec, Eigen::VectorXd const &it, Args const &... args)
Defines the transition kernel used by an MCMC algorithm.
const char * last
Definition: json.h:15399