Loading...
Searching...
No Matches
SliceSampler.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4//
5// Slice sampler for the GP NLML hyperparameter posterior.
6//
7// Implements Neal 2003 doubling slice sampling (Annals of Statistics 31:705),
8// with random-direction Gibbs proposals over the p-dim hyperparameter
9// space. Targets the negative log marginal likelihood,
10// log p(theta | y) = -NLML(theta) = -1/2 (n log(2 pi) + log |K(theta)|
11// + y^T K(theta)^-1 y) - log priors,
12// providing M posterior samples for the Laplace-slice marginalisation arm
13// (Murray & Adams 2010 NIPS, "Slice sampling covariance hyperparameters of
14// latent Gaussian models", arxiv 1006.0868).
15//
16// Per-sample cost: ~3-10 slice steps at p=4, each step ~1-5 NLML evaluations
17// (1 Cholesky each). Total burst per fit: M * ~5 Cholesky factorisations,
18// paid once. Predicts then average over the cached (theta_k, alpha_k)
19// tuples with no new Cholesky -- O(M * n) per potential, O(M * n^2) per
20// variance -- replacing the CCD-INLA per-predict cost of K * O(n^3).
21//
22// MPI: sampling runs on rank 0 (sequential by Neal's correctness proof),
23// then the cached state is broadcast. Predicts are local on every rank.
24
25#ifndef GPR_ML_LAPLACE_SLICE_SAMPLER_H_
26#define GPR_ML_LAPLACE_SLICE_SAMPLER_H_
27
28#include <Eigen/Core>
29#include <cstdint>
30#include <functional>
31#include <random>
32#include <vector>
33
34namespace gpr {
35namespace laplace {
36
49 int n_samples = 16;
51 int n_burnin = 4;
57 double initial_width = 0.5;
61 int max_doublings = 10;
65 int max_shrinks = 32;
68 uint64_t seed = 42;
69};
70
79 std::vector<Eigen::VectorXd> theta_samples;
83 std::vector<Eigen::VectorXd> alpha_cache;
85 int n_steps = 0;
90 int n_accepted = 0;
92 int n_stuck = 0;
95 bool valid = false;
96};
97
101public:
107 using LogTarget = std::function<double(const Eigen::VectorXd&)>;
108
112 using AlphaCacheFn = std::function<Eigen::VectorXd(const Eigen::VectorXd&)>;
113
114 explicit SliceSampler(SliceSamplerOptions opts = {});
115
121 bool sample(const Eigen::VectorXd& theta_init, const LogTarget& log_target,
122 const AlphaCacheFn& alpha_fn, SliceSamplerState& out);
123
124private:
130 bool sliceStep(const Eigen::VectorXd& theta_curr, double log_p_curr,
131 const LogTarget& log_target, Eigen::VectorXd& theta_next,
132 double& log_p_next, int& n_evals);
133
136 Eigen::VectorXd randomUnitDirection(int p);
137
139 std::mt19937_64 rng_;
140};
141
142} // namespace laplace
143} // namespace gpr
144
145#endif // GPR_ML_LAPLACE_SLICE_SAMPLER_H_
SliceSampler(SliceSamplerOptions opts={})
std::function< Eigen::VectorXd(const Eigen::VectorXd &)> AlphaCacheFn
std::function< double(const Eigen::VectorXd &)> LogTarget
bool sample(const Eigen::VectorXd &theta_init, const LogTarget &log_target, const AlphaCacheFn &alpha_fn, SliceSamplerState &out)
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
int n_burnin
Number of leading samples to discard for chain warm-up.
std::vector< Eigen::VectorXd > theta_samples
std::vector< Eigen::VectorXd > alpha_cache
int n_stuck
Number of stuck steps (max_shrinks reached without accepting).
int n_steps
Total slice steps taken (including burn-in).
int n_accepted
Number of accepted proposals (for acceptance-rate diagnostics).