@@ -36,8 +36,11 @@ set.seed(10)
3636# Should the results be loaded from file (to quickly generate plots)
3737loadSavedWorkspace <- FALSE
3838
39- # Save plot to file
40- savePlotToFile <- FALSE
39+ # Should the proposals be tuned by a pilot run
40+ tuneProposals <- FALSE
41+
42+ # Should we use the tuned proposals (requires "../savedWorkspaces/example4-sv-varyingN-proposals.RData")
43+ useTunedProposals <- TRUE
4144
4245
4346# #############################################################################
@@ -61,7 +64,7 @@ y <- as.numeric(100 * diff(log(d$"Index Value")))
6164theta <- c(- 0.12 , 0.96 , 0.17 )
6265
6366# No. particles in the particle filter to try out
64- noParticles <- c(10 , 20 , 50 , 100 , 200 , 500 , 1000 )
67+ noParticles <- c(50 , 100 , 200 , 300 , 400 , 500 )
6568
6669# No. repetitions of log-likelihood estimate
6770noSimulations <- 1000
@@ -72,9 +75,7 @@ logLikelihoodVariance <- rep(0, length(noParticles))
7275computationalTimePerSample <- rep(0 , length(noParticles ))
7376
7477# Main loop
75- if (loadSavedWorkspace ) {
76- load(" ../savedWorkspaces/example4-sv-varyingN.RData" )
77- } else {
78+ if (! loadSavedWorkspace ) {
7879 for (k in 1 : length(noParticles )) {
7980 # Save the current time
8081 ptm <- proc.time()
@@ -97,6 +98,7 @@ if (loadSavedWorkspace) {
9798 }
9899}
99100
101+
100102# #############################################################################
101103# Parameter estimation using PMH
102104# #############################################################################
@@ -110,22 +112,14 @@ noBurnInIterations <- 2500
110112noIterations <- 7500
111113
112114# The standard deviation in the random walk proposal
113- stepSize <- matrix (
114- c(
115- 0.137255431 ,
116- - 0.0016258103 ,
117- 0.0015047492 ,
118- - 0.0016258103 ,
119- 0.0004802053 ,
120- - 0.0009973058 ,
121- 0.0015047492 ,
122- - 0.0009973058 ,
123- 0.0031307062
124- ),
125- ncol = 3 ,
126- nrow = 3
127- )
128- stepSize <- 0.8 * stepSize
115+ if (useTunedProposals ) {
116+ load(file = " ../savedWorkspaces/example4-sv-varyingN-proposals.RData" )
117+ } else {
118+ proposals <- array (0 , dim = c(length(noParticles ), 3 , 3 ))
119+ for (k in 1 : length(noParticles )) {
120+ proposals [k , , ] <- diag(c(0.10 , 0.01 , 0.05 ) ^ 2 )
121+ }
122+ }
129123
130124# Main loop
131125if (loadSavedWorkspace ) {
@@ -140,7 +134,7 @@ if (loadSavedWorkspace) {
140134 ptm <- proc.time()
141135
142136 # Run the PMH algorithm
143- res <- particleMetropolisHastingsSVmodel(y , initialTheta , noParticles [k ], noIterations , stepSize )
137+ res <- particleMetropolisHastingsSVmodel(y , initialTheta , noParticles [k ], noIterations , stepSize = proposals [ k , ,] )
144138
145139 # Save the parameter trace
146140 resTheta [k , ,] <- res $ theta [noBurnInIterations : noIterations ,]
@@ -154,6 +148,7 @@ if (loadSavedWorkspace) {
154148 }
155149}
156150
151+
157152# #############################################################################
158153# Post-processing (computing IACT and IACT * time)
159154# #############################################################################
@@ -162,48 +157,31 @@ resThetaIACT <- matrix(0, nrow = length(noParticles), ncol = 3)
162157resThetaIACTperSecond <- matrix (0 , nrow = length(noParticles ), ncol = 3 )
163158
164159for (k in 1 : length(noParticles )) {
165- acf_mu <- acf(resTheta [k , , 1 ], plot = FALSE , lag.max = 200 )
166- acf_phi <- acf(resTheta [k , , 2 ], plot = FALSE , lag.max = 200 )
167- acf_sigmav <- acf(resTheta [k , , 3 ], plot = FALSE , lag.max = 200 )
160+ acf_mu <- acf(resTheta [k , , 1 ], plot = FALSE , lag.max = 100 )
161+ acf_phi <- acf(resTheta [k , , 2 ], plot = FALSE , lag.max = 100 )
162+ acf_sigmav <- acf(resTheta [k , , 3 ], plot = FALSE , lag.max = 100 )
168163
169164 resThetaIACT [k , ] <- 1 + 2 * c(sum(acf_mu $ acf ), sum(acf_phi $ acf ), sum(acf_sigmav $ acf ))
170165 resThetaIACTperSecond [k , ] <- resThetaIACT [k , ] / computationalTimePerIteration [k ]
171166}
172167
173- print(rbind(noParticles , apply(resThetaIACT , 1 , max ), apply(resThetaIACTperSecond , 1 , max )))
174-
175-
176-
177- # # Export plot to file
178- # if (savePlotToFile) {
179- # cairo_pdf("figures/example4-sv-varyingN.pdf",
180- # height = 10,
181- # width = 8)
182- # }
183- #
184- # layout(matrix(1:14, 7, 2, byrow = FALSE))
185- # par(mar = c(4, 5, 0, 0))
186- #
187- # for (k in 1:length(noParticles)) {
188- # xlab = ""
189- # if (k == length(noParticles)) { xlab = "log-likelihood"}
190- # hist(
191- # logLikelihoodEstimates[k, ],
192- # breaks = floor(sqrt(noSimulations)),
193- # col = rgb(t(col2rgb("#1B9E77")) / 256, alpha = 0.25),
194- # border = NA,
195- # xlab = xlab,
196- # ylab = "",
197- # xlim = c(-730, -680),
198- # main = "",
199- # freq = FALSE
200- # )
201- # }
202- #
203- # # Close the plotting device
204- # if (savePlotToFile) {
205- # dev.off()
206- # }
168+ table <- rbind(noParticles , logLikelihoodVariance , 100 * acceptProbability , apply(resThetaIACT , 1 , max ), apply(resThetaIACT , 1 , max ) * computationalTimePerIteration , computationalTimePerIteration )
169+ table <- round(table , 2 )
170+ print(table )
171+
172+
173+ # #############################################################################
174+ # Tune the PMH proposal using a pilot run
175+ # #############################################################################
176+
177+ if (tuneProposals ) {
178+ proposals <- array (0 , dim = c(length(noParticles ), 3 , 3 ))
179+
180+ for (k in 1 : length(noParticles )) {
181+ proposals [k , , ] <- cov(resTheta [k , , ]) * 2.562 ^ 2 / 3
182+ }
183+ save(proposals , file = " ../savedWorkspaces/example4-sv-varyingN-proposals.RData" )
184+ }
207185
208186
209187# #############################################################################
0 commit comments