-
Notifications
You must be signed in to change notification settings - Fork 0
/
spm_LAP_eval.m
151 lines (114 loc) · 4.26 KB
/
spm_LAP_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
function [p,dp] = spm_LAP_eval(M,qu,qh)
% Evaluate precisions for a LAP model
% FORMAT [p dp] = spm_LAP_eval(M,qu,qh)
%
% p.h - vector of precisions for causal states (v)
% p.g - vector of precisions for hidden states (x)
%
% dp.h.dx - dp.h/dx
% dp.h.dv - dp.h/dv
% dp.h.dh - dp.h/dh
%
% dp.g.dx - dp.g/dx
% dp.g.dv - dp.g/dv
% dp.g.dg - dp.g/dg
%__________________________________________________________________________
% Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
% Karl Friston
% $Id: spm_LAP_eval.m 6290 2014-12-20 22:11:50Z karl $
% Get states {qu.v{1},qu.x{1}} in hierarchical form (v{i},x{i})
%--------------------------------------------------------------------------
N = length(M);
v = cell(N,1);
x = cell(N,1);
v(1:N - 1) = spm_unvec(qu.v{1},{M(1 + 1:N).v});
x(1:N - 1) = spm_unvec(qu.x{1},{M(1:N - 1).x});
% precisions
%==========================================================================
for i = 1:N
% precision of causal and hidden states
%----------------------------------------------------------------------
try
h{i,1} = spm_vec(feval(M(i).ph,x{i},v{i},qh.h{i},M(i)));
catch
h{i,1} = sparse(M(i).l,1);
end
try
g{i,1} = spm_vec(feval(M(i).pg,x{i},v{i},qh.g{i},M(i)));
catch
g{i,1} = sparse(M(i).n,1);
end
end
% Concatenate over hierarchical levels
%--------------------------------------------------------------------------
p.h = spm_cat(h);
p.g = spm_cat(g);
if nargout < 2, return, end
% gradients
%==========================================================================
% assume precisions can be functions of hyper-parameters and states
%--------------------------------------------------------------------------
try method.h = M(1).E.method.h; catch, method.h = 1; end
try method.g = M(1).E.method.g; catch, method.g = 1; end
try method.x = M(1).E.method.x; catch, method.x = 1; end
try method.v = M(1).E.method.v; catch, method.v = 1; end
% number of variables
%--------------------------------------------------------------------------
nx = numel(spm_vec(x));
nv = numel(spm_vec(v));
hn = numel(spm_vec(qh.h));
gn = numel(spm_vec(qh.g));
nh = size(p.h,1);
ng = size(p.g,1);
dp.h.dh = sparse(nh,hn);
dp.g.dg = sparse(ng,gn);
dp.h.dx = sparse(nh,nx);
dp.h.dv = sparse(nh,nv);
dp.g.dx = sparse(ng,nx);
dp.g.dv = sparse(ng,nv);
% gradients w.r.t. h only (no state-dependent noise)
%----------------------------------------------------------------------
if method.h || method.g
for i = 1:N
% precision of causal and hidden states
%--------------------------------------------------------------
dhdh{i,i} = spm_diff(M(i).ph,x{i},v{i},qh.h{i},M(i),3);
dgdg{i,i} = spm_diff(M(i).pg,x{i},v{i},qh.g{i},M(i),3);
end
% Concatenate over hierarchical levels
%------------------------------------------------------------------
dp.h.dh = spm_cat(dhdh);
dp.g.dg = spm_cat(dgdg);
end
% gradients w.r.t. causal states
%----------------------------------------------------------------------
if method.v
for i = 1:N
% precision of causal states
%--------------------------------------------------------------
dhdv{i,i} = spm_diff(M(i).ph,x{i},v{i},qh.h{i},M(i),2);
% precision of hidden states
%--------------------------------------------------------------
dgdv{i,i} = spm_diff(M(i).pg,x{i},v{i},qh.g{i},M(i),2);
end
% Concatenate over hierarchical levels
%------------------------------------------------------------------
dp.h.dv = spm_cat(dhdv);
dp.g.dv = spm_cat(dgdv);
end
% gradients w.r.t. hidden states
%----------------------------------------------------------------------
if method.x
for i = 1:N
% precision of causal states
%--------------------------------------------------------------
dhdx{i,i} = spm_diff(M(i).ph,x{i},v{i},qh.h{i},M(i),1);
% precision of hidden states
%--------------------------------------------------------------
dgdx{i,i} = spm_diff(M(i).pg,x{i},v{i},qh.g{i},M(i),1);
end
% Concatenate over hierarchical levels
%------------------------------------------------------------------
dp.h.dx = spm_cat(dhdx);
dp.g.dx = spm_cat(dgdx);
end