Skip to content

Commit 6d55c42

Browse files
author
Johan Dahlin
committed
updated code when revising the tutorial
Former-commit-id: 3dd1310 [formerly 3dd1310 [formerly 5768bd9]] Former-commit-id: a7e0f6ed90597448b38d8fd4639ab22380d9a418 Former-commit-id: 03ec09f
1 parent 2384d93 commit 6d55c42

4 files changed

Lines changed: 75 additions & 50 deletions

File tree

r/example1-lgss.R

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,53 +45,46 @@ x = data$x
4545
y = data$y
4646

4747
# Export plot to file
48-
#cairo_pdf("example1-lgss.pdf", height = 10, width = 8)
48+
cairo_pdf("example1-lgss.pdf", height = 10, width = 8)
4949

5050
# Plot the latent state and observations
51-
layout(matrix(1:3, 3, 1, byrow = TRUE));
51+
layout(matrix(c(1,1,2,2,3,4), 3, 2, byrow = TRUE));
5252
par(mar=c(4,5,0,0))
5353
grid = seq(0,T);
5454

5555
plot(grid,y,col="#1B9E77",lwd=1.5,type="l",xlab="time",ylab="observation",bty="n",ylim=c(-6,6))
5656
polygon(c(grid,rev(grid)),c(y,rep(-7,T+1)),border=NA,col=rgb(t(col2rgb("#1B9E77"))/256,alpha=0.25))
5757

58-
plot(grid,x,col="#D95F02",lwd=1.5,type="l",xlab="time",ylab="latent state",bty="n",ylim=c(-6,6))
59-
polygon(c(grid,rev(grid)),c(x,rep(-7,T+1)),border=NA,col=rgb(t(col2rgb("#D95F02"))/256,alpha=0.25))
60-
6158
###################################################################################
62-
# State estimation using the particle filter
59+
# State estimation using the particle filter and Kalman filter
6360
###################################################################################
6461

65-
grid = seq(0,T-1);
66-
6762
# Using N = 20 particles and plot the estimate of the latent state
6863
N <- 20;
6964
outPF <- sm(y,phi,sigmav,sigmae,N,T,x0)
70-
plot(grid,outPF$xh,col="#7570B3",lwd=1.5,type="l",xlab="time",ylab="state estimate",bty="n",ylim=c(-6,6))
71-
polygon(c(grid,rev(grid)),c(outPF$xh,rep(-7,T)),border=NA,col=rgb(t(col2rgb("#7570B3"))/256,alpha=0.25))
65+
outKF <- kf(y,phi,sigmav,sigmae,x0,0.01);
66+
diff <- outPF$xh-outKF$xh[-(T+1)];
7267

73-
###################################################################################
74-
# State estimation using the Kalman filter
75-
###################################################################################
68+
grid = seq(0,T-1);
69+
plot(grid,diff,col="#7570B3",lwd=1.5,type="l",xlab="time",ylab="difference in state estimate",bty="n",ylim=c(-0.1,0.1))
70+
polygon(c(grid,rev(grid)),c(diff,rep(-0.10,T)),border=NA,col=rgb(t(col2rgb("#7570B3"))/256,alpha=0.25))
7671

77-
outKF <- kf(y,phi,sigmav,sigmae,x0,0.01);
78-
lines( grid, outKF$xh[-(T+1)], col="black", lty="dashed",lwd=1.5)
79-
80-
#dev.off()
81-
82-
# Compute bias
83-
# log( mean( abs(sm(y,phi,sigmav,sigmae,10,T,x0)$xh-outKF$xh[-(T+1)]) ) )
84-
# log( mean( abs(sm(y,phi,sigmav,sigmae,20,T,x0)$xh-outKF$xh[-(T+1)]) ) )
85-
# log( mean( abs(sm(y,phi,sigmav,sigmae,50,T,x0)$xh-outKF$xh[-(T+1)]) ) )
86-
# log( mean( abs(sm(y,phi,sigmav,sigmae,100,T,x0)$xh-outKF$xh[-(T+1)]) ) )
87-
# log( mean( abs(sm(y,phi,sigmav,sigmae,200,T,x0)$xh-outKF$xh[-(T+1)]) ) )
88-
89-
# Compute MSE
90-
# log( mean( (sm(y,phi,sigmav,sigmae,10,T,x0)$xh-outKF$xh[-(T+1)])^2 ) )
91-
# log( mean( (sm(y,phi,sigmav,sigmae,20,T,x0)$xh-outKF$xh[-(T+1)])^2 ) )
92-
# log( mean( (sm(y,phi,sigmav,sigmae,50,T,x0)$xh-outKF$xh[-(T+1)])^2 ) )
93-
# log( mean( (sm(y,phi,sigmav,sigmae,100,T,x0)$xh-outKF$xh[-(T+1)])^2 ) )
94-
# log( mean( (sm(y,phi,sigmav,sigmae,200,T,x0)$xh-outKF$xh[-(T+1)])^2 ) )
72+
# Compute bias and MSE
73+
logBiasMSE = matrix( 0, nrow=7, ncol=2);
74+
gridN = c( 10, 20, 50, 100, 200, 500, 1000);
75+
76+
for ( ii in 1:length(gridN) ) {
77+
logBiasMSE[ ii, 1 ] = log( mean( abs(sm(y,phi,sigmav,sigmae,gridN[ii],T,x0)$xh-outKF$xh[-(T+1)]) ) )
78+
logBiasMSE[ ii, 2 ] = log( mean( (sm(y,phi,sigmav,sigmae,gridN[ii],T,x0)$xh-outKF$xh[-(T+1)])^2 ) )
79+
}
80+
81+
# Plot the bias and MSE for comparison
82+
plot( gridN,logBiasMSE[ , 1 ],col="#E7298A",lwd=1.5,type="l",xlab="no. particles (N)",ylab="log-bias",bty="n",ylim=c(-7,-3))
83+
points( gridN,logBiasMSE[ , 1 ],col="#E7298A",pch=19)
84+
plot( gridN,logBiasMSE[ , 2 ],col="#66A61E",lwd=1.5,type="l",xlab="no. particles (N)",ylab="log-MSE",bty="n",ylim=c(-12,-6))
85+
points( gridN,logBiasMSE[ , 2 ],col="#66A61E",pch=19)
86+
87+
dev.off()
9588

9689
###################################################################################
9790
# End of file

r/example2-lgss.R

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,10 @@ nPart = 100;
5858
nBurnIn = 1000;
5959
nIter = 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")
7983
lines(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))
92110
polygon(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))
93111
abline(h=1.96/sqrt(nIter-nBurnIn),lty="dotted")
94112
abline(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

r/example2-lgss.RData

195 KB
Binary file not shown.

r/example2-lgss.RData.REMOVED.git-id

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)