@@ -58,11 +58,10 @@ nPart = 100;
5858nBurnIn = 1000 ;
5959nIter = 5000 ;
6060
61- # The standard deviation in the random walk proposal
62- stepSize = 0.10 ;
63-
6461# Run the PMH algorithm
65- res = pmh(data $ y ,initPar ,sigmav ,sigmae ,nPart ,T ,x0 ,nIter ,stepSize )
62+ res1 = pmh(data $ y ,initPar ,sigmav ,sigmae ,nPart ,T ,x0 ,nIter ,stepSize = 0.01 )
63+ res2 = pmh(data $ y ,initPar ,sigmav ,sigmae ,nPart ,T ,x0 ,nIter ,stepSize = 0.10 )
64+ res3 = pmh(data $ y ,initPar ,sigmav ,sigmae ,nPart ,T ,x0 ,nIter ,stepSize = 0.50 )
6665
6766# #############################################################################
6867# Plot the results
@@ -71,32 +70,66 @@ res = pmh(data$y,initPar,sigmav,sigmae,nPart,T,x0,nIter,stepSize)
7170# Export plot to file
7271# cairo_pdf("example2-lgss.pdf", height = 10, width = 8)
7372
73+ layout(matrix (1 : 9 , 3 , 3 , byrow = TRUE )); par(mar = c(4 ,5 ,0 ,0 ))
74+
7475# Plot the parameter posterior estimate
75- # Solid black line indicate posterior mean
76- layout(matrix (1 : 3 , 3 , 1 , byrow = TRUE )); par(mar = c(4 ,5 ,0 ,0 ))
77- hist(res [nBurnIn : nIter ], breaks = floor(sqrt(nIter - nBurnIn )), col = rgb(t(col2rgb(" #7570B3" ))/ 256 ,alpha = 0.25 ),border = NA ,xlab = expression(phi ),ylab = " posterior estimate" ,main = " " ,xlim = c(0.5 ,0.8 ),ylim = c(0 ,12 ),freq = F )
78- lines(density(res [nBurnIn : nIter ],kernel = " e" ,from = 0.5 ,to = 0.8 ),lwd = 2 ,col = " #7570B3" )
76+ hist(res1 [nBurnIn : nIter ], breaks = floor(sqrt(nIter - nBurnIn )), col = rgb(t(col2rgb(" #7570B3" ))/ 256 ,alpha = 0.25 ),border = NA ,xlab = expression(phi ),ylab = " posterior estimate" ,main = " " ,xlim = c(0.5 ,0.8 ),ylim = c(0 ,12 ),freq = F )
77+ lines(density(res1 [nBurnIn : nIter ],kernel = " e" ,from = 0.5 ,to = 0.8 ),lwd = 2 ,col = " #7570B3" )
78+ lines(seq(0.5 ,1.0 ,0.01 ),dnorm(seq(0.5 ,1.0 ,0.01 ),0 ,1 ),lwd = 1 ,col = " darkgrey" )
79+ abline(v = mean(res1 [nBurnIn : nIter ]),lwd = 1 ,lty = " dotted" )
80+
81+ hist(res2 [nBurnIn : nIter ], breaks = floor(sqrt(nIter - nBurnIn )), col = rgb(t(col2rgb(" #E7298A" ))/ 256 ,alpha = 0.25 ),border = NA ,xlab = expression(phi ),ylab = " posterior estimate" ,main = " " ,xlim = c(0.5 ,0.8 ),ylim = c(0 ,12 ),freq = F )
82+ lines(density(res2 [nBurnIn : nIter ],kernel = " e" ,from = 0.5 ,to = 0.8 ),lwd = 2 ,col = " #E7298A" )
7983lines(seq(0.5 ,1.0 ,0.01 ),dnorm(seq(0.5 ,1.0 ,0.01 ),0 ,1 ),lwd = 1 ,col = " darkgrey" )
80- abline(v = mean(res [nBurnIn : nIter ]),lwd = 1 ,lty = " dotted" )
84+ abline(v = mean(res2 [nBurnIn : nIter ]),lwd = 1 ,lty = " dotted" )
85+
86+ hist(res3 [nBurnIn : nIter ], breaks = floor(sqrt(nIter - nBurnIn )), col = rgb(t(col2rgb(" #66A61E" ))/ 256 ,alpha = 0.25 ),border = NA ,xlab = expression(phi ),ylab = " posterior estimate" ,main = " " ,xlim = c(0.5 ,0.8 ),ylim = c(0 ,12 ),freq = F )
87+ lines(density(res3 [nBurnIn : nIter ],kernel = " e" ,from = 0.5 ,to = 0.8 ),lwd = 2 ,col = " #66A61E" )
88+ lines(seq(0.5 ,1.0 ,0.01 ),dnorm(seq(0.5 ,1.0 ,0.01 ),0 ,1 ),lwd = 1 ,col = " darkgrey" )
89+ abline(v = mean(res3 [nBurnIn : nIter ]),lwd = 1 ,lty = " dotted" )
90+
91+ # Plot the trace of the Markov chain during 1000 iterations after the burn-in
92+ plot(seq(nBurnIn ,nBurnIn + 1000 ,1 ),res1 [nBurnIn : (nBurnIn + 1000 )],col = ' #7570B3' , type = " l" ,xlab = " iteration" ,ylab = expression(phi ),bty = " n" ,ylim = c(0.5 ,0.8 ))
93+ grid = seq(nBurnIn ,nBurnIn + 1000 ,1 )
94+ polygon(c(grid ,rev(grid )),c(res1 [nBurnIn : (nBurnIn + 1000 )],rep(0 ,length(res1 [nBurnIn : (nBurnIn + 1000 )]))),border = NA ,col = rgb(t(col2rgb(" #7570B3" ))/ 256 ,alpha = 0.25 ))
95+ abline(h = mean(res1 [nBurnIn : nIter ]),lwd = 1 ,lty = " dotted" )
96+
97+ plot(seq(nBurnIn ,nBurnIn + 1000 ,1 ),res2 [nBurnIn : (nBurnIn + 1000 )],col = ' #E7298A' , type = " l" ,xlab = " iteration" ,ylab = expression(phi ),bty = " n" ,ylim = c(0.5 ,0.8 ))
98+ grid = seq(nBurnIn ,nBurnIn + 1000 ,1 )
99+ polygon(c(grid ,rev(grid )),c(res2 [nBurnIn : (nBurnIn + 1000 )],rep(0 ,length(res2 [nBurnIn : (nBurnIn + 1000 )]))),border = NA ,col = rgb(t(col2rgb(" #E7298A" ))/ 256 ,alpha = 0.25 ))
100+ abline(h = mean(res2 [nBurnIn : nIter ]),lwd = 1 ,lty = " dotted" )
81101
82- # Plot the trace of the Markov chain after burn-in
83- # Solid black line indicate posterior mean
84- plot(seq(nBurnIn ,nIter ,1 ),res [nBurnIn : nIter ],col = ' #7570B3' , type = " l" ,xlab = " iteration" ,ylab = expression(phi ),bty = " n" ,ylim = c(0.5 ,0.8 ))
85- grid = seq(nBurnIn ,nIter ,1 )
86- polygon(c(grid ,rev(grid )),c(res [nBurnIn : nIter ],rep(0 ,length(res [nBurnIn : nIter ]))),border = NA ,col = rgb(t(col2rgb(" #7570B3" ))/ 256 ,alpha = 0.25 ))
87- abline(h = mean(res [nBurnIn : nIter ]),lwd = 1 ,lty = " dotted" )
102+ plot(seq(nBurnIn ,nBurnIn + 1000 ,1 ),res3 [nBurnIn : (nBurnIn + 1000 )],col = ' #66A61E' , type = " l" ,xlab = " iteration" ,ylab = expression(phi ),bty = " n" ,ylim = c(0.5 ,0.8 ))
103+ grid = seq(nBurnIn ,nBurnIn + 1000 ,1 )
104+ polygon(c(grid ,rev(grid )),c(res3 [nBurnIn : (nBurnIn + 1000 )],rep(0 ,length(res3 [nBurnIn : (nBurnIn + 1000 )]))),border = NA ,col = rgb(t(col2rgb(" #66A61E" ))/ 256 ,alpha = 0.25 ))
105+ abline(h = mean(res3 [nBurnIn : nIter ]),lwd = 1 ,lty = " dotted" )
88106
89107# Plot the ACF of the Markov chain
90- foo = acf( res [nBurnIn : nIter ], plot = F )
91- plot(foo $ lag ,foo $ acf ,col = ' #7570B3' , type = " l" ,xlab = " iteration" ,ylab = " ACF" ,bty = " n" ,lwd = 2 )
108+ foo = acf( res1 [nBurnIn : nIter ], plot = F , lag.max = 60 )
109+ plot(foo $ lag ,foo $ acf ,col = ' #7570B3' , type = " l" ,xlab = " iteration" ,ylab = " ACF" ,bty = " n" ,lwd = 2 , ylim = c( - 0.2 , 1 ) )
92110polygon(c(foo $ lag ,rev(foo $ lag )),c(foo $ acf ,rep(0 ,length(foo $ acf ))),border = NA ,col = rgb(t(col2rgb(" #7570B3" ))/ 256 ,alpha = 0.25 ))
93111abline(h = 1.96 / sqrt(nIter - nBurnIn ),lty = " dotted" )
94112abline(h = - 1.96 / sqrt(nIter - nBurnIn ),lty = " dotted" )
95113
114+ foo = acf( res2 [nBurnIn : nIter ], plot = F , lag.max = 60 )
115+ plot(foo $ lag ,foo $ acf ,col = ' #E7298A' , type = " l" ,xlab = " iteration" ,ylab = " ACF" ,bty = " n" ,lwd = 2 ,ylim = c(- 0.2 ,1 ))
116+ polygon(c(foo $ lag ,rev(foo $ lag )),c(foo $ acf ,rep(0 ,length(foo $ acf ))),border = NA ,col = rgb(t(col2rgb(" #E7298A" ))/ 256 ,alpha = 0.25 ))
117+ abline(h = 1.96 / sqrt(nIter - nBurnIn ),lty = " dotted" )
118+ abline(h = - 1.96 / sqrt(nIter - nBurnIn ),lty = " dotted" )
119+
120+
121+ foo = acf( res3 [nBurnIn : nIter ], plot = F , lag.max = 60 )
122+ plot(foo $ lag ,foo $ acf ,col = ' #66A61E' , type = " l" ,xlab = " iteration" ,ylab = " ACF" ,bty = " n" ,lwd = 2 ,ylim = c(- 0.2 ,1 ))
123+ polygon(c(foo $ lag ,rev(foo $ lag )),c(foo $ acf ,rep(0 ,length(foo $ acf ))),border = NA ,col = rgb(t(col2rgb(" #66A61E" ))/ 256 ,alpha = 0.25 ))
124+ abline(h = 1.96 / sqrt(nIter - nBurnIn ),lty = " dotted" )
125+ abline(h = - 1.96 / sqrt(nIter - nBurnIn ),lty = " dotted" )
126+
96127# dev.off()
97128
98129# Estimate the parameter posterior mean
99- mean( res [nBurnIn : nIter ] )
130+ mean( res1 [nBurnIn : nIter ] )
131+ mean( res2 [nBurnIn : nIter ] )
132+ mean( res3 [nBurnIn : nIter ] )
100133
101134# #############################################################################
102135# End of file
0 commit comments