Skip to content

Commit f09102b

Browse files
author
compops
committed
changed to Poisson IID data
Former-commit-id: 08805b9
1 parent aed2d7e commit f09102b

2 files changed

Lines changed: 13 additions & 32 deletions

File tree

matlab-seminar/complete/example1_iid.m

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,15 @@
1717
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1818

1919
% The inital guess of the parameter
20-
initPar = [ 0.5 0.5 15 ];
20+
initPar = mean(y);
2121

2222
% The length of the burn-in and the no. iterations of MH algorithm
2323
% ( nBurnIn < nIterations )
2424
nBurnIn = 10000;
2525
nIterations = 50000;
2626

2727
% 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;
3229

3330
% Run the MH algorithm
3431
th = mh( y, initPar, nIterations, Sigma );
@@ -44,42 +41,26 @@
4441
figure(2);
4542

4643
% Plot the data and the fitted model
47-
subplot(4,2,[1 2]);
44+
subplot(2,2,[1 2]);
4845
hist( y, 25 )
4946
xlabel( 'time' );
5047
ylabel( 'no. earthquakes' );
5148

5249
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 );
5552
hold off;
5653

5754
% Plot the parameter posterior estimate
5855
% Plot the trace of the Markov chain after burn-in
59-
subplot(4,2,3);
56+
subplot(2,2,3);
6057
hist( th( nBurnIn:nIterations, 1 ), floor( sqrt( nIterations - nBurnIn ) ) );
61-
xlabel('nu');
58+
xlabel('theta');
6259
ylabel('posterior density estimate');
6360

64-
subplot(4,2,4);
61+
subplot(2,2,4);
6562
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');
8364

8465
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8566
% End of file

matlab-seminar/complete/mh.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
theta(1,:) = initialTheta;
2929

3030
% Compute the initial log-likelihood
31-
loglikelihood(1) = sum( dt(observations, theta(1,:)) );
31+
loglikelihood(1) = sum( dpoisson(observations, theta(1,:)) );
3232

3333
%=====================================================================
3434
% Run main loop
@@ -38,9 +38,9 @@
3838
% Propose a new parameter
3939
theta_proposed = mvnrnd( theta(kk-1,:), Sigma );
4040

41-
% Estimate the log-likelihood if DOF and sigma is positive
42-
if ( ( theta_proposed(1) > 0.0 ) && ( theta_proposed(3) > 0.0 ) )
43-
loglikelihood_proposed = sum( dt(observations, theta_proposed) );
41+
% Estimate the log-likelihood if the intensity is positive
42+
if ( theta_proposed(1) > 0.0 )
43+
loglikelihood_proposed = sum( dpoisson(observations, theta_proposed) );
4444
else
4545
loglikelihood_proposed = -inf;
4646
end

0 commit comments

Comments
 (0)