You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+7-3Lines changed: 7 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,18 +2,22 @@
2
2
3
3
This code was downloaded from https://github.com/compops/pmh-tutorial and contains the code used to produce the results in the tutorial:
4
4
5
-
J. Dahlin and T. B. Schön, **Getting started with particle Metropolis-Hastings for inference in nonlinear models**. Pre-print, arXiv:1511:01707, 2017.
5
+
J. Dahlin and T. B. Schön, **Getting started with particle Metropolis-Hastings for inference in nonlinear models**. Pre-print, arXiv:1511:01707, 2017.
6
6
7
-
The tutorial is available as a preprint from http://arxiv.org/pdf/1511.01707. An R package is also provided on CRAN with the implementation of the turorial in R. The source code (almost identitical to the code in the subdirectory R/) is found at https://github.com/compops/pmh-tutorial-rpkg.
7
+
The tutorial is available as a preprint from http://arxiv.org/pdf/1511.01707. An R package is also provided on CRAN with the implementation of the tutorial in R. The source code (almost identical to the code in the subdirectory R/) is found at https://github.com/compops/pmh-tutorial-rpkg.
8
8
9
9
Included material
10
10
--------------
11
-
**r/** This is the main implementation. The complete R code developed and implemented in the tutorial. This code was used to make all the numerical illustrations in the tutorial including the figures and tables. The workspaces for these runs are also provided as a [zip-file in the latest release of the code](https://github.com/compops/pmh-tutorial/releases/latest) to reproduce all the figures in the tutorial.
11
+
**r/** This is the main implementation. The complete R code developed and implemented in the tutorial. This code was used to make all the numerical illustrations in the tutorial including the figures and tables. The workspaces for these runs are also provided as a [zip-file in the latest release of the code](https://github.com/compops/pmh-tutorial/releases/latest) to reproduce all the figures in the tutorial.
12
12
13
13
**python/** Code for Python to implement the basic algorithms covered in the tutorial. Implementations for the advanced topics are not provided. Only simple plotting is implemented and no figures or saved data from runs are provided.
14
14
15
15
**matlab/** Code for MATLAB to implement the basic algorithms covered in the tutorial. Implementations for the advanced topics are not provided. Only simple plotting is implemented and no figures or saved data from runs are provided.
16
16
17
+
Generalisations
18
+
--------------
19
+
There is source code available for Python that implements some of the generalisations discussed in the tutorial. See the README file under *python/* for more information.
Copy file name to clipboardExpand all lines: matlab/README.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# MATLAB code for PMH tutorial
2
2
3
-
This MATLAB code implements the Kalman filter (KF), particle filter (PF) and particle Metropolis-Hastings (PMH) algorithm for two different dynamical models: a linear Gaussian state-space (LGSS) model and a stochastic volatilty (SV) model. Note that the Kalman filter can only be employed for the first of these two models. The details of the code is described in the tutorial paper available at http://arxiv.org/pdf/1511.01707
3
+
This MATLAB code implements the Kalman filter (KF), particle filter (PF) and particle Metropolis-Hastings (PMH) algorithm for two different dynamical models: a linear Gaussian state-space (LGSS) model and a stochastic volatility (SV) model. Note that the Kalman filter can only be employed for the first of these two models. The details of the code is described in the tutorial paper available at http://arxiv.org/pdf/1511.01707
4
4
5
5
Note that the MATLAB code in this folder covers the basic implementations in the paper. The notation of the variables has been changed sligthly compared with the tutorial paper to improve readability of the code. However, it should be easy to translate between the two. See the R code in r/ for all the implementations and to recreate the results in the tutorial.
6
6
@@ -49,4 +49,4 @@ Copyright information
49
49
# with this program; if not, write to the Free Software Foundation, Inc.,
50
50
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Copy file name to clipboardExpand all lines: python/README.md
+7-3Lines changed: 7 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Python code for PMH tutorial
2
2
3
-
This Python code implements the Kalman filter (KF), particle filter (PF) and particle Metropolis-Hastings (PMH) algorithm for two different dynamical models: a linear Gaussian state-space (LGSS) model and a stochastic volatilty (SV) model. Note that the Kalman filter can only be employed for the first of these two models. The details of the code is described in the tutorial paper available at http://arxiv.org/pdf/1511.01707
3
+
This Python code implements the Kalman filter (KF), particle filter (PF) and particle Metropolis-Hastings (PMH) algorithm for two different dynamical models: a linear Gaussian state-space (LGSS) model and a stochastic volatility (SV) model. Note that the Kalman filter can only be employed for the first of these two models. The details of the code is described in the tutorial paper available at http://arxiv.org/pdf/1511.01707
4
4
5
5
Note that the Python code in this folder covers the basic implementations in the paper. The notation of the variables has been changed sligthly compared with the tutorial paper to improve readability of the code. However, it should be easy to translate between the two. See the R code in r/ for all the implementations and to recreate the results in the tutorial.
6
6
@@ -26,7 +26,7 @@ Supporting files (helpers/)
26
26
--------------
27
27
**dataGeneration.py** Generates data from a LGSS model.
28
28
29
-
**parameterEstimation.py** Implements the PMH algorithm for the LGSS model (particleMetropolisHastings) and the SV model (particleMetropolisHastingsSVModel).
29
+
**parameterEstimation.py** Implements the PMH algorithm for the LGSS model (particleMetropolisHastings) and the SV model (particleMetropolisHastingsSVModel).
30
30
31
31
**stateEstimation.py** Implements the faPF for the LGSS model (particleFilter), the Kalman filter for the LGSS model (kalmanFilter) and the bPF for the SV model (paticleFilterSVmodel).
32
32
@@ -35,6 +35,10 @@ Adapting the code for another model
35
35
--------------
36
36
See the discussion in *README.MD* in the directory *r/*.
37
37
38
+
Generalisations
39
+
--------------
40
+
Some generalisations and improvements of this code is discussed in the tutorial, see the last paragraph in Section 7. Python code for PMH1 and PMH2 is available in the repo [pmh-stco2015](https://github.com/compops/pmh-stco2015), Python code for qPMH2 is availabe in the repo [https://github.com/compops/qpmh2-sysid2015](qpmh2-sysid2015) and Python code for correlated pseudo-marginal Metropolis-Hastings is available in the repo [https://github.com/compops/pmmh-correlated2015](pmmh-correlated2015). These are excellent resources for getting up to speed with the current frontier in research connected to PMH.
41
+
38
42
Copyright information
39
43
--------------
40
44
```R
@@ -53,4 +57,4 @@ Copyright information
53
57
# with this program; if not, write to the Free Software Foundation, Inc.,
54
58
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Copy file name to clipboardExpand all lines: r/README.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,27 +1,27 @@
1
1
# R code for PMH tutorial
2
2
3
-
This R code implements the Kalman filter (KF), particle filter (PF) and particle Metropolis-Hastings (PMH) algorithm for two different dynamical models: a linear Gaussian state-space (LGSS) model and a stochastic volatilty (SV) model. Note that the Kalman filter can only be employed for the first of these two models. The details of the code is described in the tutorial paper available at: http://arxiv.org/pdf/1511.01707
3
+
This R code implements the Kalman filter (KF), particle filter (PF) and particle Metropolis-Hastings (PMH) algorithm for two different dynamical models: a linear Gaussian state-space (LGSS) model and a stochastic volatility (SV) model. Note that the Kalman filter can only be employed for the first of these two models. The details of the code is described in the tutorial paper available at: http://arxiv.org/pdf/1511.01707
4
4
5
5
Requirements
6
6
--------------
7
7
The code is written and tested for R 3.2.2 and makes use of the packages Quandl and mvtnorm. These can be installed in R by executing the command:
8
8
```R
9
-
install.packages(c("mvtnorm", "Quandl"))
9
+
install.packages(c("mvtnorm", "Quandl"))
10
10
```
11
11
12
12
Main script files
13
13
--------------
14
14
These are the main script files that implement the various algorithms discussed in the tutorial.
15
15
16
-
**example1-lgss.R** State estimation in a LGSS model using the KM and a fully-adapted PF (faPF). The code is discussed in Section 3.1 and the results are presented in Section 3.2 as Figure 4 and Table 1.
16
+
**example1-lgss.R** State estimation in a LGSS model using the KF and a fully-adapted PF (faPF). The code is discussed in Section 3.1 and the results are presented in Section 3.2 as Figure 4 and Table 1.
17
17
18
18
**example2-lgss.R** Parameter estimation of one parameter in the LGSS model using PMH with the faPF as the likelihood estimator. The code is discussed in Section 4.1 and the results are presented in Section 4.2 as Figure 5.
19
19
20
20
**example3-sv.R** Parameter estimation of three parameters in the SV model using PMH with the bootstrap PF as the likelihood estimator. The code is discussed in Section 5.1 and the results are presented in Section 5.2 as Figure 6. The code takes about an hour to run.
21
21
22
22
**example4-sv.R** Modified version of the code in *example3-sv.R* to make use of a better tailored parameter proposal. The details are discussed in Section 6.3.2 and the results are presented in the same section as Figures 7 and 8. Note that the only difference in the code is that the variable stepSize is changed.
23
23
24
-
**example5-sv.R** Modified version of the code in *example3-sv.R* to make use of another parameterisation of the model and a better tailored parameter proposal. The details are discussed in Section 6.3.3 and the results are presented in the same section. Note that the differences in the code is the use of another implemenation of PMH ant that the variable stepSize is changed.
24
+
**example5-sv.R** Modified version of the code in *example3-sv.R* to make use of another parameterisation of the model and a better tailored parameter proposal. The details are discussed in Section 6.3.3 and the results are presented in the same section. Note that the differences in the code is the use of another implementation of PMH ant that the variable stepSize is changed.
25
25
26
26
27
27
Additional script files for creating plots for tutorial (extra-code-for-tutorial/)
@@ -55,14 +55,14 @@ These directories are placeholders for the output from running the code. The wor
55
55
56
56
Adapting the code for another model
57
57
--------------
58
-
The code provided in *helpers/stateInference.R* and *helpers/parameterInferernce.R* is quite general. To adapt this code for your own model, you can start with the code in *example3-sv.R* together with the functions *particleFilterSVmodel* and *particleMetropolisHastings* from the helpers.
58
+
The code provided in *helpers/stateInference.R* and *helpers/parameterInferernce.R* is quite general. To adapt this code for your own model, you can start with the code in *example3-sv.R* together with the functions *particleFilterSVmodel* and *particleMetropolisHastings* from the helpers.
59
59
60
60
### Particle filter
61
61
In the particle filter, you need to change the lines connected to: (i) the sampling of the initial state, (ii) the propagation of particles and (iii) the computation of the weights. For (i), you need to rewrite:
to fit your model. Two simple choices are to make use of the stationary distribution of the state (as is done for the SV model) computed by hand or to initialise all particles to some value (as is done in the LGSS model) by:
65
+
to fit your model. Two simple choices are to make use of the stationary distribution of the state (as is done for the SV model) computed by hand or to initialize all particles to some value (as is done in the LGSS model) by:
66
66
```R
67
67
particles[, 1] <-initialState
68
68
```
@@ -82,10 +82,10 @@ For (iii), you need to change:
82
82
```
83
83
to something else. For the bPF, this corresponds to the observation process of your state-space model.
84
84
85
-
Finally, note that the particle filter implemetation can only be used for state-space models where the state and observation are scalar. However, it is quite straightforward to make use of particle filtering when the state and/or observations are multivariate. It is basically only bookkeeping. If the dimension of the state is larger than say 5, good proposals are usually required to not run into the curse of dimensionality. This is a hot current research topic in the particle filtering literature.
85
+
Finally, note that the particle filter implementation can only be used for state-space models where the state and observation are scalar. However, it is quite straightforward to make use of particle filtering when the state and/or observations are multivariate. It is basically only bookkeeping. If the dimension of the state is larger than say 5, good proposals are usually required to not run into the curse of dimensionality. This is a hot current research topic in the particle filtering literature.
86
86
87
87
### Particle Metropolis-Hastings
88
-
The implemenation of the PMH algorithm is general and does not require any larger changes if the model is changed. The dimensionality of the variables *xHatFiltered*, *xHatFilteredProposed*, *theta* and *thetaProposed* needs to be altered to match the dimensionality of the state and the number of parameters in the new state-space model. Moreover, the initial value of theta and the proposal distribution need to be calibrated for your new model. The simplest way to do this is by so-called pilot runs. Set the initial value to something reasonable and stepSize to a diagonal matrix with quite small elements, so that you get at least some accepted proposed values. After the pilot run, adapt the proposal as is discussed in 6.3.2 and initialise the PMH algorithm in the estimated posterior mean. Repeat this one or two more times or until you are satisfied.
88
+
The implementation of the PMH algorithm is general and does not require any larger changes if the model is changed. The dimensionality of the variables *xHatFiltered*, *xHatFilteredProposed*, *theta* and *thetaProposed* needs to be altered to match the dimensionality of the state and the number of parameters in the new state-space model. Moreover, the initial value of theta and the proposal distribution need to be calibrated for your new model. The simplest way to do this is by so-called pilot runs. Set the initial value to something reasonable and stepSize to a diagonal matrix with quite small elements, so that you get at least some accepted proposed values. After the pilot run, adapt the proposal as is discussed in 6.3.2 and initialise the PMH algorithm in the estimated posterior mean. Repeat this one or two more times or until you are satisfied.
89
89
90
90
It is known that this simple version of PMH performs bad when the number of parameters is larger than about 5. To circumvent this problem, see the suggestions in Sections 4.3 and 6. It is also discussed there how to choose the number of particles *noParticles* and the number of iterations *noIterations* to use in the PMH algorithm. *noBurnInIterations* can be selected by looking at the trace plot for when the Markov chain has reached its steady-state/stationarity. I usually use *noIterations* as 10,000 or 30,000 (with *noBurnInIterations* as 3,000 or 10,0000) to get good posterior estimates but these runs take time. Also, using *noParticles* as somewhere between *T* and 2*T* is a good place to start.
91
91
@@ -107,4 +107,4 @@ Copyright information
107
107
# with this program; if not, write to the Free Software Foundation, Inc.,
108
108
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
0 commit comments