-
Notifications
You must be signed in to change notification settings - Fork 2
/
spm_DEM_M_set.m
386 lines (328 loc) · 11.9 KB
/
spm_DEM_M_set.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
function [M] = spm_DEM_M_set(M)
% sets indices and performs checks on hierarchical models
% FORMAT [M] = spm_DEM_M_set(M)
%
% for each level (i); required fields
%
% M(i).g = y(t) = g(x,v,P) {inline function, string or m-file}
% M(i).f = dx/dt = f(x,v,P) {inline function, string or m-file}
%
% and
%
% M(i).m = number of inputs v(i + 1);
% M(i).n = number of states x(i);
% M(i).l = number of output v(i);
%
% or
%
% M(i).x = hidden states;
% M(i).v = causal states;
%
% for each level (i); optional fields
%
% M(i).pE = prior expectation of p model-parameters
% M(i).pC = prior covariances of p model-parameters
% M(i).hE = prior expectation of h hyper-parameters (input noise)
% M(i).hC = prior covariances of h hyper-parameters (input noise)
% M(i).gE = prior expectation of g hyper-parameters (state noise)
% M(i).gC = prior covariances of g hyper-parameters (state noise)
% M(i).Q = precision components (input noise)
% M(i).R = precision components (state noise)
% M(i).V = fixed precision (input noise)
% M(i).W = fixed precision (state noise)
%
%
% sets fields, checks internal consistency of model specification and sets
% estimation parameters. If a single hyperparameter is supplied i.i.d
% components are assumed (i.e., Q = I, R = I)
%--------------------------------------------------------------------------
%
% M(1).E.s; = smoothness (s.d. in time bins)
% M(1).E.d; = embedding order q(v) (i.e., number of derivatives)
% M(1).E.n; = embedding order q(x)
%
% If the highest level involves any dynamic or static transformation
% of its inputs a further level is added with flat priors
%__________________________________________________________________________
% Copyright (C) 2005 Wellcome Department of Imaging Neuroscience
% Karl Friston
% $Id$
% order
%--------------------------------------------------------------------------
g = length(M);
% set missing fields
%==========================================================================
% check for specification of hidden states
%--------------------------------------------------------------------------
if isfield(M,'f') && ~isfield(M,'n') && ~isfield(M,'x')
msgbox('please specify hidden states or their number')
error(' ')
end
% check supra-ordinate level and add one (with flat priors) if necessary
%--------------------------------------------------------------------------
try
fcnchk(M(g).g);
g = g + 1;
M(g).l = M(g - 1).m;
end
M(g).m = 0;
M(g).n = 0;
% default fields for static models (hidden states)
%--------------------------------------------------------------------------
if ~isfield(M,'f')
[M.f] = deal(inline('sparse(0,1)','x','v','P'));
[M.x] = deal(sparse(0,1));
[M.n] = deal(0);
end
for i = 1:g
try
fcnchk(M(i).f);
catch
M(i).f = inline('sparse(0,1)','x','v','P');
M(i).x = sparse(0,1);
end
end
% consistency and format check on states, parameters and functions
%==========================================================================
% prior expectation of parameters M.pE
%--------------------------------------------------------------------------
try
M.pE;
catch
% Assume fixed parameters
%----------------------------------------------------------------------
for i = 1:g
M(i).pE = sparse(0,0);
end
end
% and priors covariances - p
%--------------------------------------------------------------------------
try
M.pC;
catch
% Assume fixed parameters
%----------------------------------------------------------------------
for i = 1:g
p = length(spm_vec(M(i).pE));
M(i).pC = sparse(p,p);
end
end
% check pC if user specified
%--------------------------------------------------------------------------
for i = 1:g
% Assume fixed parameters if not specified
%----------------------------------------------------------------------
if length(M(i).pC) == 0
p = length(spm_vec(M(i).pE));
M(i).pC = sparse(p,p);
end
% convert variances to covariances if necessary
%----------------------------------------------------------------------
if length(M(i).pC) == 1
M(i).pC = sparse(diag(M(i).pC));
end
% check size
%----------------------------------------------------------------------
if length(M(i).pC) ~= length(spm_vec(M(i).pE))
errordlg(sprintf('please check: M(%i).pC',i))
end
end
% get inputs
%--------------------------------------------------------------------------
try
v = M(g).v;
catch
v = sparse(0,0);
end
if ~length(v)
try
v = sparse(M(g - 1).m,1);
end
end
if ~length(v)
try
v = sparse(M(g).l,1);
end
end
M(g).l = length(spm_vec(v));
M(g).v = v;
% check functions
%--------------------------------------------------------------------------
for i = (g - 1):-1:1
try
x = M(i).x;
catch
x = sparse(M(i).n,1);
end
if ~length(x) && M(i).n
x = sparse(M(i).n,1);
end
% check f(x,v,P)
%----------------------------------------------------------------------
try
M(i).f = fcnchk(M(i).f,'x','v','P');
end
try
f = feval(M(i).f,x,v,M(i).pE);
if length(spm_vec(x)) ~= length(spm_vec(f))
str = sprintf('please check: M(%i).f(x,v,P)',i);
msgbox(str)
error(' ')
end
catch
errordlg(sprintf('evaluation failure: M(%i).f(x,v,P)',i))
end
% check g(x,v,P)
%----------------------------------------------------------------------
try
M(i).g = fcnchk(M(i).g,'x','v','P');
end
try
M(i).m = length(spm_vec(v));
v = feval(M(i).g,x,v,M(i).pE);
M(i).l = length(spm_vec(v));
M(i).n = length(spm_vec(x));
M(i).v = v;
M(i).x = x;
catch
errordlg(sprintf('evaluation failure: M(%i).g(x,v,P)',i))
end
end
% remove empty levels
%--------------------------------------------------------------------------
try
g = min(find(~spm_vec(M.m)));
M = M(1:g);
catch
errordlg('please specify number of variables')
end
% number of x (hidden states)
%--------------------------------------------------------------------------
nx = sum(spm_vec(M.n));
% Hyperparameters and components (causes: Q V and hidden states R, W)
%==========================================================================
try, M.Q; catch, M(1).Q = []; end
try, M.R; catch, M(1).R = []; end
try, M.V; catch, M(1).V = []; end
try, M.W; catch, M(1).W = []; end
try, M.hE; catch, M(1).hE = []; end
try, M.gE; catch, M(1).gE = []; end
% check hyperpriors hE - [log]hyper-parameters and components
%--------------------------------------------------------------------------
for i = 1:g
% make sure components are cell arrays
%----------------------------------------------------------------------
if length(M(i).Q) & ~iscell(M(i).Q), M(i).Q = {M(i).Q}; end
if length(M(i).R) & ~iscell(M(i).R), M(i).R = {M(i).R}; end
% check hyperpriors
%======================================================================
% vectorise
%----------------------------------------------------------------------
M(i).hE = spm_vec(M(i).hE);
M(i).gE = spm_vec(M(i).gE);
% check hyperpriors (expectations)
%----------------------------------------------------------------------
if ~length(M(i).hE), M(i).hE = sparse(length(M(i).Q),1); end
if ~length(M(i).gE), M(i).gE = sparse(length(M(i).R),1); end
% check hyperpriors (covariances)
%----------------------------------------------------------------------
try, M(i).hC*M(i).hE; catch, M(i).hC = speye(length(M(i).hE))*256; end
try, M(i).gC*M(i).gE; catch, M(i).gC = speye(length(M(i).gE))*256; end
if ~length(M(i).hC), M(i).hC = speye(length(M(i).hE))*256; end
if ~length(M(i).gC), M(i).gC = speye(length(M(i).gE))*256; end
% check Q and R (precision components)
%======================================================================
% check components and assume i.i.d if not specified
%----------------------------------------------------------------------
if length(M(i).Q) > length(M(i).hE)
M(i).hE = sparse(length(M(i).Q),1);
end
if length(M(i).Q) < length(M(i).hE)
M(i).Q = {speye(M(i).l,M(i).l)};
M(i).hE = M(i).hE(1);
end
if length(M(i).R) > length(M(i).gE)
M(i).gE = sparse(length(M(i).R),1);
end
if length(M(i).R) < length(M(i).gE)
M(i).R = {speye(M(i).n,M(i).n)};
M(i).gE = M(i).gE(1);
end
% check consistency and sizes (Q)
%----------------------------------------------------------------------
for j = 1:length(M(i).Q)
if length(M(i).Q{j}) ~= M(i).l
errordlg(sprintf('wrong size; M(%d).Q{%d}',i,j))
end
end
% check consistency and sizes (R)
%----------------------------------------------------------------------
for j = 1:length(M(i).R)
if length(M(i).R{j}) ~= M(i).n
errordlg(sprintf('wrong size; M(%d).R{%d}',i,j))
end
end
% check V and W (expansion point for precisions)
%======================================================================
% check V and assume unit precision if improperly specified
%----------------------------------------------------------------------
if length(M(i).V) ~= M(i).l
try
M(i).V = speye(M(i).l,M(i).l)*M(i).V(1);
catch
M(i).V = speye(M(i).l,M(i).l);
end
end
% remove fixed components if hyperparameters exist
%----------------------------------------------------------------------
if length(M(i).hE)
M(i).V = sparse(M(i).l,M(i).l);
end
% check W and assume unit precision if improperly specified
%----------------------------------------------------------------------
if length(M(i).W) ~= M(i).n
try
M(i).W = speye(M(i).n,M(i).n)*M(i).W(1);
catch
M(i).W = speye(M(i).n,M(i).n);
end
end
% remove fixed components if hyperparameters exist
%----------------------------------------------------------------------
if length(M(i).gE)
M(i).W = sparse(M(i).n,M(i).n);
end
end
% estimation parameters M(1).E.s, n,...
%==========================================================================
% E.s; % smoothness (seconds)
% E.dt; % time step
% E.d; % approximation order of q(x,v)
% E.n; % order of embedding (n >= d)
% temporal smoothness - s.d. of kernel
%--------------------------------------------------------------------------
try M(1).E.s; catch, if nx, M(1).E.s = 1/2; else M(1).E.s = 0; end, end
% time step
%--------------------------------------------------------------------------
try M(1).E.dt; catch M(1).E.dt = 1; end
% embedding orders
%--------------------------------------------------------------------------
try M(1).E.d; catch, if nx, M(1).E.d = 2; else M(1).E.d = 0; end, end
try M(1).E.n; catch, if nx, M(1).E.n = 6; else M(1).E.n = 0; end, end
% number of iterations
%--------------------------------------------------------------------------
try M(1).E.nD; catch, if nx, M(1).E.nD = 1; else M(1).E.nD = 8; end, end
try M(1).E.nE; catch, M(1).E.nE = 1; end
try M(1).E.nM; catch, M(1).E.nM = 8; end
try M(1).E.nN; catch, M(1).E.nN = 16; end
% checks on estimability
%==========================================================================
% check that there are informative priors on the states or the causes
%--------------------------------------------------------------------------
Q = ~norm(M(end).V,1);
for i = 1:(g - 1)
P = norm(M(i).pC,1) > exp(8);
if P & Q
warndlg('please use informative priors on causes or parameters')
end
end