-
Notifications
You must be signed in to change notification settings - Fork 3
/
SHE_test.py
80 lines (71 loc) · 2.41 KB
/
SHE_test.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
from SHE.SHE import SHE
from Components.Draw import draw
from Components.Distribution import zipf
import math
import random
# protocol is class
def analytical(protocol, d, n):
vars = [] # return a list of variances
for step in range(10): # 10 values
epsilon = 0.5 + 0.5 * step
x = protocol(d, epsilon, n)
#x.set_n(n)
vars.append(x.var_analytical())
return vars
# protocol is class, fixing epsilon
def analytical_eps(protocol, epsilon, n):
vars = [] # return a list of variances
for step in range(7): # 9 values
d = 2**(2 + step*2)
x = protocol(d, epsilon, n)
#x.set_n(n)
vars.append(x.var_analytical())
return vars
def empirical_eps(protocol, epsilon, n):
vars = []
for step in range(7): # 9 values
d = 2**(2 + step*2)
#f = []
x = protocol(d, epsilon, n)
users = zipf.zipf(1.1, d, n)
for i in range(n): # random PE
if i%1000 == 0:
print(i,step)
#m = random.randint(1,d)
#x.PE(m)
x.PE(users[i]+1)
f = zipf.probList(1.1, d, n)
#for i in range(d):
# f.append(1.0/d)
#print(f)
x.aggregation()
vars.append(x.var_empirical(f))
return vars
# like Figure 1(b)
def numerical_values_of_var2():
varslist = [] # return a list of (a list of variances)
d=2**10 # 2^10
vars = analytical(SHE, d, 10000)
for i in range(len(vars)):
vars[i] = math.log(vars[i]) / math.log(10)
varslist.append(vars)
epss = [0.5+0.5*item for item in range(10)] # 0.5, 1.0, ..., 5.0
draw.lines(epss, varslist, ['SHE'], ylabel='Var(log10(y))')
# Figure 2(a)
def Comparing_empirical_and_analytical_variance1():
varslist = [] # return a list of (a list of variances)
# Analytical DE
vars = analytical_eps(SHE, 4, 10000)
for i in range(len(vars)): #log10
vars[i] = math.log(vars[i]) / math.log(10)
varslist.append(vars)
# Empirical DE
vars = empirical_eps(SHE, 4, 10000)
for i in range(len(vars)): #log10
vars[i] = math.log(vars[i]) / math.log(10)
varslist.append(vars)
# Draw
d = [item for item in range(2, 16, 2)] # 2^2, 2^4, ..., 2^18
draw.lines(d, varslist, ['Analytical SHE', 'Empirical SHE'], xlabel='Vary d(log2(d))', ylabel='Vary n(log10(n))')
#numerical_values_of_var2()
Comparing_empirical_and_analytical_variance1()