|
17 | 17 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
18 | 18 |
|
19 | 19 | % The inital guess of the parameter |
20 | | -initPar = [ 0.5 0.5 15 ]; |
| 20 | +initPar = mean(y); |
21 | 21 |
|
22 | 22 | % The length of the burn-in and the no. iterations of MH algorithm |
23 | 23 | % ( nBurnIn < nIterations ) |
24 | 24 | nBurnIn = 10000; |
25 | 25 | nIterations = 50000; |
26 | 26 |
|
27 | 27 | % The covariance matrix in the random walk proposal |
28 | | -Sigma = [ [ 121.3141 1.4331 3.1442]; |
29 | | - [ 1.4331 0.4692 0.0443]; |
30 | | - [ 3.1442 0.0443 0.3539] ]; |
31 | | -Sigma = 0.8 * Sigma; |
| 28 | +Sigma = 0.17; |
32 | 29 |
|
33 | 30 | % Run the MH algorithm |
34 | 31 | th = mh( y, initPar, nIterations, Sigma ); |
|
44 | 41 | figure(2); |
45 | 42 |
|
46 | 43 | % Plot the data and the fitted model |
47 | | -subplot(4,2,[1 2]); |
| 44 | +subplot(2,2,[1 2]); |
48 | 45 | hist( y, 25 ) |
49 | 46 | xlabel( 'time' ); |
50 | 47 | ylabel( 'no. earthquakes' ); |
51 | 48 |
|
52 | 49 | hold on; |
53 | | -grid = 5:0.1:45; |
54 | | -plot( grid, T .* exp( dt( grid, thhat ) ), 'r', 'LineWidth', 2 ); |
| 50 | +grid = 5:1:45; |
| 51 | +plot( grid, T .* exp( poisson( grid, thhat ) ), 'r', 'LineWidth', 2 ); |
55 | 52 | hold off; |
56 | 53 |
|
57 | 54 | % Plot the parameter posterior estimate |
58 | 55 | % Plot the trace of the Markov chain after burn-in |
59 | | -subplot(4,2,3); |
| 56 | +subplot(2,2,3); |
60 | 57 | hist( th( nBurnIn:nIterations, 1 ), floor( sqrt( nIterations - nBurnIn ) ) ); |
61 | | -xlabel('nu'); |
| 58 | +xlabel('theta'); |
62 | 59 | ylabel('posterior density estimate'); |
63 | 60 |
|
64 | | -subplot(4,2,4); |
| 61 | +subplot(2,2,4); |
65 | 62 | plot( nBurnIn:nIterations, th( nBurnIn:nIterations, 1 ) ); |
66 | | -xlabel('iteration'); ylabel('trace of nu'); |
67 | | - |
68 | | -subplot(4,2,5); |
69 | | -hist( th( nBurnIn:nIterations, 2 ), floor( sqrt( nIterations - nBurnIn ) ) ); |
70 | | -xlabel('mu'); ylabel('posterior density estimate'); |
71 | | - |
72 | | -subplot(4,2,6); |
73 | | -plot( nBurnIn:nIterations, th( nBurnIn:nIterations, 2 ) ); |
74 | | -xlabel('iteration'); ylabel('trace of mu'); |
75 | | - |
76 | | -subplot(4,2,7); |
77 | | -hist( th( nBurnIn:nIterations, 3 ), floor( sqrt( nIterations - nBurnIn ) ) ); |
78 | | -xlabel('sigma'); ylabel('posterior density estimate'); |
79 | | - |
80 | | -subplot(4,2,8); |
81 | | -plot( nBurnIn:nIterations, th( nBurnIn:nIterations, 3 ) ); |
82 | | -xlabel('iteration'); ylabel('trace of sigma'); |
| 63 | +xlabel('iteration'); ylabel('trace of theta'); |
83 | 64 |
|
84 | 65 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
85 | 66 | % End of file |
|
0 commit comments