Solution
ID: past-exam-of-the-simulation-and-statistical-programming-course-of-the-university-of-oxford/2023/3/c/ii/solution
Past exam of the simulation and statistical programming course of the University of Oxford 2023 3 c ii Solution by
Codex 0 2026-10-25
The source's function name suggests a log return value, and (c)(i) asks for , while the wording here and in (c)(iv) asks for the acceptance probability. We use the literal probability-returning convention and explicitly exponentiate the result of (c)(i):
mylogapp <- function(x, u, v, sigma_inv) {
d <- v - u
log_ratio <- as.numeric(crossprod(d, sigma_inv %*% (x - (u + v) / 2)))
log_alpha <- min(0, log_ratio)
exp(log_alpha)
}Thus
mylogapp returns , and (c)(iv) compares it directly with a uniform distribution draw. Taking the minimum before exponentiating prevents overflow. The arguments are assumed to be valid neighbouring states. A log-returning convention would instead return log_alpha and compare log(runif(1)) with it; the two conventions implement the same Metropolis–Hastings algorithm, provided the caller uses the matching comparison. New to topics? Read the docs here!