MUQ  0.4.3
muq::SamplingAlgorithms::ParallelTempering Class Reference

Defines an MCMC sampler with multiple chains running on problems with different temperatues. More...

#include <ParallelTempering.h>

Detailed Description

Defines an MCMC sampler with multiple chains running on problems with different temperatues.

Configuration Parameters:

Parameter Key Type Default Value Description
"NumSamples" Int - The total number of steps (including burnin) to take, i.e., the length of the Markov chain with temperature 1.
"BurnIn" Int 0 The number of steps at the beginning of the chain to ignore.
"PrintLevel" Int 3 The amount of information to print to std::cout. Valid values are in [0,1,2,3] with 0 = Nothing, 3 = The most
"Kernel Lists" String - A semi-colon separated list of comma separated lists containing the name of other parameter blocks that define the transition kernels for each Metropolis-in-Gibbs block.
"Inverse Temperatures" String - A comma-separated list of floats containing the inverse temperature for each chain. Values of 0 correspond to the prior, values of 1 correspond to the posterior. The last value must be 1.
"Swap Increment" Int 2 How often to apply the swap kernel, which might exchange states between chains. The first swap occurs after "Swap Increment" steps have been taken. It is therefore possible to prevent any swaps from occuring by setting the swap increment to a value larger than the number of samples. For large number of chains, the DEO option will typically perform better than the SEO method. Similar efficiency is often observed for a small number of chains.
"Swap Type" String "DEO" The type of swapping mechanism to use. Current options are "DEO" and "SEO" corresponding to the variants described in [Syed2019]
"Adapt Start" Int 100 The number of steps to take before starting to adapt temperatures. Setting this value larger than NumSamples will disable adaptation.

Definition at line 32 of file ParallelTempering.h.

Public Member Functions

 ParallelTempering (boost::property_tree::ptree opts, std::shared_ptr< InferenceProblem > const &problem)
 
 ParallelTempering (boost::property_tree::ptree opts, Eigen::VectorXd inverseTemps, std::vector< std::shared_ptr< TransitionKernel >> kernels)
 
 ParallelTempering (boost::property_tree::ptree opts, Eigen::VectorXd inverseTemps, std::vector< std::vector< std::shared_ptr< TransitionKernel >>> kernels)
 
void SetState (std::vector< std::shared_ptr< SamplingState >> const &x0)
 Set the state of the MCMC chain. More...
 
void SetState (std::vector< Eigen::VectorXd > const &x0)
 
void SetState (std::vector< std::vector< Eigen::VectorXd >> const &x0)
 
template<typename... Args>
void SetState (Args const &... args)
 
double GetInverseTemp (unsigned int chainInd) const
 
std::vector< std::shared_ptr< TransitionKernel > > const & Kernels (unsigned int chainInd) const
 
std::shared_ptr< MarkovChainRun ()
 
std::shared_ptr< MarkovChainRun (Eigen::VectorXd const &x0)
 
std::shared_ptr< MarkovChainRun (std::vector< Eigen::VectorXd > const &x0)
 
std::shared_ptr< MarkovChainRun (std::vector< std::vector< Eigen::VectorXd >> const &initialPoints)
 
void AddNumSamps (unsigned int numNewSamps)
 
unsigned int NumSamps () const
 
std::shared_ptr< MarkovChainGetSamples () const
 
std::shared_ptr< MarkovChainGetQOIs () const
 

Public Attributes

const unsigned int numTemps
 Number of temperatures in the temperature schedule. More...
 

Constructor & Destructor Documentation

◆ ParallelTempering() [1/3]

ParallelTempering::ParallelTempering ( boost::property_tree::ptree  opts,
std::shared_ptr< InferenceProblem > const &  problem 
)

Definition at line 19 of file ParallelTempering.cpp.

◆ ParallelTempering() [2/3]

ParallelTempering::ParallelTempering ( boost::property_tree::ptree  opts,
Eigen::VectorXd  inverseTemps,
std::vector< std::shared_ptr< TransitionKernel >>  kernels 
)

Definition at line 23 of file ParallelTempering.cpp.

◆ ParallelTempering() [3/3]

ParallelTempering::ParallelTempering ( boost::property_tree::ptree  opts,
Eigen::VectorXd  inverseTemps,
std::vector< std::vector< std::shared_ptr< TransitionKernel >>>  kernels 
)

Definition at line 30 of file ParallelTempering.cpp.

References chains, kernels, problems, and sampNums.

Member Function Documentation

◆ AdaptTemperatures()

void ParallelTempering::AdaptTemperatures ( )
protected

Adapts the temperatures according to the procedure outlined in Section 5 of [Syed2019].

