-
Notifications
You must be signed in to change notification settings - Fork 8
/
M_obs.m
418 lines (349 loc) · 12.6 KB
/
M_obs.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
function outpars = M_obs(pars,Ms,P0,sum_CP,sum_MP,sum_Ms2,...
sum_Mxy,sum_P,sum_Pb,sum_yy,x0,control,equal,fixed,scale,skip)
abstol = control.abstol;
reltol = control.reltol;
verbose = control.verbose;
outpars = pars;
[N,r,M] = size(pars.C);
p = size(sum_Pb,1) / size(sum_P,1);
T = size(Ms,2);
% Mask of rxr diagonal blocks in a (p*r)x(p*r) matrix
% (used to update Sigma in M-step)
Sigmamask = reshape(find(kron(eye(p),ones(r))),r,r,p);
% Unconstrained estimates
% A = ( sum(t=p+1:T) P(t,t-1|T) ) * ( sum(t=p+1:T) P~(t-1|T) )^{-1}
% Cj = (sum(t=1:T) Wj(t) y(t) xj(t)') * (sum(t=1:T) Wj(t) Pj(t|T))^{-1}
% Qj = (sum(t=2:T) Wj(t) Pj(t) - Aj * sum(t=2:T) Wj(t) Pj(t-1,t)') / sum(t=2:T) Wj(t)
% R = sum(t=1:T) y(t) y(t)' / T - sum(t=1:T) x(t|T) y(t)'
%
% where Wj(t) = P(S(t)=j|y(1:T)),
% xj(t|T) = E(x(t)|S(t)=j,y(1:T)),
% x(t|T) = E(x(t)|y(1:T)),
% Pj(t|T) = E(x(t)x(t)'|S(t)=j,y(1:T)),
% P~(t-1|T) = E(x(t-1)x(t-1)'|S(t)=j,y(1:T))
% and P(t,t-1|T) = E(x(t)x(t-1)'|y(1:T))
% Scale constraints for C are handled with a
% projected gradient technique (maximize Q-function under constraint)
%=========================================================================%
% Update A %
%=========================================================================%
% Case: no fixed coefficient constraints
if isempty(fixed.A)
if equal.A && equal.Q
sum_Pb_all = sum(sum_Pb,3);
sum_CP_all = sum(sum_CP,3);
Ahat = sum_CP_all / sum_Pb_all;
if any(isnan(Ahat(:))|isinf(Ahat(:)))
Ahat = sum_CP_all * pinv(sum_Pb_all);
end
Ahat = repmat(Ahat,[1,1,M]);
elseif equal.A
lhs = zeros(p*r*r);
rhs = zeros(r,p*r);
for j = 1:M
Qinv_j = myinv(pars.Q(:,:,j));
lhs = lhs + kron(sum_Pb(:,:,j),Qinv_j);
rhs = rhs + Qinv_j * sum_CP(:,:,j);
end
Ahat = lhs\rhs(:);
if any(isnan(Ahat))|| any(isinf(Ahat))
Ahat = pinv(lhs) * rhs(:);
end
Ahat = repmat(reshape(Ahat,r,p*r),1,1,M);
else
Ahat = zeros(r,p*r,M);
for j = 1:M
A_j = sum_CP(:,:,j) / sum_Pb(:,:,j);
if any(isnan(A_j(:)) | isinf(A_j(:)))
A_j = sum_CP(:,:,j) * pinv(sum_Pb(:,:,j));
end
Ahat(:,:,j) = A_j;
end
end
end
% Case: fixed coefficient constraints on A --> Vectorize matrices and
% solve associated problem after discarding rows associated with fixed
% coefficients. Recall: there cannot be both fixed coefficient
% constraints *and* equality constraints on A
if ~skip.A && ~isempty(fixed.A)
for j = 1:M
% Linear indices of free coefficients in A(j)
idx = (fixed.A(:,1) > (j-1)*p*r^2) & (fixed.A(:,1) <= j*p*r^2);
fixed_Aj = fixed.A(idx,:);
fixed_Aj(:,1) = fixed_Aj(:,1) - (j-1)*p*r^2;
free = setdiff(1:p*r^2,fixed_Aj(:,1));
free = reshape(free,[],1);
Qinv_j = myinv(pars.Q(:,:,j));
% Matrix problem min(X) trace(W(-2*B1*X' + X*B2*X'))
% (under fixed coefficient constraints) becomes vector problem
% min(x) x' kron(B2,W) x - 2 x' vec(W*B1)
% with X = A(j), x = vec(A(j)), W = Q(j)^(-1), B1 = sum_CP(j),
% and B2 = sum_Pb(j) (remove fixed entries in x)
mat = kron(sum_Pb(:,:,j),Qinv_j);
vec = reshape(Qinv_j * sum_CP(:,:,j),p*r^2,1);
A_j = zeros(p*r^2,1);
A_j(fixed_Aj(:,1)) = fixed_Aj(:,2);
A_j(free) = mat(free,free)\vec(free);
if any(isnan(A_j)|isinf(A_j))
A_j(free) = pinv(mat(free,free)) * vec(free);
end
Ahat(:,:,j) = reshape(A_j,r,p*r);
end
end
% Check eigenvalues of estimate and regularize if less than 'scale.A'.
% Regularization: algebraic method if no fixed coefficients or all
% fixed coefficients are zero, projected gradient otherwise
if ~skip.A
Abig = diag(ones((p-1)*r,1),-r);
for j = 1:M
% Check eigenvalues
Abig(1:r,:) = Ahat(:,:,j);
eigval = eig(Abig);
if any(abs(eigval) > scale.A)
if verbose
warning(['Eigenvalues of A%d greater than %f.',...
' Regularizing.'],j,scale.A)
end
c = .999 * scale.A / max(abs(eigval));
A_j = reshape(Ahat(:,:,j),[r,r,p]);
for l = 1:p
A_j(:,:,l) = c^l * A_j(:,:,l);
end
Ahat(:,:,j) = reshape(A_j,[r,p*r]);
end
if equal.A
Ahat = repmat(Ahat(:,:,1),[1,1,M]);
break
end
end
% Check that parameter update actually increases Q-function
% If not, keep previous parameter estimate
Qval_old = Q_obs(outpars,Ms,P0,sum_CP,sum_MP,sum_Ms2,sum_Mxy,...
sum_P,sum_Pb,sum_yy,x0);
outpars.A = Ahat;
Qval = Q_obs(outpars,Ms,P0,sum_CP,sum_MP,sum_Ms2,sum_Mxy,...
sum_P,sum_Pb,sum_yy,x0);
if Qval < Qval_old
outpars.A = pars.A;
end
end
if skip.C
Chat = pars.C;
elseif isempty(fixed.C) && isempty(scale.C)
if equal.C
sum_yx = sum(sum_Mxy,3).';
Chat = sum_yx / sum_P;
if any(isnan(Chat(:))|isinf(Chat(:)))
Chat = sum_yx * pinv(sum_P);
end
Chat = repmat(Chat,1,1,M);
else
Chat = zeros(N,r,M);
for j=1:M
C_j = (sum_Mxy(:,:,j).')/sum_MP(:,:,j);
if any(isnan(C_j(:)))|| any(isinf(C_j(:)))
C_j = (sum_Mxy(:,:,j).') * pinv(sum_MP(:,:,j));
end
Chat(:,:,j) = C_j;
end
end
else
if equal.C
sum_xy = sum(sum_Mxy,3);
if ~isempty(fixed.C)
idx = fixed.C(:,1) <= N*r;
fixed.C = fixed.C(idx,:);
end
Chat = PG_C(pars.C(:,:,1),sum_xy,sum_P,pars.R,scale.C,fixed.C);
Chat = repmat(Chat,1,1,M);
else
Chat = zeros(N,r,M);
for j=1:M
C_j = pars.C(:,:,j);
sum_Mxyj = sum_Mxy(:,:,j);
sum_MPj = sum_MP(:,:,j);
fixed_Cj = [];
if ~isempty(fixed.C)
idx = (fixed.C(:,1) > (j-1)*N*r) & (fixed.C(:,1) <= j*N*r);
fixed_Cj = fixed.C(idx,:);
end
Chat(:,:,j) = PG_C(C_j,R,sum_yy,sum_Mxyj,sum_MPj,scale.C,fixed_Cj);
end
end
% Check that parameter update actually increases Q-function
% If not, keep previous parameter estimate
Qval_old = Q_obs(outpars,Ms,P0,sum_CP,sum_MP,sum_Ms2,sum_Mxy,...
sum_P,sum_Pb,sum_yy,x0);
outpars.C = Chat;
Qval = Q_obs(outpars,Ms,P0,sum_CP,sum_MP,sum_Ms2,sum_Mxy,...
sum_P,sum_Pb,sum_yy,x0);
if Qval < Qval_old
outpars.C = pars.C;
end
end
%=========================================================================%
% Update Q %
%=========================================================================%
% Unconstrained solution
if ~skip.Q
Qhat = zeros(r,r,M);
for j=1:M
A_j = Ahat(1:r,:,j);
sum_Pj = sum_P(:,:,j);
sum_CPj = sum_CP(:,:,j);
sum_Pbj = sum_Pb(:,:,j);
Q_j = (sum_Pj - (sum_CPj * A_j.') - ...
(A_j * sum_CPj.') + A_j * sum_Pbj * A_j.') / (T-1);
Qhat(:,:,j) = 0.5 * (Q_j + Q_j.');
end
% Apply equality constraints
if equal.Q
Qhat = repmat(mean(Qhat,3),1,1,M);
end
% Enforce fixed coefficient constraints
if ~isempty(fixed.Q)
Qhat(fixed.Q(:,1)) = fixed.Q(:,2);
end
% Regularize estimate if needed
for j = 1:M
eigval = eig(Qhat(:,:,j));
if min(eigval) < max(abstol,max(eigval)*reltol)
if verbose
warning(['Q%d ill-conditioned and/or nearly singular.', ...
' Regularizing.'],j);
end
Qhat(:,:,j) = regfun(Qhat(:,:,j),abstol,reltol);
end
if equal.Q
Qhat = repmat(Qhat(:,:,1),[1,1,M]);
break
end
end
% Apply fixed coefficient constraints
if ~isempty(fixed.Q)
Qhat(fixed.Q(:,1)) = fixed.Q(:,2);
end
% Check that estimate Qhat increases Q-function. If not, keep
% estimate from previous iteration
Qval_old = Q_obs(outpars,Ms,P0,sum_CP,sum_MP,sum_Ms2,sum_Mxy,...
sum_P,sum_Pb,sum_yy,x0);
outpars.Q = Qhat;
Qval = Q_obs(outpars,Ms,P0,sum_CP,sum_MP,sum_Ms2,sum_Mxy,...
sum_P,sum_Pb,sum_yy,x0);
if Qval < Qval_old
outpars.Q = pars.Q;
end
end
%=========================================================================%
% Update R %
%=========================================================================%
if ~skip.R
Rhat = sum_yy;
for j=1:M
C_j = Chat(:,:,j);
sum_MPj = sum_MP(:,:,j);
sum_Mxyj = sum_Mxy(:,:,j);
Rhat = Rhat - (C_j*sum_Mxyj) - (C_j*sum_Mxyj)' + (C_j*sum_MPj*C_j');
end
Rhat = Rhat / T;
Rhat = 0.5 * (Rhat+Rhat');
Rhat(fixed.R(:,1)) = fixed.R(:,2);
% Regularize R if needed
eigval = eig(Rhat);
if min(eigval) < max(abstol,max(eigval)*reltol)
if verbose
warning('R ill-conditioned and/or nearly singular. Regularizing.');
end
Rhat = regfun(Rhat,abstol,reltol);
Rhat(fixed.R(:,1)) = fixed.R(:,2);
end
% Make sure that parameter update increases Q-function
% If not, do not update parameter estimate
Qval_old = Q_obs(outpars,Ms,P0,sum_CP,sum_MP,sum_Ms2,sum_Mxy,...
sum_P,sum_Pb,sum_yy,x0);
outpars.R = Rhat;
Qval = Q_obs(outpars,Ms,P0,sum_CP,sum_MP,sum_Ms2,sum_Mxy,...
sum_P,sum_Pb,sum_yy,x0);
if Qval < Qval_old
outpars.R = pars.R;
end
end
%=========================================================================%
% Update mu %
%=========================================================================%
if skip.mu
muhat = mu;
else
muhat = reshape(x0,r,p,M); % unconstrained solution
muhat = reshape(mean(muhat,2),r,M);
% Assume E(x(1,j))=E(x(0,j))=...=E(x(1-p+1,j) for j=1:M
if equal.mu
muhat = repmat(mean(muhat,2),1,M);
end
end
if ~isempty(fixed.mu)
muhat(fixed.mu(:,1)) = fixed.mu(:,2);
Qval_old = Q_obs(outpars,Ms,P0,sum_CP,sum_MP,sum_Ms2,sum_Mxy,...
sum_P,sum_Pb,sum_yy,x0);
outpars.mu = muhat;
Qval = Q_obs(outpars,Ms,P0,sum_CP,sum_MP,sum_Ms2,sum_Mxy,...
sum_P,sum_Pb,sum_yy,x0);
if Qval < Qval_old
outpars.mu = pars.mu;
end
end
%=========================================================================%
% Update Sigma %
%=========================================================================%
if ~skip.Sigma
Sigmahat = zeros(r,r,M);
for j = 1:M
mu_j = repmat(muhat(:,j),p,1); % replicate mu(j) to size pr x 1
B_j = P0(:,:,j) - x0(:,j) * mu_j.' - ...
mu_j * x0(:,j).' + (mu_j * mu_j.');
S_j = mean(B_j(Sigmamask),3);
Sigmahat(:,:,j) = 0.5 * (S_j+S_j.');
end
if equal.Sigma
Sigmahat = repmat(mean(Sigmahat,3),1,1,M);
end
Sigmahat(fixed.Sigma(:,1)) = fixed.Sigma(:,2);
% Enforce semi-positive definiteness if needed
for j = 1:M
S_j = Sigmahat(1:r,1:r,j);
eigval = eig(S_j);
if ~all(eigval >= 0)
if verbose
warning('Sigma%d non semi-positive definite. Regularizing.',j);
end
Sigmahat(:,:,j) = regfun(S_j,0,0);
end
end
Sigmahat(fixed.Sigma(:,1)) = fixed.Sigma(:,2);
% Make sure that parameter update increases Q-function
% If not, do not update parameter estimate
Qval_old = Q_obs(outpars,Ms,P0,sum_CP,sum_MP,sum_Ms2,sum_Mxy,...
sum_P,sum_Pb,sum_yy,x0);
outpars.Sigma = Sigmahat;
Qval = Q_obs(outpars,Ms,P0,sum_CP,sum_MP,sum_Ms2,sum_Mxy,...
sum_P,sum_Pb,sum_yy,x0);
if Qval < Qval_old
outpars.Sigma = pars.Sigma;
end
end
%=========================================================================%
% Update Pi %
%=========================================================================%
if ~skip.Pi
outpars.Pi = Ms(:,1);
end
%=========================================================================%
% Update Z %
%=========================================================================%
if ~skip.Z
Zhat = sum_Ms2 ./ repmat(sum(sum_Ms2,2),1,M);
if ~isempty(fixed.Z)
Zhat(fixed.Z(:,1)) = fixed.Z(:,2);
end
outpars.Z = Zhat;
end