1+ % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2+ %
3+ % Example of Particle Metropolis-Hastings (PMH) for the Earthquake model
4+ %
5+ % Copyright (C) 2015 Johan Dahlin < johan.dahlin (at) liu.se >
6+ %
7+ % This program is free software; you can redistribute it and/or modify
8+ % it under the terms of the GNU General Public License as published by
9+ % the Free Software Foundation; either version 2 of the License, or
10+ % (at your option) any later version.
11+ %
12+ % This program is distributed in the hope that it will be useful,
13+ % but WITHOUT ANY WARRANTY; without even the implied warranty of
14+ % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+ % GNU General Public License for more details.
16+ %
17+ % You should have received a copy of the GNU General Public License along
18+ % with this program; if not, write to the Free Software Foundation, Inc.,
19+ % 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20+ %
21+ % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
22+
23+
24+ % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
25+ % Load data
26+ % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
27+ load(' earthquake_data.mat' );
28+ T = 114 ;
29+
30+
31+ % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
32+ % Parameter estimation using PMH
33+ % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34+
35+ % The inital guess of the parameter
36+ initPar = [ 0.5 0.5 15 ];
37+
38+ % No. particles in the particle filter ( choose nPart ~ T )
39+ nPart = 100 ;
40+
41+ % The length of the burn-in and the no. iterations of PMH
42+ % ( nBurnIn < nIter )
43+ nBurnIn = 250 ;
44+ nIter = 2000 ;
45+
46+ % The covariance matrix in the random walk proposal
47+ stepSize = diag( [0.07 0.03 2 ].^2 );
48+ stepSize = 0.8 * stepSize ;
49+
50+ % Run the PMH algorithm
51+ [th , xh ] = pmh_earthquake( y , initPar , nPart , T , nIter , stepSize ) ;
52+
53+ % Compute posterior means
54+ thhat = mean( thhat , 1 )
55+ xhhat = mean( xh( nBurnIn : nIter , 2 : (T + 1 ) ), 1 )
56+
57+
58+ % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59+ % Plot the results
60+ % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61+
62+ figure(2 );
63+
64+ % Plot the expected versus observed no. earthquakes
65+ subplot(4 ,2 ,[1 2 ]);
66+ plot( y )
67+ xlabel( ' time' );
68+ ylabel( ' no. earthquakes' );
69+
70+ hold on ;
71+ plot( thhat(3 ) * exp(xhhat ), ' r' );
72+ hold off ;
73+
74+ % Plot the parameter posterior estimate
75+ % Plot the trace of the Markov chain after burn-in
76+ subplot(4 ,2 ,3 );
77+ hist( th( nBurnIn : nIter , 1 ), floor( sqrt( nIter - nBurnIn ) ) ) ;
78+ xlabel(' phi' );
79+ ylabel(' posterior density estimate' );
80+
81+ subplot(4 ,2 ,4 );
82+ plot( nBurnIn : nIter , th( nBurnIn : nIter , 1 ) ) ;
83+ xlabel(' iteration' ); ylabel(' trace of phi' );
84+
85+ subplot(4 ,2 ,5 );
86+ hist( th( nBurnIn : nIter , 2 ), floor( sqrt( nIter - nBurnIn ) ) ) ;
87+ xlabel(' sigmav' ); ylabel(' posterior density estimate' );
88+
89+ subplot(4 ,2 ,6 );
90+ plot( nBurnIn : nIter , th( nBurnIn : nIter , 2 ) ) ;
91+ xlabel(' iteration' ); ylabel(' trace of sigmav' );
92+
93+ subplot(4 ,2 ,7 );
94+ hist( th( nBurnIn : nIter , 3 ), floor( sqrt( nIter - nBurnIn ) ) ) ;
95+ xlabel(' beta' ); ylabel(' posterior density estimate' );
96+
97+ subplot(4 ,2 ,8 );
98+ plot( nBurnIn : nIter , th( nBurnIn : nIter , 3 ) ) ;
99+ xlabel(' iteration' ); ylabel(' trace of beta' );
100+
101+
102+ % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103+ % End of file
104+ % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0 commit comments