-
Notifications
You must be signed in to change notification settings - Fork 3
/
maxent_pca.m
395 lines (340 loc) · 11.7 KB
/
maxent_pca.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
%% MAXENT_PCA Compute the principle component analysis of the
% Modulated Gravity Model (MGM) and the Non-stationary Conditionally
% Independent Model (NCIM).
%
% file: maxent_pca.m, (c) Paul Tune, Mon Dec 22 2014
% created: Mon Dec 22 2014
% author: Paul Tune
% email: paul.tune@adelaide.edu.au
clear;
% load Abilene traffic matrices; we will extract the Fourier coefficients
% from these series of traffic matrices
load tm_real
% turn into 3D array and compute row and column marginals
tm_tensor = reshape(tm_real,N,N,n_TMs);
r = squeeze(sum(tm_tensor,2)); % row marginals
c = squeeze(sum(tm_tensor,1)); % column marginals
% total traffic in each time interval
T = sum(tm_real,1);
% compute mean
mean_r = mean(r,2);
mean_c = mean(c,2);
mean_T = mean(T);
% extract Fourier coefficients and form k-sparse version
fT = fft(T-mean_T);
[fTmag,indf] = sort(abs(fT(1:(n_TMs/2))),2,'descend');
% choose k coefficients based on nMDL
k = 23;
fs = zeros(1,n_TMs/2);
fs(indf(1:k)) = fT(indf(1:k));
fsparse = [fs fT(n_TMs/2+1) conj(fs(end:-1:2))];
norm_T = real(ifft(fsparse));
%% noise parameter \sigma^2 for both models
sigmasq = 10;
%% modulated gravity based on sparse Fourier, nMDL selected coefficients
rate = 5;
X = zeros(num_nodes,num_nodes,n_TMs);
Tk = mean(T);
rk = mean(r,2);
ck = mean(c,2);
tic
pU = rk/Tk;
pV = ck/Tk;
U = HMC_exact(eye(N), zeros(N,1), eye(N)*sigmasq/Tk^2, pU, true, 2, pU);
V = HMC_exact(eye(N), zeros(N,1), eye(N)*sigmasq/Tk^2, pV, true, 2, pV);
M0 = (Tk+norm_T)/Tk;
M = HMC_exact(eye(n_TMs),zeros(n_TMs,1),sigmasq/Tk^2,M0',true,2,M0');
G = Tk*U(:,2)*V(:,2)';
X = reshape(reshape(G,N^2,1)*M(:,2)',N,N,n_TMs);
toc
%% non-stationary conditionally independent model (NCIM) with delayed rows,
% columns to represent time zone shifts
% need to construct the row and column spatiotemporal signals
% choose New York's as reference
idNY = 9;
rs = zeros(N,n_TMs);
cs = zeros(N,n_TMs);
rs(idNY,:) = freq_sparsify(r(idNY,:),k); % nMDL calculated optimal choice of frequencies
cs(idNY,:) = freq_sparsify(c(idNY,:),k);
% each measurement interval is 5 minutes, so need to convert hours to
% minutes
m = 60/5;
% delay due to time zones (everywhere except NY)
D = m*[0 0 1 2 1 0 1 3 3 3 0];
location = [1:8 10:N]; % everywhere except NY
% build constraints
rscale = mean_r/mean_r(idNY);
cscale = mean_c/mean_c(idNY);
for i=1:length(location)
rs(location(i),:) = circshift(rscale(location(i))*rs(idNY,:),[0 D(i)]);
cs(location(i),:) = circshift(cscale(location(i))*cs(idNY,:),[0 D(i)]);
end
Ts = sum(rs,1);
tic
for i=1:n_TMs
pU = rs(:,i)/Ts(i);
pV = cs(:,i)/Ts(i);
U = HMC_exact(eye(N), zeros(N,1), eye(N)*sigmasq, pU, true, 2, pU);
V = HMC_exact(eye(N), zeros(N,1), eye(N)*sigmasq, pV, true, 2, pV);
Y(:,i) = U(:,2);
Z(:,i) = V(:,2);
Yd(:,i) = pU;
Zd(:,i) = pV;
Xc(:,:,i) = Ts(i)*U(:,2)*V(:,2)';
end
toc
%% Compute traffic decomposition via PCA
x_real = reshape(tm_real,N^2,n_TMs);
x_mod_gravity = reshape(X,N^2,n_TMs);
x_nscim = reshape(Xc,N^2,n_TMs);
% x_g = reshape(Xg,N^2,n_TMs);
mean_x_real = mean(x_real,2);
mean_x_mod_gravity = mean(x_mod_gravity,2);
mean_x_nscim = mean(x_nscim,2);
% mean_x_g = mean(x_g,2);
coeff_real = princomp(x_real);
coeff_mod_gravity = princomp(x_mod_gravity);
coeff_nscim = princomp(x_nscim);
spatial_real = svd(x_real);
[spatial_mod_gravity] = eig(x_mod_gravity*x_mod_gravity'-mean_x_mod_gravity *mean_x_mod_gravity');
spatial_nscim = eig(x_nscim*x_nscim'-mean_x_nscim*mean_x_nscim');
% spatial_g = eig(x_g*x_g'-mean_x_g*mean_x_g');
temporal_real = eig(x_real'*x_real);
temporal_mod_gravity = eig(x_mod_gravity'*x_mod_gravity - mean_x_mod_gravity'*mean_x_mod_gravity);
temporal_nscim = eig(x_nscim'*x_nscim - mean_x_nscim'*mean_x_nscim);
% temporal_g = eig(x_g'*x_g - mean_x_g'*mean_x_g);
% remove machine noise
spatial_real(spatial_real < 0) = 0;
spatial_mod_gravity(spatial_mod_gravity < 0) = 0;
spatial_nscim(spatial_nscim < 0) = 0;
% remove machine noise
temporal_real(temporal_real < 0) = 0;
temporal_mod_gravity(temporal_mod_gravity < 0) = 0;
temporal_nscim(temporal_nscim < 0) = 0;
%% Parameters for plotting
dgreen = [0.05 0.5 0.05];
dgrey = [0.65 0.65 0.65];
fontsize = 18;
print_figures = 0; % set to print figures into .eps, or not
paper_position = [0 0 14 12];
device = '-depsc';
thick_line = 1.1;
line = 1;
outfile = 'maxent_pca_';
%% Plot total sum
figure(1)
plot(sum(tm_real,1),'color',dgrey,'linewidth',thick_line);
hold on
plot(squeeze(sum(sum(X))),'color',dgrey);
plot(squeeze(sum(sum(Xc))),'color',dgreen);
hold off
set(gca,'fontsize', fontsize);
xlim([0,n_TMs]);
xlabel('t_k');
ylabel('Gbps');
%% Plot first three coeffs of real, modulated gravity model and
% non-stationary conditionally independent model
figure(10)
plot(coeff_real(:,1),'color',dgrey,'linewidth',thick_line);
hold on
plot(coeff_mod_gravity(:,1),'color',dgrey);
plot(coeff_nscim(:,1),'color',dgreen);
hold off
set(gca,'fontsize', fontsize);
xlim([0,n_TMs]);
xlabel('t_k');
ylabel('Coefficient magnitude');
figure(11)
plot(coeff_real(:,2),'color',dgrey,'linewidth',thick_line);
hold on
plot(coeff_mod_gravity(:,2),'color',dgrey);
plot(coeff_nscim(:,2),'color',dgreen);
hold off
set(gca,'fontsize', fontsize);
xlim([0,n_TMs]);
xlabel('t_k');
ylabel('Coefficient magnitude');
figure(12)
plot(coeff_real(:,3),'color',dgrey,'linewidth',thick_line);
hold on
plot(coeff_mod_gravity(:,3),'color',dgrey);
plot(coeff_nscim(:,3),'color',dgreen);
hold off
set(gca,'fontsize', fontsize);
xlim([0,n_TMs]);
xlabel('t_k');
ylabel('Coefficient magnitude');
%% Compare coeffs between real TM and models
figure(13)
id = 5;
L1 = length(coeff_real(:,id));
L2 = length(coeff_mod_gravity(:,id));
L3 = length(coeff_nscim(:,id));
semilogy(sort(coeff_real(:,id)),(1:L1)/L1,'color',dgrey,'linewidth',thick_line);
hold on
semilogy(sort(coeff_mod_gravity(:,id)),(1:L2)/L2,'r');
semilogy(sort(coeff_nscim(:,id)),(1:L3)/L3,'color',dgreen);
hold off
set(gca,'fontsize', fontsize);
xlabel('Coefficient value');
ylabel('CDF');
figure(23)
id1 = 1;
h1 = plot(coeff_mod_gravity(:,id1),'color',dgrey);
hold on
h2 = plot(coeff_nscim(:,id1),'color',dgreen,'linewidth',thick_line);
hold off
set(gca,'fontsize', fontsize);
xlim([0,n_TMs]);
xlabel('t_k');
ylabel('Coefficient magnitude');
ylim([0.01 0.035]);
if (sigmasq == 1e-5)
hlegend = legend([h1 h2], 'Modulated gravity', 'NCIM', 'Location','NorthWest');
end
if (print_figures)
set(gcf, 'PaperPosition', paper_position);
print(device, [outfile 'coeff_' num2str(id1) '_sigmasq_' num2str(sigmasq,'%.0e') '.eps']);
fprintf('printing %s\n', [outfile 'coeff_' num2str(id1) '_sigmasq_' num2str(sigmasq,'%.0e') '.eps']);
end
figure(33)
id2 = 2;
h1 = plot(coeff_mod_gravity(:,id2),'color',dgrey);
hold on
h2 = plot(coeff_nscim(:,id2),'color',dgreen,'linewidth',thick_line);
hold off
set(gca,'fontsize', fontsize);
xlim([0,n_TMs]);
xlabel('t_k');
ylabel('Coefficient magnitude');
ylim([0.01 0.035]);
% hlegend = legend([h1 h2], 'Modulated gravity', 'NCIM');
if (print_figures)
set(gcf, 'PaperPosition', paper_position);
print(device, [outfile 'coeff_' num2str(id2) '_sigmasq_' num2str(sigmasq,'%.0e') '.eps']);
fprintf('printing %s\n', [outfile 'coeff_' num2str(id2) '_sigmasq_' num2str(sigmasq,'%.0e') '.eps']);
end
figure(43)
id3 = 143;
h1 = plot(coeff_mod_gravity(:,id3),'color',dgrey);
hold on
h2 = plot(coeff_nscim(:,id3),'color',dgreen,'linewidth',thick_line);
hold off
set(gca,'fontsize', fontsize);
xlim([0,n_TMs]);
xlabel('t_k');
ylabel('Coefficient magnitude');
ylim([-0.1 0.1]);
% hlegend = legend([h1 h2], 'Modulated gravity', 'NCIM');
if (print_figures)
set(gcf, 'PaperPosition', paper_position);
print(device, [outfile 'coeff_' num2str(id3) '_sigmasq_' num2str(sigmasq,'%.0e') '.eps']);
fprintf('printing %s\n', [outfile 'coeff_' num2str(id3) '_sigmasq_' num2str(sigmasq,'%.0e') '.eps']);
end
%% Perform normal distribution fit on modulated gravity's coeff
figure(14)
id4 = 143;
L = length(coeff_mod_gravity(:,id4));
[muhat,sigmahat,muci,sigmaci] = normfit(coeff_mod_gravity(:,id4));
coeff_gaussian = normcdf(coeff_mod_gravity(:,id4),muhat,sigmahat);
[mod_grav_sorted,id_sort] = sort(coeff_mod_gravity(:,id4));
h1 = semilogy(mod_grav_sorted,coeff_gaussian(id_sort),'r','linewidth',thick_line);
hold on
h2 = semilogy(mod_grav_sorted,(1:L)/L,'color',dgrey,'linewidth',thick_line);
hold off
set(gca,'fontsize', fontsize);
xlabel('Coefficient value');
ylabel('CDF');
xlim([mod_grav_sorted(1) mod_grav_sorted(end)])
ylabel('CDF');
hlegend = legend([h1], 'Normal fit', 'Location', 'SouthEast');
% ylim([-0.1 0.1]);
if (print_figures)
set(gcf, 'PaperPosition', paper_position);
print(device, [outfile 'coeff_' num2str(id3) '_sigmasq_' num2str(sigmasq,'%.0e') '_normfit.eps']);
fprintf('printing %s\n', [outfile 'coeff_' num2str(id3) '_sigmasq_' num2str(sigmasq,'%.0e') '_normfit.eps']);
end
figure(15)
h1 = semilogy(sort(sqrt(spatial_mod_gravity),'descend'),'color',dgrey,'linewidth',thick_line);
hold on
h2 = semilogy(sort(sqrt(spatial_nscim),'descend'),'color',dgreen,'linewidth',thick_line);
hold off
set(gca,'fontsize', fontsize);
xlabel('Spatial coefficient order');
ylabel('Magnitude');
xlim([0 25]);
ylim([10^(-2) 10^(4)])
if (sigmasq == 1e-5)
hlegend = legend([h1 h2], 'Modulated gravity', 'NCIM', 'Location','NorthEast');
end
if (print_figures)
set(gcf, 'PaperPosition', paper_position);
print(device, [outfile 'sigmasq_' num2str(sigmasq,'%.0e') '_spatial.eps']);
fprintf('printing %s\n', [outfile 'sigmasq_' num2str(sigmasq,'%.0e') '_spatial.eps']);
end
figure(16)
h1 = semilogy(sort(sqrt(temporal_mod_gravity),'descend'),'color',dgrey,'linewidth',thick_line);
hold on
h2 = semilogy(sort(sqrt(temporal_nscim),'descend'),'color',dgreen,'linewidth',thick_line);
hold off
set(gca,'fontsize', fontsize);
xlabel('Temporal coefficient order');
ylabel('Magnitude');
max_x = max(sum(temporal_mod_gravity > 0),sum(temporal_nscim > 0));
xlim([0 25]);
ylim([10^(-2) 10^4]);
if (sigmasq == 1e-5)
hlegend = legend([h1 h2], 'Modulated gravity', 'NCIM', 'Location','NorthEast');
end
if (print_figures)
set(gcf, 'PaperPosition', paper_position);
print(device, [outfile 'sigmasq_' num2str(sigmasq,'%.0e') '_temporal.eps']);
fprintf('printing %s\n', [outfile 'sigmasq_' num2str(sigmasq,'%.0e') '_temporal.eps']);
end
%
% figure(17)
% h1 = semilogy(sort(spatial_mod_gravity,'descend'),'color',dgrey,'linewidth',thick_line);
% hold on
% h2 = semilogy(sort(spatial_g,'descend'),'color','b','linewidth',thick_line);
% hold off
% set(gca,'fontsize', fontsize);
% xlabel('Spatial coefficient order');
% ylabel('Magnitude');
%
% max_x = max(sum(temporal_mod_gravity > 0),sum(temporal_nscim > 0));
% % xlim([0 25]);
% % ylim([10^3 10^7]);
%
% if (sigmasq == 1e-5)
% hlegend = legend([h1 h2], 'Modulated gravity', 'Outer', 'Location','NorthEast');
% end
%
% if (print_figures)
% set(gcf, 'PaperPosition', paper_position);
% print(device, [outfile 'sigmasq_' num2str(sigmasq,'%.0e') '_outer_spatial.eps']);
% fprintf('printing %s\n', [outfile 'sigmasq_' num2str(sigmasq,'%.0e') '_outer_spatial.eps']);
% end
%
%
% figure(18)
% h1 = semilogy(sort(spatial_mod_gravity,'descend'),'color',dgrey,'linewidth',thick_line);
% hold on
% h2 = semilogy(sort(spatial_g,'descend'),'color','b','linewidth',thick_line);
% hold off
% set(gca,'fontsize', fontsize);
% xlabel('Temporal coefficient order');
% ylabel('Magnitude');
%
% max_x = max(sum(temporal_mod_gravity > 0),sum(temporal_nscim > 0));
% % xlim([0 25]);
% % ylim([10^3 10^7]);
%
% if (sigmasq == 1e-5)
% hlegend = legend([h1 h2], 'Modulated gravity', 'Outer', 'Location','NorthEast');
% end
%
% if (print_figures)
% set(gcf, 'PaperPosition', paper_position);
% print(device, [outfile 'sigmasq_' num2str(sigmasq,'%.0e') '_outer_temporal.eps']);
% fprintf('printing %s\n', [outfile 'sigmasq_' num2str(sigmasq,'%.0e') '_outer_temporal.eps']);
% end