Skip to content

Commit b6774b9

Browse files
committed
updating, more to come
1 parent 5a0ef5e commit b6774b9

File tree

6 files changed

+2
-234
lines changed

6 files changed

+2
-234
lines changed

assignments/A1.pdf

-228 KB
Binary file not shown.

assignments/A1_solutions.pdf

-337 KB
Binary file not shown.

assignments/A2.pdf

-217 KB
Binary file not shown.

assignments/A2.tex

Lines changed: 0 additions & 148 deletions
This file was deleted.

code/cauchy_bimodal.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ chains_coupled = function(pot = U,
167167
xmat
168168
}
169169

170-
mc3 <- chains_coupled(tune = 1)
170+
mc3 <- chains_coupled(tune = .1)
171171

172172
mcmcSummary(mc3, rows = length(temps))
173173

@@ -185,4 +185,4 @@ for (k in 1:ncol(mc3)){
185185
mean(theta.draws) ## MCMC (HMC)
186186
posterior.mean.quadrature ## quadrature
187187
m_is ## Importance sampling
188-
colMeans(mc3)
188+
colMeans(mc3)

code/cauchy_bimodal_PT.r

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -45,87 +45,3 @@ curve(
4545
main = "Cauchy example -- BC exercise 1.28"
4646
)
4747

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

Comments
 (0)