slice_stepping_out: Slice sampler using the Stepping Out and Shrinkage Procedures

View source: R/slice_samplers_univariate.R

slice_stepping_outR Documentation

Slice sampler using the Stepping Out and Shrinkage Procedures

Description

Single update for the univariate slice sampler of Neal (2003) using the "stepping out" procedure, followed by the "shrinkage" procedure.

Usage

slice_stepping_out(x, log_target, w, max = Inf, diagnostics = NULL)

Arguments

x

The current state (as a numeric scalar).

log_target

A function taking numeric scalar that evaluates the (potentially unnormalized) log-target density, returning a numeric scalar.

w

A numeric scalar tuning the algorithm which gives the typical slice width. This is a main tuning parameter of the algorithm.

max

The limit m of Neal (2003) on the size of the initial interval, in units of w; at most max - 1 steps out are taken in total (max of zero or one gives no stepping out). Setting max to zero avoids some evaluations of log_target, but may lead to relatively high autocorrelation if w is too small. If w is too small, setting max to a large value (even Inf) should lead to low autocorrelation at the cost of more evaluations for log_target.

diagnostics

Logical, indicating whether the sampler should be run in diagnostic mode for informative error handling. When diagnostics is NULL, it is reset to match the value of getOption("qslice.diagnostics") which defaults to FALSE if the option is not set. Default for diagnostics is NULL.

Value

A list with two elements:

x is the new state.

nEvaluations is the number of evaluations of the target function used to obtain the new state.

References

Neal, R. M. (2003), "Slice sampling," The Annals of Statistics, 31, 705-767. \Sexpr[results=rd]{tools:::Rd_expr_doi("https://doi.org/10.1214/aos/1056562461")}

Examples

lf <- function(x) dbeta(x, 3, 4, log = TRUE)
draws <- numeric(10) + 0.5 # set to numeric(1e3) for more complete illustration
nEvaluations <- 0L
for (i in seq.int(2, length(draws))) {
  out <- slice_stepping_out(draws[i - 1], log_target = lf, w = 0.7, max = Inf)
  draws[i] <- out$x
  nEvaluations <- nEvaluations + out$nEvaluations
}
nEvaluations / (length(draws) - 1)
plot(density(draws), xlim = c(0, 1))
curve(exp(lf(x)), 0, 1, col = "blue", add = TRUE)


qslice documentation built on Oct. 22, 2026, 1:06 a.m.