CMP++: Uncertainty Quantification & Bayesian Calibration
Loading...
Searching...
No Matches
cmp::mcmc::MarkovChain< ProposalType > Class Template Reference

Represents a single Markov Chain utilizing Delayed Rejection Adaptive Metropolis (DRAM) for sampling. More...

#include <mcmc.h>

Public Member Functions

 MarkovChain (const ProposalType &proposal, std::default_random_engine &rng, const double &targetAcceptanceRatio=0.234)
 Construct a new mcmc chain object.
 
 MarkovChain ()=default
 
 ~MarkovChain ()=default
 
void increaseSteps ()
 Increase the number of steps.
 
bool accept (const Eigen::Ref< const Eigen::VectorXd > &par, double score)
 Accept or reject a candidate.
 
void step (const score_t &getScore, const bool &delayedRejection=false, const double &gamma=0.1)
 Perform a single MCMC step.
 
void update ()
 Updates the value of the mean and covariance matrix.
 
Eigen::MatrixXd getAdaptedCovariance () const
 Get a covariance matrix adapted to the samples.
 
void reset ()
 
Eigen::VectorXd getCurrent () const
 Gets the current parameters of the chain.
 
double getScore () const
 Gets the current log probability score of the chain.
 
size_t getDim () const
 Gets the dimensionality of the parameter space.
 
size_t getSteps () const
 Gets the total number of steps run in this chain.
 
double getAcceptanceRatio () const
 Gets the current acceptance ratio of proposals.
 
Eigen::VectorXd getMean () const
 Gets the running mean of the parameter samples.
 
Eigen::MatrixXd getCovariance () const
 Gets the running covariance of the parameter samples.
 
void info () const
 

Protected Attributes

double score_ {0.0}
 Current log probability score value.
 
size_t dim_
 Dimension of parameter space.
 
size_t nSteps_ {0}
 Number of steps taken.
 
size_t nAccepts_ {0}
 Number of accepted proposals.
 
Eigen::VectorXd mean_
 Parameter sample running mean vector.
 
Eigen::MatrixXd cov_
 Parameter sample running covariance matrix.
 
ProposalType proposal_
 Proposal distribution model (copied by value).
 
std::uniform_real_distribution< double > distU_ {0.0, 1.0}
 Uniform distribution helper.
 
std::default_random_engine rng_
 Random engine.
 
double scale_
 Proposal covariance scale parameter.
 
double targetAcceptanceRatio_
 Target acceptance ratio for adaptive tuning.
 

Detailed Description

template<typename ProposalType>
class cmp::mcmc::MarkovChain< ProposalType >

Represents a single Markov Chain utilizing Delayed Rejection Adaptive Metropolis (DRAM) for sampling.

Template Parameters
ProposalTypeThe class type of the proposal distribution used to generate candidate states.

Mathematical Foundations & DRAM Algorithm

