GPRidge v1.0.0
GPR-accelerated saddle point searches on potential energy surfaces
☾
Toggle main menu visibility
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
34
namespace
gpr
{
35
namespace
laplace
{
36
41
struct
SliceSamplerOptions
{
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
76
struct
SliceSamplerState
{
79
std::vector<Eigen::VectorXd>
theta_samples
;
83
std::vector<Eigen::VectorXd>
alpha_cache
;
85
int
n_steps
= 0;
88
int
n_target_evals
= 0;
90
int
n_accepted
= 0;
92
int
n_stuck
= 0;
95
bool
valid
=
false
;
96
};
97
100
class
SliceSampler
{
101
public
:
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
124
private
:
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
138
SliceSamplerOptions
opts_;
139
std::mt19937_64 rng_;
140
};
141
142
}
// namespace laplace
143
}
// namespace gpr
144
145
#endif
// GPR_ML_LAPLACE_SLICE_SAMPLER_H_
gpr::laplace::SliceSampler::SliceSampler
SliceSampler(SliceSamplerOptions opts={})
Definition
SliceSampler.cpp:13
gpr::laplace::SliceSampler::AlphaCacheFn
std::function< Eigen::VectorXd(const Eigen::VectorXd &)> AlphaCacheFn
Definition
SliceSampler.h:112
gpr::laplace::SliceSampler::LogTarget
std::function< double(const Eigen::VectorXd &)> LogTarget
Definition
SliceSampler.h:107
gpr::laplace::SliceSampler::sample
bool sample(const Eigen::VectorXd &theta_init, const LogTarget &log_target, const AlphaCacheFn &alpha_fn, SliceSamplerState &out)
Definition
SliceSampler.cpp:120
out
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
Definition
eon_relax_engine.h:240
gpr::laplace
Definition
GaussianProcessRegression.h:86
gpr
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition
Macros.h:16
gpr::laplace::SliceSamplerOptions
Definition
SliceSampler.h:41
gpr::laplace::SliceSamplerOptions::initial_width
double initial_width
Definition
SliceSampler.h:57
gpr::laplace::SliceSamplerOptions::max_doublings
int max_doublings
Definition
SliceSampler.h:61
gpr::laplace::SliceSamplerOptions::max_shrinks
int max_shrinks
Definition
SliceSampler.h:65
gpr::laplace::SliceSamplerOptions::n_samples
int n_samples
Definition
SliceSampler.h:49
gpr::laplace::SliceSamplerOptions::seed
uint64_t seed
Definition
SliceSampler.h:68
gpr::laplace::SliceSamplerOptions::n_burnin
int n_burnin
Number of leading samples to discard for chain warm-up.
Definition
SliceSampler.h:51
gpr::laplace::SliceSamplerState
Definition
SliceSampler.h:76
gpr::laplace::SliceSamplerState::valid
bool valid
Definition
SliceSampler.h:95
gpr::laplace::SliceSamplerState::theta_samples
std::vector< Eigen::VectorXd > theta_samples
Definition
SliceSampler.h:79
gpr::laplace::SliceSamplerState::alpha_cache
std::vector< Eigen::VectorXd > alpha_cache
Definition
SliceSampler.h:83
gpr::laplace::SliceSamplerState::n_stuck
int n_stuck
Number of stuck steps (max_shrinks reached without accepting).
Definition
SliceSampler.h:92
gpr::laplace::SliceSamplerState::n_steps
int n_steps
Total slice steps taken (including burn-in).
Definition
SliceSampler.h:85
gpr::laplace::SliceSamplerState::n_target_evals
int n_target_evals
Definition
SliceSampler.h:88
gpr::laplace::SliceSamplerState::n_accepted
int n_accepted
Number of accepted proposals (for acceptance-rate diagnostics).
Definition
SliceSampler.h:90
gpr
ml
laplace
SliceSampler.h
Generated by
1.17.0
Generated by
Doxygen 1.17.0
Analytics by
Antics
provided by
TurtleTech ehf