Definition at line 206 of file ParallelTempering.cpp.

References attemptedSwaps, CollectInverseTemps(), cumulativeSwapProb, nlohmann::detail::dtoa_impl::e, numTemps, problems, successfulSwaps, and nlohmann::detail::dtoa_impl::w.

Referenced by Run().

◆ AddNumSamps()

void muq::SamplingAlgorithms::ParallelTempering::AddNumSamps ( unsigned int  numNewSamps)
inline

When the Run method is called, the Sample() method is called until the total number of samples generated by this class is equal to a private member variable numSamps, which is generally set in the options passed to the constructor. In order to generate more samples after an initial call to Run, the numSamps variables needs to be increasd. This function essentially sets numSamps=numSamps+numNewSamps.

Typical usage will be something like:

std::shared_ptr<SampleCollection> samps = mcmc->Run(startPt);
for(unsigned int batchInd=1; batchInd<numBatches; ++batchInd){
mcmc->AddNumSamps(batchSizes);
mcmc->Run();
}
Parameters
[in]numNewSampsThe number of new samples we want to add to numSamps. After calling this function, the next call to Run will add an additional numNewSamps to the SampleCollection.

Definition at line 117 of file ParallelTempering.h.

References numSamps.

◆ CheckForMeta()

void ParallelTempering::CheckForMeta ( std::shared_ptr< SamplingState > const &  state)
staticprivate

Checks a sampling state to make sure it has the metadata necessary to swap states.

Definition at line 292 of file ParallelTempering.cpp.

Referenced by SwapStates().

◆ CollectInverseTemps()

Eigen::VectorXd ParallelTempering::CollectInverseTemps ( ) const
protected

Definition at line 255 of file ParallelTempering.cpp.

References numTemps, and problems.

Referenced by AdaptTemperatures(), and PrintStatus().

◆ ExtractKernels()

std::vector< std::vector< std::shared_ptr< TransitionKernel > > > ParallelTempering::ExtractKernels ( boost::property_tree::ptree  opts,
std::shared_ptr< InferenceProblem > const &  prob 
)
staticprivate

◆ ExtractTemps()

Eigen::VectorXd ParallelTempering::ExtractTemps ( boost::property_tree::ptree  opts)
staticprivate

Definition at line 453 of file ParallelTempering.cpp.

References muq::Utilities::StringUtilities::Split().

◆ GetInverseTemp()

double ParallelTempering::GetInverseTemp ( unsigned int  chainInd) const

Returns the inverse temperature of one of the chains.

Parameters
[in]chainIndThe index of the chain.
Returns
The inverse temperature that multiplies the log-likelihood in this chain

Definition at line 139 of file ParallelTempering.cpp.

References problems.

◆ GetQOIs()

std::shared_ptr<MarkovChain> muq::SamplingAlgorithms::ParallelTempering::GetQOIs ( ) const
inline

Returns the Quantities of Interest (if any) computed so far.

Definition at line 130 of file ParallelTempering.h.

References numTemps, and QOIs.

◆ GetSamples()

std::shared_ptr<MarkovChain> muq::SamplingAlgorithms::ParallelTempering::GetSamples ( ) const
inline

Returns the samples generated by the algorithm so far.

Definition at line 127 of file ParallelTempering.h.

References chains, and numTemps.

◆ Kernels()

std::vector< std::shared_ptr< TransitionKernel > > const & ParallelTempering::Kernels ( unsigned int  chainInd) const

Returns the transition kernel used by one of the parallel chains @params[in] chainInd The index of the chain.

Definition at line 144 of file ParallelTempering.cpp.

References kernels.

◆ NumSamps()

unsigned int muq::SamplingAlgorithms::ParallelTempering::NumSamps ( ) const
inline

Returns the current value of the private numSamps variable. When Run is called, it calls the Sample function until this number of samples has been generated. Note that unless numSamps is updated by calling AddNumSamps, subsequent calls to Run will not produce any new samples.

Definition at line 124 of file ParallelTempering.h.

References numSamps.

◆ PrintStatus() [1/2]

void ParallelTempering::PrintStatus ( std::string  prefix,
unsigned int  currInd 
) const
protected

◆ PrintStatus() [2/2]

void muq::SamplingAlgorithms::ParallelTempering::PrintStatus ( unsigned int  currInd) const
inlineprotected

Definition at line 142 of file ParallelTempering.h.

References PrintStatus().

Referenced by PrintStatus(), and Run().

◆ Run() [1/4]

std::shared_ptr< MarkovChain > ParallelTempering::Run ( )

Runs each parallel chain, starting from the current state. Before this method is called, the state should be set using either the SetState function or another version of the Run method.