This class implements Delayed Rejection Adaptive Metropolis (DRAM), combining adaptive covariance tuning with multi-stage candidate proposals:

  1. Stage 1 (Adaptive Metropolis): A candidate state \(\boldsymbol{\theta}^*\) is proposed from a Gaussian distribution centered at the current state \(\boldsymbol{\theta}^{(t)}\) with scaling covariance \(\boldsymbol{\Sigma}_t\):

    \[ \boldsymbol{\theta}^* \sim \mathcal{N}\left(\boldsymbol{\theta}^{(t)}, \gamma_1 \boldsymbol{\Sigma}_t\right) \]

    The Stage 1 acceptance probability is:

    \[ \alpha_1(\boldsymbol{\theta}^{(t)}, \boldsymbol{\theta}^*) = \min\left(1, \frac{\pi(\boldsymbol{\theta}^*)}{\pi(\boldsymbol{\theta}^{(t)})}\right) \]

    where \(\pi(\boldsymbol{\theta})\) is the target density.
  2. Stage 2 (Delayed Rejection): If the Stage 1 proposal is rejected, a Stage 2 candidate \(\boldsymbol{\theta}^{**}\) is drawn from a narrower proposal distribution:

    \[ \boldsymbol{\theta}^{**} \sim \mathcal{N}\left(\boldsymbol{\theta}^{(t)}, \gamma_2 \boldsymbol{\Sigma}_t\right) \]

    with \(\gamma_2 < \gamma_1\). To preserve detailed balance, the Stage 2 acceptance probability must account for the first-stage rejection:

    \[ \alpha_2(\boldsymbol{\theta}^{(t)}, \boldsymbol{\theta}^*, \boldsymbol{\theta}^{**}) = \min\left(1, \frac{\pi(\boldsymbol{\theta}^{**}) q_1(\boldsymbol{\theta}^* | \boldsymbol{\theta}^{**}) [1 - \alpha_1(\boldsymbol{\theta}^{**}, \boldsymbol{\theta}^*)]}{\pi(\boldsymbol{\theta}^{(t)}) q_1(\boldsymbol{\theta}^* | \boldsymbol{\theta}^{(t)}) [1 - \alpha_1(\boldsymbol{\theta}^{(t)}, \boldsymbol{\theta}^*)]}\right) \]

    where \(q_1(\mathbf{x} | \mathbf{y})\) is the probability density of proposing \(\mathbf{x}\) given current state \(\mathbf{y}\) under the Stage-1 covariance.
  3. Adaptive Covariance Scaling: The proposal covariance \(\boldsymbol{\Sigma}_t\) is updated recursively using the sample covariance of the history of the chain:

    \[ \boldsymbol{\Sigma}_t = s_d \mathrm{Cov}\left(\boldsymbol{\theta}^{(1)}, \dots, \boldsymbol{\theta}^{(t)}\right) + s_d \epsilon \mathbf{I}_d \]

    where \(s_d = \frac{2.4^2}{d}\) is the optimal scaling factor for \(d\)-dimensional Gaussian targets, and \(\epsilon > 0\) is a regularization term.

Implementation Algorithms

  • Triangular Backsubstitution: The multivariate proposals evaluate Mahalanobis distances \((\mathbf{x}-\mathbf{y})^T \boldsymbol{\Sigma}^{-1} (\mathbf{x}-\mathbf{y})\) using LDLT factorization.
  • Robbins-Monro Tuning: Scale factor \(s_d\) is tuned dynamically to match the target acceptance rate:

    \[ \log s_{t+1} = \log s_t + \eta_t (\alpha_{1} - \alpha_{\text{target}}) \]

Constraints & Invariants

  • Detailed Balance: The multi-stage rejection ratio calculation must use log-space subtraction to prevent numerical underflow.
  • Positive Definiteness: Regularization nugget \(\epsilon\) must be added to the diagonal of the covariance matrix to guarantee invertibility.

Constructor & Destructor Documentation

◆ MarkovChain() [1/2]

template<typename ProposalType >
cmp::mcmc::MarkovChain< ProposalType >::MarkovChain ( const ProposalType &  proposal,
std::default_random_engine &  rng,
const double &  targetAcceptanceRatio = 0.234 
)
inline

Construct a new mcmc chain object.

Parameters
proposalThe proposal distribution (copied by value)
rngthe random number generator
targetAcceptanceRatiothe target acceptance ratio

◆ MarkovChain() [2/2]

template<typename ProposalType >
cmp::mcmc::MarkovChain< ProposalType >::MarkovChain ( )
default

◆ ~MarkovChain()

template<typename ProposalType >
cmp::mcmc::MarkovChain< ProposalType >::~MarkovChain ( )
default

Member Function Documentation

◆ accept()

template<typename ProposalType >
bool cmp::mcmc::MarkovChain< ProposalType >::accept ( const Eigen::Ref< const Eigen::VectorXd > &  par,
double  score 
)
inline

Accept or reject a candidate.

Parameters
parThe candidate
scoreThe score of the candidate

◆ getAcceptanceRatio()

template<typename ProposalType >
double cmp::mcmc::MarkovChain< ProposalType >::getAcceptanceRatio ( ) const
inline

Gets the current acceptance ratio of proposals.

Returns
Acceptance ratio value in [0, 1].

◆ getAdaptedCovariance()

template<typename ProposalType >
Eigen::MatrixXd cmp::mcmc::MarkovChain< ProposalType >::getAdaptedCovariance ( ) const
inline

Get a covariance matrix adapted to the samples.

◆ getCovariance()

