-
Notifications
You must be signed in to change notification settings - Fork 0
/
spm_ADEM_M_set.m
292 lines (252 loc) · 8.07 KB
/
spm_ADEM_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
function [M] = spm_ADEM_M_set(M)
% Set indices and perform checks on hierarchical action models
% FORMAT [M] = spm_ADEM_M_set(M)
%
% for each level (i); required fields
%
% M(i).g = y(t) = g(x,v,a,P) {inline function, string or m-file}
% M(i).f = dx/dt = f(x,v,a,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);
% M(i).k = number of action a(i);
%
% or
%
% M(i).x = hidden states;
% M(i).v = causal states;
% M(i).a = action states;
%
% for each level (i); optional fields
%
% M(i).pE = prior expectation of p model-parameters
% M(i).V = precision (input noise)
% M(i).W = precision (state noise)
% M(i).U = precision (action)
%
%
% sets fields, checks internal consistency of model specification and sets
% estimation parameters. If (V,W) are not specified infinite precision is
% assumed.
%--------------------------------------------------------------------------
%
% 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) 2008-2014 Wellcome Trust Centre for Neuroimaging
% Karl Friston
% $Id: spm_ADEM_M_set.m 5962 2014-04-17 12:47:43Z spm $
% 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','a','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','a','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
% get inputs
%--------------------------------------------------------------------------
try
v = M(g).v;
catch
v = sparse(0,1);
end
if isempty(v)
try
v = sparse(M(g - 1).m,1);
end
end
if isempty(v)
try
v = sparse(M(g).l,1);
end
end
M(g).l = length(spm_vec(v));
M(g).v = v;
% ensure action is specified
%--------------------------------------------------------------------------
for i = 1:g
try
a = M(i).a;
catch
a = sparse(0,1);
end
if isempty(a)
try
a = sparse(M(i).k,1);
end
end
M(i).k = length(spm_vec(a));
M(i).a = a;
end
% check functions
%--------------------------------------------------------------------------
for i = (g - 1):-1:1
try
x = M(i).x;
catch
x = sparse(M(i).n,1);
end
if isempty(x) && M(i).n
x = sparse(M(i).n,1);
end
% check f(x,v,P)
%----------------------------------------------------------------------
try
M(i).f = fcnchk(M(i).f);
if nargin(M(i).f) ~= 4
M(i).f = inline(char(M(i).f),'x','v','a','P');
end
end
try
f = feval(M(i).f,x,v,a,M(i).pE);
if length(spm_vec(x)) ~= length(spm_vec(f))
errordlg(sprintf('please check nargout: G(%i).f(x,v,a,P)',i));
end
catch
errordlg(sprintf('evaluation failure: G(%i).f(x,v,a,P)',i))
end
% check g(x,v,P)
%----------------------------------------------------------------------
try
M(i).g = fcnchk(M(i).g);
if nargin(M(i).g) ~= 4
M(i).g = inline(char(M(i).g),'x','v','a','P');
end
end
try
M(i).m = length(spm_vec(v));
v = feval(M(i).g,x,v,a,M(i).pE);
a = M(i).a;
M(i).k = length(spm_vec(a));
M(i).l = length(spm_vec(v));
M(i).n = length(spm_vec(x));
M(i).a = a;
M(i).v = v;
M(i).x = x;
catch
errordlg(sprintf('evaluation failure: G(%i).g(x,v,a,P)',i))
end
end
% remove empty levels
%--------------------------------------------------------------------------
try
g = find(~spm_vec(M.m),1);
M = M(1:g);
catch
errordlg('please specify number of variables')
end
% number of x (hidden states)
%--------------------------------------------------------------------------
nx = sum(spm_vec(M.n));
% check precisions
%==========================================================================
try, M.V; catch, M(1).V = []; end
try, M.W; catch, M(1).W = []; end
% check precisions
%--------------------------------------------------------------------------
for i = 1:g
% check V and assume unit precision if improperly specified
%----------------------------------------------------------------------
if isvector(M(i).V), M(i).V = diag(M(i).V); end
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)*exp(32);
end
end
% check W and assume unit precision if improperly specified
%----------------------------------------------------------------------
if isvector(M(i).W), M(i).W = diag(M(i).W); end
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)*exp(32);
end
end
end
% check restiction of precision for action (gain)
%==========================================================================
try, M(1).U; catch, M(1).U = []; end
if isvector(M(1).U), M(1).U = diag(M(1).U); end
if length(M(1).U) ~= M(1).l
try
M(1).U = speye(M(1).l,M(1).l)*M(1).U(1);
catch
M(1).U = speye(M(1).l,M(1).l)*exp(2);
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
M(1).E.d = min(M(1).E.d,M(1).E.n);
% checks on smoothness hyperparameter
%==========================================================================
for i = 1:g
try, M(i).sv; catch, M(i).sv = M(1).E.s; end
try, M(i).sw; catch, M(i).sw = M(1).E.s; end
if ~isscalar(M(i).sv), M(i).sv = M(1).E.s; end
if ~isscalar(M(i).sw), M(i).sw = M(1).E.s; end
end