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
102 : Sampler(d, l, pr), gen(rd()), n_data(d.get_n()) {
103 // Pre-allocate to max size (all points in separate clusters + 1 new
104 // cluster)
105 log_likelihoods.reserve(n_data + 1);
106 weights.reserve(n_data + 1);
107
108 indices.resize(n_data);
109 std::iota(indices.begin(), indices.end(), 0);
110 }
111
112 // ========== MCMC Interface ==========
113
128 void step() override;
129};
Abstract base class for MCMC sampling algorithms.
Manages distance matrices and cluster allocations for points.
Definition Data.hpp:26
Abstract base class for likelihood computation.
Definition Likelihood.hpp:26
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:17
std::mt19937 gen
Mersenne Twister random number generator for sampling operations.
Definition neal.hpp:48
int n_data
Definition neal.hpp:84
Neal3(Data &d, Likelihood &l, Process &pr)
Constructor for Neal's Algorithm 3 sampler.
Definition neal.hpp:101
void step() override
Perform one complete iteration of Neal's Algorithm 3.
Definition neal.cpp:83
void step_1_observation(int index)
Sample cluster assignment for a single observation.
Definition neal.cpp:47
std::vector< int > indices
Definition neal.hpp:81
std::vector< double > log_likelihoods
Definition neal.hpp:82
Abstract base class for Bayesian nonparametric processes.
Definition Process.hpp:40
std::random_device rd
Random device for generating random numbers across sampling algorithms.
Definition Sampler.hpp:67
Sampler(Data &d, const Likelihood &l, Process &pr)
Constructor initializing sampler with required components.
Definition Sampler.hpp:89