Skip to content

Commit f355a9b

Browse files
committed
simplifies acceptance ratio
1 parent 814254c commit f355a9b

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

vignettes/Chapter07.Rmd

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,13 +1134,6 @@ c0 <- C0 <- 0.01
11341134
cthetas <- c(.01, .1, 1)
11351135
11361136
# Allocate space for the draws
1137-
# Sample theta via RW-MH
1138-
epsprop <- filter(dat, -thetaprop, "recursive", init = eps0)
1139-
1140-
logR <- -0.5 / sigma2 * (eps0^2 + sum(epsprop^2)) -
1141-
-0.5 / sigma2 * (eps0^2 + sum(eps^2)) +
1142-
dunif(thetaprop, -1, 1, log = TRUE) -
1143-
dunif(theta, -1, 1, log = TRUE)
11441137
eps0s <- sigma2s <- thetas <- matrix(NA_real_, ndraws, length(cthetas))
11451138
naccepts <- rep(0L, length(cthetas))
11461139
@@ -1163,11 +1156,20 @@ for (i in seq_along(cthetas)) {
11631156
CN <- C0 + 0.5 * (eps0^2 + sum(eps^2))
11641157
sigma2 <- rinvgamma(1, cN, CN)
11651158
1166-
if (log(runif(1)) < logR) {
1167-
theta <- thetaprop
1168-
}
1159+
# Sample theta via RW-MH
11691160
thetaprop <- rnorm(1, theta, cthetas[i])
1161+
epsprop <- filter(dat, -thetaprop, "recursive", init = eps0)
1162+
1163+
# Now we accept/reject. Note that because we have a uniform prior on
1164+
# (-1, 1), it suffices to check whether the proposed value is in that
1165+
# interval and we do not need to include the prior in the acceptance ratio
1166+
if (abs(thetaprop) < 1) {
1167+
logR <- -0.5 / sigma2 * (sum(epsprop^2) - sum(eps^2))
1168+
if (log(runif(1)) < logR) {
1169+
theta <- thetaprop
11701170
if (m > nburn) naccepts[i] <- naccepts[i] + 1L
1171+
}
1172+
}
11711173
11721174
# Store the results
11731175
if (m > nburn) {

0 commit comments

Comments
 (0)