MUQ  0.4.3
ParallelTempering.h
Go to the documentation of this file.
1 #ifndef PARALLELTEMPERING_H
2 #define PARALLELTEMPERING_H
3 
8 
9 #include <vector>
10 
11 #include <boost/property_tree/ptree.hpp>
12 
13 namespace muq{
14  namespace SamplingAlgorithms{
15 
33  {
34 
35  public:
36 
37 
38  ParallelTempering(boost::property_tree::ptree opts,
39  std::shared_ptr<InferenceProblem> const& problem);
40 
41  ParallelTempering(boost::property_tree::ptree opts,
42  Eigen::VectorXd inverseTemps,
43  std::vector<std::shared_ptr<TransitionKernel>> kernels);
44 
45  ParallelTempering(boost::property_tree::ptree opts,
46  Eigen::VectorXd inverseTemps,
47  std::vector<std::vector<std::shared_ptr<TransitionKernel>>> kernels);
48 
49 
51 
54  void SetState(std::vector<std::shared_ptr<SamplingState>> const& x0);
55  void SetState(std::vector<Eigen::VectorXd> const& x0);
56  void SetState(std::vector<std::vector<Eigen::VectorXd>> const& x0);
57 
58  template<typename... Args>
59  inline void SetState(Args const&... args) {
60  std::vector<Eigen::VectorXd> vec;
61  SetStateRecurse(vec, args...);
62  }
63 
64 
69  double GetInverseTemp(unsigned int chainInd) const;
70 
74  std::vector<std::shared_ptr<TransitionKernel>> const& Kernels(unsigned int chainInd) const;
75 
76 
80  std::shared_ptr<MarkovChain> Run();
81 
82  std::shared_ptr<MarkovChain> Run(Eigen::VectorXd const& x0){return Run(std::vector<Eigen::VectorXd>(numTemps,x0));}
83 
86  std::shared_ptr<MarkovChain> Run(std::vector<Eigen::VectorXd> const& x0);
87 
89  std::shared_ptr<MarkovChain> Run(std::vector<std::vector<Eigen::VectorXd>> const& initialPoints);
90 
91 
92  //double TotalTime() { return totalTime; }
93 
117  void AddNumSamps(unsigned int numNewSamps){numSamps+=numNewSamps;};
118 
124  unsigned int NumSamps() const{return numSamps;};
125 
127  std::shared_ptr<MarkovChain> GetSamples() const{return chains.at(numTemps-1);}
128 
130  std::shared_ptr<MarkovChain> GetQOIs() const{return QOIs.at(numTemps-1);}
131 
133  const unsigned int numTemps;
134 
135  protected:
136 
137  Eigen::VectorXd cumulativeSwapProb; // A running sum of the swap probabilities
138  Eigen::VectorXd successfulSwaps; // The number of succesfull swaps between chains i and i+1
139  Eigen::VectorXd attemptedSwaps; // The number of attempted swaps between chains i and i+1
140 
141 
142  void PrintStatus(unsigned int currInd) const{PrintStatus("",currInd);};
143  void PrintStatus(std::string prefix, unsigned int currInd) const;
144 
146  void AdaptTemperatures();
147 
149  void Sample();
150 
152  void SwapStates();
153 
154  void SaveSamples(std::vector<std::vector<std::shared_ptr<SamplingState>>> const& newStates);
155 
159  bool ShouldSave(unsigned int chainInd, unsigned int sampNum) const;
160 
161  Eigen::VectorXd CollectInverseTemps() const;
162 
163  // Samples and quantities of interest will be stored in the MarkovChain class
164  std::vector<std::shared_ptr<InferenceProblem>> problems;
165  std::vector<std::shared_ptr<MarkovChain>> chains;
166  std::vector<std::shared_ptr<MarkovChain>> QOIs;
167 
168  std::shared_ptr<SaveSchedulerBase> scheduler;
169  std::shared_ptr<SaveSchedulerBase> schedulerQOI;
170 
171  unsigned int numSamps;
172  unsigned int burnIn;
173  unsigned int printLevel;
174  unsigned int swapIncr;
175  bool seoSwaps = false; // True if stochastic "SEO" swaps of Syed et al. 2019 should be used. Otherwise the deterministic DEO swaps are employed.
176  unsigned int adaptIncr = 2;
177  unsigned int nextAdaptInd;
178 
182  std::vector<std::vector<std::shared_ptr<TransitionKernel>>> kernels;
183 
184  std::vector<std::shared_ptr<SamplingState>> prevStates;
185 
186  private:
187 
188  bool evenSwap = true;
189 
190  std::vector<unsigned int> sampNums;
191  double totalTime = 0.0;
192 
193  static std::vector<std::vector<std::shared_ptr<TransitionKernel>>> StackKernels(std::vector<std::shared_ptr<TransitionKernel>> const& kerns);
194  static Eigen::VectorXd ExtractTemps(boost::property_tree::ptree opts);
195  static std::vector<std::vector<std::shared_ptr<TransitionKernel>>> ExtractKernels(boost::property_tree::ptree opts, std::shared_ptr<InferenceProblem> const& prob);
196 
197  template<typename T>
198  static std::vector<std::vector<T>> StackObjects(std::vector<T> const& kerns)
199  {
200  std::vector<std::vector<T>> newKernels(kerns.size(), std::vector<T>(1));
201  for(unsigned int i=0; i<kerns.size(); ++i)
202  newKernels.at(i).at(0) = kerns.at(i);
203  return newKernels;
204  }
205 
207  static void CheckForMeta(std::shared_ptr<SamplingState> const& state);
208 
209 
210 
211  // void Setup(boost::property_tree::ptree pt,
212  // std::vector<std::shared_ptr<TransitionKernel>> const& kernelsIn);
213 
214  // void Setup(boost::property_tree::ptree pt, std::shared_ptr<AbstractSamplingProblem> const& problem);
215 
216  }; // class ParallelTempering
217 
218  } // namespace SamplingAlgorithms
219 } // namespace muq
220 
221 #endif // #ifndef SINGLECHAINMCMC_H
Defines an MCMC sampler with multiple chains running on problems with different temperatues.
std::vector< std::vector< std::shared_ptr< TransitionKernel > > > kernels
const unsigned int numTemps
Number of temperatures in the temperature schedule.
std::vector< std::shared_ptr< MarkovChain > > chains
std::shared_ptr< MarkovChain > GetQOIs() const
void SaveSamples(std::vector< std::vector< std::shared_ptr< SamplingState >>> const &newStates)
double GetInverseTemp(unsigned int chainInd) const
void SetState(std::vector< std::shared_ptr< SamplingState >> const &x0)
Set the state of the MCMC chain.
std::shared_ptr< MarkovChain > Run(Eigen::VectorXd const &x0)
void AddNumSamps(unsigned int numNewSamps)
static std::vector< std::vector< std::shared_ptr< TransitionKernel > > > StackKernels(std::vector< std::shared_ptr< TransitionKernel >> const &kerns)
std::shared_ptr< SaveSchedulerBase > scheduler
std::shared_ptr< SaveSchedulerBase > schedulerQOI
static Eigen::VectorXd ExtractTemps(boost::property_tree::ptree opts)
void AdaptTemperatures()
Adapts the temperatures according to the procedure outlined in Section 5 of .
std::vector< std::shared_ptr< SamplingState > > prevStates
void PrintStatus(unsigned int currInd) const
std::vector< std::shared_ptr< MarkovChain > > QOIs
static std::vector< std::vector< std::shared_ptr< TransitionKernel > > > ExtractKernels(boost::property_tree::ptree opts, std::shared_ptr< InferenceProblem > const &prob)
std::vector< std::shared_ptr< TransitionKernel > > const & Kernels(unsigned int chainInd) const
std::shared_ptr< MarkovChain > Run()
ParallelTempering(boost::property_tree::ptree opts, std::shared_ptr< InferenceProblem > const &problem)
std::shared_ptr< MarkovChain > GetSamples() const
static void CheckForMeta(std::shared_ptr< SamplingState > const &state)
Checks a sampling state to make sure it has the metadata necessary to swap states.
bool ShouldSave(unsigned int chainInd, unsigned int sampNum) const
Returns true if a sample of a particular chain should be saved.
static std::vector< std::vector< T > > StackObjects(std::vector< T > const &kerns)
std::vector< std::shared_ptr< InferenceProblem > > problems