slice_hyperrect: Multivariate Slice Sampler with Shrinking Hyperrectangle

View source: R/hyperrectangle_slice.R

slice_hyperrectR Documentation

Multivariate Slice Sampler with Shrinking Hyperrectangle

Description

Multivariate slice sampler in Algorithm 8 of Neal (2003) using the "shrinkage" procedure.

Usage

slice_hyperrect(
  x,
  log_target,
  w = NULL,
  L = NULL,
  R = NULL,
  diagnostics = NULL
)

Arguments

x

The current state (as a numeric vector).

log_target

A function taking numeric vector that evaluates the log-target density, returning a numeric scalar.

w

A numeric vector tuning the algorithm which gives the typical slice width in each dimension. This is a main tuning parameter of the algorithm. If NULL, the sampler begins shrinking from the supplied boundaries L and R (which should correspond with the support).

L

Optional numeric vector giving the lower boundary of support in each dimension.

R

Optional numeric vector giving the upper boundary of support in each dimension. Will be used if w is null. If all of L, R, and w are null, then the boundaries default to those of the unit hypercube.

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 containing two elements: "x" is the new state and "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[1], 3, 4, log = TRUE) + dbeta(x[2], 5, 3, log = TRUE)
n_iter <- 10 # set to 1e4 for more complete illustration
draws <- matrix(0.2, nrow = n_iter, ncol = 2)
nEvaluations <- 0L
for (i in seq.int(2, n_iter)) {
 out <- slice_hyperrect(draws[i - 1, ], log_target = lf, w = c(0.5, 0.5))
 draws[i,] <- out$x
 nEvaluations <- nEvaluations + out$nEvaluations
 cat(i, '\r')
}
nEvaluations / (nrow(draws) - 1)
plot(draws[,1], draws[,2], xlim = c(0, 1))
hist(draws[,1], freq = FALSE); curve(dbeta(x, 3, 4), col = "blue", add = TRUE)
hist(draws[,2], freq = FALSE); curve(dbeta(x, 5, 3), col = "blue", add = TRUE)


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