Docs
Loading...
Searching...
No Matches
RWMH Class Reference

Random Walk Metropolis-Hastings sampler for updating the latent variable U. More...

#include <RWMH.hpp>

Inheritance diagram for RWMH:
U_sampler

Public Member Functions

 RWMH (Params &p, Data &d, bool use_V=false, double prop_sd=1, bool tuning=false)
 Constructor for the RWMH sampler.
void update_U () override
 Updates the latent variable U using Random Walk Metropolis-Hastings.
Public Member Functions inherited from U_sampler
 U_sampler (Params &p, Data &d)
 Constructor for the U_sampler class.
double get_U () const
 Getter for the current value of U.
double get_acceptance_rate () const
 Getter for the acceptance rate of U updates.
virtual ~U_sampler ()=default
 Virtual destructor for the U_sampler class.

Private Member Functions

void Robbins_Monro_tuning ()
 Performs Robbins-Monro adaptive tuning of the proposal standard deviation.
Sampling Methods

Private methods implementing RWMH updates on different scales.

void sampling_U ()
 Performs one RWMH update step for U on the original scale.
void sampling_V ()
 Performs one RWMH update step for V = log(U).

Private Attributes

double proposal_sd = 1.0
 Standard deviation for the Gaussian proposal distribution.
bool accept = false
 Flag indicating whether the last proposal was accepted.
bool use_V = false
 Flag to use V = log(U) scale for sampling instead of U directly.
bool tuning_enabled = false
 Flag to enable/disable Robbins-Monro automatic tuning.

Additional Inherited Members

Protected Member Functions inherited from U_sampler
double log_conditional_density_U (double u) const
 Computes the log conditional density of U given the partition.
double log_conditional_density_V (double v) const
 Computes the log conditional density of V = log(U) given the partition.
Protected Attributes inherited from U_sampler
Paramsparams
 Reference to the parameters object containing NGGP parameters.
Datadata
 Reference to the data object containing observations and cluster assignments.
int total_iterations = 0
 Counter for total MCMC iterations performed.
int accepted_U = 0
 Counter of accepted U.
std::random_device rd
 Random device for seeding.
std::mt19937 gen
 Mersenne Twister random number generator.
const double a_over_sigma = params.a / params.sigma
 Ratio a/sigma for efficient computation.
const double tau_power_sigma = std::pow(params.tau, params.sigma)
 Pre-computed tau^sigma for efficient computation.
const int n = data.get_n()
 Number of observations n.
double U = 1.0
 Current value of the latent variable U (initialized to 1.0).

Detailed Description

Random Walk Metropolis-Hastings sampler for updating the latent variable U.

This class implements a Random Walk Metropolis-Hastings (RWMH) algorithm for sampling the latent variable U in NGGP mixture models. The sampler uses a Gaussian proposal distribution centered at the current value.

The sampler can operate on either:

  • U scale directly (using a truncated normal proposal)
  • V = log(U) scale (using an unrestricted normal proposal)

The class also supports automatic tuning of the proposal standard deviation using the Robbins-Monro stochastic approximation algorithm to achieve an optimal acceptance rate of approximately 0.44 for one-dimensional proposals.

See also
U_sampler

Constructor & Destructor Documentation

◆ RWMH()

RWMH::RWMH ( Params & p,
Data & d,
bool use_V = false,
double prop_sd = 1,
bool tuning = false )
inline

Constructor for the RWMH sampler.

Initializes the Random Walk Metropolis-Hastings sampler with specified configuration parameters.

Parameters
pReference to the parameters object containing NGGP parameters.
dReference to the data object containing observations and cluster assignments.
use_VIf true, perform sampling on V = log(U) scale; if false, sample U directly.
prop_sdInitial standard deviation for the Gaussian proposal distribution (default: 1.0).
tuningIf true, enable Robbins-Monro adaptive tuning of proposal_sd (default: false).
Note
When use_V = false, proposals are truncated to ensure U > 0.
When tuning = true, proposal_sd will be automatically adjusted during sampling.

Member Function Documentation

◆ Robbins_Monro_tuning()

void RWMH::Robbins_Monro_tuning ( )
private

Performs Robbins-Monro adaptive tuning of the proposal standard deviation.

Adjusts the proposal standard deviation to target an optimal acceptance rate of 0.44 for one-dimensional random walk proposals. The tuning follows the Robbins-Monro stochastic approximation scheme:

\‍[\log(\sigma_{\text{prop}}^{(t+1)}) = \log(\sigma_{\text{prop}}^{(t)})
+ \frac{c}{t} \cdot \delta_t
\‍]

where:

  • $\delta_t = \mathbb{1}_{\{\text{accept}\}} - \alpha_{\text{target}}$
  • $c = 1 / (\alpha_{\text{target}} \cdot (1 - \alpha_{\text{target}}))$
  • $\alpha_{\text{target}} = 0.44$ (optimal for 1D random walk)
  • $t$ is the current iteration number
Note
Reference: "Adaptive optimal scaling of Metropolis-Hastings algorithms using the Robbins-Monro process" by Garthwaite et al. (2016).
See also
update_U()

◆ sampling_U()

void RWMH::sampling_U ( )
private

Performs one RWMH update step for U on the original scale.

Proposes a new value from a truncated normal distribution:

\‍[U' \sim \mathcal{N}(U_{\text{current}}, \sigma_{\text{prop}}^2) \quad
\text{truncated to } (0, \infty) \‍]

The acceptance probability accounts for the asymmetric proposal due to truncation:

\‍[ \alpha = \min\left(1, \frac{f(U')}{f(U)} \cdot
\frac{q(U|U')}{q(U'|U)}\right) \‍]

where $q(U'|U)$ is the truncated normal proposal density.

See also
sampling_V()

◆ sampling_V()

void RWMH::sampling_V ( )
private

Performs one RWMH update step for V = log(U).

Proposes a new value from an unrestricted normal distribution:

\‍[V' \sim \mathcal{N}(V_{\text{current}}, \sigma_{\text{prop}}^2)
\‍]

The acceptance probability simplifies to:

\‍[\alpha = \min\left(1, \frac{f(V')}{f(V)}\right)
\‍]

Note
since the proposal is symmetric. Sampling on the log scale can improve efficiency and avoids truncation issues.
See also
sampling_U()

◆ update_U()

void RWMH::update_U ( )
overridevirtual

Updates the latent variable U using Random Walk Metropolis-Hastings.

This method performs one iteration of the RWMH algorithm. It:

  1. Increments the iteration counter
  2. Performs either sampling_U() or sampling_V() depending on use_V flag
  3. Optionally tunes the proposal standard deviation if tuning is enabled

The choice between U and V scale affects the proposal distribution and acceptance probability calculation.

See also
sampling_U(), sampling_V(), Robbins_Monro_tuning()

Implements U_sampler.

Member Data Documentation

◆ accept

bool RWMH::accept = false
private

Flag indicating whether the last proposal was accepted.

◆ proposal_sd

double RWMH::proposal_sd = 1.0
private

Standard deviation for the Gaussian proposal distribution.

This parameter controls the step size of the random walk proposal. It can be automatically tuned using Robbins-Monro adaptation if enabled.

◆ tuning_enabled

bool RWMH::tuning_enabled = false
private

Flag to enable/disable Robbins-Monro automatic tuning.

◆ use_V

bool RWMH::use_V = false
private

Flag to use V = log(U) scale for sampling instead of U directly.


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