versatile-mcmc  0.1.7
Classes | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
vmcmc::MetropolisHastings Class Reference

Implementation of the Metropolis-Hastings algorithm. More...

#include <metropolis.hpp>

Inheritance diagram for vmcmc::MetropolisHastings:
Inheritance graph
[legend]
Collaboration diagram for vmcmc::MetropolisHastings:
Collaboration graph
[legend]

Classes

struct  ChainConfig
 Private class encapsulating Sample chains for parallel tempering. More...
 

Public Member Functions

virtual void Initialize () override
 
virtual void Advance (size_t nSteps=1) override
 
virtual void Finalize () override
 
virtual size_t NumberOfChains () override
 
virtual const Chain & GetChain (size_t cIndex=0) override
 
void SetNumberOfChains (size_t nChains)
 
template<typename ContainerT = std::initializer_list<double>>
void SetBetas (ContainerT betas)
 
const std::vector< double > & GetBetas () const
 
template<typename ProposalT , typename... ArgsT>
void SetProposalFunction (ArgsT &&...args)
 
void SetProposalFunction (std::shared_ptr< Proposal > proposalFunction)
 
std::shared_ptr< ProposalGetProposalFunction ()
 
std::shared_ptr< const ProposalGetProposalFunction () const
 
void SetRandomizeStartPoint (bool randomizeStartPoint)
 
bool IsRandomizeStartPoint () const
 
void SetMultiThreading (bool enable)
 
bool IsMultiThreading () const
 
double GetSwapAcceptanceRate (size_t iChain, ptrdiff_t iBeta=-1) const
 Get the fraction of accepted swaps between tempered chains. More...
 
- Public Member Functions inherited from vmcmc::Algorithm
void SetParameterConfig (const ParameterConfig &paramConfig)
 Set the parameter configuration. More...
 
const ParameterConfigGetParameterConfig () const
 
void SetPrior (DefaultCallable prior)
 
template<size_t NParams, typename CallableT >
void SetPrior (CallableT prior)
 
void SetLikelihood (DefaultCallable likelihood)
 
template<size_t NParams, typename CallableT >
void SetLikelihood (CallableT likelihood)
 
void SetNegLogLikelihood (DefaultCallable negLoglikelihood)
 
template<size_t NParams, typename CallableT >
void SetNegLogLikelihood (CallableT negLoglikelihood)
 
void SetTotalLength (size_t length)
 
size_t GetTotalLength () const
 
template<typename WriterT , typename... ArgsT>
void AddWriter (ArgsT &&...args)
 Add an output writer by specifying its type and passing constructor arguments. More...
 
void AddWriter (std::shared_ptr< Writer > writer)
 
double EvaluatePrior (const std::vector< double > &paramValues) const
 Evaluate the prior for the given parameter values. More...
 
double EvaluateLikelihood (const std::vector< double > &paramValues) const
 Evaluate the target function likelihood for the given parameter values. More...
 
double EvaluateNegLogLikelihood (const std::vector< double > &paramValues) const
 Evaluate the target function -log(likelihood) for the given parameter values. More...
 
bool Evaluate (Sample &sample) const
 Evalutate the target function prior, likelihood and -log(likelihood) at the position defined by the sample, and update the sample accordingly. More...
 
void Run ()
 Start sampling!
 
ChainSetStatisticsGetStatistics ()
 
const ChainSetStatisticsGetStatistics () const
 

Static Public Member Functions

static double CalculateMHRatio (const Sample &prevState, const Sample &nextState, double proposalAsymmetry=1.0, double beta=1.0)
 

Protected Member Functions

void AdvanceChainConfig (size_t iChainConfig, size_t iBeta, size_t nSteps=1)
 
void ProposePtSwapping (size_t iChainConfig)
 

Protected Attributes

bool fRandomizeStartPoint
 
std::vector< double > fBetas
 
std::shared_ptr< ProposalfProposalFunction
 
size_t fPtFrequency
 
std::vector< std::unique_ptr< ChainConfig > > fChainConfigs
 
- Protected Attributes inherited from vmcmc::Algorithm
ParameterConfig fParameterConfig
 
DefaultCallable fPrior
 
DefaultCallable fLikelihood
 
DefaultCallable fNegLogLikelihood
 
size_t fTotalLength
 
size_t fCycleLength
 
std::vector< std::shared_ptr< Writer > > fWriters
 
ChainSetStatistics fStatistics
 

Additional Inherited Members

- Public Types inherited from vmcmc::Algorithm
using DefaultCallable = std::function< double(const std::vector< double > &)>
 

Detailed Description

Implementation of the Metropolis-Hastings algorithm.

The transition kernel alias proposal function can be defined by the user according to the interface of #Proposal. By default, a multivariate Gaussian is used, based on the parameter configuration.

Parallel tempering (useful for multimodal likelihoods) can be activated by specifying additional values for beta < 1 (reciprocal temperature), thus constructing flatter distributions. For each value of beta, an individual sampling chain, together with it's own parameter configuration and proposal function is set up.

Member Function Documentation

void vmcmc::MetropolisHastings::Advance ( size_t  nSteps = 1)
overridevirtual

In case of multi-core parallelization, all active chains (number of chain sets * number of PT beta values) are progressed in parallel by nSteps each.

Implements vmcmc::Algorithm.

double vmcmc::MetropolisHastings::GetSwapAcceptanceRate ( size_t  iChain,
ptrdiff_t  iBeta = -1 
) const

Get the fraction of accepted swaps between tempered chains.

Parameters
iChainIndex of the chain set.
iBetaIndex of the tempered chain pair. If < 0, average over all pairs.
Returns

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