View source: R/hyperrectangle_slice.R
slice_hyperrect | R Documentation |
Multivariate slice sampler in Algorithm 8 of Neal (2003) using the "shrinkage" procedure.
slice_hyperrect(x, log_target, w = NULL, L = NULL, R = NULL)
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 |
L |
Numeric vector giving the lower boundary of support in each dimension. |
R |
Numeric vector giving the upper boundary of support in each dimension.
Will be used if |
A list contains two elements: "x" is the new state and "nEvaluations" is the number of evaluations of the target function used to obtain the new state.
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")}
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.