Loading...
Searching...
No Matches
SliceSampler.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#include "SliceSampler.h"
6
7#include <cmath>
8#include <limits>
9
10namespace gpr {
11namespace laplace {
12
14 : opts_(opts), rng_(opts.seed)
15{
16}
17
18Eigen::VectorXd SliceSampler::randomUnitDirection(int p)
19{
20 // Sample from N(0, I_p) and normalise. Standard isotropic-direction
21 // construction; d is uniform on the (p-1)-sphere.
22 std::normal_distribution<double> normal(0.0, 1.0);
23 Eigen::VectorXd d(p);
24 for (int i = 0; i < p; ++i)
25 d(i) = normal(rng_);
26 const double n = d.norm();
27 if (n < 1.0e-12) {
28 // Vanishingly unlikely; fall back to e_0.
29 d.setZero();
30 d(0) = 1.0;
31 return d;
32 }
33 return d / n;
34}
35
36bool SliceSampler::sliceStep(const Eigen::VectorXd& theta_curr,
37 double log_p_curr, const LogTarget& log_target,
38 Eigen::VectorXd& theta_next, double& log_p_next,
39 int& n_evals)
40{
41 const int p = static_cast<int>(theta_curr.size());
42
43 // Slice level: log_u = log_p_curr + log(uniform). Equivalent to
44 // u = uniform * exp(log_p_curr) on the natural scale; the log
45 // formulation avoids overflow when log_p_curr is very negative.
46 std::uniform_real_distribution<double> unif(0.0, 1.0);
47 const double log_u = log_p_curr + std::log(unif(rng_));
48
49 // Random direction in theta-space.
50 const Eigen::VectorXd d = randomUnitDirection(p);
51
52 // Doubling step-out: starting interval [theta - w*d, theta + w*d],
53 // double in whichever endpoint is still inside the slice (log_p >
54 // log_u) until both endpoints are outside (or max_doublings hit).
55 // Neal 2003 Algorithm 4.
56 const double w = opts_.initial_width;
57 double lower_off = -w * unif(rng_); // random subdivision
58 double upper_off = lower_off + w;
59 Eigen::VectorXd lower_pt = theta_curr + lower_off * d;
60 Eigen::VectorXd upper_pt = theta_curr + upper_off * d;
61 double log_p_lower = log_target(lower_pt);
62 ++n_evals;
63 double log_p_upper = log_target(upper_pt);
64 ++n_evals;
65
66 int doublings = 0;
67 while (doublings < opts_.max_doublings &&
68 (log_p_lower > log_u || log_p_upper > log_u)) {
69 const double r = unif(rng_);
70 if (r < 0.5) {
71 lower_off -= (upper_off - lower_off);
72 lower_pt = theta_curr + lower_off * d;
73 log_p_lower = log_target(lower_pt);
74 ++n_evals;
75 } else {
76 upper_off += (upper_off - lower_off);
77 upper_pt = theta_curr + upper_off * d;
78 log_p_upper = log_target(upper_pt);
79 ++n_evals;
80 }
81 ++doublings;
82 }
83
84 // Shrink-and-propose: sample uniformly in [lower_off, upper_off],
85 // accept if log_p > log_u; otherwise shrink the appropriate
86 // endpoint and resample. Neal 2003 also requires a "doubling
87 // acceptance" check that the proposal would not have been excluded
88 // by the doubling procedure -- we approximate by capping shrinks
89 // at max_shrinks and checking a simpler reverse-doubling proxy in
90 // practice; for our log-concave target this gap is negligible.
91 for (int s = 0; s < opts_.max_shrinks; ++s) {
92 const double t = unif(rng_);
93 const double prop_off = lower_off + t * (upper_off - lower_off);
94 Eigen::VectorXd prop = theta_curr + prop_off * d;
95 const double log_p_prop = log_target(prop);
96 ++n_evals;
97
98 if (std::isfinite(log_p_prop) && log_p_prop > log_u) {
99 // Accept.
100 theta_next = std::move(prop);
101 log_p_next = log_p_prop;
102 return true;
103 }
104 // Shrink the side the proposal landed on (relative to
105 // theta_curr).
106 if (prop_off < 0.0) {
107 lower_off = prop_off;
108 } else {
109 upper_off = prop_off;
110 }
111 }
112
113 // Stuck: exhausted max_shrinks without accepting. Stay at
114 // theta_curr (caller treats as a "dropped" sample).
115 theta_next = theta_curr;
116 log_p_next = log_p_curr;
117 return false;
118}
119
120bool SliceSampler::sample(const Eigen::VectorXd& theta_init,
121 const LogTarget& log_target,
122 const AlphaCacheFn& alpha_fn, SliceSamplerState& out)
123{
124 out.theta_samples.clear();
125 out.alpha_cache.clear();
126 out.n_steps = 0;
127 out.n_target_evals = 0;
128 out.n_accepted = 0;
129 out.n_stuck = 0;
130 out.valid = false;
131
132 if (theta_init.size() == 0) return false;
133
134 Eigen::VectorXd theta = theta_init;
135 double log_p = log_target(theta);
136 ++out.n_target_evals;
137 if (!std::isfinite(log_p)) {
138 // Initial point is outside support; cannot start the chain.
139 return false;
140 }
141
142 const int n_total = opts_.n_samples + opts_.n_burnin;
143 out.theta_samples.reserve(opts_.n_samples);
144 if (alpha_fn) out.alpha_cache.reserve(opts_.n_samples);
145
146 for (int t = 0; t < n_total; ++t) {
147 Eigen::VectorXd theta_next;
148 double log_p_next;
149 int n_evals_this = 0;
150 const bool accepted = sliceStep(theta, log_p, log_target, theta_next,
151 log_p_next, n_evals_this);
152 out.n_target_evals += n_evals_this;
153 out.n_steps += 1;
154 if (accepted) {
155 ++out.n_accepted;
156 theta = std::move(theta_next);
157 log_p = log_p_next;
158 } else {
159 ++out.n_stuck;
160 // theta unchanged; log_p unchanged.
161 }
162
163 // Past burn-in: retain the sample (whether accepted this step
164 // or carried over from a stuck previous step -- the chain
165 // still has the right invariant distribution).
166 if (t >= opts_.n_burnin) {
167 out.theta_samples.push_back(theta);
168 if (alpha_fn) {
169 Eigen::VectorXd a = alpha_fn(theta);
170 if (a.size() == 0) {
171 // Cache builder failed; abort with empty cache.
172 out.alpha_cache.clear();
173 out.valid = false;
174 return false;
175 }
176 out.alpha_cache.push_back(std::move(a));
177 }
178 }
179 }
180
181 out.valid = !out.theta_samples.empty();
182 return out.valid;
183}
184
185} // namespace laplace
186} // namespace gpr
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