Loading...
Searching...
No Matches
gpr::laplace::SliceSampler Class Reference

#include <gpr/ml/laplace/SliceSampler.h>

Public Types

using LogTarget = std::function<double(const Eigen::VectorXd&)>
using AlphaCacheFn = std::function<Eigen::VectorXd(const Eigen::VectorXd&)>

Public Member Functions

 SliceSampler (SliceSamplerOptions opts={})
bool sample (const Eigen::VectorXd &theta_init, const LogTarget &log_target, const AlphaCacheFn &alpha_fn, SliceSamplerState &out)

Detailed Description

Doubling slice sampler over a p-dim continuous parameter, with random-direction Gibbs proposals.

Definition at line 100 of file SliceSampler.h.

Member Typedef Documentation

◆ AlphaCacheFn

using gpr::laplace::SliceSampler::AlphaCacheFn = std::function<Eigen::VectorXd(const Eigen::VectorXd&)>

Caller-supplied alpha cache builder: given theta, returns alpha = K(theta)^-1 y. Called once per accepted sample. If null, alpha_cache is left empty.

Definition at line 112 of file SliceSampler.h.

◆ LogTarget

using gpr::laplace::SliceSampler::LogTarget = std::function<double(const Eigen::VectorXd&)>

Caller-supplied target: returns log p(theta | y) (or any quantity proportional to it on the natural log scale – the slice algorithm is invariant under additive constants). Returning a non-finite value (NaN, -inf) is interpreted as "outside support" and treated as below any slice level.

Definition at line 107 of file SliceSampler.h.

Constructor & Destructor Documentation

◆ SliceSampler()

gpr::laplace::SliceSampler::SliceSampler ( SliceSamplerOptions opts = {})
explicit

Definition at line 13 of file SliceSampler.cpp.

14 : opts_(opts), rng_(opts.seed)
15{
16}

Member Function Documentation

◆ sample()

bool gpr::laplace::SliceSampler::sample ( const Eigen::VectorXd & theta_init,
const LogTarget & log_target,
const AlphaCacheFn & alpha_fn,
SliceSamplerState & out )

Draw opts.n_samples + opts.n_burnin samples starting from theta_init. Returns true and fills out.theta_samples / out.alpha_cache on success; returns false and leaves out.valid == false if the initial point has non-finite log-target (no valid starting state).

Definition at line 120 of file SliceSampler.cpp.

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}
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out

The documentation for this class was generated from the following files: