-
Notifications
You must be signed in to change notification settings - Fork 1
/
mLDP_KDE.py
322 lines (273 loc) · 11.1 KB
/
mLDP_KDE.py
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
import numpy as np
from kde_tools import main_P_L2, main_P_L1
from hashes import l2_lsh, hash_l2_lsh, l1_lsh, hash_l1_lsh, ang_lsh, hash_ang_lsh
from RACE import RACE
import time
''' Function for mLDP-KDE (L2 kernel)'''
def mldp_kde_l2kernel_kde(query, epsilon, data, reps, hash_range, dim, bandwidth, n, d, seed_l2lsh, seed_grr_rehash, flag=1):
start_time = time.perf_counter()
# Generate a series of L2-LSH parameters
W, b = l2_lsh(reps, dim, bandwidth, seed_l2lsh, flag)
# 1 − k(x, x′)
one_kernel = 1 - main_l2kernel_kde(d, bandwidth)
# As the monotonic property of KL divergence, approximate an extreme value using binary search
s = binary_search_l1_l2(one_kernel, reps, hash_range)
temp = 0.8 * d / bandwidth # coefficient 0.8 r / ω
# Set gamma by 'Corollary 1'
gamma = epsilon / (reps * (((hash_range - 1) / hash_range) * temp + s))
hash_values = [] # hash values of all data
noisy_data = [] # noisy hash values of all data
# Calculate the probability of GRR
p_self = np.exp(gamma) / (np.exp(gamma) + reps - 1)
# Generate a hash matrix where the 'i-th' row represents 'reps' hash values for the 'i-th' user.
for index, d in enumerate(data):
hash_values_int = np.array(hash_l2_lsh(W, b, bandwidth, x=d)).astype(int)
hash_values.append(hash_values_int)
end_time = time.perf_counter()
const_time_1 = end_time - start_time
move_list = [] # offset for each column
np.random.seed(seed_grr_rehash)
start_time = time.perf_counter()
for hash_values_int in zip(*hash_values):
# For each column in the hash matrix, use offsets to find the interval [left, right] with the highest density of hash values.
left, right = find_max_subarray(hash_values_int, hash_range)
move_list.append(left)
# Offset operation
rehash_x = map_to_range(hash_values_int, left, hash_range)
noisy_d = [] # results of adding noise to a single column in the hash matrix
for hash_value in rehash_x:
P = np.random.uniform(0, 1)
if P < p_self:
# i = v case in Equation 2.
noisy_d.append(hash_value)
else:
# otherwise case in Equation 2.
while True:
random_num = np.random.randint(0, hash_range)
if random_num != hash_value:
break
noisy_d.append(random_num)
noisy_data.append(noisy_d)
end_time = time.perf_counter()
construct_time_2 = end_time - start_time
start_time = time.perf_counter()
hash_query = []
# Obtain hash values for query data
for index, q in enumerate(query):
query_hash_value = hash_l2_lsh(W, b, bandwidth, x=q)
hash_values_int = np.array(query_hash_value).astype(int)
hash_query.append(hash_values_int)
end_time = time.perf_counter()
query_time = (end_time - start_time) / (len(query))
# Sketch construction and query process in server side
l2lsh_race_kde, sub_const_time, sub_query_time, counts = mldp_kde_count_race_l1_l2(hash_query, noisy_data, reps, hash_range, gamma, n, move_list)
return l2lsh_race_kde, const_time_1 + construct_time_2 + sub_const_time, query_time + sub_query_time, counts
''' Function for mLDP-KDE (L1 kernel)'''
def mldp_kde_l1kernel_kde(query, epsilon, data, reps, hash_range, dim, bandwidth, n, seed_l1lsh, seed_grr_rehash):
# Generate a series of L1-LSH parameters
W, b = l1_lsh(reps, dim, bandwidth, seed_l1lsh)
one_kernel = 1 - main_l1kernel_kde(0.05, bandwidth)
s = binary_search_l1_l2(one_kernel, reps, hash_range)
c1 = 1.2
c2 = 0.1
temp1 = (c1 * (hash_range - 1) * 0.05) / (bandwidth * hash_range)
temp2 = (c2 * (hash_range - 1)) / hash_range
# Calculate gamma for L1
gamma = epsilon / (reps * (temp1 + temp2 + s))
hash_values = []
noisy_data = []
p_self = np.exp(gamma) / (np.exp(gamma) + reps - 1)
for index, d in enumerate(data):
hash_values_int = np.array(hash_l1_lsh(W, b, bandwidth, x=d)).astype(int)
hash_values.append(hash_values_int)
move_list = []
np.random.seed(seed_grr_rehash)
for hash_values_int in zip(*hash_values):
left, right = find_max_subarray(hash_values_int, hash_range)
move_list.append(left)
rehash_x = map_to_range(hash_values_int, left, hash_range)
noisy_d = []
for hash_value in rehash_x:
P = np.random.uniform(0, 1)
if P < p_self:
noisy_d.append(hash_value)
else:
while True:
random_num = np.random.randint(0, hash_range)
if random_num != hash_value:
break
noisy_d.append(random_num)
noisy_data.append(noisy_d)
hash_query = []
for index, q in enumerate(query):
query_hash_value = hash_l1_lsh(W, b, bandwidth, x=q)
hash_values_int = np.array(query_hash_value).astype(int)
hash_query.append(hash_values_int)
l1lsh_race_kde, _, _, _ = mldp_kde_count_race_l1_l2(hash_query, noisy_data, reps, hash_range, gamma, n, move_list)
return l1lsh_race_kde
''' Function for mLDP-KDE (Angular kernel)'''
def mldp_kde_angkernel_kde(query, epsilon, data, reps, hash_range, dim, n, seed_anglsh, seed_grr_rehash):
# Generate parameter a for Angular-LSH
a = ang_lsh(reps, dim, seed_anglsh)
s = binary_search_ang(reps)
# Calculate gamma for Angular kernel
gamma = epsilon / (reps * ((0.001 / np.pi) + s))
hash_values = []
p_self = np.exp(gamma) / (np.exp(gamma) + reps - 1)
noisy_data = []
for index, d in enumerate(data):
hash_values_int = np.array(hash_ang_lsh(a, x=d)).astype(int)
hash_values.append(hash_values_int)
np.random.seed(seed_grr_rehash)
for hash_values_int in zip(*hash_values):
noisy_d = []
for hash_value in hash_values_int:
P = np.random.uniform(0, 1)
if P < p_self:
noisy_d.append(hash_value)
else:
while True:
random_num = np.random.randint(0, hash_range)
if random_num != hash_value:
break
noisy_d.append(random_num)
noisy_data.append(noisy_d)
hash_query = []
for index, q in enumerate(query):
query_hash_value = hash_ang_lsh(a, x=q)
hash_values_int = np.array(query_hash_value).astype(int)
hash_query.append(hash_values_int)
anglsh_race_kde = mldp_kde_count_race_ang(hash_query, noisy_data, reps, hash_range, gamma, n)
return anglsh_race_kde
def map_to_range(num, start, reps):
mapped_data = []
for d in num:
d = d + (-start)
mapped_data.append(d % reps)
return mapped_data
def find_max_subarray(nums, reps):
cnt = {}
Min = 9999999999
Max = -Min
for i in nums:
if i in cnt:
cnt[i] += 1
else:
cnt[i] = 1
if i > Max:
Max = i
if i < Min:
Min = i
subarray_sum = 0
max_sum = -99999999
left = 0
right = 0
for i in range(Min, Max + 1):
if (i - Min) < reps:
if i in cnt:
subarray_sum = subarray_sum + cnt[i]
else:
subarray_sum = subarray_sum + 0
if subarray_sum > max_sum:
max_sum = subarray_sum
left = Min
right = i
else:
if i in cnt:
subarray_sum = subarray_sum + cnt[i]
else:
subarray_sum = subarray_sum + 0
if (i - reps) in cnt:
subarray_sum = subarray_sum - cnt[i - reps]
else:
subarray_sum = subarray_sum + 0
if subarray_sum > max_sum:
max_sum = subarray_sum
left = i - reps + 1
right = i
return left, right
def main_l2kernel_kde(d, bandwidth):
main_kde = np.array(main_P_L2(d, bandwidth))
return main_kde
def main_l1kernel_kde(d, bandwidth):
main_kde = np.array(main_P_L1(d, bandwidth))
return main_kde
def binary_search_l1_l2(one_kernel, reps, hash_range):
lower_bound = 0.00001
upper_bound = 1 - ((hash_range - 1) / hash_range) * one_kernel - 0.00001
precision = 0.0000001
max_iterations = 100000
guess = (lower_bound + upper_bound) / 2
for _ in range(max_iterations):
result = inequality_function_l1_l2(one_kernel, guess, reps, hash_range)
if abs(result) < precision:
break
elif result > 0:
upper_bound = guess
else:
lower_bound = guess
guess = (lower_bound + upper_bound) / 2
return guess
def binary_search_ang(reps):
lower_bound = 0.00001
p = 0.001 / np.pi
upper_bound = 1 - p - 0.00001
precision = 0.0000001
max_iterations = 100000
guess = (lower_bound + upper_bound) / 2
for _ in range(max_iterations):
result = inequality_function_ang(p, guess, reps)
if abs(result) < precision:
break
elif result > 0:
upper_bound = guess
else:
lower_bound = guess
guess = (lower_bound + upper_bound) / 2
return guess
def inequality_function_l1_l2(one_kernel, s, reps, hash_range):
a = ((hash_range - 1) / hash_range) * one_kernel + s
b = ((hash_range - 1) / hash_range) * one_kernel
return (a * np.log((a / b)) + (1 - a) * np.log((1 - a) / (1 - b))) - (np.log(10) / reps)
def inequality_function_ang(p, s, reps):
a = p + s
b = p
return (a * np.log((a / b)) + (1 - a) * np.log((1 - a) / (1 - b))) - (np.log(10) / reps)
def subtract_lists(list1, list2):
result = [x - y for x, y in zip(list1, list2)]
return result
def mldp_kde_count_race_l1_l2(l2lsh_query, hash_codes, reps, hash_range, gamma, n, move_list):
start_time = time.perf_counter()
mldp_kde_result = [] # kde results for query points
# Sketch construction
S = RACE(n, repetitions=reps, hash_range=hash_range)
ind = 0
for d in hash_codes:
S.main_add(d, ind)
ind = ind + 1
# S.print() # sketch visualization
counts = S.counts()
end_time = time.perf_counter()
sub_construct_time = end_time - start_time
start_time = time.perf_counter()
for hash_q in l2lsh_query:
# Offset query points to target hash range based on previously calculated offsets
rehash_q = subtract_lists(hash_q, move_list)
mldp_kde_result.append(S.main_query_l1_l2(rehash_q, gamma))
mldp_kde_result = np.array(mldp_kde_result)
end_time = time.perf_counter()
sub_query_time = (end_time - start_time) / (len(l2lsh_query))
S.clear()
return mldp_kde_result, sub_construct_time, sub_query_time, counts
def mldp_kde_count_race_ang(l2lsh_query, hash_codes, reps, hash_range, gamma, n):
mldp_kde_result = []
S = RACE(n, repetitions=reps, hash_range=hash_range)
ind = 0
for d in hash_codes:
S.main_add(d, ind)
ind = ind + 1
for hash_q in l2lsh_query:
mldp_kde_result.append(S.main_query_ang(hash_q, gamma))
mldp_kde_result = np.array(mldp_kde_result)
S.clear()
return mldp_kde_result