Definition at line 150 of file ParallelTempering.cpp.

Referenced by Run().

◆ Run() [2/4]

std::shared_ptr<MarkovChain> muq::SamplingAlgorithms::ParallelTempering::Run ( Eigen::VectorXd const &  x0)
inline

Definition at line 82 of file ParallelTempering.h.

References numTemps, and Run().

Referenced by Run().

◆ Run() [3/4]

std::shared_ptr< MarkovChain > ParallelTempering::Run ( std::vector< Eigen::VectorXd > const &  x0)

Runs each parallel chain. Assumes each chain operates on a density with a single input.

Definition at line 152 of file ParallelTempering.cpp.

References Run(), and StackObjects().

◆ Run() [4/4]

std::shared_ptr< MarkovChain > ParallelTempering::Run ( std::vector< std::vector< Eigen::VectorXd >> const &  initialPoints)

Runs each parallel chain. Starts each chain at a different initial point.

Definition at line 156 of file ParallelTempering.cpp.

References adaptIncr, AdaptTemperatures(), chains, nextAdaptInd, numSamps, numTemps, printLevel, PrintStatus(), Sample(), sampNums, SetState(), swapIncr, SwapStates(), and totalTime.

◆ Sample()

void ParallelTempering::Sample ( )
protected

Takes one "step" with each kernel. Updates the prevStates member variable and saves states to the chains variable.

Definition at line 354 of file ParallelTempering.cpp.

References kernels, numTemps, prevStates, sampNums, SaveSamples(), and totalTime.

Referenced by Run().

◆ SaveSamples()

void ParallelTempering::SaveSamples ( std::vector< std::vector< std::shared_ptr< SamplingState >>> const &  newStates)
protected

Definition at line 411 of file ParallelTempering.cpp.

References chains, numSamps, numTemps, prevStates, QOIs, sampNums, and ShouldSave().

Referenced by Sample().

◆ SetState() [1/4]

template<typename... Args>
void muq::SamplingAlgorithms::ParallelTempering::SetState ( Args const &...  args)
inline

Definition at line 59 of file ParallelTempering.h.

◆ SetState() [2/4]

void ParallelTempering::SetState ( std::vector< Eigen::VectorXd > const &  x0)

Definition at line 114 of file ParallelTempering.cpp.

References numTemps, and SetState().

◆ SetState() [3/4]

void ParallelTempering::SetState ( std::vector< std::shared_ptr< SamplingState >> const &  x0)

Set the state of the MCMC chain.

If no steps have been taken, this function sets the starting point.

Definition at line 102 of file ParallelTempering.cpp.

References numTemps, and prevStates.

Referenced by Run(), and SetState().

◆ SetState() [4/4]

void ParallelTempering::SetState ( std::vector< std::vector< Eigen::VectorXd >> const &  x0)

Definition at line 123 of file ParallelTempering.cpp.

References numTemps, and SetState().

◆ ShouldSave()

bool ParallelTempering::ShouldSave ( unsigned int  chainInd,
unsigned int  sampNum 
) const
protected

Returns true if a sample of a particular chain should be saved.

Definition at line 494 of file ParallelTempering.cpp.

References burnIn, and scheduler.

Referenced by SaveSamples().

◆ StackKernels()

std::vector< std::vector< std::shared_ptr< TransitionKernel > > > ParallelTempering::StackKernels ( std::vector< std::shared_ptr< TransitionKernel >> const &  kerns)
staticprivate

Definition at line 443 of file ParallelTempering.cpp.

◆ StackObjects()

template<typename T >
static std::vector<std::vector<T> > muq::SamplingAlgorithms::ParallelTempering::StackObjects ( std::vector< T > const &  kerns)
inlinestaticprivate

Definition at line 198 of file ParallelTempering.h.

Referenced by Run().

◆ SwapStates()

void ParallelTempering::SwapStates ( )
protected

Swap states between chains. Updates the prevStates private member variable.

Definition at line 313 of file ParallelTempering.cpp.

References attemptedSwaps, CheckForMeta(), cumulativeSwapProb, evenSwap, numTemps, prevStates, problems, seoSwaps, and successfulSwaps.

Referenced by Run().

Member Data Documentation

◆ adaptIncr

unsigned int muq::SamplingAlgorithms::ParallelTempering::adaptIncr = 2
protected

Definition at line 176 of file ParallelTempering.h.

Referenced by Run().

◆ attemptedSwaps

Eigen::VectorXd muq::SamplingAlgorithms::ParallelTempering::attemptedSwaps
protected

Definition at line 139 of file ParallelTempering.h.

Referenced by AdaptTemperatures(), PrintStatus(), and SwapStates().

◆ burnIn

