Skip to content

Commit e7f9df6

Browse files
committed
R code: bug fixes. Python code: refactorization for new draft.
Former-commit-id: e8e60c8 [formerly e8e60c8 [formerly d5a6b6e]] Former-commit-id: 7ba14ebea25cc1c9557440fc7daf4e83c3e68103 Former-commit-id: c875526
1 parent 5a68253 commit e7f9df6

28 files changed

Lines changed: 804 additions & 1052 deletions
3.07 KB
Binary file not shown.

python/example1-lgss.py

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,34 @@
33
# Example of particle filtering
44
# in a linear Gaussian state space model
55
#
6-
# Copyright (C) 2015 Johan Dahlin < johan.dahlin (at) liu.se >
7-
#
6+
# Copyright (C) 2017 Johan Dahlin < liu (at) johandahlin.com >
7+
#
88
# This program is free software; you can redistribute it and/or modify
99
# it under the terms of the GNU General Public License as published by
1010
# the Free Software Foundation; either version 2 of the License, or
1111
# (at your option) any later version.
12-
#
12+
#
1313
# This program is distributed in the hope that it will be useful,
1414
# but WITHOUT ANY WARRANTY; without even the implied warranty of
1515
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1616
# GNU General Public License for more details.
17-
#
17+
#
1818
# You should have received a copy of the GNU General Public License along
1919
# with this program; if not, write to the Free Software Foundation, Inc.,
2020
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2121
#
2222
##############################################################################
2323

24-
# Import some libraries
24+
# Import libraries
2525
import matplotlib.pylab as plt
26-
import numpy as np;
27-
import stateEstimationHelper as helpState
26+
import numpy as np
27+
28+
# Import helpers
29+
from helpers.stateEstimation import generateData, particleFilter, kalmanFilter
2830

2931
# Set the random seed to replicate results in tutorial
30-
np.random.seed( 10 );
32+
np.random.seed(10)
33+
3134

3235
##############################################################################
3336
# Define the model
@@ -40,51 +43,54 @@
4043
#
4144
# where v[tt] ~ N(0,1) and e[tt] ~ N(0,1)
4245

43-
# Set the parameters of the model (par[0],par[1],par[2])
44-
par = np.zeros(3)
45-
par[0] = 0.75
46-
par[1] = 1.00
47-
par[2] = 1.00
46+
# Set the parameters of the model (phi, sigmav, sigmae)
47+
theta = np.zeros(3)
48+
theta[0] = 0.75
49+
theta[1] = 1.00
50+
theta[2] = 1.00
4851

4952
# Set the number of time steps to simulate
50-
T = 250;
53+
T = 250
5154

5255
# Set the initial state
53-
x0 = 0;
56+
initialState = 0
57+
5458

5559
##############################################################################
5660
# Generate data
5761
##############################################################################
5862

59-
(x, y) = helpState.generateData(par, T, x0)
63+
(x, y) = generateData(theta, T, initialState)
6064

6165
# Plot the measurement
62-
plt.subplot(3,1,1);
63-
plt.plot(y,color = '#1B9E77', linewidth=1.5);
64-
plt.xlabel("time"); plt.ylabel("measurement")
66+
plt.subplot(3, 1, 1)
67+
plt.plot(y, color='#1B9E77', linewidth=1.5)
68+
plt.xlabel("time")
69+
plt.ylabel("measurement")
6570

6671
# Plot the states
67-
plt.subplot(3,1,2);
68-
plt.plot(x,color = '#D95F02', linewidth=1.5);
69-
plt.xlabel("time"); plt.ylabel("latent state")
72+
plt.subplot(3, 1, 2)
73+
plt.plot(x, color='#D95F02', linewidth=1.5)
74+
plt.xlabel("time")
75+
plt.ylabel("latent state")
76+
7077

7178
##############################################################################
72-
# State estimation using the particle filter
79+
# State estimation
7380
##############################################################################
7481

75-
plt.subplot(3,1,3);
76-
7782
# Using N = 100 particles and plot the estimate of the latent state
78-
( xhat, ll ) = helpState.pf(y,par,100,T,x0);
79-
plt.plot(xhat,color = '#7570B3', linewidth=1.5)
80-
plt.xlabel("time"); plt.ylabel("state estimate")
83+
xHatFilteredParticleFilter, _ = particleFilter(y, theta, 100, initialState)
84+
85+
# Using the Kalman filter
86+
xHatFilteredKalmanFilter = kalmanFilter(y, theta, initialState, 0.01)
87+
8188

82-
###################################################################################
83-
# State estimation using the Kalman filter
84-
###################################################################################
89+
plt.subplot(3, 1, 3)
90+
plt.plot(xHatFilteredKalmanFilter - xHatFilteredParticleFilter, color='#7570B3', linewidth=1.5)
91+
plt.xlabel("time")
92+
plt.ylabel("difference in estimate")
8593

86-
xhat = helpState.kf(y,par,T,x0,0.01);
87-
plt.plot(xhat,'k', linewidth=1.5)
8894

8995
##############################################################################
9096
# End of file

python/example1-lgss.spydata

-20 KB
Binary file not shown.

