= Solution
The source's function name suggests a log return value, and (c)(i) asks for $\log\alpha$, 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 $\alpha\in[0,1]$, and (c)(iv) compares it directly with a <uniform distribution> draw. Taking the minimum before exponentiating prevents overflow. The arguments $u,v$ 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.
Back to article page