-
Notifications
You must be signed in to change notification settings - Fork 5
/
NORST_random.m
180 lines (148 loc) · 6.01 KB
/
NORST_random.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
function [L_hat, P_hat, S_hat, t_hat, ...
P_track_full, T_calc]= NORST_random(M, T, r, ev_thresh, alpha, K,R,hop)
%Algorithm that implments the NORST random function for subspace tracking
%with missing data or dynamic matrix completion
%%% Inputs %%%
%%% M - measurement matrix %%%
%%% ev_thres - threshold for subspace change detection %%%
%%% P_init - an initial estimate of the subspace %%%
%%% t_train - the dimension of the training data %%%
%%% Algorithm parameters %%%
%%% alpha - frame length %%%
%%% K - number of projection PCA steps %%%
%%% omega - threshold for non-zero value in S %%%
%%% K_CS - number of CoSaMP iterations %%%
%%% outc - an upper bound on estimate of fraction of outliers per column
%%% Outputs %%%
%%% L_hat - estimate of the low rank matrix %%%
%%% P_hat - estimate of the subspace in which L lies %%%
%%% S_hat - estimate of the sparse signal %%%
%%% t_hat - estimate of subspace change times %%%
%% Initializations
%thresh = ev_thresh / 2;
%[~, r_init] = size(P_init);
[n, t_max] = size(M);
P_init = orth(randn(n, r));
P_hat = P_init;
%T_hat = zeros(n, t_max);
S_hat = zeros(n, t_max);
S_hat_buffer = zeros(n, t_max);
L_hat = zeros(n, t_max);
L_hat_buffer = zeros(n, t_max);
t_hat = [];
k = 0;
cnt = 1;
P_track_full{cnt} = P_hat;
T_calc(cnt) = 1;
ph = 1; %ph - 0 => detect, 1 => pca
nextbatch_start = 0;
alpha0 = alpha;
permit = 0;
%% Main Algorithm Loop
for ii = 1 : t_max
%% Estimate support
Atf.times = @(x) x - (P_hat * (P_hat' * x));
Atf.trans = @(y) y - (P_hat * (P_hat' * y));
phi.times = @(x) x - (P_hat * (P_hat' * x));
y_t = Atf.times(M(:, ii));
% opts.tol = 1e-4;
% opts.print = 0;
%
% opts.delta = omega * 2 / 15;
%
% x_t_hat_cs = yall1(Atf, y_t, opts);
% omega = sqrt(M(:, ii)' * M(:, ii) / n);
%
% t_hat_temp = find(abs(x_t_hat_cs) > omega);
%T_hat(t_hat_temp, ii) = 255;
%LS.times = @(x) phi(:, t_hat_temp) * x;
%LS.trans = @(y) phi(:, t_hat_temp)' * x;
%y_t = M(:, ii) - (P_hat * (P_hat' * M(:, ii)));
%DecayRate = 0.9; %other values work, may make it slower
%x_t_hat_cs = cosamp_cgls(phi_t, ...
% y_t, outc, DecayRate, K_CS, 1e-6);
%t_hat_temp = find(abs(x_t_hat_cs) > omega);
% T_hat(t_hat_temp, ii) = 1;
%% Estimate signal components
% % [S_hat(t_hat_temp, ii), ~] = ...
% % lsqr(phi_t(:, t_hat_temp), y_t, 1e-6, 50);
% S_hat(t_hat_temp, ii) = phi_t(:, t_hat_temp) \ y_t;
if (ii == 1)
x0 = zeros(n,1);
else
% x0 = S_hat(:,ii-1);
end
T_union = find(T(:, ii) == 0);
tol = 1e-16;
S_hat(T_union, ii) = ccgls(@Phifun, y_t, T_union, P_hat, ...
0, tol, 20);
L_hat(:, ii) = M(:, ii) - S_hat(:, ii);
%% Subspace update
% if(~mod(ii + 1 , alpha))
if(ii == alpha0)
permit = 1;
end
if (ii == nextbatch_start + alpha)
permit = 1;
end
if(permit == 1)
idx = nextbatch_start + 1 : nextbatch_start + alpha;
nextbatch_start = idx(hop);
if(idx(end) > t_max)
idx = nextbatch_start : t_max;
end
L_temp = L_hat(:, idx);
%MM = L_temp - (P_hat_old *(P_hat_old' * L_temp));
MM = phi.times(L_temp);
if(~ph) %%detect phase
% phi_t = eye(n) - P_hat * P_hat';
if(svds(MM, 1) >= sqrt(alpha * ev_thresh))
ph = 1;
t_hat = [t_hat, ii];
k = 0;
end
else %%update phase
%P_hat = simpleEVD(L_hat(:, idx), r);
P_hat = simpleEVD_fed(L_hat(:, idx), r, 500, 1, 1e-6);
if( ii==idx(end) && R > 0)
% fprintf("fine tuning the subspace at %d\n",ii);
idx_buffer = idx;
% ctr = [ctr, ii];
% idx_buffer = ctr(end - 1) + 1 : ctr(end);
% idx_buffer = t_hat(end) : ii;
for reuse = 1:R
for kk = idx_buffer
Atf.times = @(x) x - (P_hat * (P_hat' * x));
Atf.trans = @(y) y - (P_hat * (P_hat' * y));
y_t_buffer = Atf.times(M(:, kk));
% x_t_hat_cs_buffer = yall1(Atf, y_t_buffer, opts);
% x_cs_hat_buffer(:,kk) = x_t_hat_cs_buffer;
% omega = 1 * sqrt(M(:, kk)' * M(:, kk) / n);
% t_hat_temp_buffer = find(abs(x_t_hat_cs_buffer) > omega);
% T_hat_buffer(t_hat_temp_buffer, kk) = 255;
% T_union_buffer = t_hat_temp_buffer;
T_union_buffer = find(T(:,kk) == 0);
S_hat_buffer(T_union_buffer, kk) = ccgls(@Phifun, y_t_buffer, ...
T_union_buffer, P_hat, ...
0, tol, 20);
L_hat_buffer(:, kk) = M(:, kk) - S_hat_buffer(:, kk);
end
% permuted_idx = randperm(buffer) + nextbatch_start - alpha;
P_hat = simpleEVD_fed(L_hat_buffer(:, idx_buffer), r, 500, 1, 1e-6);
end
end
% phi_t = speye(n) - P_hat * P_hat';
k = k + 1;
if k == K
ph = 0;
end
end
%% Return subspace
% if((ii == 1) || ~(mod(ii + 1, alpha)))
cnt = cnt + 1;
P_track_full{cnt} = P_hat;
T_calc(cnt) = ii;
permit = 0;
end
end
end