Docs
Loading...
Searching...
No Matches
splitmerge_LSS_SDDS.hpp
Go to the documentation of this file.
1
19
20#pragma once
21
23#include "../utils/Sampler.hpp"
24
69private:
71
72 std::vector<double> row_max_distances;
74
75 // ========== Random Number Generation ==========
76
79 mutable std::mt19937 gen;
80
81 // ========== Move Selection Variables ==========
82
84 int idx_i;
85
87 int idx_j;
88
90 int ci;
91
93 int cj;
94
95 // ========== Algorithm Configuration ==========
96
98 bool shuffle_bool = false;
99
100 // ========== State Management ==========
101
103 Eigen::VectorXi launch_state;
104
106 Eigen::VectorXi S;
107
110
112 const Eigen::VectorXi &original_allocations;
113
114 // ========== Proposal Probabilities ==========
115
118 std::uniform_real_distribution<> dis_real;
119
121 std::uniform_int_distribution<> dis_int;
122
126 Eigen::Vector2d log_probs;
127
130 Eigen::Vector2d probs;
131
135
139
142 const double rand_split_prob = log(0.5);
143
144 // ========== Debug variables ==========
148 int split_moves = 0;
149 int merge_moves = 0;
151
152 // ========== Move Selection Methods ==========
153
166 void choose_indeces(bool similarity = false);
167
175
176 // ========== SAMS-Specific Proposal Generation ==========
177
195 void sequential_allocation(int iterations, bool only_probabilities = false,
196 bool sequential = true);
197
198 // ========== Split Move Implementation ==========
199
210 void smart_split_move();
211
222 void dumb_split_move();
223
238 double compute_acceptance_ratio_split(double likelihood_old_cluster);
239
240 // ========== Merge Move Implementation ==========
241
252 void smart_merge_move();
253
262 void dumb_merge_move();
263
271 double compute_acceptance_ratio_merge(double likelihood_old_clusters);
272
273 // ========== Shuffle Move Implementation ==========
274
284 void shuffle();
285
295 double compute_acceptance_ratio_shuffle(double likelihood_old_clusters,
296 int old_ci_size, int old_cj_size);
297
298public:
299 // ========== Constructor ==========
300
322 bool shuffle)
323 : Sampler(d, l, pr), params(p), shuffle_bool(shuffle),
324 original_allocations(pr.old_allocations_view()), gen(rd()),
325 dis_real(0.0, 1.0), dis_int(0, 1) {
326
327 // Precompute row max distances for efficiency
328 row_max_distances.resize(data.get_n());
329 for (int i = 0; i < data.get_n(); ++i) {
330 row_max_distances[i] = params.D.row(i).maxCoeff();
331 }
332 const int n = data.get_n();
333
334 // Take all pairwise distances (i < j)
335 std::vector<double> pairwise_dists;
336 pairwise_dists.reserve(n * (n - 1) / 2);
337 for (int i = 0; i < n; ++i) {
338 for (int j = i + 1; j < n; ++j) {
339 pairwise_dists.push_back(params.D(i, j));
340 }
341 }
342
343 // Compute the 50th percentile of pairwise distances
344 if (!pairwise_dists.empty()) {
345 auto q_it = pairwise_dists.begin() +
346 static_cast<size_t>(pairwise_dists.size() * 0.50);
347 std::nth_element(pairwise_dists.begin(), q_it, pairwise_dists.end());
348 temperature = *q_it;
349 if (temperature < 1e-12)
350 temperature = 1.0;
351 } else {
352 temperature = 1.0;
353 }
354 };
355
356 // ========== MCMC Interface ==========
357
384 void step() override final;
385
386 // ========== Accessor Methods ==========
391 double get_accepted_split() const {
392 return split_moves > 0 ? static_cast<double>(accepted_split) / split_moves : 0.0;
393 };
394
399 double get_accepted_merge() const {
400 return merge_moves > 0 ? static_cast<double>(accepted_merge) / merge_moves : 0.0;
401 };
402
407 double get_accepted_shuffle() const {
408 return shuffle_moves > 0 ? static_cast<double>(accepted_shuffle) / shuffle_moves : 0.0;
409 };
410};
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
Abstract base class for Bayesian nonparametric processes.
Definition Process.hpp:40
Data & data
Reference to the data object containing observations and current allocations.
Definition Sampler.hpp:53
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
const utils_params & params
Definition splitmerge_LSS_SDDS.hpp:70
std::uniform_int_distribution dis_int
uniform integer distribution for general use
Definition splitmerge_LSS_SDDS.hpp:121
int ci
Cluster assignment of first observation.
Definition splitmerge_LSS_SDDS.hpp:90
int split_moves
Definition splitmerge_LSS_SDDS.hpp:148
void step() override final
Perform one iteration of the LSS-SDDS Split-Merge algorithm.
Definition splitmerge_LSS_SDDS.cpp:406
std::uniform_real_distribution dis_real
uniform distribution between 0 and 1
Definition splitmerge_LSS_SDDS.hpp:118
int shuffle_moves
Definition splitmerge_LSS_SDDS.hpp:150
double temperature
Definition splitmerge_LSS_SDDS.hpp:73
void dumb_split_move()
Execute a dumb split move with random allocation.
Definition splitmerge_LSS_SDDS.cpp:271
Eigen::Vector2d log_probs
Log probabilities of allocating a point to clusters ci and cj.
Definition splitmerge_LSS_SDDS.hpp:126
double compute_acceptance_ratio_split(double likelihood_old_cluster)
Compute acceptance ratio for LSS split move.
Definition splitmerge_LSS_SDDS.cpp:301
SplitMerge_LSS_SDDS(Data &d, utils_params &p, Likelihood &l, Process &pr, bool shuffle)
Constructor for LSS-SDDS (Locality Sensitive Sampling with Smart-split, Dumb-merge,...
Definition splitmerge_LSS_SDDS.hpp:321
int accepted_merge
Definition splitmerge_LSS_SDDS.hpp:146
double compute_acceptance_ratio_shuffle(double likelihood_old_clusters, int old_ci_size, int old_cj_size)
Compute acceptance ratio for LSS shuffle move.
Definition splitmerge_LSS_SDDS.cpp:344
int accepted_split
Definition splitmerge_LSS_SDDS.hpp:145
int idx_i
Index of first randomly chosen observation.
Definition splitmerge_LSS_SDDS.hpp:84
void dumb_merge_move()
Execute a dumb merge move with direct merging.
Definition splitmerge_LSS_SDDS.cpp:222
void smart_merge_move()
Execute a smart merge move using sequential allocation.
Definition splitmerge_LSS_SDDS.cpp:196
void sequential_allocation(int iterations, bool only_probabilities=false, bool sequential=true)
Generate proposal state via sequential allocation.
Definition splitmerge_LSS_SDDS.cpp:115
int merge_moves
Definition splitmerge_LSS_SDDS.hpp:149
double get_accepted_merge() const
Get number of accepted merge moves for diagnostics.
Definition splitmerge_LSS_SDDS.hpp:399
const double rand_split_prob
Constant log probability for random split allocation (dumb split).
Definition splitmerge_LSS_SDDS.hpp:142
Eigen::Vector2d probs
Probabilities of allocating a point to clusters ci and cj.
Definition splitmerge_LSS_SDDS.hpp:130
const Eigen::VectorXi & original_allocations
Cached reference to original allocations stored in the process.
Definition splitmerge_LSS_SDDS.hpp:112
double get_accepted_shuffle() const
Get number of accepted shuffle moves for diagnostics.
Definition splitmerge_LSS_SDDS.hpp:407
Eigen::VectorXi launch_state
Launch state for sequential allocation.
Definition splitmerge_LSS_SDDS.hpp:103
void choose_clusters_shuffle()
Select clusters for shuffle move.
Definition splitmerge_LSS_SDDS.cpp:362
void choose_indeces(bool similarity=false)
Randomly select two observations for split-merge proposal.
Definition splitmerge_LSS_SDDS.cpp:22
std::mt19937 gen
Mersenne Twister random number generator for sampling operations.
Definition splitmerge_LSS_SDDS.hpp:79
bool shuffle_bool
Flag to enable shuffle moves (Mena and Martinez, 2014).
Definition splitmerge_LSS_SDDS.hpp:98
void shuffle()
Execute a shuffle move using LSS.
Definition splitmerge_LSS_SDDS.cpp:317
Eigen::VectorXi S
Indices of observations in clusters ci and cj.
Definition splitmerge_LSS_SDDS.hpp:106
double get_accepted_split() const
Get number of accepted split moves for diagnostics.
Definition splitmerge_LSS_SDDS.hpp:391
int accepted_shuffle
Definition splitmerge_LSS_SDDS.hpp:147
int idx_j
Index of second randomly chosen observation.
Definition splitmerge_LSS_SDDS.hpp:87
double compute_acceptance_ratio_merge(double likelihood_old_clusters)
Compute acceptance ratio for LSS merge move.
Definition splitmerge_LSS_SDDS.cpp:177
int cj
Cluster assignment of second observation.
Definition splitmerge_LSS_SDDS.hpp:93
double log_merge_gibbs_prob
Log probability of generating current state via sequential allocation (merge direction).
Definition splitmerge_LSS_SDDS.hpp:138
std::vector< double > row_max_distances
Definition splitmerge_LSS_SDDS.hpp:72
double log_split_gibbs_prob
Log probability of generating current state via sequential allocation (split direction).
Definition splitmerge_LSS_SDDS.hpp:134
int launch_state_size
Size of the launch state and S vectors.
Definition splitmerge_LSS_SDDS.hpp:109
void smart_split_move()
Execute a smart split move using sequential allocation.
Definition splitmerge_LSS_SDDS.cpp:248
Parameter struct for utility parameters and data structures.
Definition utils-params.hpp:17
Parameter management for useful parameters and data structures.