Skip to content

Commit fdb715b

Browse files
author
compops
committed
added R package files
Former-commit-id: 1c5704e [formerly 1c5704e [formerly 3426f0b]] Former-commit-id: bebb8d2ff20ce5277a1f743e85db16dfe03b08ea Former-commit-id: 9f8b4c5
1 parent 6bbdf3b commit fdb715b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+7358
-2
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The implementation in Python makes use of NumPy 1.7.1, SciPy 0.12.0, Matplotlib
1616

1717
The implementation in Matlab makes use of the statistics toolbox and the Quandl package. See < https://github.com/quandl/Matlab > for more installation and to download the toolbox. Note that urlread2 is required by the Quandl toolbox and should be installed as detailed in the README file of the Quandl toolbox.
1818

19-
Included files
19+
Included files (folders matlab, python and r)
2020
--------------
2121
**example1-lgss.[R,py,m]** Implements the numerical illustration in Section 3.2 of state estimation in a linear Gaussian state space (LGSS) model using the fully-adapted particle filter (faPF). The output is the filtered state estimated as presented in Figure 3.
2222

@@ -30,10 +30,20 @@ Included files
3030

3131
**example*.[RData,mat,spdata]** A saved copy of the workspace after running thr corresponding code. Can be used to directly recreate the plots in the tutorial and to conduct additional analysis.
3232

33-
Supporting files
33+
Supporting files (folders matlab, python and r)
3434
--------------
3535
**stateEstimationHelper.[py,R]**
3636
Implementes the data generation for the LGSS model (generateData), the faPF for the LGSS model (sm), the Kalman filter for the LGSS model (kf) and the bPF for the SV model (sm_sv). In Matlab, these functions are defined in four seperate m-files with the corresponding file names.
3737

3838
**parameterEstimationHelper.[py,R]**
3939
Implementes the PMH algorithm for the LGSS model (pmh), the SV model (pmh_sv) and the reparameterised SV model (pmh_sv_reparametrised). In Matlab, these functions are defined in two seperate m-files with the corresponding file names.
40+
41+
Included files (folder r-package)
42+
--------------
43+
The files for the R package pmhtutorial. These should be virtually the same as the files in the folder r but packaged as an R package. The folder r is maintained to keep the code for the three languages as similar as possible. However, the r package is simple to download and use directly.
44+
45+
Included files (folder matlab-skeleton)
46+
--------------
47+
Skeleton code files for MATLAB to help step-by-step implementation during courses and seminars.
48+
49+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cran-comments.md
2+
examples

r-package/pmhtutorial/DESCRIPTION

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Package: pmhtutorial
2+
Type: Package
3+
Title: Minimal Working Examples for Particle Metropolis-Hastings
4+
Version: 1.0.1
5+
Date: 2016-01-25
6+
Author: Johan Dahlin <johan.dahlin@liu.se>
7+
Maintainer: Johan Dahlin <johan.dahlin@liu.se>
8+
Depends: R (>= 3.2.3)
9+
URL: https://github.com/compops/pmh-tutorial
10+
Description: Routines for state estimate in a linear
11+
Gaussian state space model and a simple stochastic volatility model using
12+
particle filtering. Parameter inference is also carried out in these models
13+
using the particle Metropolis-Hastings algorithm that includes the particle
14+
filter to provided an unbiased estimator of the likelihood. This package is
15+
a collection of minimal working examples of these algorithms and is only
16+
meant for educational use and as a start for learning to them on your own.
17+
License: GPL-2
18+
Imports: mvtnorm,
19+
Quandl,
20+
grDevices,
21+
graphics,
22+
stats
23+
RoxygenNote: 5.0.1