unsigned int muq::SamplingAlgorithms::ParallelTempering::burnIn
protected

Definition at line 172 of file ParallelTempering.h.

Referenced by ShouldSave().

◆ chains

std::vector<std::shared_ptr<MarkovChain> > muq::SamplingAlgorithms::ParallelTempering::chains
protected

Definition at line 165 of file ParallelTempering.h.

Referenced by GetSamples(), ParallelTempering(), Run(), and SaveSamples().

◆ cumulativeSwapProb

Eigen::VectorXd muq::SamplingAlgorithms::ParallelTempering::cumulativeSwapProb
protected

Definition at line 137 of file ParallelTempering.h.

Referenced by AdaptTemperatures(), PrintStatus(), and SwapStates().

◆ evenSwap

bool muq::SamplingAlgorithms::ParallelTempering::evenSwap = true
private

Definition at line 188 of file ParallelTempering.h.

Referenced by SwapStates().

◆ kernels

std::vector<std::vector<std::shared_ptr<TransitionKernel> > > muq::SamplingAlgorithms::ParallelTempering::kernels
protected

A vector of transition kernels: One for each block kernels[chainInd][blockInd]

Definition at line 182 of file ParallelTempering.h.

Referenced by ExtractKernels(), Kernels(), ParallelTempering(), PrintStatus(), and Sample().

◆ nextAdaptInd

unsigned int muq::SamplingAlgorithms::ParallelTempering::nextAdaptInd
protected

Definition at line 177 of file ParallelTempering.h.

Referenced by Run().

◆ numSamps

unsigned int muq::SamplingAlgorithms::ParallelTempering::numSamps
protected

Definition at line 171 of file ParallelTempering.h.

Referenced by AddNumSamps(), NumSamps(), PrintStatus(), Run(), and SaveSamples().

◆ numTemps

const unsigned int muq::SamplingAlgorithms::ParallelTempering::numTemps

Number of temperatures in the temperature schedule.

Definition at line 133 of file ParallelTempering.h.

Referenced by AdaptTemperatures(), CollectInverseTemps(), GetQOIs(), GetSamples(), PrintStatus(), Run(), Sample(), SaveSamples(), SetState(), and SwapStates().

◆ prevStates

std::vector<std::shared_ptr<SamplingState> > muq::SamplingAlgorithms::ParallelTempering::prevStates
protected

Definition at line 184 of file ParallelTempering.h.

Referenced by Sample(), SaveSamples(), SetState(), and SwapStates().

◆ printLevel

unsigned int muq::SamplingAlgorithms::ParallelTempering::printLevel
protected

Definition at line 173 of file ParallelTempering.h.

Referenced by PrintStatus(), and Run().

◆ problems

std::vector<std::shared_ptr<InferenceProblem> > muq::SamplingAlgorithms::ParallelTempering::problems
protected

◆ QOIs

std::vector<std::shared_ptr<MarkovChain> > muq::SamplingAlgorithms::ParallelTempering::QOIs
protected

Definition at line 166 of file ParallelTempering.h.

Referenced by GetQOIs(), and SaveSamples().

◆ sampNums

std::vector<unsigned int> muq::SamplingAlgorithms::ParallelTempering::sampNums
private

Definition at line 190 of file ParallelTempering.h.

Referenced by ParallelTempering(), Run(), Sample(), and SaveSamples().

◆ scheduler

std::shared_ptr<SaveSchedulerBase> muq::SamplingAlgorithms::ParallelTempering::scheduler
protected

Definition at line 168 of file ParallelTempering.h.

Referenced by ShouldSave().

◆ schedulerQOI

std::shared_ptr<SaveSchedulerBase> muq::SamplingAlgorithms::ParallelTempering::schedulerQOI
protected

Definition at line 169 of file ParallelTempering.h.

◆ seoSwaps

bool muq::SamplingAlgorithms::ParallelTempering::seoSwaps = false
protected

Definition at line 175 of file ParallelTempering.h.

Referenced by SwapStates().

◆ successfulSwaps

Eigen::VectorXd muq::SamplingAlgorithms::ParallelTempering::successfulSwaps
protected

Definition at line 138 of file ParallelTempering.h.

Referenced by AdaptTemperatures(), and SwapStates().

◆ swapIncr

unsigned int muq::SamplingAlgorithms::ParallelTempering::swapIncr
protected

Definition at line 174 of file ParallelTempering.h.

Referenced by Run().

◆ totalTime

double muq::SamplingAlgorithms::ParallelTempering::totalTime = 0.0
private

Definition at line 191 of file ParallelTempering.h.

Referenced by Run(), and Sample().


The documentation for this class was generated from the following files: