-
Notifications
You must be signed in to change notification settings - Fork 2
/
spm_DEM_eval.m
277 lines (237 loc) · 8.91 KB
/
spm_DEM_eval.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
273
274
275
function [E, dE] = spm_DEM_eval(M,qu,qp)
% evaluates state equations and derivatives for DEM schemes
% FORMAT [E dE] = spm_DEM_eval(M,qu,qp)
%
% M - model structure
% qu - conditional mode of states
% qu.v{i} - casual states
% qu.x(i) - hidden states
% qu.y(i) - response
% qu.u(i) - input
% qp - conditional density of parameters
% qp.p{i} - parameter deviates for i-th level
% qp.u(i) - basis set
% qp.x(i) - expansion point ( = prior expectation)
%
% E - generalised errors (i.e.., y - g(x,v,P); x[1] - f(x,v,P))
%
% dE:
% dE.du - de[1:n]/du
% dE.dy - de[1:n]/dy[1:n]
% dE.dc - de[1:n]/dc[1:d]
% dE.dp - de[1:n]/dp
% dE.dup - d/dp[de[1:n]/du
% dE.dpu - d/du[de[1:n]/dp
%
% where u = x{1:d]; v[1:d]
%__________________________________________________________________________
% Copyright (C) 2005 Wellcome Department of Imaging Neuroscience
% Karl Friston
% $Id$
% persistent variables to avoid redundant evaluations
%==========================================================================
persistent Qp dg df
% test for change in parameters and record them
%--------------------------------------------------------------------------
try
du = any(spm_vec(qp.p) - spm_vec(Qp.p)) & M(1).E.linear;
catch
du = 1;
end
Qp = qp;
%==========================================================================
nl = size(M,2); % number of levels
ne = sum(spm_vec(M.l)); % number of e (errors)
nv = sum(spm_vec(M.m)); % number of x (causal states)
nx = sum(spm_vec(M.n)); % number of x (hidden states)
np = sum(spm_vec(M.p)); % number of p (parameters)
ny = M(1).l; % number of y (inputs)
nc = M(end).l; % number of c (prior causes)
% order parameters (d = n = 1 for static models)
%==========================================================================
d = M(1).E.d + 1; % generalisation order of q(v)
n = M(1).E.n + 1; % embedding order (n >= d)
% derivatives: dgdp{i,j} = dDi(e)/dp, ... Di(e) = (d/dt)^i[e], ...
%--------------------------------------------------------------------------
fe = cell(n,1);
ge = cell(n,1);
[fe{:}] = deal(sparse(nx,1));
[ge{:}] = deal(sparse(ne,1));
df.dp = cell(nl - 1,nl - 1);
dg.dp = cell(nl ,nl - 1);
for i = 1:(nl - 1)
dg.dp{i + 1,i} = sparse(M(i).m,M(i).p);
dg.dp{i ,i} = sparse(M(i).l,M(i).p);
df.dp{i ,i} = sparse(M(i).n,M(i).p);
end
% create deriavtice w.r.t. states if du
%--------------------------------------------------------------------------
if du
% initialise cell arrays for hierarchical structure
%--------------------------------------------------------------------------
df.dv = cell(nl - 1,nl - 1);
df.dx = cell(nl - 1,nl - 1);
dg.dv = cell(nl ,nl - 1);
dg.dx = cell(nl ,nl - 1);
% & fill in hierarchical forms
%--------------------------------------------------------------------------
for i = 1:(nl - 1)
dg.dv{i + 1,i} = sparse(M(i).m,M(i).m);
dg.dx{i + 1,i} = sparse(M(i).m,M(i).n);
dg.dv{i ,i} = sparse(M(i).l,M(i).m);
dg.dx{i ,i} = sparse(M(i).l,M(i).n);
df.dv{i ,i} = sparse(M(i).n,M(i).m);
df.dx{i ,i} = sparse(M(i).n,M(i).n);
end
if np
dg.dvp = cell(np,1);
dg.dxp = cell(np,1);
df.dvp = cell(np,1);
df.dxp = cell(np,1);
[dg.dvp{:}] = deal(dg.dv);
[dg.dxp{:}] = deal(dg.dx);
[df.dvp{:}] = deal(df.dv);
[df.dxp{:}] = deal(df.dx);
end
end
% un-concatenate states {v,x} into hierarchical form
%--------------------------------------------------------------------------
v = qu.v;
x = qu.x;
y = qu.y;
u = qu.u;
vi = spm_unvec(v{1},{M(1 + 1:end).v});
xi = spm_unvec(x{1},{M(1:end - 1).x});
% inline function for evaluating projected parameters
%--------------------------------------------------------------------------
h = 'feval(f,x,v,spm_unvec(spm_vec(p) + u*q,p))';
h = inline(h,'f','x','v','q','u','p');
% Derivatives at each hierarchical level
%==========================================================================
ip = 1;
for i = 1:(nl - 1)
% states and parameters for level i
%----------------------------------------------------------------------
xvp = {xi{i},vi{i},qp.p{i},qp.u{i},M(i).pE};
% g(x,v), f(x,v) and 1st-order partial derivatives (parameters)
%----------------------------------------------------------------------
[dfdp fi] = spm_diff(h,M(i).f,xvp{:},4);
[dgdp gi] = spm_diff(h,M(i).g,xvp{:},4);
% and place in array
%----------------------------------------------------------------------
g{i,1} = gi;
f{i,1} = fi;
df.dp{i,i} = dfdp;
dg.dp{i,i} = dgdp;
% if the system is nonlinear or the parameters have changed
%======================================================================
if du
% 1st and 2nd partial derivatives (states)
%------------------------------------------------------------------
[dgdxp dgdx] = spm_diff(h,M(i).g,xvp{:},[2 4]);
[dgdvp dgdv] = spm_diff(h,M(i).g,xvp{:},[3 4]);
[dfdxp dfdx] = spm_diff(h,M(i).f,xvp{:},[2 4]);
[dfdvp dfdv] = spm_diff(h,M(i).f,xvp{:},[3 4]);
% place 1st derivatives in array
%------------------------------------------------------------------
dg.dx{i,i} = dgdx;
dg.dv{i,i} = dgdv;
df.dx{i,i} = dfdx;
df.dv{i,i} = dfdv;
% and add constant terms
%------------------------------------------------------------------
dg.dv{i + 1,i} = -speye(M(i).m,M(i).m);
% place 2nd derivatives in array
%------------------------------------------------------------------
for j = 1:length(dgdxp)
dg.dxp{ip}{i,i} = dgdxp{j};
dg.dvp{ip}{i,i} = dgdvp{j};
df.dxp{ip}{i,i} = dfdxp{j};
df.dvp{ip}{i,i} = dfdvp{j};
ip = ip + 1;
end
end
end
% concatenate hierarchical forms
%--------------------------------------------------------------------------
dgdv = spm_cat(dg.dv);
dgdx = spm_cat(dg.dx);
dfdv = spm_cat(df.dv);
dfdx = spm_cat(df.dx);
dfdp = {spm_cat(df.dp)};
dgdp = {spm_cat(dg.dp)};
for j = 1:np
dgdvp{j} = spm_cat(dg.dvp{j});
dgdxp{j} = spm_cat(dg.dxp{j});
dfdvp{j} = spm_cat(df.dvp{j});
dfdxp{j} = spm_cat(df.dxp{j});
end
% prediction errors and states
%==========================================================================
dfdy = sparse(nx,ny);
dfdc = sparse(nx,nc);
dedy = speye(ne,ny);
dedc = -flipdim(flipdim(speye(ne,nc),1),2);
% prediction error (E) - causes
%--------------------------------------------------------------------------
ge{1} = [y{1}; v{1}] - [spm_vec(g); u{1}];
for i = 2:n
ge{i} = dedy*y{i} + dedc*u{i} ... % generalised response
- dgdx*x{i} - dgdv*v{i}; % and prediction
end
% prediction error (E) - states
%--------------------------------------------------------------------------
try
fe{1} = x{2} - spm_vec(f);
end
for i = 2:n - 1
fe{i} = x{i + 1} ... % generalised motion
- dfdx*x{i} - dfdv*v{i}; % and prediction
end
% error
%--------------------------------------------------------------------------
E = spm_vec({ge, fe});
% Kronecker forms
%==========================================================================
% dE.dp (parameters)
%--------------------------------------------------------------------------
for i = 2:n
dgdp{i,1} = sparse(ny + nv,np);
dfdp{i,1} = sparse(nx,np);
for p = 1:np
dgdp{i,1}(:,p) = dgdxp{p}*x{i} + dgdvp{p}*v{i};
dfdp{i,1}(:,p) = dfdxp{p}*x{i} + dfdvp{p}*v{i};
end
end
% generalised temporal derivatives: dE.du (states)
%--------------------------------------------------------------------------
dedy = kron(spm_speye(n,n),dedy);
dedc = kron(spm_speye(n,d),dedc);
dfdy = kron(spm_speye(n,n),dfdy);
dfdc = kron(spm_speye(n,d),dfdc);
dgdx = kron(spm_speye(n,n),dgdx);
dgdv = kron(spm_speye(n,d),dgdv);
dfdv = kron(spm_speye(n,d),dfdv);
dfdx = kron(spm_speye(n,n),dfdx) - kron(spm_speye(n,n,1),speye(nx,nx));
for k = 1:np
dgdxp{k} = kron(spm_speye(n,n),dgdxp{k});
dfdxp{k} = kron(spm_speye(n,n),dfdxp{k});
dgdvp{k} = kron(spm_speye(n,d),dgdvp{k});
dfdvp{k} = kron(spm_speye(n,d),dfdvp{k});
end
% 1st error derivatives dE.du (states)
%----------------------------------------------------------------------
dE.dy = spm_cat({dedy; dfdy});
dE.dc = spm_cat({dedc; dfdc});
dE.dp = -spm_cat({dgdp; dfdp});
dE.du = -spm_cat({dgdx, dgdv;
dfdx, dfdv});
% 2nd error derivatives
%----------------------------------------------------------------------
for i = 1:np
dE.dup{i} = -spm_cat({dgdxp{i}, dgdvp{i};
dfdxp{i}, dfdvp{i}});
end
if np
dE.dpu = spm_cell_swap(dE.dup);
end