-
Notifications
You must be signed in to change notification settings - Fork 0
/
tapas_ehgf_ar1_binary.m
272 lines (218 loc) · 7.44 KB
/
tapas_ehgf_ar1_binary.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
function [traj, infStates] = tapas_ehgf_ar1_binary(r, p, varargin)
% Calculates the trajectories of the agent's representations under the HGF
%
% This function can be called in two ways:
%
% (1) tapas_ehgf_ar1_binary(r, p)
%
% where r is the structure generated by tapas_fitModel and p is the parameter vector in native space;
%
% (2) tapas_ehgf_ar1_binary(r, ptrans, 'trans')
%
% where r is the structure generated by tapas_fitModel, ptrans is the parameter vector in
% transformed space, and 'trans' is a flag indicating this.
%
% --------------------------------------------------------------------------------------------------
% Copyright (C) 2012-2020 Christoph Mathys, TNU, UZH & ETHZ
%
% This file is part of the HGF toolbox, which is released under the terms of the GNU General Public
% Licence (GPL), version 3. You can redistribute it and/or modify it under the terms of the GPL
% (either version 3 or, at your option, any later version). For further details, see the file
% COPYING or <http://www.gnu.org/licenses/>.
% Transform paramaters back to their native space if needed
if ~isempty(varargin) && strcmp(varargin{1},'trans')
p = tapas_ehgf_ar1_binary_transp(r, p);
end
% Number of levels
try
l = r.c_prc.n_levels;
catch
l = (length(p)+1)/7;
if l ~= floor(l)
error('tapas:hgf:UndetNumLevels', 'Cannot determine number of levels');
end
end
% Unpack parameters
mu_0 = p(1:l);
sa_0 = p(l+1:2*l);
phi = p(2*l+1:3*l);
m = p(3*l+1:4*l);
rho = p(4*l+1:5*l);
ka = p(5*l+1:6*l-1);
om = p(6*l:7*l-2);
th = exp(p(7*l-1));
% Add dummy "zeroth" trial
u = [0; r.u(:,1)];
% Number of trials (including prior)
n = length(u);
% Assume that if u has more than one column, the last contains t
try
if r.c_prc.irregular_intervals
if size(u,2) > 1
t = [0; r.u(:,end)];
else
error('tapas:hgf:InputSingleColumn', 'Input matrix must contain more than one column if irregular_intervals is set to true.');
end
else
t = ones(n,1);
end
catch
if size(u,2) > 1
t = [0; r.u(:,end)];
else
t = ones(n,1);
end
end
% Initialize updated quantities
% Representations
mu = NaN(n,l);
pi = NaN(n,l);
% Other quantities
muhat = NaN(n,l);
pihat = NaN(n,l);
v = NaN(n,l);
w = NaN(n,l-1);
da = NaN(n,l);
% Representation priors
% Note: first entries of the other quantities remain
% NaN because they are undefined and are thrown away
% at the end; their presence simply leads to consistent
% trial indices.
mu(1,1) = tapas_sgm(mu_0(1), 1);
pi(1,1) = Inf;
mu(1,2:end) = mu_0(2:end);
pi(1,2:end) = 1./sa_0(2:end);
% Pass through representation update loop
for k = 2:1:n
if not(ismember(k-1, r.ign))
%%%%%%%%%%%%%%%%%%%%%%
% Effect of input u(k)
%%%%%%%%%%%%%%%%%%%%%%
% 2nd level prediction
muhat(k,2) = mu(k-1,2) +t(k) *rho(2) +t(k) *phi(2) *(m(2) -mu(k-1,2));
% 1st level
% ~~~~~~~~~
% Prediction
muhat(k,1) = tapas_sgm(ka(1) *muhat(k,2), 1);
% Precision of prediction
pihat(k,1) = 1/(muhat(k,1)*(1 -muhat(k,1)));
% Updates
pi(k,1) = Inf;
mu(k,1) = u(k);
% Prediction error
da(k,1) = mu(k,1) -muhat(k,1);
% 2nd level
% ~~~~~~~~~
% Prediction: see above
% Precision of prediction
pihat(k,2) = 1/(1/pi(k-1,2) +exp(ka(2) *mu(k-1,3) +om(2)));
% Updates
pi(k,2) = pihat(k,2) +ka(1)^2/pihat(k,1);
mu(k,2) = muhat(k,2) +ka(1)/pi(k,2) *da(k,1);
% Volatility prediction error
da(k,2) = (1/pi(k,2) +(mu(k,2) -muhat(k,2))^2) *pihat(k,2) -1;
if l > 3
% Pass through higher levels
% ~~~~~~~~~~~~~~~~~~~~~~~~~~
for j = 3:l-1
% Prediction
muhat(k,j) = mu(k-1,j) +t(k) *rho(j) +t(k) *phi(j) *(m(j) -mu(k-1,j));
% Precision of prediction
pihat(k,j) = 1/(1/pi(k-1,j) +t(k) *exp(ka(j) *mu(k-1,j+1) +om(j)));
% Weighting factor
v(k,j-1) = t(k) *exp(ka(j-1) *mu(k-1,j) +om(j-1));
w(k,j-1) = v(k,j-1) *pihat(k,j-1);
% Mean update
mu(k,j) = muhat(k,j) +1/2 *1/pihat(k,j) *ka(j-1) *w(k,j-1) *da(k,j-1);
% Ingredients of precision update which depend on the mean
% update
vv = t(k) *exp(ka(j-1) *mu(k,j) +om(j-1));
pimhat = 1/(1/pi(k-1,j-1) +vv);
ww = vv *pimhat;
rr = (vv -1/pi(k-1,j-1)) *pimhat;
dd = (1/pi(k,j-1) +(mu(k,j-1) -muhat(k,j-1))^2) *pimhat -1;
% Precision update
pi(k,j) = pihat(k,j) +max(0, 1/2 *ka(j-1)^2 *ww*(ww +rr*dd));
% Volatility prediction error
da(k,j) = (1/pi(k,j) +(mu(k,j) -muhat(k,j))^2) *pihat(k,j) -1;
end
end
% Last level
% ~~~~~~~~~~
% Prediction
muhat(k,l) = mu(k-1,l) +t(k) *rho(l) +t(k) *phi(l) *(m(l) -mu(k-1,l));
% Precision of prediction
pihat(k,l) = 1/(1/pi(k-1,l) +t(k) *th);
% Weighting factor
v(k,l) = t(k) *th;
v(k,l-1) = t(k) *exp(ka(l-1) *mu(k-1,l) +om(l-1));
w(k,l-1) = v(k,l-1) *pihat(k,l-1);
% Mean update
mu(k,l) = muhat(k,l) +1/2 *1/pihat(k,l) *ka(l-1) *w(k,l-1) *da(k,l-1);
% Ingredients of the precision update which depend on the mean
% update
vv = t(k) *exp(ka(l-1) *mu(k,l) +om(l-1));
pimhat = 1/(1/pi(k-1,l-1) +vv);
ww = vv *pimhat;
rr = (vv -1/pi(k-1,l-1)) *pimhat;
dd = (1/pi(k,l-1) +(mu(k,l-1) -muhat(k,l-1))^2) *pimhat -1;
pi(k,l) = pihat(k,l) +max(0, 1/2 *ka(l-1)^2 *ww*(ww +rr*dd));
% Volatility prediction error
da(k,l) = (1/pi(k,l) +(mu(k,l) -muhat(k,l))^2) *pihat(k,l) -1;
else
mu(k,:) = mu(k-1,:);
pi(k,:) = pi(k-1,:);
muhat(k,:) = muhat(k-1,:);
pihat(k,:) = pihat(k-1,:);
v(k,:) = v(k-1,:);
w(k,:) = w(k-1,:);
da(k,:) = da(k-1,:);
end
end
% Implied learning rate at the first level
sgmmu2 = tapas_sgm(ka(1) *mu(:,2), 1);
dasgmmu2 = u -sgmmu2;
lr1 = diff(sgmmu2)./dasgmmu2(2:n,1);
lr1(da(2:n,1)==0) = 0;
% Remove representation priors
mu(1,:) = [];
pi(1,:) = [];
% Remove other dummy initial values
muhat(1,:) = [];
pihat(1,:) = [];
v(1,:) = [];
w(1,:) = [];
da(1,:) = [];
% Create result data structure
traj = struct;
traj.mu = mu;
traj.sa = 1./pi;
traj.muhat = muhat;
traj.sahat = 1./pihat;
traj.v = v;
traj.w = w;
traj.da = da;
% Updates with respect to prediction
traj.ud = mu -muhat;
% Psi (precision weights on prediction errors)
psi = NaN(n-1,l);
psi(:,2) = 1./pi(:,2);
psi(:,3:l) = pihat(:,2:l-1)./pi(:,3:l);
traj.psi = psi;
% Epsilons (precision-weighted prediction errors)
epsi = NaN(n-1,l);
epsi(:,2:l) = psi(:,2:l) .*da(:,1:l-1);
traj.epsi = epsi;
% Full learning rate (full weights on prediction errors)
wt = NaN(n-1,l);
wt(:,1) = lr1;
wt(:,2) = psi(:,2);
wt(:,3:l) = 1/2 *(v(:,2:l-1) *diag(ka(2:l-1))) .*psi(:,3:l);
traj.wt = wt;
% Create matrices for use by the observation model
infStates = NaN(n-1,l,4);
infStates(:,:,1) = traj.muhat;
infStates(:,:,2) = traj.sahat;
infStates(:,:,3) = traj.mu;
infStates(:,:,4) = traj.sa;
end