Skip to content

Commit a526f8e

Browse files
author
compops
committed
fixed typos and completed skeleton
Former-commit-id: 26ba426
1 parent f69cf04 commit a526f8e

8 files changed

Lines changed: 89 additions & 150 deletions

File tree

matlab-seminar/complete/example1_iid.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
hold on;
5050
grid = 5:1:45;
51-
plot( grid, T .* exp( poisson( grid, thhat ) ), 'r', 'LineWidth', 2 );
51+
plot( grid, T .* exp( dpoisson( grid, thhat ) ), 'r', 'LineWidth', 2 );
5252
hold off;
5353

5454
% Plot the parameter posterior estimate

matlab-seminar/complete/mh.m

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919

2020
function[ theta ] = mh( observations, initialTheta, nIterations, Sigma )
2121

22-
% Initalise variables
22+
% Initialise variables
2323
theta = zeros( nIterations, length(initialTheta) );
24-
loglikelihood = zeros( nIterations, 1 );
24+
logposterior = zeros( nIterations, 1 );
2525
accept = zeros( nIterations, 1 );
2626

2727
% Set the initial parameter
2828
theta(1,:) = initialTheta;
2929

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

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

41-
% Estimate the log-likelihood if the intensity is positive
41+
% Compute the log-posterior if the intensity is positive
4242
if ( theta_proposed(1) > 0.0 )
43-
loglikelihood_proposed = sum( dpoisson(observations, theta_proposed) );
43+
logposterior_proposed = sum( dpoisson(observations, theta_proposed) );
4444
else
45-
loglikelihood_proposed = -inf;
45+
logposterior_proposed = -inf;
4646
end
4747

4848
% Compute the acceptance probability
49-
aprob = exp( loglikelihood_proposed - loglikelihood(kk-1) );
49+
aprob = exp( logposterior_proposed - logposterior(kk-1) );
5050

5151
% Generate uniform random variable in U[0,1]
5252
u = unifrnd(0,1);
@@ -56,13 +56,13 @@
5656

5757
% Accept the parameter
5858
theta(kk,:) = theta_proposed;
59-
loglikelihood(kk) = loglikelihood_proposed;
59+
logposterior(kk) = logposterior_proposed;
6060
accept(kk) = 1.0;
6161

6262
else
6363
% Reject the parameter
6464
theta(kk,:) = theta(kk-1,:);
65-
loglikelihood(kk) = loglikelihood(kk-1);
65+
logposterior(kk) = logposterior(kk-1);
6666
accept(kk) = 0.0;
6767
end
6868

matlab-seminar/complete/pmh.m

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@
2121

2222
function[ theta ] = pmh( observations, initialTheta, nParticles, nIterations, Sigma )
2323

24-
% Initalise variables
24+
% Initialise variables
2525
theta = zeros( nIterations, length(initialTheta) );
26-
loglikelihood = zeros( nIterations, 1 );
26+
logposterior = zeros( nIterations, 1 );
2727
accept = zeros( nIterations, 1 );
2828

2929
% Set the initial parameter
3030
theta(1,:) = initialTheta;
3131

32-
% Run the initial particle filter to estimate the log-likelihood
33-
[ ~ , loglikelihood(1) ] = pf( observations, theta(1,:), nParticles );
32+
% Run the initial particle filter to estimate the log-posterior
33+
[ ~ , logposterior(1) ] = pf( observations, theta(1,:), nParticles );
3434

3535
%=====================================================================
3636
% Run main loop
@@ -40,15 +40,15 @@
4040
% Propose a new parameter
4141
theta_proposed = mvnrnd( theta(kk-1,:), Sigma );
4242

