-
Notifications
You must be signed in to change notification settings - Fork 5
/
spm_DEM_qP.m
166 lines (141 loc) · 4.51 KB
/
spm_DEM_qP.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
function spm_DEM_qP(qP,pP)
% reports on conditional estimates of parameters
% FORMAT spm_DEM_qP(qP,pP)
%
% qP.P - conditional expectations
% qP.V - conditional variance
%
% pP - optional priors
%__________________________________________________________________________
% Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
% Karl Friston
% $Id: spm_DEM_qP.m 3878 2010-05-07 19:53:54Z karl $
% unpack conditional covariances
%--------------------------------------------------------------------------
g = length(qP.P); % depth of hierarchy
ci = spm_invNcdf(1 - 0.05);
% loop over levels
%--------------------------------------------------------------------------
Label = {};
for i = 1:g
% check for last level
%----------------------------------------------------------------------
if isempty(qP.P{i}), break, end
% get lablels
%----------------------------------------------------------------------
label = {};
if isstruct(qP.P{i})
names = fieldnames(qP.P{i});
for j = 1:length(names)
for k = 1:length(spm_vec(getfield(qP.P{i},names{j})))
label{end + 1} = names{j};
end
end
end
% conditional expectations (with priors if specified)
%----------------------------------------------------------------------
qi = spm_vec(qP.P{i});
c = sqrt(spm_vec(qP.V{i}))*ci;
j = find(c);
qi = qi(j);
c = c(j);
try
label = label(j);
end
try
pi = spm_vec(pP.P{i});
pi = pi(j);
end
np = length(qi);
if np
% use current axes if P = P{1}
%------------------------------------------------------------------
if g > 1, subplot(g,1,i), end
% conditional means
%------------------------------------------------------------------
bar(qi,'Edgecolor',[1 1 1]/2,'Facecolor',[1 1 1]*.8)
title(sprintf('parameters - level %i',i),'FontSize',16);
axis square
box off
set(gca,'XLim',[0 np + 1])
% conditional variances
%------------------------------------------------------------------
for k = 1:np
line([k k], [-1 1]*c(k) + qi(k),'LineWidth',4,'Color','r');
end
% prior or true means
%------------------------------------------------------------------
try
for k = 1:np
line([-1 1]/2 + k,[0 0] + pi(k),'LineWidth',4,'Color','k');
end
end
% labels
%------------------------------------------------------------------
for k = 1:length(label)
text(k + 1/4,qi(k),label{k},'FontSize',12,'FontWeight','Bold','Color','g');
end
Label = {Label{:}, label{:}};
end
end
% conditional (or prior) covariance
%--------------------------------------------------------------------------
try
if length(qP.C) == 1;
return
else
i = find(diag(qP.C));
end
catch
return
end
subplot(g,2,g + g - 1)
if exist('pC','var')
imagesc(spm_cov2corr(pC(i,i)))
title({'prior correlations','among parameters'},'FontSize',16)
else
imagesc(qP.C(i,i))
title({'conditional covariances','among parameters'},'FontSize',16)
end
if ~isempty(Label)
set(gca,'YTickLabel',Label,'YTick',[1:length(Label)])
end
axis square
% plot evolution of hyperparameters if supplied
%==========================================================================
subplot(g,2,g + g)
try
% confidence interval and expectations
%----------------------------------------------------------------------
ns = length(qP.p);
t = 1:ns;
for i = 1:ns
v(:,i) = sqrt(diag(qP.U*qP.c{i}*qP.U'));
end
c = ci*v;
p = qP.U*spm_cat(qP.p);
i = find(any(v,2));
c = c(i,:);
p = p(i,:);
% plot
%----------------------------------------------------------------------
hold on
np = size(p,1);
for i = 1:np
fill([t fliplr(t)],[(p(i,:) + c(i,:)) fliplr(p(i,:) - c(i,:))],...
[1 1 1]*.8,'EdgeColor',[1 1 1]/2)
plot(t,p(i,:))
end
set(gca,'XLim',[1 ns])
title({'dynamics of parameters','(minus prior)'},'FontSize',16)
xlabel('time')
axis square
hold off
catch
% or correlations
%----------------------------------------------------------------------
imagesc(spm_cov2corr(qP.C(i,i)))
title({'conditional correlations','among parameters'},'FontSize',16)
axis square
drawnow
end