Skip to content

Commit a906ae5

Browse files
committed
added numerical example. changed source code to match tutorial paper.
Former-commit-id: 3d9ab58 [formerly 3d9ab58 [formerly 45601d2]] Former-commit-id: 70ba9d6c42761f5d9ec7e73014cd16621c04dd74 Former-commit-id: ffe7754
1 parent c9c4263 commit a906ae5

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

r/codeForPaper/example4-sv-varyingN.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,15 @@ resThetaIACT <- matrix(0, nrow = length(noParticles), ncol = 3)
157157
resThetaIACTperSecond <- matrix(0, nrow = length(noParticles), ncol = 3)
158158

159159
for (k in 1:length(noParticles)) {
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)
160+
acf_mu <- acf(resTheta[k, , 1], plot = FALSE, lag.max = 250)
161+
acf_phi <- acf(resTheta[k, , 2], plot = FALSE, lag.max = 250)
162+
acf_sigmav <- acf(resTheta[k, , 3], plot = FALSE, lag.max = 250)
163163

164164
resThetaIACT[k, ] <- 1 + 2 * c(sum(acf_mu$acf), sum(acf_phi$acf), sum(acf_sigmav$acf))
165165
resThetaIACTperSecond[k, ] <- resThetaIACT[k, ] / computationalTimePerIteration[k]
166166
}
167167

168-
table <- rbind(noParticles, logLikelihoodVariance, 100 * acceptProbability, apply(resThetaIACT, 1, max), apply(resThetaIACT, 1, max) * computationalTimePerIteration, computationalTimePerIteration)
168+
table <- rbind(noParticles, sqrt(logLikelihoodVariance), 100 * acceptProbability, apply(resThetaIACT, 1, max), apply(resThetaIACT, 1, max) * computationalTimePerIteration, computationalTimePerIteration)
169169
table <- round(table, 2)
170170
print(table)
171171

r/helpers/parameterEstimation.R

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,17 @@ particleMetropolisHastingsSVmodel <- function(y, initialTheta, noParticles, noIt
197197
}
198198

199199
# Compute difference in the log-priors
200-
priorMu <- dnorm(thetaProposed[k, 1], 0, 1, log = TRUE) - dnorm(theta[k - 1, 1], 0, 1, log = TRUE)
201-
priorPhi <- dnorm(thetaProposed[k, 2], 0.95, 0.05, log = TRUE) - dnorm(theta[k - 1, 2], 0.95, 0.05, log = TRUE)
202-
priorSigmaV <- dgamma(thetaProposed[k, 3], 2, 10, log = TRUE) - dgamma(theta[k - 1, 3], 2, 10, log = TRUE)
200+
priorMu <- dnorm(thetaProposed[k, 1], 0, 1, log = TRUE)
201+
priorMu <- priorMu - dnorm(theta[k - 1, 1], 0, 1, log = TRUE)
202+
priorPhi <- dnorm(thetaProposed[k, 2], 0.95, 0.05, log = TRUE)
203+
priorPhi <- priorPhi - dnorm(theta[k - 1, 2], 0.95, 0.05, log = TRUE)
204+
priorSigmaV <- dgamma(thetaProposed[k, 3], 2, 10, log = TRUE)
205+
priorSigmaV <- priorSigmaV - dgamma(theta[k - 1, 3], 2, 10, log = TRUE)
206+
prior <- priorMu + priorPhi + priorSigmaV
203207

204208
# Compute the acceptance probability
205209
likelihoodDifference <- logLikelihoodProposed[k] - logLikelihood[k - 1]
206-
acceptProbability <- exp(priorMu + priorPhi + priorSigmaV + likelihoodDifference)
210+
acceptProbability <- exp(prior + likelihoodDifference)
207211

208212
# Always reject if parameter results in an unstable system
209213
acceptProbability <- acceptProbability * (abs(thetaProposed[k, 2]) < 1.0)

r/helpers/stateEstimation.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,14 @@ particleFilterSVmodel <- function(y, theta, noParticles) {
251251
# Propagate
252252
#=========================================================
253253
part1 <- mu + phi * (particles[newAncestors, t - 1] - mu)
254-
part2 <- rnorm(noParticles, 0, sigmav)
255-
particles[, t] <- part1 + part2
254+
particles[, t] <- part1 + rnorm(noParticles, 0, sigmav)
256255

257256
#=========================================================
258257
# Compute weights
259258
#=========================================================
260-
weights[, t] <- dnorm(y[t - 1], 0, exp(particles[, t] / 2), log = TRUE)
259+
yhatMean <- 0
260+
yhatVariance <- exp(particles[, t] / 2)
261+
weights[, t] <- dnorm(y[t - 1], yhatMean, yhatVariance, log = TRUE)
261262

262263
# Rescale log-weights and recover weights
263264
maxWeight <- max(weights[, t])
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1646f9979a4bd1eb87ec14fc714ec5142e08c77d
1+
63c37179c2a6d700ac3b9411bf85c2f6c5615ad9

0 commit comments

Comments
 (0)