-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo_ATD_time_varying_factor.m
221 lines (171 loc) · 7.3 KB
/
demo_ATD_time_varying_factor.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
%% This demo illustrates the tracking ability of ATD in time-varying environments
clear;clc; close all;
run_path;
n_exp = 3; % Number of independent runs
tensor_dim = [20 20 20 500]; % Dimension of streaming tensors
tucker_rank = [3 3 3 3]; % Tucker rank
fac_noise = 1e-3; % Noise level
sampling = 0.9; % 1 - Sampling = Missing density
MAG = [1e-4 1e-3 1e-2 1e-1]; % Time-varying factor
N = length(tensor_dim);
T = tensor_dim(end);
%% Main Program
PER_U = cell(1,length(MAG));
PER_X = cell(1,length(MAG));
for jj = 1 : length(MAG)
PER_U{1,jj} = zeros(N-1,T);
PER_X{1,jj} = zeros(1,T);
end
for jj = 1 : length(MAG)
mag = MAG(jj);
epsilon = mag*ones(1,tensor_dim(end));
epsilon(300) = 1; % Create an abrupt change at t = 300';
fprintf('\n \n // Time-varying factor = %f \n',mag)
PER_U_n = zeros(N-1,T);
PER_X_n = zeros(1,T);
for n = 1 : n_exp
fprintf('\n + Run [%d/%d]: ',n,n_exp);
%% Generate Tensor
[Xtrue_cell,Factor_True] = tucker_generate_tensor(tensor_dim,tucker_rank,0,epsilon);
OPTS.Factor_True = Factor_True;
OPTS.Slide_True = Xtrue_cell;
% Missing + Gaussian Noise
X_cell = cell(tensor_dim(end),1);
Omega = cell(tensor_dim(end),1);
for t = 1 : tensor_dim(end)
Omega_t = rand(tensor_dim(1:end-1));
Omega_t = 1 .* (Omega_t <= sampling);
Omega{t,1} = Omega_t;
X_cell{t,1} = Xtrue_cell{t,1} + fac_noise*randn(tensor_dim(1:end-1));
X_cell{t,1} = Omega_t .* X_cell{t,1};
end
%% Algorithm
t_start = tic;
[~,PER] = ATD(X_cell,Omega,tucker_rank,OPTS);
toc(t_start);
PER_U_n = PER_U_n + PER(1:N-1,1:end);
PER_X_n = PER_X_n + PER(N,1:end);
end
PER_U{1,jj} = PER_U_n/n_exp;
PER_X{1,jj} = PER_X_n/n_exp;
end
%% PLOT RESULTS
PER1 = PER_U{1,1};
PER2 = PER_U{1,2};
PER3 = PER_U{1,3};
PER4 = PER_U{1,4};
makerSize = 14;
numbMarkers = 50;
LineWidth = 2;
set(0, 'defaultTextInterpreter', 'latex');
color = get(groot,'DefaultAxesColorOrder');
red_o = [1,0,0];
blue_o = [0, 0, 1];
gree_o = [0, 0.5, 0];
black_o = [0.25, 0.25, 0.25];
blue_n = color(1,:);
oran_n = color(2,:);
yell_n = color(3,:);
viol_n = color(4,:);
gree_n = color(5,:);
lblu_n = color(6,:);
brow_n = color(7,:);
lbrow_n = [0.5350 0.580 0.2840];
%%
fig = figure;
subplot(212);
k = 1;
K = tensor_dim(end);
hold on;
d1 = semilogy(1:k:K,PER1(end,1:k:end),...
'linestyle','-','color',red_o,'LineWidth',LineWidth);
d11 = plot(1:100:K,PER1(end,1:100:end),...
'marker','h','markersize',makerSize,...
'linestyle','none','color',red_o,'LineWidth',LineWidth);
d12 = semilogy(1:1,PER1(end,1),...
'marker','h','markersize',makerSize,...
'linestyle','-','color',red_o,'LineWidth',LineWidth);
d2 = semilogy(1:k:K,PER2(end,1:k:end),...
'linestyle','-','color',blue_o,'LineWidth',LineWidth);
d21 = plot(1:100:K,PER2(end,1:100:end),...
'marker','o','markersize',makerSize,...
'linestyle','none','color',blue_o,'LineWidth',LineWidth);
d22 = semilogy(1:1,PER2(end,1),...
'marker','o','markersize',makerSize,...
'linestyle','-','color',blue_o,'LineWidth',LineWidth);
d3 = semilogy(1:k:K,PER3(end,1:k:end),...
'linestyle','-','color','g','LineWidth',LineWidth);
% 'marker','d','markersize',makerSize,'markerfacecolor','w',...
d31 = plot(1:100:K,PER3(end,1:100:end),...
'marker','s','markersize',makerSize,...
'linestyle','none','color','g','LineWidth',LineWidth);
d32 = semilogy(1:1,PER3(end,1:1),...
'marker','s','markersize',makerSize,...
'linestyle','-','color','g','LineWidth',LineWidth);
d4 = semilogy(1:k:K,PER4(end,1:k:end),...
'linestyle','-','color',gree_o,'LineWidth',LineWidth);
d41 = plot(1:100:K,PER4(end,1:100:end),...
'marker','d','markersize',makerSize,...
'linestyle','none','color',gree_o,'LineWidth',LineWidth);
d42 = semilogy(1:1,PER4(end,1),...
'marker','d','markersize',makerSize,...
'linestyle','-','color',gree_o,'LineWidth',LineWidth);
ylabel('SEP $(\mathbf{U}_{tr}, \mathbf{U}_{es})$','interpreter','latex','FontSize',13,'FontName','Times New Roman');
xlabel('Time Index - $t$','interpreter','latex','FontSize',13,'FontName','Times New Roman');
set(gca, 'YScale', 'log');
set(gca,'FontSize',16,'XGrid','on','YGrid','on','GridLineStyle','-','MinorGridLineStyle','-','FontName','Times New Roman');
set(gca,'Xtick',0:100:K,'FontSize',16,'XGrid','on','YGrid','on','GridLineStyle',':','MinorGridLineStyle','none',...
'FontName','Times New Roman');
set(gca,'FontSize', 24);
grid on;
box on;
set(fig, 'units', 'inches', 'position', [0.5 0.5 8 7]);
subplot(211);
hold on;
d1 = semilogy(1:k:K,PER_X{1,1}(end,1:k:end),...
'linestyle','-','color',red_o,'LineWidth',LineWidth);
d11 = plot(1:100:K,PER_X{1,1}(end,1:100:end),...
'marker','h','markersize',makerSize,...
'linestyle','none','color',red_o,'LineWidth',LineWidth);
d12 = semilogy(1:1,PER_X{1,1}(end,1),...
'marker','h','markersize',makerSize,...
'linestyle','-','color',red_o,'LineWidth',LineWidth);
d2 = semilogy(1:k:K,PER_X{1,2}(end,1:k:end),...
'linestyle','-','color',blue_o,'LineWidth',LineWidth);
d21 = plot(1:100:K,PER_X{1,2}(end,1:100:end),...
'marker','o','markersize',makerSize,...
'linestyle','none','color',blue_o,'LineWidth',LineWidth);
d22 = semilogy(1:1,PER_X{1,2}(end,1),...
'marker','o','markersize',makerSize,...
'linestyle','-','color',blue_o,'LineWidth',LineWidth);
d3 = semilogy(1:k:K,PER_X{1,3}(end,1:k:end),...
'linestyle','-','color','g','LineWidth',LineWidth);
% 'marker','d','markersize',makerSize,'markerfacecolor','w',...
d31 = plot(1:100:K,PER_X{1,3}(end,1:100:end),...
'marker','s','markersize',makerSize,...
'linestyle','none','color','g','LineWidth',LineWidth);
d32 = semilogy(1:1,PER_X{1,3}(end,1:1),...
'marker','s','markersize',makerSize,...
'linestyle','-','color','g','LineWidth',LineWidth);
d4 = semilogy(1:k:K,PER_X{1,4}(end,1:k:end),...
'linestyle','-','color',gree_o,'LineWidth',LineWidth);
d41 = plot(1:100:K,PER_X{1,4}(end,1:100:end),...
'marker','d','markersize',makerSize,...
'linestyle','none','color',gree_o,'LineWidth',LineWidth);
d42 = semilogy(1:1,PER_X{1,4}(end,1),...
'marker','d','markersize',makerSize,...
'linestyle','-','color',gree_o,'LineWidth',LineWidth);
lgd = legend([d12 d22 d32 d42],'$\epsilon = 10^{-4}$','$\epsilon = 10^{-3}$','$\epsilon = 10^{-2}$', ...
'$\epsilon = 10^{-1}$');
lgd.FontSize = 22;
set(lgd, 'Interpreter', 'latex', 'Color', [0.95, 0.95, 0.95]);
ylabel('RE $(\mathcal{X}_{tr}, \mathcal{X}_{es})$','interpreter','latex','FontSize',13,'FontName','Times New Roman');
h=gca;
set(gca, 'YScale', 'log');
set(h,'FontSize',16,'XGrid','on','YGrid','on','GridLineStyle','-','MinorGridLineStyle','-','FontName','Times New Roman');
set(h,'Xtick',0:100:K,'FontSize',16,'XGrid','on','YGrid','on','GridLineStyle',':','MinorGridLineStyle','none',...
'FontName','Times New Roman');
set(h,'FontSize', 24);
%axis([0 K 5e-4 1e0]);
grid on;
box on;