|
9 | 9 |
|
10 | 10 | % Note to readers: be sure to run sections individually
|
11 | 11 |
|
| 12 | + |
| 13 | +%% Static perception |
| 14 | + |
12 | 15 | clear
|
13 | 16 | close all
|
14 | 17 | rng('default')
|
15 | 18 |
|
16 |
| - |
17 |
| -%% Static perception |
18 |
| - |
19 | 19 | % priors
|
20 | 20 | D = [.75 .25]';
|
21 | 21 |
|
|
27 | 27 | o = [1 0]';
|
28 | 28 |
|
29 | 29 | % 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); |
31 | 31 |
|
32 | 32 | % 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); |
34 | 38 |
|
35 | 39 | % Note: Because the natural log of 0 is undefined, for numerical reasons
|
36 | 40 | % the nat_log function here replaces zero values with very small values. This
|
|
42 | 46 | %% Dynamic perception
|
43 | 47 |
|
44 | 48 | clear
|
| 49 | +close all |
| 50 | +rng('default') |
45 | 51 |
|
46 | 52 | % priors
|
47 | 53 | D = [.5 .5]';
|
|
51 | 57 | .1 .9];
|
52 | 58 |
|
53 | 59 | % transitions
|
54 |
| -B = [1 0; |
55 |
| - 0 1]; |
| 60 | + B = [1 0; |
| 61 | + 0 1]; |
56 | 62 |
|
57 | 63 | % observations
|
58 | 64 | o{1,1} = [1 0]';
|
59 | 65 | o{1,2} = [0 0]';
|
60 | 66 | o{2,1} = [1 0]';
|
61 | 67 | o{2,2} = [1 0]';
|
| 68 | + |
62 | 69 | % number of timesteps
|
63 | 70 | T = 2;
|
64 | 71 |
|
|
74 | 81 | lnD = nat_log(D);% past
|
75 | 82 | lnBs = nat_log(B'*Qs(:,tau+1));% future
|
76 | 83 | 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 |
81 | 85 | end
|
82 | 86 | % likelihood
|
83 | 87 | lnAo = nat_log(A'*o{t,tau});
|
84 | 88 | % update equation
|
85 | 89 | if tau == 1
|
86 |
| - lns = .5*lnD + .5*lnBs + lnAo; |
| 90 | + lns = .5*lnD + .5*lnBs + lnAo; |
87 | 91 | elseif tau == T
|
88 |
| - lns = .5*lnD + lnAo; |
| 92 | + lns = .5*lnBs + lnAo; |
89 | 93 | end
|
90 | 94 | % normalize using a softmax function to find posterior
|
91 |
| - Qs(:,tau) = (exp(lns)/sum(exp(lns))); |
| 95 | + Qs(:,tau) = (exp(lns)/sum(exp(lns))) |
92 | 96 | end
|
93 | 97 | end
|
94 | 98 |
|
95 | 99 | Qs % final posterior beliefs over states
|
96 | 100 |
|
| 101 | +disp('Posterior over states q(s):'); |
| 102 | +disp(' '); |
| 103 | +disp(Qs); |
| 104 | + |
97 | 105 | %% functions
|
98 | 106 |
|
99 | 107 | % natural log that replaces zero values with very small values for numerical reasons.
|
|
0 commit comments