r-package/pmhtutorial/DESCRIPTION~

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Package: pmhtutorial
2+
Type: Package
3+
Title: Minimal Working Examples for Particle Metropolis-Hastings
4+
Version: 1.0
5+
Date: 2016-01-12
6+
Author: Johan Dahlin <johan.dahlin@liu.se>
7+
Maintainer: Johan Dahlin <johan.dahlin@liu.se>
8+
URL: https://github.com/compops/pmh-tutorial
9+
Description: Routines for state estimate in a linear
10+
Gaussian state space model and a simple stochastic volatility model using
11+
particle filtering. Parameter inference is also carried out in these models
12+
using the particle Metropolis-Hastings algorithm that includes the particle
13+
filter to provided an unbiased estimator of the likelihood. The code is developed
14+
in the paper and all the details are explained there. This package is meant as a
15+
minimal working example of these algorithm and is only meant for educational use
16+
and as a start for learning to implement these algorithms on your own.
17+
License: GPL-2
18+
Imports:
19+
mvtnorm,
20+
Quandl
21+
RoxygenNote: 5.0.1

r-package/pmhtutorial/NAMESPACE

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Generated by roxygen2: do not edit by hand
2+
3+
export(example1_lgss)
4+
export(example2_lgss)
5+
export(example3_sv)
6+
export(example4_sv)
7+
export(example5_sv)
8+
export(generateData)
9+
export(kf)
10+
export(pmh)
11+
export(pmh_sv)
12+
export(pmh_sv_reparameterised)
13+
export(sm)
14+
export(sm_sv)
15+
importFrom(grDevices,col2rgb)
16+
importFrom(grDevices,rgb)
17+
importFrom(graphics,abline)
18+
importFrom(graphics,hist)
19+
importFrom(graphics,layout)
20+
importFrom(graphics,lines)
21+
importFrom(graphics,par)
22+
importFrom(graphics,plot)
23+
importFrom(graphics,points)
24+
importFrom(stats,acf)
25+
importFrom(stats,density)
26+
importFrom(stats,dgamma)
27+
importFrom(stats,dnorm)
28+
importFrom(stats,rnorm)
29+
importFrom(stats,runif)
30+
importFrom(stats,sd)
31+
importFrom(stats,var)

r-package/pmhtutorial/NEWS.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# pmhtutorial 1.0.1
2+
* Bug fixes related the the plots.
3+
* Added the Markov chain trace to the variables
4+
retured from example3_sv, example4_sv and
5+
example5_sv.
6+
7+
# pmhtutorial 1.0.0
8+
* Initial release.

