Skip to content

Commit 462f865

Browse files
committed
Updated e-mail address
1 parent 9202b9f commit 462f865

13 files changed

+130
-130
lines changed

r/example1-lgss.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
##############################################################################
22
# State estimation in a LGSS model using particle and Kalman filters
33
#
4-
# Johan Dahlin <liu (at) johandahlin.com.nospam>
4+
# Johan Dahlin <uni (at) johandahlin.com.nospam>
55
# Documentation at https://github.com/compops/pmh-tutorial
66
# Published under GNU General Public License
77
##############################################################################
@@ -105,7 +105,7 @@ for (ii in 1:length(gridN)) {
105105
particleFilter(y, c(phi, sigmav, sigmae), gridN[ii], initialState)
106106
pfEstimate <- pfEstimate$xHatFiltered
107107
kfEstimate <- outputKF$xHatFiltered[-(T + 1)]
108-
108+
109109
logBiasMSE[ii, 1] <- log(mean(abs(pfEstimate - kfEstimate)))
110110
logBiasMSE[ii, 2] <- log(mean((pfEstimate - kfEstimate) ^ 2))
111111
}
@@ -158,7 +158,7 @@ if (savePlotToFile) {
158158
# Print a table (no. particles, log-bias, log-mse)
159159
print(t(rbind(gridN, t(logBiasMSE))))
160160

161-
# gridN
161+
# gridN
162162
# [1,] 10 -3.696997 -6.938594
163163
# [2,] 20 -3.964671 -7.493297
164164
# [3,] 50 -4.567552 -8.718346

r/example2-lgss.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
##############################################################################
22
# Parameter estimation using particle Metropolis-Hastings in a LGSS model.
33
#
4-
# Johan Dahlin <liu (at) johandahlin.com.nospam>
4+
# Johan Dahlin <uni (at) johandahlin.com.nospam>
55
# Documentation at https://github.com/compops/pmh-tutorial
66
# Published under GNU General Public License
77
##############################################################################

r/example3-sv.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
##############################################################################
22
# Parameter estimation using particle Metropolis-Hastings in a SV model
33
#
4-
# Johan Dahlin <liu (at) johandahlin.com.nospam>
4+
# Johan Dahlin <uni (at) johandahlin.com.nospam>
55
# Documentation at https://github.com/compops/pmh-tutorial
66
# Published under GNU General Public License
77
##############################################################################

r/example4-sv.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Parameter estimation using particle Metropolis-Hastings in a SV
33
# with a proposal adapted from a pilot run.
44
#
5-
# Johan Dahlin <liu (at) johandahlin.com.nospam>
5+
# Johan Dahlin <uni (at) johandahlin.com.nospam>
66
# Documentation at https://github.com/compops/pmh-tutorial
77
# Published under GNU General Public License
88
##############################################################################

r/example5-sv.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Parameter estimation using particle Metropolis-Hastings in a reparameterised version of a
33
# stochastic volatility model with a proposal adapted from a pilot run.
44
#
5-
# Johan Dahlin <liu (at) johandahlin.com.nospam>
5+
# Johan Dahlin <uni (at) johandahlin.com.nospam>
66
# Documentation at https://github.com/compops/pmh-tutorial
77
# Published under GNU General Public License
88
##############################################################################

r/extra-code-for-tutorial/example1-lgss-plotData.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
##############################################################################
22
# Generates and plots data from a LGSS model.
33
#
4-
# Johan Dahlin <liu (at) johandahlin.com.nospam>
4+
# Johan Dahlin <uni (at) johandahlin.com.nospam>
55
# Documentation at https://github.com/compops/pmh-tutorial
66
# Published under GNU General Public License
77
##############################################################################

r/extra-code-for-tutorial/example2-lgss-varyingT.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
##############################################################################
2-
# Runs the particle Metropolis-Hastings algorithm from different number
2+
# Runs the particle Metropolis-Hastings algorithm from different number
33
# of observations generated from a LGSS model.
44
#
5-
# Johan Dahlin <liu (at) johandahlin.com.nospam>
5+
# Johan Dahlin <uni (at) johandahlin.com.nospam>
66
# Documentation at https://github.com/compops/pmh-tutorial
77
# Published under GNU General Public License
88
##############################################################################
@@ -44,7 +44,7 @@ if (loadSavedWorkspace) {
4444
load("../savedWorkspaces/example2-lgss-varyingT.RData")
4545
} else {
4646
for (i in 1:length(TT)) {
47-
47+
4848
set.seed(10)
4949
data <- generateData(c(phi, sigmav, sigmae), TT[i], initialState)
5050
res <-
@@ -58,7 +58,7 @@ if (loadSavedWorkspace) {
5858
noIterations,
5959
stepSize
6060
)
61-
61+
6262
Tmean[i] <- mean(res[noBurnInIterations:noIterations])
6363
Tvar[i] <- var(res[noBurnInIterations:noIterations])
6464
}

r/extra-code-for-tutorial/example4-sv-plotProposals.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
##############################################################################
2-
# Ugly code to plot the estimate of the posterior distribution and the
3-
# proposal distribution adapted from a pilot run of particle
2+
# Ugly code to plot the estimate of the posterior distribution and the
3+
# proposal distribution adapted from a pilot run of particle
44
# Metropolis-Hastings.
55
#
6-
# Johan Dahlin <liu (at) johandahlin.com.nospam>
6+
# Johan Dahlin <uni (at) johandahlin.com.nospam>
77
# Documentation at https://github.com/compops/pmh-tutorial
88
# Published under GNU General Public License
99
##############################################################################
@@ -15,7 +15,7 @@ library("mvtnorm")
1515
# Save plot to file
1616
savePlotToFile <- FALSE
1717

18-
# Load the run
18+
# Load the run
1919
load("../savedWorkspaces/example3-sv.RData")
2020

2121
##############################################################################
@@ -85,7 +85,7 @@ for (ii in 1:length(gridth2)) {
8585

8686

8787
##############################################################################
88-
# Evaluate the proposal distribution over the grid centered at the
88+
# Evaluate the proposal distribution over the grid centered at the
8989
# posterior mean
9090
##############################################################################
9191

r/extra-code-for-tutorial/example4-sv-varyingN.R

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Example of particle Metropolis-Hastings in a stochastic volatility model
33
# The effect on mixing while varying N.
44
#
5-
# Johan Dahlin <liu (at) johandahlin.com.nospam>
5+
# Johan Dahlin <uni (at) johandahlin.com.nospam>
66
# Documentation at https://github.com/compops/pmh-tutorial
77
# Published under GNU General Public License
88
##############################################################################
@@ -58,19 +58,19 @@ if (!loadSavedWorkspace) {
5858
for (k in 1:length(noParticles)) {
5959
# Save the current time
6060
ptm <- proc.time()
61-
61+
6262
for (i in 1:noSimulations) {
6363
# Run the particle filter
6464
res <- particleFilterSVmodel(y, theta, noParticles[k])
65-
65+
6666
# Save the log-Likelihood estimate
6767
logLikelihoodEstimates[k, i] <- res$logLikelihood
6868
}
69-
69+
7070
# Compute the variance of the log-likelihood and computational time per sample
7171
logLikelihoodVariance[k] <- var(logLikelihoodEstimates[k, ])
7272
computationalTimePerSample[k] <- (proc.time() - ptm)[3] / noSimulations
73-
73+
7474
# Print to screen
7575
print(paste(paste(paste(paste("Simulation: ", k, sep = ""), " of ", sep = ""), length(noParticles), sep = ""), " completed.", sep = ""))
7676
print(paste(paste(paste(paste("No. particles: ", noParticles[k], sep = ""), " requires ", sep = ""), computationalTimePerSample[k], sep = ""), " seconds for computing one sample.", sep = ""))
@@ -104,21 +104,21 @@ if (loadSavedWorkspace) {
104104
resTheta <- array(0, dim = c(length(noParticles), noIterations - noBurnInIterations + 1, 3))
105105
computationalTimePerIteration <- rep(0, length(noParticles))
106106
acceptProbability <- rep(0, length(noParticles))
107-
107+
108108
for (k in 1:length(noParticles)) {
109109
# Save the current time
110110
ptm <- proc.time()
111-
111+
112112
# Run the PMH algorithm
113113
res <- particleMetropolisHastingsSVmodel(y, initialTheta, noParticles[k], noIterations, stepSize = proposals[k, ,])
114-
114+
115115
# Save the parameter trace
116116
resTheta[k, ,] <- res$theta[noBurnInIterations:noIterations,]
117-
117+
118118
# Compute acceptance probability and computational time per sample
119-
computationalTimePerIteration[k] <- (proc.time() - ptm)[3] / noIterations
119+
computationalTimePerIteration[k] <- (proc.time() - ptm)[3] / noIterations
120120
acceptProbability[k] <- mean(res$proposedThetaAccepted[noBurnInIterations:noIterations])
121-
121+
122122
# Print to screen
123123
print(paste(paste(paste(paste("Simulation: ", k, sep = ""), " of ", sep = ""), length(noParticles), sep = ""), " completed.", sep = ""))
124124
}
@@ -134,7 +134,7 @@ for (k in 1:length(noParticles)) {
134134
acf_mu <- acf(resTheta[k, , 1], plot = FALSE, lag.max = 250)
135135
acf_phi <- acf(resTheta[k, , 2], plot = FALSE, lag.max = 250)
136136
acf_sigmav <- acf(resTheta[k, , 3], plot = FALSE, lag.max = 250)
137-
137+
138138
resThetaIACT[k, ] <- 1 + 2 * c(sum(acf_mu$acf), sum(acf_phi$acf), sum(acf_sigmav$acf))
139139
resThetaIACTperSecond[k, ] <- resThetaIACT[k, ] / computationalTimePerIteration[k]
140140
}
@@ -148,7 +148,7 @@ print(table)
148148
##############################################################################
149149
if (tuneProposals) {
150150
proposals <- array(0, dim = c(length(noParticles), 3, 3))
151-
151+
152152
for (k in 1:length(noParticles)) {
153153
proposals[k, , ] <- cov(resTheta[k, , ]) * 2.562^2 / 3
154154
}

r/helpers/dataGeneration.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
##############################################################################
22
# Generating data from a LGSS model
33
#
4-
# Johan Dahlin <liu (at) johandahlin.com.nospam>
4+
# Johan Dahlin <uni (at) johandahlin.com.nospam>
55
# Documentation at https://github.com/compops/pmh-tutorial
66
# Published under GNU General Public License
77
##############################################################################
8-
generateData <- function(theta, noObservations, initialState)
8+
generateData <- function(theta, noObservations, initialState)
99
{
10-
phi <- theta[1]
10+
phi <- theta[1]
1111
sigmav <- theta[2]
1212
sigmae <- theta[3]
13-
13+
1414
state <- matrix(0, nrow = noObservations + 1, ncol = 1)
1515
observation <- matrix(0, nrow = noObservations + 1, ncol = 1)
16-
16+
1717
state[1] <- initialState
1818
observation[1] <- NA
19-
19+
2020
for (t in 2:(noObservations + 1)) {
2121
state[t] <- phi * state[t - 1] + sigmav * rnorm(1)
2222
observation[t] <- state[t] + sigmae * rnorm(1)
2323
}
24-
24+
2525
list(x = state, y = observation)
2626
}

0 commit comments

Comments
 (0)