|
| 1 | +xs <- c(-3, 2) |
| 2 | + |
| 3 | +### The likelihood |
| 4 | + |
| 5 | +c_loglik <- function(theta) { |
| 6 | + sum(dcauchy( |
| 7 | + x = xs, |
| 8 | + location = theta, |
| 9 | + log = TRUE |
| 10 | + )) |
| 11 | +} |
| 12 | +c_loglik <- Vectorize(c_loglik) |
| 13 | + |
| 14 | +Mu <- 0 |
| 15 | +Sigma <- 1 |
| 16 | + |
| 17 | +c_logposterior_kernel <- function(theta) { |
| 18 | + dnorm(x = theta, mean = Mu, sd = Sigma) + |
| 19 | + c_loglik(theta) |
| 20 | +} |
| 21 | +c_logposterior_kernel <- Vectorize(c_logposterior_kernel) |
| 22 | + |
| 23 | + |
| 24 | +minT <- -abs(4 * min(xs)) |
| 25 | +maxT <- abs(4 * max(xs)) |
| 26 | + |
| 27 | + |
| 28 | +## marginal likelihood via quadrature |
| 29 | +m_of_x <- integrate(function(t) |
| 30 | + exp(c_logposterior_kernel(t)), -Inf, Inf) |
| 31 | + |
| 32 | +c_posterior <- function(theta) { |
| 33 | + exp(c_logposterior_kernel(theta) - log(m_of_x$value)) |
| 34 | +} |
| 35 | +c_posterior <- Vectorize(c_posterior) |
| 36 | + |
| 37 | + |
| 38 | +curve( |
| 39 | + c_posterior, |
| 40 | + minT, |
| 41 | + maxT, |
| 42 | + lwd = 4, |
| 43 | + xlab = expression(theta), |
| 44 | + ylab = "Posterior density", |
| 45 | + main = "Cauchy example -- BC exercise 1.28" |
| 46 | +) |
| 47 | + |
| 48 | +############ Parallel tempering |
| 49 | + |
| 50 | + |
| 51 | +U = function(gam, x) |
| 52 | +{ |
| 53 | + - gam * c_logposterior_kernel(x) |
| 54 | +} |
| 55 | + |
| 56 | +curried = function(gam) |
| 57 | +{ |
| 58 | + message(paste("Returning a function for gamma =", gam)) |
| 59 | + function(x) |
| 60 | + U(gam, x) |
| 61 | +} |
| 62 | +U4 = curried(4) |
| 63 | + |
| 64 | +op = par(mfrow = c(2, 1)) |
| 65 | +curve(U4(x), minT, maxT, main = "Potential function, U(x)") |
| 66 | +curve(exp(-U4(x)), minT, maxT, main = "Unnormalised density function, exp(-U(x))") |
| 67 | +par(op) |
| 68 | + |
| 69 | + |
| 70 | +chain = function(target, tune = 0.1, init = 1) |
| 71 | +{ |
| 72 | + x = init |
| 73 | + xvec = numeric(iters) |
| 74 | + for (i in 1:iters) { |
| 75 | + can = x + rnorm(1, 0, tune) |
| 76 | + logA = target(x) - target(can) |
| 77 | + if (log(runif(1)) < logA) |
| 78 | + x = can |
| 79 | + xvec[i] = x |
| 80 | + } |
| 81 | + xvec |
| 82 | +} |
| 83 | + |
| 84 | +temps = 2 ^ (0:3) |
| 85 | +iters = 1e5 |
| 86 | + |
| 87 | +mat = sapply(lapply(temps, curried), chain) |
| 88 | +colnames(mat) = paste("gamma=", temps, sep = "") |
| 89 | + |
| 90 | +require(smfsb) |
| 91 | +mcmcSummary(mat, rows = length(temps)) |
| 92 | + |
| 93 | + |
| 94 | +chains_coupled = function(pot = U, |
| 95 | + tune = 0.1, |
| 96 | + init = 1) |
| 97 | +{ |
| 98 | + x = rep(init, length(temps)) |
| 99 | + xmat = matrix(0, iters, length(temps)) |
| 100 | + for (i in 1:iters) { |
| 101 | + can = x + rnorm(length(temps), 0, tune) |
| 102 | + logA = unlist(Map(pot, temps, x)) - unlist(Map(pot, temps, can)) |
| 103 | + accept = (log(runif(length(temps))) < logA) |
| 104 | + x[accept] = can[accept] |
| 105 | + # now the coupling update |
| 106 | + swap = sample(1:length(temps), 2) |
| 107 | + logA = pot(temps[swap[1]], x[swap[1]]) + pot(temps[swap[2]], x[swap[2]]) - |
| 108 | + pot(temps[swap[1]], x[swap[2]]) - pot(temps[swap[2]], x[swap[1]]) |
| 109 | + if (log(runif(1)) < logA) |
| 110 | + x[swap] = rev(x[swap]) |
| 111 | + # end of the coupling update |
| 112 | + xmat[i, ] = x |
| 113 | + } |
| 114 | + colnames(xmat) = paste("gamma=", temps, sep = "") |
| 115 | + xmat |
| 116 | +} |
| 117 | + |
| 118 | +mc3 <- chains_coupled(tune = 1) |
| 119 | + |
| 120 | +mcmcSummary(mc3, rows = length(temps)) |
| 121 | + |
| 122 | + |
| 123 | +par(mfrow = c(2, 2)) |
| 124 | +for (k in 1:ncol(mc3)){ |
| 125 | + hist(mc3[, k], probability = TRUE, |
| 126 | + xlim = c(minT, maxT), |
| 127 | + main = paste("gamma =", temps[k]), |
| 128 | + xlab = expression(theta)) |
| 129 | + curve(c_posterior, lwd = 3, add = TRUE) |
| 130 | +} |
| 131 | + |
0 commit comments