-
Notifications
You must be signed in to change notification settings - Fork 2
/
spm_DEM_qU.m
176 lines (154 loc) · 5.02 KB
/
spm_DEM_qU.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
function spm_DEM_qU(qU,pU)
% displays conditional estimates of states (qU)
% FORMAT spm_DEM_qU(qU,pU);
%
% qU.v{i} - causal states (V{1} = y = predicted response)
% qU.x{i} - hidden states
% qU.e{i} - prediction error
% qU.C{N} - conditional covariance - [causal states] for N samples
% qU.S{N} - conditional covariance - [hidden states] for N samples
%
% pU - optional input for known states
%__________________________________________________________________________
% Copyright (C) 2005 Wellcome Department of Imaging Neuroscience
% Karl Friston
% $Id$
% unpack
%--------------------------------------------------------------------------
clf
V = qU.v;
X = qU.x;
E = qU.z;
try
C = qU.C;
S = qU.S;
end
try
pV = pU.v;
pX = pU.x;
end
try
pA = qU.a;
end
% time-series specification
%--------------------------------------------------------------------------
g = length(V); % order of hierarchy
N = size(V{1},2); % length of data sequence
dt = 1; % time step
t = [1:N]*dt; % time
% unpack conditional covariances
%--------------------------------------------------------------------------
ci = spm_invNcdf(1 - 0.05);
s = [];
c = [];
try
for i = 1:N
c = [c sqrt(diag(C{i}))];
s = [s sqrt(diag(S{i}))];
end
end
% loop over levels
%--------------------------------------------------------------------------
for i = 1:g
if N == 1
% causal states and error - single observation
%------------------------------------------------------------------
subplot(g,2,2*i - 1)
t = 1:size(V{i},1);
plot(t,full(E{i}),'r:',t,full(V{i}))
% conditional covariances
%------------------------------------------------------------------
if i > 1 & size(c,1)
hold on
j = [1:size(V{i},1)];
y = ci*c(j,:);
c(j,:) = [];
fill([t fliplr(t)],[full(V{i} + y)' fliplr(full(V{i} - y)')],...
[1 1 1]*.8,'EdgeColor',[1 1 1]*.8)
plot(t,full(E{i}),'r:',t,full(V{i}))
hold off
end
% title and grid
%------------------------------------------------------------------
title(sprintf('causal states - level %i',i));
xlabel('states')
grid on
axis square
set(gca,'XLim',[t(1) t(end)])
else
% causal states and error - time series
%------------------------------------------------------------------
subplot(g,2,2*i - 1)
try,
plot(t,pV{i},'linewidth',2,'color',[1 1 1]/2)
end, hold on
plot(t,full(E{i}(:,1:N)),'r:',t,full(V{i}))
hold off
set(gca,'XLim',[t(1) t(end)])
a = axis;
% conditional covariances
%------------------------------------------------------------------
if i > 1 & size(c,1)
hold on
j = [1:size(V{i},1)];
y = ci*c(j,:);
c(j,:) = [];
fill([t fliplr(t)],[full(V{i} + y) fliplr(full(V{i} - y))],...
[1 1 1]*.8,'EdgeColor',[1 1 1]*.8)
plot(t,full(E{i}(:,1:N)),'r:',t,full(V{i}))
hold off
end
% title, action, grid and true casues (if available)
%------------------------------------------------------------------
if i == 1
title('predicted response and error');
else
title(sprintf('causal states - level %i',i));
try, hold on
plot(t,pV{i},'linewidth',2,'color',[1 1 1]/2)
end, hold off
try, hold on
plot(t,pA{i - 1},'linewidth',1,'color',[1 0 0])
end, hold off
end
xlabel('time {bins}')
ylabel('states (a.u.)')
grid on
axis square
axis(a)
% hidden states
%------------------------------------------------------------------
try
subplot(g,2,2*i)
plot(t,full(X{i}))
try, hold on
plot(t,pX{i},'linewidth',2,'color',[1 1 1]/2)
end, hold off
set(gca,'XLim',[t(1) t(end)])
a = axis;
if length(s)
hold on
j = [1:size(X{i},1)];
y = ci*s(j,:);
s(j,:) = [];
fill([t fliplr(t)],[full(X{i} + y) fliplr(full(X{i} - y))],...
[1 1 1]*.8,'EdgeColor',[1 1 1]*.8)
plot(t,full(X{i}))
hold off
end
try, hold on
plot(t,pX{i},'linewidth',2,'color',[1 1 1]/2)
end, hold off
% title and grid
%--------------------------------------------------------------
title('hidden states')
xlabel('time {bins}')
grid on
axis square
axis(a);
catch
delete(gca)
end
end
end
drawnow