imh_pseudo: Independence Metropolis-Hastings

View source: R/independence_MH.R

imh_pseudoR Documentation

Independence Metropolis-Hastings

Description

Single update using an independence Metropolis-Hastings sampler with a pseudo-target as the proposal distribution.

Usage

imh_pseudo(x, log_target, pseudo, diagnostics = NULL)

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 x, each element is itself a list that specifies the pseudo-target independently for each corresponding dimension with functions ld that evaluates the log density for that dimension, and q that evaluates the quantile (inverse-CDF) function for that dimension. If the dimension of x is one, then supply only the inner list specifying the single pseudo-target.

If x is a vector but a single pseudo-target is supplied, the list must contain a log-density function ld that accepts a vector, and a function r that takes no arguments and generates a single multivariate draw from the proposal distribution.

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 the new state, x, whether the proposed value was accepted, logical accpt, and the number of evaluations of the target function, nEvaluations.

Examples

lf <- function(x) dbeta(x[1], 3, 4, log = TRUE) + dbeta(x[2], 5, 3, log = TRUE)
n_iter <- 10 # 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)

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