@@ -28,6 +28,12 @@ source("stateEstimationHelper.R")
2828# Set the random seed to replicate results in tutorial
2929set.seed(10 )
3030
31+ # Should the results be loaded from file (to quickly generate plots)
32+ loadSavedWorkspace <- FALSE
33+
34+ # Save plot to file
35+ savePlotToFile <- TRUE
36+
3137
3238# #############################################################################
3339# Define the model
@@ -51,6 +57,7 @@ T <- 250
5157# Set the initial state
5258initialState <- 0
5359
60+
5461# #############################################################################
5562# Generate data
5663# #############################################################################
@@ -59,6 +66,13 @@ data <- generateData(c(phi, sigmav, sigmae), T, initialState)
5966x <- data $ x
6067y <- data $ y
6168
69+ # Export plot to file
70+ if (savePlotToFile ) {
71+ cairo_pdf(" figures/example1-lgss.pdf" ,
72+ height = 10 ,
73+ width = 8 )
74+ }
75+
6276# Plot the latent state and observations
6377layout(matrix (c(1 , 1 , 2 , 2 , 3 , 4 ), 3 , 2 , byrow = TRUE ))
6478par (mar = c(4 , 5 , 0 , 0 ))
@@ -75,16 +89,28 @@ plot(
7589 ylim = c(- 6 , 6 ),
7690 bty = " n"
7791)
92+ polygon(c(grid , rev(grid )),
93+ c(y , rep(- 6 , T + 1 )),
94+ border = NA ,
95+ col = rgb(t(col2rgb(" #1B9E77" )) / 256 , alpha = 0.25 ))
96+
7897
7998# ##################################################################################
8099# State estimation using the particle filter and Kalman filter
81100# ##################################################################################
82101
83- # Using noParticles = 20 particles and plot the estimate of the latent state
84- noParticles <- 20
85- outputPF <- particleFilter(y , c(phi , sigmav , sigmae ), noParticles , initialState )
86- outputKF <- kalmanFilter(y , c(phi , sigmav , sigmae ), initialState , 0.01 )
87- difference <- outputPF $ xHatFiltered - outputKF $ xHatFiltered [- (T + 1 )]
102+ if (loadSavedWorkspace ) {
103+ load(" savedWorkspaces/example1-lgss.RData" )
104+ } else {
105+ # Using noParticles = 20 particles and plot the estimate of the latent state
106+ noParticles <- 20
107+ outputPF <-
108+ particleFilter(y , c(phi , sigmav , sigmae ), noParticles , initialState )
109+ outputKF <-
110+ kalmanFilter(y , c(phi , sigmav , sigmae ), initialState , 0.01 )
111+ difference <-
112+ outputPF $ xHatFiltered - outputKF $ xHatFiltered [- (T + 1 )]
113+ }
88114
89115grid <- seq(0 , T - 1 )
90116plot(
@@ -97,18 +123,25 @@ plot(
97123 ylim = c(- 0.1 , 0.1 ),
98124 bty = " n"
99125)
126+ polygon(
127+ c(grid , rev(grid )),
128+ c(difference , rep(- 0.1 , T )),
129+ border = NA ,
130+ col = rgb(t(col2rgb(" #7570B3" )) / 256 , alpha = 0.25 )
131+ )
100132
101133# Compute bias and MSE
102134logBiasMSE <- matrix (0 , nrow = 7 , ncol = 2 )
103135gridN <- c(10 , 20 , 50 , 100 , 200 , 500 , 1000 )
104136
105137for (ii in 1 : length(gridN )) {
106- pfEstimate <- particleFilter(y , c(phi , sigmav , sigmae ), gridN [ii ], initialState )
138+ pfEstimate <-
139+ particleFilter(y , c(phi , sigmav , sigmae ), gridN [ii ], initialState )
107140 pfEstimate <- pfEstimate $ xHatFiltered
108141 kfEstimate <- outputKF $ xHatFiltered [- (T + 1 )]
109142
110- logBiasMSE [ii , 1 ] <- log(mean(abs(pfEstimate - kfEstimate )))
111- logBiasMSE [ii , 2 ] <- log(mean((pfEstimate - kfEstimate )^ 2 ))
143+ logBiasMSE [ii , 1 ] <- log(mean(abs(pfEstimate - kfEstimate )))
144+ logBiasMSE [ii , 2 ] <- log(mean((pfEstimate - kfEstimate ) ^ 2 ))
112145}
113146
114147# Plot the bias and MSE for comparison
@@ -119,9 +152,15 @@ plot(
119152 type = " l" ,
120153 xlab = " no. particles (N)" ,
121154 ylab = " log-bias" ,
122- ylim = c(- 7 , - 3 ),
155+ ylim = c(- 7 ,- 3 ),
123156 bty = " n"
124157)
158+ polygon(
159+ c(gridN , rev(gridN )),
160+ c(logBiasMSE [, 1 ], rep(- 7 , length(gridN ))),
161+ border = NA ,
162+ col = rgb(t(col2rgb(" #E7298A" )) / 256 , alpha = 0.25 )
163+ )
125164points(gridN , logBiasMSE [, 1 ], col = " #E7298A" , pch = 19 )
126165
127166plot(
@@ -132,13 +171,32 @@ plot(
132171 type = " l" ,
133172 xlab = " no. particles (N)" ,
134173 ylab = " log-MSE" ,
135- ylim = c(- 12 , - 6 ),
174+ ylim = c(- 12 ,- 6 ),
136175 bty = " n"
137176)
177+ polygon(
178+ c(gridN , rev(gridN )),
179+ c(logBiasMSE [, 2 ], rep(- 12 , length(gridN ))),
180+ border = NA ,
181+ col = rgb(t(col2rgb(" #66A61E" )) / 256 , alpha = 0.25 )
182+ )
138183points(gridN , logBiasMSE [, 2 ], col = " #66A61E" , pch = 19 )
139184
185+ # Close the plotting device
186+ if (savePlotToFile ) {
187+ dev.off()
188+ }
189+
190+
191+ # #############################################################################
192+ # Compute and save the results
193+ # #############################################################################
194+
140195# Save the workspace to file
141- save(" example1-lgss.RData" )
196+ if (! loadSavedWorkspace ) {
197+ save.image(" savedWorkspaces/example1-lgss.RData" )
198+ }
199+
142200
143201# ##################################################################################
144202# End of file
0 commit comments