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

Implementation of Neal's Algorithm 3 for collapsed Gibbs sampling. More...

#include <neal.hpp>

Inheritance diagram for Neal3:
Sampler

Public Member Functions

 Neal3 (Data &d, Params &p, Likelihood &l, Process &pr)
 Constructor for Neal's Algorithm 3 sampler.
void step () override
 Perform one complete iteration of Neal's Algorithm 3.
Public Member Functions inherited from Sampler
 Sampler (Data &d, const Params &p, const Likelihood &l, Process &pr)
 Constructor initializing sampler with required components.
virtual ~Sampler ()=default
 Virtual destructor for proper cleanup of derived classes.

Private Member Functions

void step_1_observation (int index)
 Sample cluster assignment for a single observation.
int sample_from_log_probs (int num_clusters)
 Sample an index from log-probabilities using the log-sum-exp trick.

Private Attributes

std::mt19937 gen
 Mersenne Twister random number generator for sampling operations.
std::vector< int > indices
std::vector< double > log_likelihoods
std::vector< double > weights
int n_data

Additional Inherited Members

Protected Attributes inherited from Sampler
Datadata
 Reference to the data object containing observations and current allocations.
const Paramsparams
 Reference to the parameters object containing model hyperparameters and MCMC settings.
const Likelihoodlikelihood
 Reference to the likelihood computation object for evaluating cluster assignments.
Processprocess
 Reference to the stochastic process object (DP, NGGP, DPW, NGGPW).
std::random_device rd
 Random device for generating random numbers across sampling algorithms.

Detailed Description

Implementation of Neal's Algorithm 3 for collapsed Gibbs sampling.

This class implements Neal's Algorithm 3, a standard collapsed Gibbs sampler for Bayesian nonparametric mixture models. The algorithm sequentially updates cluster assignments by sampling from the full conditional distribution of each observation, integrating out the cluster parameters.

Algorithm 3 is characterized by:

  • Sequential updates: One observation at a time
  • Collapsed sampling: Cluster parameters are integrated out analytically
  • Full conditionals: Each assignment is sampled from its exact posterior
  • Automatic cluster creation/deletion: Clusters are created when needed and deleted when empty

The algorithm is particularly effective for models where the likelihood can be computed in closed form after integrating out cluster-specific parameters.

Note
reference Neal, R. M. (2000). "Markov Chain Sampling Methods for Dirichlet Process Mixture Models"
See also
Sampler, Process, Likelihood

Constructor & Destructor Documentation

◆ Neal3()

Neal3::Neal3 ( Data & d,
Params & p,
Likelihood & l,
Process & pr )
inline

Constructor for Neal's Algorithm 3 sampler.

Parameters
dReference to Data object containing observations
pReference to Params object with hyperparameters
lReference to Likelihood object for probability computations
prReference to Process object (DP, NGGP, etc.) defining the prior

Initializes the Gibbs sampler with all required components. The random number generator is seeded from the inherited random device. Pre-allocates vectors to maximum possible size to avoid resizing during sampling.

Member Function Documentation

◆ sample_from_log_probs()

int Neal3::sample_from_log_probs ( int num_clusters)
private

Sample an index from log-probabilities using the log-sum-exp trick.

Parameters
num_clustersNumber of clusters (including new cluster)
Returns
Index sampled according to the probabilities

This method performs sampling from a discrete distribution defined by log-probabilities. It uses the log-sum-exp trick for numerical stability and implements roulette wheel selection.

◆ step()

void Neal3::step ( )
overridevirtual

Perform one complete iteration of Neal's Algorithm 3.

Executes one full sweep of the collapsed Gibbs sampler by sequentially updating the cluster assignment of each observation. The order of updates is typically randomized to avoid systematic bias.

After each full sweep:

  • All observations have been considered for reassignment
  • Cluster structure may have changed (clusters created/destroyed)
  • The Markov chain has advanced by one step
See also
step_1_observation()

Performs a single step of the Neal 3 algorithm for all the dataset.

Implements Sampler.

◆ step_1_observation()

void Neal3::step_1_observation ( int index)
private

Sample cluster assignment for a single observation.

Parameters
indexIndex of the observation to update

This method implements the core of Algorithm 3:

  1. Remove the observation from its current cluster
  2. Compute probabilities for all existing clusters plus a new cluster
  3. Sample new assignment from the full conditional distribution
  4. Update cluster assignments and clean up empty clusters

The probabilities combine prior information from the Process with likelihood information computed by integrating out cluster parameters.

Performs a step in the DPNeal2 sampling process.

This method is responsible for updating the allocations of the data points based on the current state of the model.

Parameters
indexThe index of the data point to update.

Member Data Documentation

◆ gen

std::mt19937 Neal3::gen
mutableprivate

Mersenne Twister random number generator for sampling operations.

◆ indices

std::vector<int> Neal3::indices
private

◆ log_likelihoods

std::vector<double> Neal3::log_likelihoods
private

◆ n_data

int Neal3::n_data
private

◆ weights

std::vector<double> Neal3::weights
private

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