forked from eth-sri/dp-sniper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
report_noisy_max.py
74 lines (54 loc) · 2.06 KB
/
report_noisy_max.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
import numpy as np
from dpsniper.mechanisms.abstract import Mechanism
class ReportNoisyMax1(Mechanism):
"""
Alg. 5 from:
Zeyu Ding, YuxinWang, GuanhongWang, Danfeng Zhang, and Daniel Kifer. 2018.
Detecting Violations of Differential Privacy. CCS 2018.
"""
def __init__(self, eps: float = 0.1):
self.eps = eps
def m(self, a, n_samples: int = 1):
v = np.atleast_2d(a)
# each row in m is one sample
m = v + np.random.laplace(scale=2/self.eps, size=(n_samples, a.shape[0]))
return np.argmax(m, axis=1)
class ReportNoisyMax2(Mechanism):
"""
Alg. 6 from:
Zeyu Ding, YuxinWang, GuanhongWang, Danfeng Zhang, and Daniel Kifer. 2018.
Detecting Violations of Differential Privacy. CCS 2018.
"""
def __init__(self, eps: float = 0.1):
self.eps = eps
def m(self, a, n_samples: int = 1):
v = np.atleast_2d(a)
# each row in m is one sample
m = v + np.random.exponential(scale=2/self.eps, size=(n_samples, a.shape[0]))
return np.argmax(m, axis=1)
class ReportNoisyMax3(Mechanism):
"""
Alg. 7 from:
Zeyu Ding, YuxinWang, GuanhongWang, Danfeng Zhang, and Daniel Kifer. 2018.
Detecting Violations of Differential Privacy. CCS 2018.
"""
def __init__(self, eps: float = 0.1):
self.eps = eps
def m(self, a, n_samples: int = 1):
v = np.atleast_2d(a)
# each row in m is one sample
m = v + np.random.laplace(scale=2/self.eps, size=(n_samples, a.shape[0]))
return np.amax(m, axis=1)
class ReportNoisyMax4(Mechanism):
"""
Alg. 8 from:
Zeyu Ding, YuxinWang, GuanhongWang, Danfeng Zhang, and Daniel Kifer. 2018.
Detecting Violations of Differential Privacy. CCS 2018.
"""
def __init__(self, eps: float = 0.1):
self.eps = eps
def m(self, a, n_samples: int = 1):
v = np.atleast_2d(a)
# each row in m is one sample
m = v + np.random.exponential(scale=2/self.eps, size=(n_samples, a.shape[0]))
return np.amax(m, axis=1)