-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.m
148 lines (111 loc) · 3.84 KB
/
main.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
%% PSMF Convergence Experiment - Main script
%
% This file is part of the PSMF codebase.
% See the LICENSE file for copyright and licensing information.
%
clc;
clear;
close all;
load data.mat
rng(5),
% Standard Kalman
X0k = 5 * randn(r,1);
P0k = 5 * eye(r);
Ck = Ctrue;
t = 1;
Ppk = P0k + Q;
Sk = eye(d) / (R + Ck * Ppk * Ck');
Xk(:,t) = X0k + Ppk * Ck' * Sk * (Y(:,t) - Ck * X0k);
Pk(:,:,t) = Ppk - Ppk * Ck' * Sk * Ck * Ppk;
for t = 2:n
Ppk = Pk(:,:,t-1) + Q;
Sk = eye(d) / (R + Ck * Ppk * Ck');
Xk(:,t) = Xk(:,t-1) + Ppk * Ck' * Sk * (Y(:,t) - Ck * Xk(:,t-1));
Pk(:,:,t) = Ppk - Ppk * Ck' * Sk * Ck * Ppk;
end
% psmf filter
%
X0ps = 5 * randn(r,1);
P0ps = 5 * P0k;
C = 10 * abs(randn(d,r));
% C = CrootInit * CrootInit';
v = 1;
V = v * eye(r);
R = 1 * R;
Q = 1 * Q;
Cerr = zeros(1,n);
for iter = 1:10000
Cerrold = Cerr;
t = 1;
if iter > 1
X0ps = Xps(:,1);
P0ps = Pps(:,1);
V = v * eye(r);
end
Ppps = P0ps + Q;
Xpps = X0ps;
eta_k = trace(R + C * Ppps * C')/d;
Rb = R + eye(d) .* (Xpps' * V * Xpps);
Skps = eye(d) / (Rb + C * Ppps * C');
Xps(:,t) = Xpps + Ppps * C' * Skps * (Y(:,t) - C * Xpps);
Pps(:,:,t) = Ppps - Ppps * C' * Skps * C * Ppps;
C = C + ((Y(:,t) - C * Xpps) * Xpps' * V)/(Xpps' * V * Xpps + eta_k);
V = V - (V * (Xpps * Xpps') * V)/(Xpps' * V * Xpps + eta_k);
Cerr(t) = norm(C - Ctrue);
for t = 2:n
Ppps = Pps(:,:,t-1) + Q;
Xpps = Xps(:,t-1);
% V = V + eye(r) * 1e-6;
eta_k = trace(R + C * Ppps * C')/d;
Rb = R + eye(d) .* (Xpps' * V * Xpps);
Skps = eye(d) / (Rb + C * Ppps * C');
Xps(:,t) = Xpps + Ppps * C' * Skps * (Y(:,t) - C * Xpps);
Pps(:,:,t) = Ppps - Ppps * C' * Skps * C * Ppps;
C = C + ((Y(:,t) - C * Xpps) * Xpps' * V)/(Xpps' * V * Xpps + eta_k);
V = V - (V * (Xpps * Xpps') * V)/(Xpps' * V * Xpps + eta_k);
Cerr(t) = norm(C - Ctrue);
W(t) = Wasserstein2(Xps(:,t),Xk(:,t),Pps(:,:,t),Pk(:,:,t));
if mod(t,2500) == 0 && iter == 1
figure(1),
clf,
subplot(221),loglog(W(1:t)),
subplot(222),loglog(Cerr(1:t));
subplot(2,2,3); plot(X(:,1:t)','b');hold on; plot(Xk(:,1:t)','k'); hold on; plot(Xps(:,1:t)','r');
% subplot(2,2,4),plot(ErM);
drawnow,
end
end
ErM(iter) = norm(Y - C * Xps);
avW(iter) = mean(W);
CerrI(iter) = norm(C - Ctrue);
if mod(iter,100) == 0
figure(1),
clf,
subplot(221),loglog(avW,'LineWidth',2,'Color','black'),
xlabel({'Iterations','(a)'});ylabel('Averaged Wasserstein distance','FontSize',12);
subplot(222),loglog(CerrI,'LineWidth',2,'Color','black');
xlabel({'Iterations','(b)'});ylabel('$\|C_k - C^\star\|$','Interpreter','latex','FontSize',14);
subplot(2,2,[3,4]);
plot(Xk','k'); hold on; plot(Xps','r');
legend('Optimal filter mean estimate','Approximate filter mean estimate','FontSize',14);
xlabel({'Time Index','(c)'},'FontSize',14);
drawnow,
% display(norm(Cerrold-Cerr))
end
end
save expdata.mat
%%
figure(1),
clf,
subplot(131),loglog(avW,'LineWidth',2,'Color','black'),
xlabel({'Iterations','(a)'},'FontSize',14);
ylabel('Averaged Wasserstein distance','FontSize',11);
subplot(132),loglog(CerrI,'LineWidth',2,'Color','black');
xlabel({'Iterations','(b)'},'FontSize',14);
ylabel('$\|C_k - C^\star\|$','Interpreter','latex','FontSize',14);
subplot(133);
plot(Xk','k'); hold on; plot(Xps','r');
legend('Optimal filter','Approximate filter (PSMF)','FontSize',14);
xlabel({'Time Index','(c)'},'FontSize',14);
drawnow,
print(figure(1),'-depsc','Fig1');