forked from eth-sri/dp-sniper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noisy_hist.py
40 lines (30 loc) · 1.04 KB
/
noisy_hist.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
import numpy as np
from dpsniper.mechanisms.abstract import Mechanism
class NoisyHist1(Mechanism):
"""
Alg. 9 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):
l = a.shape[0]
v = np.atleast_2d(a)
# each row in m is one sample
m = v + np.random.laplace(scale=1/self.eps, size=(n_samples, l))
return m
class NoisyHist2(Mechanism):
"""
Alg. 10 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):
l = a.shape[0]
v = np.atleast_2d(a)
# each row in m is one sample
m = v + np.random.laplace(scale=self.eps, size=(n_samples, l))
return m