Skip to content

Commit 81c2ac8

Browse files
authored
Update Pencil_and_paper_exercise_solutions.m
1 parent ca20182 commit 81c2ac8

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

Pencil_and_paper_exercise_solutions.m

+22-14
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99

1010
% Note to readers: be sure to run sections individually
1111

12+
13+
%% Static perception
14+
1215
clear
1316
close all
1417
rng('default')
1518

16-
17-
%% Static perception
18-
1919
% priors
2020
D = [.75 .25]';
2121

@@ -27,10 +27,14 @@
2727
o = [1 0]';
2828

2929
% express generative model in terms of update equations
30-
lns = nat_log(D) + nat_log(A')*o;
30+
lns = nat_log(D) + nat_log(A'*o);
3131

3232
% normalize using a softmax function to find posterior
33-
s = (exp(lns)/sum(exp(lns)))
33+
s = (exp(lns)/sum(exp(lns)));
34+
35+
disp('Posterior over states q(s):');
36+
disp(' ');
37+
disp(s);
3438

3539
% Note: Because the natural log of 0 is undefined, for numerical reasons
3640
% the nat_log function here replaces zero values with very small values. This
@@ -42,6 +46,8 @@
4246
%% Dynamic perception
4347

4448
clear
49+
close all
50+
rng('default')
4551

4652
% priors
4753
D = [.5 .5]';
@@ -51,14 +57,15 @@
5157
.1 .9];
5258

5359
% transitions
54-
B = [1 0;
55-
0 1];
60+
B = [1 0;
61+
0 1];
5662

5763
% observations
5864
o{1,1} = [1 0]';
5965
o{1,2} = [0 0]';
6066
o{2,1} = [1 0]';
6167
o{2,2} = [1 0]';
68+
6269
% number of timesteps
6370
T = 2;
6471

@@ -74,26 +81,27 @@
7481
lnD = nat_log(D);% past
7582
lnBs = nat_log(B'*Qs(:,tau+1));% future
7683
elseif tau == T % last time point
77-
lnD = nat_log(B'*Qs(:,tau-1));% no contribution from future
78-
else % 1 > tau > T
79-
lnD = nat_log(B*Q(:,tau-1));
80-
lnBs = nat_log(B'*Q(:,tau+1));
84+
lnBs = nat_log(B'*Qs(:,tau-1));% no contribution from future
8185
end
8286
% likelihood
8387
lnAo = nat_log(A'*o{t,tau});
8488
% update equation
8589
if tau == 1
86-
lns = .5*lnD + .5*lnBs + lnAo;
90+
lns = .5*lnD + .5*lnBs + lnAo;
8791
elseif tau == T
88-
lns = .5*lnD + lnAo;
92+
lns = .5*lnBs + lnAo;
8993
end
9094
% normalize using a softmax function to find posterior
91-
Qs(:,tau) = (exp(lns)/sum(exp(lns)));
95+
Qs(:,tau) = (exp(lns)/sum(exp(lns)))
9296
end
9397
end
9498

9599
Qs % final posterior beliefs over states
96100

101+
disp('Posterior over states q(s):');
102+
disp(' ');
103+
disp(Qs);
104+
97105
%% functions
98106

99107
% natural log that replaces zero values with very small values for numerical reasons.

0 commit comments

Comments
 (0)