14 : opts_(opts), rng_(opts.seed)
18Eigen::VectorXd SliceSampler::randomUnitDirection(
int p)
22 std::normal_distribution<double> normal(0.0, 1.0);
24 for (
int i = 0; i < p; ++i)
26 const double n = d.norm();
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,
41 const int p =
static_cast<int>(theta_curr.size());
46 std::uniform_real_distribution<double> unif(0.0, 1.0);
47 const double log_u = log_p_curr + std::log(unif(rng_));
50 const Eigen::VectorXd d = randomUnitDirection(p);
57 double lower_off = -w * unif(rng_);
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);
63 double log_p_upper = log_target(upper_pt);
68 (log_p_lower > log_u || log_p_upper > log_u)) {
69 const double r = unif(rng_);
71 lower_off -= (upper_off - lower_off);
72 lower_pt = theta_curr + lower_off * d;
73 log_p_lower = log_target(lower_pt);
76 upper_off += (upper_off - lower_off);
77 upper_pt = theta_curr + upper_off * d;
78 log_p_upper = log_target(upper_pt);
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);
98 if (std::isfinite(log_p_prop) && log_p_prop > log_u) {
100 theta_next = std::move(prop);
101 log_p_next = log_p_prop;
106 if (prop_off < 0.0) {
107 lower_off = prop_off;
109 upper_off = prop_off;
115 theta_next = theta_curr;
116 log_p_next = log_p_curr;
124 out.theta_samples.clear();
125 out.alpha_cache.clear();
127 out.n_target_evals = 0;
132 if (theta_init.size() == 0)
return false;
134 Eigen::VectorXd theta = theta_init;
135 double log_p = log_target(theta);
136 ++
out.n_target_evals;
137 if (!std::isfinite(log_p)) {
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);
146 for (
int t = 0; t < n_total; ++t) {
147 Eigen::VectorXd theta_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;
156 theta = std::move(theta_next);
166 if (t >= opts_.n_burnin) {
167 out.theta_samples.push_back(theta);
169 Eigen::VectorXd a = alpha_fn(theta);
172 out.alpha_cache.clear();
176 out.alpha_cache.push_back(std::move(a));
181 out.valid = !
out.theta_samples.empty();
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.