Monte Carlo Integration Library 1.0
High-performance Monte Carlo methods for numerical integration and optimization
Public Member Functions | List of all members
mc::mcmc::MetropolisHastingsSampler< dim > Class Template Reference

Metropolis-Hastings MCMC sampler for arbitrary target distributions. More...

#include <metropolisHastingsSampler.hpp>

Collaboration diagram for mc::mcmc::MetropolisHastingsSampler< dim >:

Public Member Functions

 MetropolisHastingsSampler (const mc::domains::IntegrationDomain< dim > &d, const std::function< double(const mc::geom::Point< dim > &)> &p, mc::geom::Point< dim > x0, double deviation)
 Construct Metropolis-Hastings sampler with target distribution.
 
mc::geom::Point< dimnext (std::mt19937 &rng)
 Generate the next sample from the Markov chain.
 
double target_pdf (const mc::geom::Point< dim > &x)
 Evaluate target probability density at a point.
 
double acceptance_rate () const
 Get the current acceptance rate of the Markov chain.
 

Detailed Description

template<size_t dim>
class mc::mcmc::MetropolisHastingsSampler< dim >

Metropolis-Hastings MCMC sampler for arbitrary target distributions.

Template Parameters
dimDimensionality of the sample space.

Generates a Markov chain of samples asymptotically distributed according to target_pdf(x) using the Metropolis-Hastings algorithm.

Sampling process:

Usage:

  1. Construct sampler with target density and initial point
  2. Call next() repeatedly to generate Markov chain
  3. Discard initial burn-in samples
  4. Use remaining samples for Monte Carlo integration
  5. Monitor acceptance_rate() to tune deviation

Performance tuning:

Note
The target density should return 0 outside the domain rather than relying on explicit domain checks (for flexibility).
Warning
Samples are correlated; use thinning for variance reduction.

Definition at line 78 of file metropolisHastingsSampler.hpp.

Constructor & Destructor Documentation

◆ MetropolisHastingsSampler()

template<size_t dim>
mc::mcmc::MetropolisHastingsSampler< dim >::MetropolisHastingsSampler ( const mc::domains::IntegrationDomain< dim > &  d,
const std::function< double(const mc::geom::Point< dim > &)> &  p,
mc::geom::Point< dim x0,
double  deviation 
)
explicit

Construct Metropolis-Hastings sampler with target distribution.

Parameters
dIntegration domain (defines valid region and constraints).
pTarget probability density function π(x). Should return 0 outside the domain and positive values inside. Must be finite and > 0 at x0.
x0Initial point for the Markov chain. Must satisfy domain.isInside(x0).
deviationStandard deviation σ of random walk proposal N(0, σ²). Tune this parameter to achieve ~23.4% acceptance rate.
Exceptions
std::invalid_argumentIf x0 is outside the domain.

Initializes sampler state and validates initial conditions. The random walk proposal will use a normal distribution with standard deviation deviation for each dimension.

Member Function Documentation

◆ acceptance_rate()

template<size_t dim>
double mc::mcmc::MetropolisHastingsSampler< dim >::acceptance_rate ( ) const

Get the current acceptance rate of the Markov chain.

Returns
Fraction of proposed moves that were accepted: n_accept / n_steps. Returns 0.0 if no steps have been taken.

The acceptance rate indicates chain quality:

  • Too high (>50%): Increase deviation to explore wider
  • Optimal (~23.4%): Good for high-dimensional problems (d > 5)
  • Too low (<10%): Decrease deviation to accept more proposals

Use this metric to tune the deviation parameter for efficiency. Generally aim for 20-40% in practice depending on dimensionality.

Note
This is a cumulative rate across all next() calls. Reset the sampler to start fresh statistics if needed.

◆ next()

template<size_t dim>
mc::geom::Point< dim > mc::mcmc::MetropolisHastingsSampler< dim >::next ( std::mt19937 &  rng)

Generate the next sample from the Markov chain.

Parameters
rngRandom number generator (must be seeded externally).
Returns
Next point in the chain: either the proposed point or current point.

Implements one iteration of the Metropolis-Hastings algorithm:

  1. Propose y = current + N(0, σ²) where σ = deviation
  2. Compute target densities: π(current), π(y)
  3. Compute acceptance ratio: α = min(1, π(y)/π(current))
  4. Accept y with probability α, update current = y and increment counter
  5. Return updated current point

Important: The target density p(x) is expected to handle domain constraints by returning 0 outside valid regions (not throwing exceptions).

Exceptions
std::runtime_errorIf π(current) becomes ≤ 0 or non-finite. This indicates the algorithm entered an invalid state.
Note
Automatically tracks acceptance statistics via n_steps and n_accept.
Call acceptance_rate() to monitor chain quality.

◆ target_pdf()

template<size_t dim>
double mc::mcmc::MetropolisHastingsSampler< dim >::target_pdf ( const mc::geom::Point< dim > &  x)

Evaluate target probability density at a point.

Parameters
xQuery point in the domain.
Returns
π(x), the target probability density at x.

Evaluates the target probability density function provided at construction. This is a simple accessor for the internal target.

Note
For points outside the domain, the target should return 0.

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