template<typename ProposalType >
Eigen::MatrixXd cmp::mcmc::MarkovChain< ProposalType >::getCovariance ( ) const
inline

Gets the running covariance of the parameter samples.

Returns
Covariance matrix.

◆ getCurrent()

template<typename ProposalType >
Eigen::VectorXd cmp::mcmc::MarkovChain< ProposalType >::getCurrent ( ) const
inline

Gets the current parameters of the chain.

Returns
Parameter vector.

◆ getDim()

template<typename ProposalType >
size_t cmp::mcmc::MarkovChain< ProposalType >::getDim ( ) const
inline

Gets the dimensionality of the parameter space.

Returns
Parameter dimension.

◆ getMean()

template<typename ProposalType >
Eigen::VectorXd cmp::mcmc::MarkovChain< ProposalType >::getMean ( ) const
inline

Gets the running mean of the parameter samples.

Returns
Mean vector.

◆ getScore()

template<typename ProposalType >
double cmp::mcmc::MarkovChain< ProposalType >::getScore ( ) const
inline

Gets the current log probability score of the chain.

Returns
Log score.

◆ getSteps()

template<typename ProposalType >
size_t cmp::mcmc::MarkovChain< ProposalType >::getSteps ( ) const
inline

Gets the total number of steps run in this chain.

Returns
Steps count.

◆ increaseSteps()

template<typename ProposalType >
void cmp::mcmc::MarkovChain< ProposalType >::increaseSteps ( )
inline

Increase the number of steps.

◆ info()

template<typename ProposalType >
void cmp::mcmc::MarkovChain< ProposalType >::info ( ) const
inline

Log the total number of steps, acceptance ratio, data mean and covariance.

◆ reset()

template<typename ProposalType >
void cmp::mcmc::MarkovChain< ProposalType >::reset ( )
inline

Reset the value of the mean and of the covariance matrix.

◆ step()

template<typename ProposalType >
void cmp::mcmc::MarkovChain< ProposalType >::step ( const score_t getScore,
const bool &  delayedRejection = false,
const double &  gamma = 0.1 
)
inline

Perform a single MCMC step.

◆ update()

template<typename ProposalType >
void cmp::mcmc::MarkovChain< ProposalType >::update ( )
inline

Updates the value of the mean and covariance matrix.

Member Data Documentation

◆ cov_

template<typename ProposalType >
Eigen::MatrixXd cmp::mcmc::MarkovChain< ProposalType >::cov_
protected

Parameter sample running covariance matrix.

◆ dim_

template<typename ProposalType >
size_t cmp::mcmc::MarkovChain< ProposalType >::dim_
protected

Dimension of parameter space.

◆ distU_

template<typename ProposalType >
std::uniform_real_distribution<double> cmp::mcmc::MarkovChain< ProposalType >::distU_ {0.0, 1.0}
protected

Uniform distribution helper.

◆ mean_

template<typename ProposalType >
Eigen::VectorXd cmp::mcmc::MarkovChain< ProposalType >::mean_
protected

Parameter sample running mean vector.

◆ nAccepts_

template<typename ProposalType >
size_t cmp::mcmc::MarkovChain< ProposalType >::nAccepts_ {0}
protected

Number of accepted proposals.

◆ nSteps_

template<typename ProposalType >
size_t cmp::mcmc::MarkovChain< ProposalType >::nSteps_ {0}
protected

Number of steps taken.

◆ proposal_

template<typename ProposalType >
ProposalType cmp::mcmc::MarkovChain< ProposalType >::proposal_
protected

Proposal distribution model (copied by value).

◆ rng_

template<typename ProposalType >
std::default_random_engine cmp::mcmc::MarkovChain< ProposalType >::rng_
protected

Random engine.

◆ scale_

template<typename ProposalType >
double cmp::mcmc::MarkovChain< ProposalType >::scale_
protected

Proposal covariance scale parameter.

◆ score_

template<typename ProposalType >
double cmp::mcmc::MarkovChain< ProposalType >::score_ {0.0}
protected

Current log probability score value.

◆ targetAcceptanceRatio_

template<typename ProposalType >
double cmp::mcmc::MarkovChain< ProposalType >::targetAcceptanceRatio_
protected

Target acceptance ratio for adaptive tuning.


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