Skip to content

Commit 7dcf3dc

Browse files
author
Johan Dahlin
committed
updates for the arXiv-version
Former-commit-id: 64843fd [formerly 64843fd [formerly 2debf13]] Former-commit-id: 4b8afdcec2a00931e4714c18e8d52328225f6d76 Former-commit-id: 0a15fe9
1 parent 6d55c42 commit 7dcf3dc

23 files changed

Lines changed: 477 additions & 110 deletions

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
This code was downloaded from < https://github.com/compops/pmh-tutorial > or from < http://users.isy.liu.se/en/rt/johda87/ > and contains the code used to produce the results in the tutorial
44

5-
* J. Dahlin and T. B. Schön, *Getting started with particle Metropolis-Hastings for inference in nonlinear models*. Pre-print, arXiv:1510:xxxxv1, 2015.
5+
* J. Dahlin and T. B. Schön, *Getting started with particle Metropolis-Hastings for inference in nonlinear models*. Pre-print, arXiv:1511:xxxxv1, 2015.
66

7-
The papers are available as a preprint from < http://arxiv.org/pdf/1510.xxxxv1 > and < http://users.isy.liu.se/en/rt/johda87/ >.
7+
The papers are available as a preprint from < http://arxiv.org/pdf/1511.xxxxv1 > and < http://users.isy.liu.se/en/rt/johda87/ >.
88

99
Requirements
1010
--------------
@@ -22,14 +22,14 @@ Included files
2222

2323
**example4-sv.[R,py,m]** Implements the numerical illustration in Section 4.2, which makes use of the same setup as in Section 4.1 but with a tuned proposal distribution. The output is the estimated ACF and IACT as presented in Figure 7. The code takes some time (hours to execute).
2424

25-
**example*.[RData,mat]** A saved copy of the workspace after running to code.
25+
**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.
2626

2727
Supporting files
2828
--------------
29-
**stateEstimationHelper.[py,m]**
29+
**stateEstimationHelper.[py,R]**
3030
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.
3131

32-
**parameterEstimationHelper.[py,m]**
32+
**parameterEstimationHelper.[py,R]**
3333
Implementes the PMH algorithm for the LGSS model (pmh) and the SV model (pmh_sv). In Matlab, these functions are defined in two seperate m-files with the corresponding file names.
3434

3535
**omxs30data.csv**

matlab/example1_lgss.mat

8.4 KB
Binary file not shown.

matlab/example2_lgss.mat

22 KB
Binary file not shown.

matlab/example3_sv.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
% Solid black line indicate posterior mean
6666
subplot(4,2,3);
6767
hist(thhat(nBurnIn:nRuns,1), floor(sqrt(nRuns-nBurnIn)) );
68-
xlabel('phi'); ylabel('posterior density estimate');
68+
xlabel('mu'); ylabel('posterior density estimate');
6969

7070
h = findobj(gca,'Type','patch');
7171
set(h,'FaceColor',[117 112 179]/256,'EdgeColor','w');
@@ -74,24 +74,24 @@
7474
subplot(4,2,4);
7575
plot(nBurnIn:nRuns,thhat(nBurnIn:nRuns,1), 'Color', [117 112 179]/256);
7676
hold on; plot([nBurnIn nRuns],[1 1] * mean(thhat(nBurnIn:nRuns,1)), 'k'); hold off;
77-
xlabel('iteration'); ylabel('trace of phi');
77+
xlabel('iteration'); ylabel('trace of mu');
7878

7979
subplot(4,2,5);
8080
hist(thhat(nBurnIn:nRuns,2), floor(sqrt(nRuns-nBurnIn)) );
81-
xlabel('sigmav'); ylabel('posterior density estimate');
81+
xlabel('phi'); ylabel('posterior density estimate');
8282

8383
h = findobj(gca,'Type','patch');
8484
set(h,'FaceColor',[231 41 138]/256,'EdgeColor','w');
8585
hold on; plot([1 1] * mean(thhat(nBurnIn:nRuns,2)), [0 500], 'k'); hold off;
8686

8787
subplot(4,2,6);
8888
plot(nBurnIn:nRuns,thhat(nBurnIn:nRuns,2), 'Color', [231 41 138]/256);
89-
xlabel('iteration'); ylabel('trace of sigmav');
89+
xlabel('iteration'); ylabel('trace of phi');
9090
hold on; plot([nBurnIn nRuns],[1 1] * mean(thhat(nBurnIn:nRuns,2)), 'k'); hold off;
9191

9292
subplot(4,2,7);
9393
hist(thhat(nBurnIn:nRuns,3), floor(sqrt(nRuns-nBurnIn)) );
94-
xlabel('beta'); ylabel('posterior density estimate');
94+
xlabel('sigmav'); ylabel('posterior density estimate');
9595

9696
h = findobj(gca,'Type','patch');
9797
set(h,'FaceColor',[102 166 30]/256,'EdgeColor','w');
@@ -100,7 +100,7 @@
100100
subplot(4,2,8);
101101
plot(nBurnIn:nRuns,thhat(nBurnIn:nRuns,3), 'Color', [102 166 30]/256);
102102
hold on; plot([nBurnIn nRuns],[1 1] * mean(thhat(nBurnIn:nRuns,3)), 'k'); hold off;
103-
xlabel('iteration'); ylabel('trace of beta');
103+
xlabel('iteration'); ylabel('trace of sigmav');
104104

105105
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106106
% End of file
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
d5935fe8d440d8277110a00f97551bf398434497

