-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathARemsub.m
More file actions
79 lines (63 loc) · 2 KB
/
Copy pathARemsub.m
File metadata and controls
79 lines (63 loc) · 2 KB
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
%----------------------------------------------------
%
% EM for AR(p) with missing data
%
%----------------------------------------------------
%
% Parameter inference in AR processes with missing data
%
% Authors: Johan Dahlin, Fredrik Lindsten,
% Thomas B. Schön
%
% Presented at ERNSI workshop.
% Maastricht, NL, 2012
%
%----------------------------------------------------
function output = ARemsub(sys,data,svar,opt)
thetahat(1,:)=opt.initialtheta;
if svar.outliers==1; data.tmissing=data.toutlying; end
ys=data.ye;
for kk=1:opt.miter
% E-step
% Estimate missing data using maxmization of the conditional likelihood
if ((svar.rate>0) && ((svar.missingdata==1)||(svar.outliers==1)))
term1=1+sum(thetahat(kk,:).^2);
for i=sort(data.tmissing)
term2=0;
for k=1:sys.n
tmp=thetahat(kk,k)';
for j=1+k:sys.n; tmp=tmp-thetahat(kk,j)'*thetahat(kk,j-k)'; end
term2=term2+tmp*(ys(i+k)+ys(i-k));
end
ys(i)=term2/term1;
end
end
yy(kk,:)=ys;
ll(kk)=sum(data.ye'-BuildPhi(ys,sys.n)*thetahat(kk,:)').^2;
% M-step
% Estimate theta using LS
thetahat(kk+1,:)=BuildPhi(ys,sys.n)\ys';
if ~(kk==1)
output.breakreason='maxiter';
if abs(ll(kk)-ll(kk-1)) < opt.minlldiff
output.breakreason='lldiff';
break
end
if norm(thetahat(kk+1,:)-thetahat(kk,:)) < opt.coefdiff
output.breakreason='coefdiff';
break
end
end
end
% Model testing
output.initialtheta=opt.initialtheta;
output.ll=ll;
output.thetahatEM=thetahat(kk+1,:);
output.yhatEM=BuildPhi(data.yt,data.ye,sys.n)*output.thetahatEM';
output.mfEM=1-norm(data.yt'-output.yhatEM)/norm(data.yt'-mean(data.yt'));
output.mseEM=mean((-output.thetahatEM-sys.a).^2);
output.y=ys;
end
%-----------------------------------------------
% End of File
%-----------------------------------------------