Nothing
## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
library("qslice")
## ----target-------------------------------------------------------------------
## Target is a Gamma(shape = alpha, rate = 1) distribution
alpha <- 2.5
ltarget <- function(x) ifelse(x > 0.0, (alpha - 1.0) * log(x) - x, -Inf)
## ----step---------------------------------------------------------------------
## Set up MCMC
n_iter <- 3e3
x_sample <- numeric(n_iter)
state <- list(x = 0.5) # initialize
n_eval <- 0 # count target evaluations
## Run step-and-shrink slice sampler
for (i in 1:n_iter) {
tmp <- slice_stepping_out(x = state$x,
log_target = ltarget,
w = 2.0)
n_eval <- n_eval + tmp$nEvaluations
state$x <- tmp$x
x_sample[i] <- state$x
}
## Check samples
n_eval / n_iter # target evaluations per MCMC iteration
hist(x_sample, freq = FALSE, n = 30)
curve(dgamma(x, shape = alpha), col = "blue", lwd = 2, add = TRUE)
## Null hypothesis: Samples are iid Gamma(alpha, 1) (thin for better approximation)
ks.test(x_sample[seq(1, n_iter, by = 10)], pgamma, shape = alpha)
## ----pseudo1------------------------------------------------------------------
pseu <- pseudo_list(family = "t",
params = list(loc = 0, sc = 3, df = 1),
lb = 0)
## ----vis_pseudo1, fig.width=4.5, fig.height=3---------------------------------
utility_pseudo(pseudo = pseu, log_target = ltarget,
type = "function",
utility_type = "AUC", plot = TRUE)
## ----qslice1, fig.show='hold'-------------------------------------------------
## Set up MCMC
n_iter <- 3e3
x_sample <- psi_sample <- numeric(n_iter)
state <- list(x = 0.5) # initialize
n_eval <- 0 # count target evaluations
## Run quantile slice sampler
for (i in 1:n_iter) {
tmp <- slice_quantile(x = state$x,
log_target = ltarget,
pseudo = pseu)
n_eval <- n_eval + tmp$nEvaluations
state$x <- tmp$x
x_sample[i] <- tmp$x
psi_sample[i] <- tmp$u
}
## Check samples
n_eval / n_iter # target evaluations per iteration of MCMC
hist(x_sample, freq = FALSE, n = 30)
curve(dgamma(x, shape = alpha), col = "blue", lwd = 2, add = TRUE)
hist(psi_sample, freq = FALSE, n = 30, xlim = c(0,1))
utility_shrinkslice(u = psi_sample, utility_type = "AUC") # estimate AUC from transformed samples
## Null hypothesis: Samples are iid Gamma(alpha, 1)
ks.test(x_sample[seq(1, n_iter, by = 10)], pgamma, shape = alpha)
## ----pseudo2, fig.width=4.5, fig.height=3-------------------------------------
pseu_opt <- pseudo_opt(log_target = ltarget,
type = "function",
family = "t", df = c(1, 5, 20),
lb = 0,
utility_type = "AUC",
plot = TRUE)
## ----pseudo3------------------------------------------------------------------
pseu_opt <- pseudo_opt(samples = x_sample,
type = "samples",
family = "t", df = c(1, 5, 20),
lb = 0,
utility_type = "AUC",
plot = TRUE, nbins = 20)
names(pseu_opt)
names(pseu_opt$pseudo)
pseu_opt$pseudo$txt
## ----qslice2, fig.show='hold'-------------------------------------------------
## Set up MCMC
n_iter <- 3e3
x_sample <- psi_sample <- numeric(n_iter)
state <- list(x = 0.5) # initialize
n_eval <- 0 # count target evaluations
## Run optimized quantile slice sampler
for (i in 1:n_iter) {
tmp <- slice_quantile(x = state$x,
log_target = ltarget,
pseudo = pseu_opt$pseudo)
n_eval <- n_eval + tmp$nEvaluations
state$x <- tmp$x
x_sample[i] <- tmp$x
psi_sample[i] <- tmp$u
}
## Check samples
n_eval / n_iter # target evaluations per iteration of MCMC
hist(x_sample, freq = FALSE, n = 30)
curve(dgamma(x, shape = alpha), col = "blue", lwd = 2, add = TRUE)
hist(psi_sample, freq = FALSE, n = 30, xlim = c(0,1))
utility_shrinkslice(u = psi_sample, utility_type = "AUC") # estimate AUC from transformed samples
## Null hypothesis: Samples are iid Gamma(alpha, 1)
ks.test(x_sample[seq(1, n_iter, by = 10)], pgamma, shape = alpha)
## ----diagnostics_arg, eval = FALSE--------------------------------------------
# slice_quantile(..., diagnostics = TRUE)
## ----diagnostics_options, eval = FALSE----------------------------------------
# options(qslice.diagnostics = TRUE)
## ----diagnostics_getOption, eval = FALSE--------------------------------------
# getOption("qslice.diagnostics")
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.