-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_utility_loss.m
347 lines (282 loc) · 8.17 KB
/
test_utility_loss.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
% The script is designed to evaluate the utility loss between F-PCA with
% perturbation masks against MOD-SuLQ (symmetric & non-symmetric).
%
% In all instances adaptive rank estimation is disabled for F-PCA.
%
% Based on work of Grammenos et al.: https://arxiv.org/abs/1907.08059
%
% Author: Andreas Grammenos (ag926@cl.cam.ac.uk)
%
% Last touched date: 31/05/2020
%
% License: GPLv3
%
%% Initialisation
clc; clear; close all;
% for reproducibility
rng(300, 'twister');
% setup the variables
ul_params.type = "utility-loss";
% enable printing
ul_params.pflag = 1;
% print pdfs
ul_params.pdf_print = 1;
% configure the parameters
ul_params = setup_vars(ul_params);
% number of simulations to run
sims = 100;
% target rank (or seed for adaptive)
r = 5;
%rr = 2;
rr = 1;
% number of features in each vector
feats = 20; % 1k, 200 - for paper we used 20
% number of feature vectors to process
T = 5000; % 10k, 2k - for paper we used 5k
% singular value scaler
sv_scaler = 1;
% the alpha value range
alphas = [0.01, 0.1, .5, 1];
% show spectrums
show_spectra = 1;
% check if we use normal distribution
use_normal = 0;
% the privacy deltas
deltas = [.05]; % .01
% the epsilon values
epsilons = 0.1:0.1:4; % 3
synth_params.spectrum_type = "pl";
%synth_params.spectrum_type = "singular";
%synth_params.spectrum_type = "mod-sulq";
% F-PCA parameters
adaptive = 0;
private = 1;
% loop over the alphas
a_len = size(alphas, 2);
e_len = size(epsilons, 2);
d_len = size(deltas, 2);
% preallocate trade-off arrays
u_tradeoff = zeros(a_len, d_len, e_len);
ms_u_tradeoff = zeros(a_len, d_len, e_len);
ms_nonsym_u_tradeoff = zeros(a_len, d_len, e_len);
% pre-allocate the spectrum arrays only if we plot them
if show_spectra == 1
sv_spectra = zeros(a_len, feats);
end
% preallocate cells for legends
ll = cell(a_len, 1);
%% Run the utility loss experiments
for j = 1:size(alphas, 2)
% configure power law, if it is the elected distribution type.
if synth_params.spectrum_type == "pl"
synth_params.alpha = alphas(j);
synth_params.lambda = sv_scaler;
synth_params.rand_type = "normal";
end
% generate the synthetic data
if show_spectra == 1
[Y, sv_spectra(j, :)] = synthetic_data_gen(feats, T, synth_params);
else
[Y, ~] = synthetic_data_gen(feats, T, synth_params);
end
[UamY, ~, ~] = svds(Y, rr);
% get the correct amount of PC's to compare against (normally the first)
Uam = UamY(:, 1:rr);
% compute the Vperp
Vperp = null(Uam');
% run the for number of simulations
for kk = 1:sims
% run for the delta range
for d = 1:size(deltas, 2)
delta = deltas(d);
% run for the epsilon range
for i = 1:size(epsilons, 2)
e_p = epsilons(i);
% normal mod-sulq
[Ums, ~, ~] = svds(mod_sulq(Y, e_p, delta), rr);
Ums = Ums(:, 1:rr);
% non-symmetric mod-sulq
[Ums_nonsym, ~, ~] = svds(mod_sulq(Y, e_p, delta, 0), rr);
Ums_nonsym = Ums_nonsym(:, 1:rr);
% configure f-pca
params.adaptive = adaptive;
params.private = private;
params.e_p = e_p;
params.delta = delta;
params.blk_size = 50;
% run f-pca
[Uam_p, ~, ~] = fpca_edge(Y, r, params);
Uam_p = Uam_p(:, 1:rr);
% compute the trade off for f-pca
u_tradeoff(j, d, i) = u_tradeoff(j, d, i) + norm(Uam_p'*UamY);
% non symmetric mod-sulq
ms_nonsym_u_tradeoff(j, d, i) = ms_nonsym_u_tradeoff(j, d, i) + norm(Ums_nonsym'*UamY);
% compute the trade-off for mod-sulq
ms_u_tradeoff(j, d, i) = ms_u_tradeoff(j, d, i) + norm(Ums'*UamY);
end
end
end
end
%% Finally, plot the results
fig = figure;
% adjust the number of figures based if we show spectra or not
if show_spectra == 1
plot_num = 4;
else
plot_num = 3;
end
subplot(plot_num, 1, 1)
hold on
% run through the alphas and plot the results
for i = 1:a_len
ff = u_tradeoff(i, :, :);
plot(squeeze(ff)/sims, 'LineWidth', 2);
s = sprintf("d at a %.2f", alphas(i));
ll{i} = sprintf('a=%.2f', alphas(i));
end
hold off
% this is to output the "\varepsilon" equivalent
xlabel(char(949));
ylabel('q_{A}');
% configure the xticks
xlabels = xticks;
xlabels_sz = size(xlabels, 2);
xtick_labels = cell(1, xlabels_sz);
xtick_labels{1} = epsilons(1);
% assign the rest of the tick labels
for i = 2:xlabels_sz
xtick_labels{i} = epsilons(xlabels(i));
end
% assign the ticks
xticklabels(xtick_labels);
title('fpca');
legend(ll);
subplot(plot_num, 1, 2)
hold on
% run through the alphas and plot the results
for i = 1:a_len
ff = ms_nonsym_u_tradeoff(i, :, :);
plot(squeeze(ff)/sims, 'LineWidth', 2);
s = sprintf("d at a %.2f", alphas(i));
ll{i} = sprintf('a=%.2f', alphas(i));
end
hold off
% this is to output the "\varepsilon" equivalent
xlabel(char(949));
ylabel('q_{A}');
% assign the ticks
xticklabels(xtick_labels);
title('mod-sulq (non-symmetric)');
legend(ll);
subplot(plot_num, 1, 3)
hold on
% run through the alphas and plot the results
for i = 1:a_len
ff = ms_u_tradeoff(i, :, :);
plot(squeeze(ff)/sims, 'LineWidth', 2);
s = sprintf("d at a %.2f", alphas(i));
ll{i} = sprintf('a=%.2f', alphas(i));
end
hold off
% this is to output the "\varepsilon" equivalent
xlabel(char(949));
ylabel('q_{A}');
% assign the ticks
xticklabels(xtick_labels);
title('mod-sulq (symmetric)');
legend(ll);
% check if we plot spectra and plot if we enabled.
if show_spectra == 1
subplot(plot_num, 1, 4)
hold on
for i = 1:a_len
ff = squeeze(sv_spectra(i, :));
plot(ff, 'LineWidth', 2);
end
hold off
xlabel('singular values')
ylabel('magnitude');
title('Singular Value True Spectrum')
legend(ll);
end
% configure filename for the figure
st = sprintf("utility_loss_spectrum_type_%s_d_%d_r_%d_T_%d_sims_%d", ...
synth_params.spectrum_type, feats, r, T, sims);
% print the figure for the utility loss
print_fig(fig, st, ul_params);
%% Plot them individually
fig = figure;
hold on
% run through the alphas and plot the results
for i = 1:a_len
ff = u_tradeoff(i, :, :);
plot(squeeze(ff)/sims, 'LineWidth', 2);
s = sprintf("d at a %.2f", alphas(i));
ll{i} = sprintf('a=%.2f', alphas(i));
end
hold off
% this is to output the "\varepsilon" equivalent
xlabel(char(949));
ylabel('q_{A}');
% configure the xticks
xlabels = xticks;
xlabels_sz = size(xlabels, 2);
xtick_labels = cell(1, xlabels_sz);
xtick_labels{1} = epsilons(1);
% assign the rest of the tick labels
for i = 2:xlabels_sz
xtick_labels{i} = epsilons(xlabels(i));
end
% assign the ticks
xticklabels(xtick_labels);
title('fpca');
legend(ll, 'location', 'best');
% configure filename for the figure
st = sprintf("fpca_utility_loss_paper_spectrum_type_%s_d_%d_r_%d_T_%d_sims_%d", ...
synth_params.spectrum_type, feats, r, T, sims);
% print the figure for the utility loss
print_fig(fig, st, ul_params);
fig = figure;
hold on
% run through the alphas and plot the results
for i = 1:a_len
ff = ms_nonsym_u_tradeoff(i, :, :);
plot(squeeze(ff)/sims, 'LineWidth', 2);
s = sprintf("d at a %.2f", alphas(i));
ll{i} = sprintf('a=%.2f', alphas(i));
end
hold off
% this is to output the "\varepsilon" equivalent
xlabel(char(949));
ylabel('q_{A}');
% assign the ticks
xticklabels(xtick_labels);
title('mod-sulq (non-symmetric)');
legend(ll, 'location', 'best');
% configure filename for the figure
st = sprintf("mod_sulq_sym_utility_loss_paper_spectrum_type_%s_d_%d_r_%d_T_%d_sims_%d", ...
synth_params.spectrum_type, feats, r, T, sims);
% print the figure for the utility loss
print_fig(fig, st, ul_params);
fig = figure;
hold on
% run through the alphas and plot the results
for i = 1:a_len
ff = ms_u_tradeoff(i, :, :);
plot(squeeze(ff)/sims, 'LineWidth', 2);
s = sprintf("d at a %.2f", alphas(i));
ll{i} = sprintf('a=%.2f', alphas(i));
end
hold off
% this is to output the "\varepsilon" equivalent
xlabel(char(949));
ylabel('q_{A}');
% assign the ticks
xticklabels(xtick_labels);
title('mod-sulq (symmetric)');
legend(ll, 'location', 'best');
% configure filename for the figure
st = sprintf("mod_sulq_utility_loss_paper_spectrum_type_%s_d_%d_r_%d_T_%d_sims_%d", ...
synth_params.spectrum_type, feats, r, T, sims);
% print the figure for the utility loss
print_fig(fig, st, ul_params);