# Example 1: Sampling from a Bivariate Normal (sequential mode)
# The 'mvtnorm' package is required for this example
if (requireNamespace("mvtnorm", quietly = TRUE)) {
log_post <- function(x) {
mvtnorm::dmvnorm(x, mean = c(0, 0), sigma = matrix(c(1, 0.8, 0.8, 1), 2, 2), log = TRUE)
}
# Run with fewer iterations for a quick example
# Set a seed for reproducibility
set.seed(123)
result_seq <- twalk(log_posterior = log_post, n_iter = 5000,
x0 = c(-1, 1), xp0 = c(1, -1))
plot(result_seq$all_samples, pch = '.', main = "t-walk Samples (Sequential)")
}
# \donttest{
# Example 2: The same problem in parallel (will run faster)
# Using 2 chains. n_iter is now per chain.
if (requireNamespace("mvtnorm", quietly = TRUE)) {
set.seed(123)
result_par <- twalk(log_posterior = log_post, n_iter = 2500,
x0 = c(-1, 1), xp0 = c(1, -1), n_chains = 2)
plot(result_par$all_samples, pch = '.', main = "t-walk Samples (Parallel)")
}
# }
Run the code above in your browser using DataLab