imh_pseudo {qslice} | R Documentation |
Independence Metropolis-Hastings
Description
Independence Metropolis-Hastings
Usage
imh_pseudo(x, log_target, pseudo)
Arguments
x |
The current state (scalar or numeric vector). |
log_target |
A function taking a scalar or numeric vector that evaluates the log-target density, returning a numeric scalar. |
pseudo |
List specifying the pseudo-target (proposal distribution). If the list length is
equal to the number of dimensions in If |
Value
A list containing the new state, x
, and whether the proposed value was accepted, logical accpt
.
Examples
lf <- function(x) dbeta(x[1], 3, 4, log = TRUE) + dbeta(x[2], 5, 3, log = TRUE)
n_iter <- 100 # set to 1e3 for more complete illustration
draws <- matrix(0.2, nrow = n_iter, ncol = 2)
nAccpt <- 0L
pseudo <- list( list(ld = function(x) dbeta(x, 2, 2, log = TRUE),
q = function(u) qbeta(u, 2, 2)),
list(ld = function(x) dbeta(x, 2, 2, log = TRUE),
q = function(u) qbeta(u, 2, 2))
)
for (i in seq.int(2, n_iter)) {
out <- imh_pseudo(draws[i - 1, ], log_target = lf, pseudo = pseudo)
draws[i,] <- out$x
nAccpt <- nAccpt + out$accpt
cat(i, '\r')
}
nAccpt / (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)