@@ -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