43-
% Estimate the log-likelihood (don't run if unstable system)
44-
if ( abs( theta_proposed(1) ) < 1.0 )
45-
[ ~, loglikelihood_proposed ] = pf( observations, theta_proposed, nParticles );
43+
% Estimate the log-posterior (don't run if unstable system)
44+
if ( ( abs( theta_proposed(1) ) < 1.0 ) && ( theta_proposed(2) > 0 ) && ( theta_proposed(3) > 0) )
45+
[ ~, logposterior_proposed ] = pf( observations, theta_proposed, nParticles );
4646
else
47-
loglikelihood_proposed = -inf;
47+
logposterior_proposed = -inf;
4848
end
4949

5050
% Compute the acceptance probability
51-
aprob = exp( loglikelihood_proposed - loglikelihood(kk-1) );
51+
aprob = exp( logposterior_proposed - logposterior(kk-1) );
5252

5353
% Generate uniform random variable in U[0,1]
5454
u = unifrnd(0,1);
@@ -58,13 +58,13 @@
5858

5959
% Accept the parameter
6060
theta(kk,:) = theta_proposed;
61-
loglikelihood(kk) = loglikelihood_proposed;
61+
logposterior(kk) = logposterior_proposed;
6262
accept(kk) = 1.0;
6363

6464
else
6565
% Reject the parameter
6666
theta(kk,:) = theta(kk-1,:);
67-
loglikelihood(kk) = loglikelihood(kk-1);
67+
logposterior(kk) = logposterior(kk-1);
6868
accept(kk) = 0.0;
6969
end
7070

matlab-seminar/skeleton/dpoisson.m

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2+
%
3+
% Computes the log-PDF of Poisson distribution
4+
%
5+
% Inputs:
6+
% x: Obserations
7+
% par: Parameters of the PDF (location, scale and shape)
8+
%
9+
% Outputs:
10+
% out: Logarithm of the PDF evaluated at each x.
11+
%
12+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13+
14+
function[out] = poisson(x, par)
15+
part1 = -log( factorial( x ) );
16+
part2 = x * log( par );
17+
part3 = -par;
18+
out = part1 + part2 + part3;
19+
end
20+
21+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
22+
% End of file
23+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

matlab-seminar/skeleton/dt.m

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

matlab-seminar/skeleton/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/skeleton/mh.m

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,54 @@
1919

2020
function[ theta ] = mh( observations, initialTheta, nIterations, Sigma )
2121

22-
% Initalise variables
22+
% Initialise variables
2323
theta = zeros( nIterations, length(initialTheta) );
24-
loglikelihood = zeros( nIterations, 1 );
24+
logposterior = zeros( nIterations, 1 );
2525
accept = zeros( nIterations, 1 );
2626

2727
% Set the initial parameter
2828
theta(1,:) = initialTheta;
2929

30-
% Compute the initial log-likelihood
31-
loglikelihood(1) = sum( dt(observations, theta(1,:)) );
30+
% Compute the initial log-posterior
31+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
32+
%%%%%%%%%%%%%%%%% ADD CODE HERE %%%%%%%%%%%%%%%%%%%%%
33+
% Hint: dpossion and observations %
34+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35+
logposterior(1) = ...
3236

3337
%=====================================================================
3438
% Run main loop
3539
%=====================================================================
3640
for kk = 2:nIterations
3741

3842
% Propose a new parameter
39-
theta_proposed = mvnrnd( theta(kk-1,:), Sigma );
4043

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) );
44+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
45+
%%%%%%%%%%%%%%%%% ADD CODE HERE %%%%%%%%%%%%%%%%%%%%%
46+
% Hint: mvnrnd and Sigma %
47+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48+
theta_proposed = ...
49+
50+
% Compute the log-posterior if the intensity is positive
51+
if ( theta_proposed(1) > 0.0 )
52+
53+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54+
%%%%%%%%%%%%%%%%% ADD CODE HERE %%%%%%%%%%%%%%%%%%%%%
55+
% Hint: dpossion and observations %
56+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57+
logposterior_proposed = ...
58+
4459
else
45-
loglikelihood_proposed = -inf;
60+
logposterior_proposed = -inf;
4661
end
4762

4863
% Compute the acceptance probability
49-
aprob = exp( loglikelihood_proposed - loglikelihood(kk-1) );
64+
65+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66+
%%%%%%%%%%%%%%%%% ADD CODE HERE %%%%%%%%%%%%%%%%%%%%%
67+
% Hint: exp, logposterior_proposed and logposterior
68+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69+
aprob = ...
5070

5171
% Generate uniform random variable in U[0,1]
5272
u = unifrnd(0,1);
@@ -56,13 +76,13 @@
5676

5777
% Accept the parameter
5878
theta(kk,:) = theta_proposed;
59-
loglikelihood(kk) = loglikelihood_proposed;
79+
logposterior(kk) = logposterior_proposed;
6080
accept(kk) = 1.0;
6181

6282
else
6383
% Reject the parameter
6484
theta(kk,:) = theta(kk-1,:);
65-
loglikelihood(kk) = loglikelihood(kk-1);
85+
logposterior(kk) = logposterior(kk-1);
6686
accept(kk) = 0.0;
6787
end
6888

0 commit comments

Comments
 (0)