View source: R/slice_samplers_univariate.R
slice_latent | R Documentation |
Single update using the latent slice sampler of Li and Walker (2023).
slice_latent(x, s, log_target, rate)
x |
The current state (as a numeric scalar). |
s |
A random variable that determines the length of the initial shrinking interval. |
log_target |
A function taking numeric scalar that evaluates the (potentially unnormalized) log-target density, returning a numeric scalar. |
rate |
The rate parameter for the distribution of |
A list containing three elements:
x
is the new state of the target variable.
s
is the new state of the latent scale variable.
nEvaluations
is the number of evaluations of the
target function used to obtain the new state.
Li, Y. and Walker, S. G. (2023), "A latent slice sampling algorithm," Computational Statistics and Data Analysis, 179, 107652. \Sexpr[results=rd]{tools:::Rd_expr_doi("https://doi.org/10.1016/j.csda.2022.107652")}
lf <- function(x) dbeta(x, 3, 4, log = TRUE)
draws <- numeric(10) # set to numeric(1e3) for more complete illustration
nEvaluations <- 0L
s <- 0.5
for (i in seq.int(2, length(draws))) {
out <- slice_latent(draws[i - 1], s, log_target = lf, rate = 0.3)
draws[i] <- out$x
s <- out$s
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.