python/example2-lgss.py

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,35 @@
33
# Example of particle Metropolis-Hastings
44
# in a linear Gaussian state space model
55
#
6-
# Copyright (C) 2015 Johan Dahlin < johan.dahlin (at) liu.se >
7-
#
6+
# Copyright (C) 2017 Johan Dahlin < liu (at) johandahlin.com >
7+
#
88
# This program is free software; you can redistribute it and/or modify
99
# it under the terms of the GNU General Public License as published by
1010
# the Free Software Foundation; either version 2 of the License, or
1111
# (at your option) any later version.
12-
#
12+
#
1313
# This program is distributed in the hope that it will be useful,
1414
# but WITHOUT ANY WARRANTY; without even the implied warranty of
1515
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1616
# GNU General Public License for more details.
17-
#
17+
#
1818
# You should have received a copy of the GNU General Public License along
1919
# with this program; if not, write to the Free Software Foundation, Inc.,
2020
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2121
#
2222
##############################################################################
2323

24-
# Import some libraries
25-
import matplotlib.pylab as plt
26-
import numpy as np
27-
import stateEstimationHelper as helpState
28-
import parameterEstimationHelper as helpParam
24+
# Import libraries
25+
import matplotlib.pylab as plt
26+
import numpy as np
27+
28+
# Import helpers
29+
from helpers.stateEstimation import generateData, particleFilter
30+
from helpers.parameterEstimation import particleMetropolisHastings
2931

3032
# Set the random seed
31-
np.random.seed( 10 );
33+
np.random.seed(10)
34+
3235

3336
##############################################################################
3437
# Define the model
@@ -41,43 +44,47 @@
4144
#
4245
# where v[tt] ~ N(0,1) and e[tt] ~ N(0,1)
4346

44-
# Set the parameters of the model (par[0],par[1],par[2])
45-
par = np.zeros(3)
46-
par[0] = 0.75
47-
par[1] = 1.00
48-
par[2] = 1.00
47+
# Set the parameters of the model (phi, sigmav, sigmae)
48+
theta = np.zeros(3)
49+
theta[0] = 0.75
50+
theta[1] = 1.00
51+
theta[2] = 1.00
4952

5053
# Set the number of time steps to simulate
51-
T = 250;
54+
T = 250
5255

5356
# Set the initial state
54-
x0 = 0;
57+
initialState = 0
58+
5559

5660
##############################################################################
5761
# Generate data
5862
##############################################################################
5963

60-
(x, y) = helpState.generateData(par, T, x0)
64+
x, y = generateData(theta, T, initialState)
65+
6166

6267
##############################################################################
6368
# Parameter estimation using PMH
6469
##############################################################################
6570

6671
# The inital guess of the parameter
67-
initPar = 0.50;
72+
initialPhi = 0.50
6873

69-
# No. particles in the particle filter ( choose nPart ~ T )
70-
nPart = 500;
74+
# No. particles in the particle filter ( choose noParticles ~ T )
75+
noParticles = 500
7176

72-
# The length of the burn-in and the no. iterations of PMH ( nBurnIn < nRuns )
73-
nBurnIn = 1000;
74-
nRuns = 5000;
77+
# The length of the burn-in and the no. iterations of PMH
78+
# ( noBurnInIterations < noIterations )
79+
noBurnInIterations = 1000
80+
noIterations = 5000
7581

7682
# The standard deviation in the random walk proposal
77-
stepSize = 0.10;
83+
stepSize = 0.10
7884

7985
# Run the PMH algorithm
80-
res = helpParam.pmh(y,initPar,par,nPart,T,x0,helpState.pf,nRuns,stepSize)
86+
res = particleMetropolisHastings(y, initialPhi, theta, noParticles, initialState,
87+
particleFilter, noIterations, stepSize)
8188

8289

8390
##############################################################################
@@ -86,17 +93,24 @@
8693

8794
# Plot the parameter posterior estimate
8895
# Solid black line indicate posterior mean
89-
plt.subplot(2,1,1);
90-
plt.hist(res[nBurnIn:nRuns], np.floor(np.sqrt(nRuns-nBurnIn)), normed=1, facecolor='#7570B3')
91-
plt.xlabel("par[0]"); plt.ylabel("posterior density estimate")
92-
plt.axvline( np.mean(res[nBurnIn:nRuns]), linewidth=2, color = 'k' )
96+
plt.subplot(2, 1, 1)
97+
plt.hist(res[noBurnInIterations:noIterations], np.floor(
98+
np.sqrt(noIterations - noBurnInIterations)), normed=1, facecolor='#7570B3')
99+
plt.xlabel("par[0]")
100+
plt.ylabel("posterior density estimate")
101+
plt.axvline(np.mean(res[noBurnInIterations:noIterations]),
102+
linewidth=2, color='k')
93103

94104
# Plot the trace of the Markov chain after burn-in
95105
# Solid black line indicate posterior mean
96-
plt.subplot(2,1,2);
97-
plt.plot(np.arange(nBurnIn,nRuns,1),res[nBurnIn:nRuns],color = '#E7298A')
98-
plt.xlabel("iteration"); plt.ylabel("par[0]")
99-
plt.axhline( np.mean(res[nBurnIn:nRuns]), linewidth=2, color = 'k' )
106+
plt.subplot(2, 1, 2)
107+
plt.plot(np.arange(noBurnInIterations, noIterations, 1),
108+
res[noBurnInIterations:noIterations], color='#E7298A')
109+
plt.xlabel("iteration")
110+
plt.ylabel("par[0]")
111+
plt.axhline(np.mean(res[noBurnInIterations:noIterations]),
112+
linewidth=2, color='k')
113+
100114

101115
##############################################################################
102116
# End of file

python/example2-lgss.spydata

-50 KB
Binary file not shown.

python/example3-lgss.py

Lines changed: 0 additions & 108 deletions
This file was deleted.

0 commit comments

Comments
 (0)