matlab/example4_sv.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
% Solid black line indicate posterior mean
7070
subplot(4,2,3);
7171
hist(thhat(nBurnIn:nRuns,1), floor(sqrt(nRuns-nBurnIn)) );
72-
xlabel('phi'); ylabel('posterior density estimate');
72+
xlabel('mu'); ylabel('posterior density estimate');
7373

7474
h = findobj(gca,'Type','patch');
7575
set(h,'FaceColor',[117 112 179]/256,'EdgeColor','w');
@@ -78,24 +78,24 @@
7878
subplot(4,2,4);
7979
plot(nBurnIn:nRuns,thhat(nBurnIn:nRuns,1), 'Color', [117 112 179]/256);
8080
hold on; plot([nBurnIn nRuns],[1 1] * mean(thhat(nBurnIn:nRuns,1)), 'k'); hold off;
81-
xlabel('iteration'); ylabel('trace of phi');
81+
xlabel('iteration'); ylabel('trace of mu');
8282

8383
subplot(4,2,5);
8484
hist(thhat(nBurnIn:nRuns,2), floor(sqrt(nRuns-nBurnIn)) );
85-
xlabel('sigmav'); ylabel('posterior density estimate');
85+
xlabel('phi'); ylabel('posterior density estimate');
8686

8787
h = findobj(gca,'Type','patch');
8888
set(h,'FaceColor',[231 41 138]/256,'EdgeColor','w');
8989
hold on; plot([1 1] * mean(thhat(nBurnIn:nRuns,2)), [0 500], 'k'); hold off;
9090

9191
subplot(4,2,6);
9292
plot(nBurnIn:nRuns,thhat(nBurnIn:nRuns,2), 'Color', [231 41 138]/256);
93-
xlabel('iteration'); ylabel('trace of sigmav');
93+
xlabel('iteration'); ylabel('trace of phi');
9494
hold on; plot([nBurnIn nRuns],[1 1] * mean(thhat(nBurnIn:nRuns,2)), 'k'); hold off;
9595

9696
subplot(4,2,7);
9797
hist(thhat(nBurnIn:nRuns,3), floor(sqrt(nRuns-nBurnIn)) );
98-
xlabel('beta'); ylabel('posterior density estimate');
98+
xlabel('sigmav'); ylabel('posterior density estimate');
9999

100100
h = findobj(gca,'Type','patch');
101101
set(h,'FaceColor',[102 166 30]/256,'EdgeColor','w');
@@ -104,7 +104,7 @@
104104
subplot(4,2,8);
105105
plot(nBurnIn:nRuns,thhat(nBurnIn:nRuns,3), 'Color', [102 166 30]/256);
106106
hold on; plot([nBurnIn nRuns],[1 1] * mean(thhat(nBurnIn:nRuns,3)), 'k'); hold off;
107-
xlabel('iteration'); ylabel('trace of beta');
107+
xlabel('iteration'); ylabel('trace of sigmav');
108108

109109
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110110
% End of file
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
eecf624ca906846eea8c070b8b05254e79899f0b

matlab/sm_sv.m

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@
2929
function[xhatf,llp] = sm_sv(y,mu,phi,sigmav,N,T)
3030

3131
% Initalise variables
32-
xhatf = zeros( T+1, 1);
32+
a = zeros( N, T+1);
3333
p = zeros( N, T+1);
34+
v = ones( N, T+1);
3435
w = ones( N, T+1) / N;
3536
llp = 0;
3637

38+
a(:,1) = 1:N;
3739
p(:,1) = mu + sigmav/sqrt(1-phi^2) * normrnd( 0, 1, N, 1 ) ;
38-
xhatf(1) = mean(p(:,1));
3940

4041
%===========================================================
4142
% Run main loop
@@ -48,6 +49,12 @@
4849
idx = randsample( N, N, true, w(:,tt-1) );
4950
idx = idx( randperm( N ) );
5051

52+
% Resample the ancestory linage
53+
a(:,1:tt-1) = a(idx,1:tt-1);
54+
55+
% Add the most recent ancestors
56+
a(:,tt) = idx;
57+
5158
%=========================================================
5259
% Propagate
5360
%=========================================================
@@ -56,23 +63,24 @@
5663
%=========================================================
5764
% Compute weights
5865
%=========================================================
59-
w(:,tt) = dnorm( y(tt-1), 0, exp(p(:,tt)/2) );
66+
v(:,tt) = dnorm( y(tt-1), 0, exp(p(:,tt)/2) );
6067

6168
% Rescale log-weights and recover weight
62-
wmax = max( w(:,tt) );
63-
w(:,tt) = exp( w(:,tt) - wmax );
69+
vmax = max( v(:,tt) );
70+
v(:,tt) = exp( v(:,tt) - vmax );
6471

6572
% Estimate the log-likelihood
66-
llp = llp + wmax + log(sum( w(:,tt) )) - log(N);
73+
llp = llp + vmax + log(sum( v(:,tt) )) - log(N);
6774

6875
% Normalize the weights
69-
w(:,tt) = w(:,tt) / sum( w(:,tt) );
70-
71-
% Estimate the state
72-
xhatf(tt) = sum( w(:,tt) .* p(:,tt) );
76+
w(:,tt) = v(:,tt) / sum( v(:,tt) );
7377

7478
end
7579

80+
% Sample the state estimate using the weights at tt=T
81+
nIdx = randsample( N, 1, true, w(:,T) );
82+
indices = sub2ind(size(p), a(nIdx,:), 1:(T+1));
83+
xhatf = p( indices );
7684
end
7785

7886
% Helper for computing the logarithm of the Gaussian density

python/example1-lgss.spydata

20 KB
Binary file not shown.

python/example2-lgss.spydata

50 KB
Binary file not shown.

0 commit comments

Comments
 (0)