Docs
Loading...
Searching...
No Matches
neal.hpp
Go to the documentation of this file.
1
13
14#pragma once
15
16#include "../utils/Sampler.hpp"
17#include <vector>
18
43class Neal3 : public Sampler {
44private:
45 // ========== Random Number Generation ==========
46
48 mutable std::mt19937 gen;
49
50 // ========== Core Algorithm Methods ==========
51
66 void step_1_observation(int index);
67
78 int sample_from_log_probs(int num_clusters);
79
80 // Pre-allocated buffers to avoid repeated allocations
81 std::vector<int> indices;
82 std::vector<double> log_likelihoods;
83 std::vector<double> weights;
84 int n_data;
85
86public:
87 // ========== Constructor ==========
88
101 Neal3(Data &d, Params &p, Likelihood &l, Process &pr) : Sampler(d, p, l, pr), gen(rd()), n_data(d.get_n()) {
102 // Pre-allocate to max size (all points in separate clusters + 1 new cluster)
103 log_likelihoods.reserve(n_data + 1);
104 weights.reserve(n_data + 1);
105
106 indices.resize(n_data);
107 std::iota(indices.begin(), indices.end(), 0);
108 }
109
110 // ========== MCMC Interface ==========
111
126 void step() override;
127};
Abstract base class for MCMC sampling algorithms.
Manages distance matrices and cluster allocations for points.
Definition Data.hpp:27
Abstract base class for likelihood computation.
Definition Likelihood.hpp:27
std::vector< double > weights
Definition neal.hpp:83
int sample_from_log_probs(int num_clusters)
Sample an index from log-probabilities using the log-sum-exp trick.
Definition neal.cpp:20
std::mt19937 gen
Mersenne Twister random number generator for sampling operations.
Definition neal.hpp:48
int n_data
Definition neal.hpp:84
void step() override
Perform one complete iteration of Neal's Algorithm 3.
Definition neal.cpp:80
void step_1_observation(int index)
Sample cluster assignment for a single observation.
Definition neal.cpp:46
std::vector< int > indices
Definition neal.hpp:81
Neal3(Data &d, Params &p, Likelihood &l, Process &pr)
Constructor for Neal's Algorithm 3 sampler.
Definition neal.hpp:101
std::vector< double > log_likelihoods
Definition neal.hpp:82
Abstract base class for Bayesian nonparametric processes.
Definition Process.hpp:41
Sampler(Data &d, const Params &p, const Likelihood &l, Process &pr)
Constructor initializing sampler with required components.
Definition Sampler.hpp:97
std::random_device rd
Random device for generating random numbers across sampling algorithms.
Definition Sampler.hpp:73
Structure containing all parameters needed for the NGGP (Normalized Generalized Gamma Process) and DP...
Definition Params.hpp:35