r-package/pmhtutorial/R/example1.R

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
##############################################################################
2+
#
3+
# Example of fully-adapted particle filtering
4+
# in a linear Gaussian state space model
5+
#
6+
# This program is free software; you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation; either version 2 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License along
17+
# with this program; if not, write to the Free Software Foundation, Inc.,
18+
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19+
#
20+
##############################################################################
21+
22+
#' State estimation in a linear Gaussian state space model
23+
#'
24+
#' @description
25+
#' Minimal working example of state estimation in a linear Gaussian state
26+
#' space model using Kalman filtering and a fully-adapted particle filter.
27+
#' The code estimates the bias and mean squared error (compared with the
28+
#' Kalman estimate) while varying the number of particles in the particle
29+
#' filter.
30+
#' @details
31+
#' The Kalman filter is a standard implementation without an input. The
32+
#' particle filter is fully adapted (i.e. takes the current observation into
33+
#' account when proposing new particles and computing the weights).
34+
#' @return
35+
#' Returns a plot with the generated observations y and the difference in the
36+
#' state estimates obtained by the Kalman filter (the optimal solution) and
37+
#' the particle filter (with 20 particles). Furthermore, the function returns
38+
#' plots of the estimated bias and mean squared error of the state estimate
39+
#' obtained using the particle filter (while varying the number of particles)
40+
#' and the Kalman estimates.
41+
#'
42+
#' The function returns a list with the elements:
43+
#' \itemize{
44+
#' \item{y: The observations generated from the model.}
45+
#' \item{x: The states generated from the model.}
46+
#' \item{xhatKF: The estimate of the state from the Kalman filter.}
47+
#' \item{xhatPF: The estimate of the state from the particle filter with
48+
#' 20 particles.}
49+
#' }
50+
#' @references
51+
#' Dahlin, J. & Schoen, T. B. "Getting started with particle
52+
#' Metropolis-Hastings for inference in nonlinear dynamical models."
53+
#' pre-print, arXiv:1511.01707, 2015.
54+
#' @author
55+
#' Johan Dahlin <johan.dahlin@liu.se>
56+
#' @note
57+
#' See Section 4.2 in the reference for more details.
58+
#' @keywords
59+
#' misc
60+
#' @export
61+
#' @importFrom grDevices col2rgb
62+
#' @importFrom grDevices rgb
63+
#' @importFrom graphics abline
64+
#' @importFrom graphics hist
65+
#' @importFrom graphics layout
66+
#' @importFrom graphics lines
67+
#' @importFrom graphics par
68+
#' @importFrom graphics plot
69+
#' @importFrom graphics points
70+
#' @importFrom stats acf
71+
#' @importFrom stats density
72+
#' @importFrom stats sd
73+
#' @importFrom stats var
74+
75+
example1_lgss <- function() {
76+
77+
# Set the random seed to replicate results in tutorial
78+
set.seed( 10 )
79+
80+
##############################################################################
81+
# Define the model
82+
##############################################################################
83+
84+
# Here, we use the following model
85+
#
86+
# x[tt+1] = phi * x[tt] + sigmav * v[tt]
87+
# y[tt] = x[tt] + sigmae * e[tt]
88+
#
89+
# where v[tt] ~ N(0,1) and e[tt] ~ N(0,1)
90+
91+
# Set the parameters of the model
92+
phi = 0.75
93+
sigmav = 1.00
94+
sigmae = 0.10
95+
96+
# Set the number of time steps to simulate
97+
T = 250
98+
99+
# Set the initial state
100+
x0 = 0
101+
102+
##############################################################################
103+
# Generate data
104+
##############################################################################
105+
106+
data <- generateData( phi, sigmav, sigmae, T, x0 )
107+
x <- data$x
108+
y <- data$y
109+
110+
# Plot the latent state and observations
111+
layout( matrix( c(1,1,2,2,3,4), 3, 2, byrow = TRUE ) )
112+
par ( mar = c(4,5,0,0) )
113+
114+
grid <- seq( 0, T )
115+
116+
plot( grid, y, col="#1B9E77", type="l", xlab="time", ylab="observation", ylim=c(-6,6), bty="n")
117+
118+
###################################################################################
119+
# State estimation using the particle filter and Kalman filter
120+
###################################################################################
121+
122+
# Using N = 20 particles and plot the estimate of the latent state
123+
N <- 20
124+
outPF <- sm( y, phi, sigmav, sigmae, N, T, x0 )
125+
outKF <- kf( y, phi, sigmav, sigmae, x0, 0.01 )
126+
diff <- outPF$xh - outKF$xh[-(T+1)]
127+
128+
grid <- seq( 0, T-1 )
129+
plot( grid, diff, col="#7570B3", type="l", xlab="time", ylab="difference in state estimate", ylim=c(-0.1,0.1), bty="n")
130+
131+
# Compute bias and MSE
132+
logBiasMSE = matrix( 0, nrow = 7, ncol = 2 )
133+
gridN = c( 10, 20, 50, 100, 200, 500, 1000)
134+
135+
for ( ii in 1:length(gridN) ) {
136+
smEstimate <- sm(y,phi,sigmav,sigmae,gridN[ii],T,x0)$xh
137+
kmEstimate <- outKF$xh[-(T+1)]
138+
139+
logBiasMSE[ ii, 1 ] <- log( mean( abs( smEstimate - kmEstimate ) ) )
140+
logBiasMSE[ ii, 2 ] <- log( mean( ( smEstimate - kmEstimate )^2 ) )
141+
}
142+
143+
# Plot the bias and MSE for comparison
144+
plot( gridN, logBiasMSE[ , 1 ], col="#E7298A", type="l", xlab="no. particles (N)", ylab="log-bias", ylim=c(-7,-3), bty="n")
145+
points( gridN, logBiasMSE[ , 1 ], col="#E7298A", pch=19 )
146+
147+
plot( gridN, logBiasMSE[ , 2 ], col="#66A61E", lwd=1.5, type="l", xlab="no. particles (N)", ylab="log-MSE", ylim=c(-12,-6), bty="n")
148+
points( gridN, logBiasMSE[ , 2 ], col="#66A61E", pch=19 )
149+
150+
list(y=y, x=x, xhatKF=outKF$xh[-(T+1)], xhatPF=outPF$xh)
151+
}
152+
153+
###################################################################################
154+
# End of file
155+
###################################################################################
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
##############################################################################
2+
#
3+
# Example of fully-adapted particle filtering
4+
# in a linear Gaussian state space model
5+
#
6+
# This program is free software; you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation; either version 2 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License along
17+
# with this program; if not, write to the Free Software Foundation, Inc.,
18+
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19+
#
20+
##############################################################################
21+
22+
example1_lgss <- function() {
23+
24+
# Set the random seed to replicate results in tutorial
25+
set.seed( 10 )
26+
27+
##############################################################################
28+
# Define the model
29+
##############################################################################
30+
31+
# Here, we use the following model
32+
#
33+
# x[tt+1] = phi * x[tt] + sigmav * v[tt]
34+
# y[tt] = x[tt] + sigmae * e[tt]
35+
#
36+
# where v[tt] ~ N(0,1) and e[tt] ~ N(0,1)
37+
38+
# Set the parameters of the model
39+
phi = 0.75
40+
sigmav = 1.00
41+
sigmae = 0.10
42+
43+
# Set the number of time steps to simulate
44+
T = 250
45+
46+
# Set the initial state
47+
x0 = 0
48+
49+
##############################################################################
50+
# Generate data
51+
##############################################################################
52+
53+
data <- generateData( phi, sigmav, sigmae, T, x0 )
54+
x <- data$x
55+
y <- data$y
56+
57+
# Plot the latent state and observations
58+
layout( matrix( c(1,1,2,2,3,4), 3, 2, byrow = TRUE ) )
59+
par ( mar = c(4,5,0,0) )
60+
61+
grid <- seq( 0, T )
62+
63+
plot( grid, y, col="#1B9E77", type="l", xlab="time", ylab="observation", ylim=c(-6,6), bty="n")
64+
65+
###################################################################################
66+
# State estimation using the particle filter and Kalman filter
67+
###################################################################################
68+
69+
# Using N = 20 particles and plot the estimate of the latent state
70+
N <- 20
71+
outPF <- sm( y, phi, sigmav, sigmae, N, T, x0 )
72+
outKF <- kf( y, phi, sigmav, sigmae, x0, 0.01 )
73+
diff <- outPF$xh - outKF$xh[-(T+1)]
74+
75+
grid <- seq( 0, T-1 )
76+
plot( grid, diff, col="#7570B3", type="l", xlab="time", ylab="difference in state estimate", ylim=c(-0.1,0.1), bty="n")
77+
78+
# Compute bias and MSE
79+
logBiasMSE = matrix( 0, nrow = 7, ncol = 2 )
80+
gridN = c( 10, 20, 50, 100, 200, 500, 1000)
81+
82+
for ( ii in 1:length(gridN) ) {
83+
smEstimate <- sm(y,phi,sigmav,sigmae,gridN[ii],T,x0)$xh
84+
kmEstimate <- outKF$xh[-(T+1)]
85+
86+
logBiasMSE[ ii, 1 ] <- log( mean( abs( smEstimate - kmEstimate ) ) )
87+
logBiasMSE[ ii, 2 ] <- log( mean( ( smEstimate - kmEstimate )^2 ) )
88+
}
89+
90+
# Plot the bias and MSE for comparison
91+
plot( gridN, logBiasMSE[ , 1 ], col="#E7298A", type="l", xlab="no. particles (N)", ylab="log-bias", ylim=c(-7,-3), bty="n")
92+
points( gridN, logBiasMSE[ , 1 ], col="#E7298A", pch=19 )
93+
94+
plot( gridN, logBiasMSE[ , 2 ], col="#66A61E", lwd=1.5, type="l", xlab="no. particles (N)", ylab="log-MSE", ylim=c(-12,-6), bty="n")
95+
points( gridN, logBiasMSE[ , 2 ], col="#66A61E", pch=19 )
96+
97+
list(y=y, x=x, xhatKF=outKF$xh[-(T+1)], xhatPF=outPF$xh)
98+
}
99+
100+
###################################################################################
101+
# End of file
102+
###################################################################################

0 commit comments

Comments
 (0)