-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathgeometric.py
233 lines (174 loc) · 8.26 KB
/
geometric.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
# MIT License
#
# Copyright (C) IBM Corporation 2019
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""
The classic geometric mechanism for differential privacy, and its derivatives.
"""
from numbers import Integral
import numpy as np
from diffprivlib.mechanisms.base import DPMechanism, TruncationAndFoldingMixin
from diffprivlib.utils import copy_docstring
class Geometric(DPMechanism):
r"""
The classic geometric mechanism for differential privacy, as first proposed by Ghosh, Roughgarden and Sundararajan.
Extended to allow for non-unity sensitivity.
Paper link: https://arxiv.org/pdf/0811.2841.pdf
Parameters
----------
epsilon : float
Privacy parameter :math:`\epsilon` for the mechanism. Must be in (0, ∞].
sensitivity : float, default: 1
The sensitivity of the mechanism. Must be in [0, ∞).
random_state : int or RandomState, optional
Controls the randomness of the mechanism. To obtain a deterministic behaviour during randomisation,
``random_state`` has to be fixed to an integer.
"""
def __init__(self, *, epsilon, sensitivity=1, random_state=None):
super().__init__(epsilon=epsilon, delta=0.0, random_state=random_state)
self.sensitivity = self._check_sensitivity(sensitivity)
self._scale = - self.epsilon / self.sensitivity if self.sensitivity > 0 else - float("inf")
@classmethod
def _check_sensitivity(cls, sensitivity):
if not isinstance(sensitivity, Integral):
raise TypeError("Sensitivity must be an integer")
if sensitivity < 0:
raise ValueError("Sensitivity must be non-negative")
return sensitivity
def _check_all(self, value):
super()._check_all(value)
self._check_sensitivity(self.sensitivity)
if not isinstance(value, Integral):
raise TypeError("Value to be randomised must be an integer")
@classmethod
def _check_epsilon_delta(cls, epsilon, delta):
if not delta == 0:
raise ValueError("Delta must be zero")
return super()._check_epsilon_delta(epsilon, delta)
@copy_docstring(DPMechanism.bias)
def bias(self, value):
return 0.0
@copy_docstring(DPMechanism.variance)
def variance(self, value):
self._check_all(value)
leading_factor = (1 - np.exp(self._scale)) / (1 + np.exp(self._scale))
geom_series = np.exp(self._scale) / (1 - np.exp(self._scale))
return 2 * leading_factor * (geom_series + 3 * (geom_series ** 2) + 2 * (geom_series ** 3))
def randomise(self, value):
"""Randomise `value` with the mechanism.
Parameters
----------
value : int
The value to be randomised.
Returns
-------
int
The randomised value.
"""
self._check_all(value)
# Need to account for overlap of 0-value between distributions of different sign
unif_rv = self._rng.random() - 0.5
unif_rv *= 1 + np.exp(self._scale)
sgn = -1 if unif_rv < 0 else 1
# Use formula for geometric distribution, with ratio of exp(-epsilon/sensitivity)
return int(np.round(value + sgn * np.floor(np.log(sgn * unif_rv) / self._scale)))
class GeometricTruncated(Geometric, TruncationAndFoldingMixin):
r"""
The truncated geometric mechanism, where values that fall outside a pre-described range are mapped back to the
closest point within the range.
Parameters
----------
epsilon : float
Privacy parameter :math:`\epsilon` for the mechanism. Must be in (0, ∞].
sensitivity : float, default: 1
The sensitivity of the mechanism. Must be in [0, ∞).
lower : int
The lower bound of the mechanism.
upper : int
The upper bound of the mechanism.
random_state : int or RandomState, optional
Controls the randomness of the mechanism. To obtain a deterministic behaviour during randomisation,
``random_state`` has to be fixed to an integer.
"""
def __init__(self, *, epsilon, sensitivity=1, lower, upper, random_state=None):
super().__init__(epsilon=epsilon, sensitivity=sensitivity, random_state=random_state)
TruncationAndFoldingMixin.__init__(self, lower=lower, upper=upper)
@classmethod
def _check_bounds(cls, lower, upper):
if not isinstance(lower, Integral) and abs(lower) != float("inf"):
raise TypeError(f"Lower bound must be integer-valued, got {lower}")
if not isinstance(upper, Integral) and abs(upper) != float("inf"):
raise TypeError(f"Upper bound must be integer-valued, got {upper}")
return super()._check_bounds(lower, upper)
@copy_docstring(DPMechanism.bias)
def bias(self, value):
raise NotImplementedError
@copy_docstring(DPMechanism.bias)
def variance(self, value):
raise NotImplementedError
def _check_all(self, value):
super()._check_all(value)
TruncationAndFoldingMixin._check_all(self, value)
return True
@copy_docstring(Geometric.randomise)
def randomise(self, value):
self._check_all(value)
noisy_value = super().randomise(value)
return int(np.round(self._truncate(noisy_value)))
class GeometricFolded(Geometric, TruncationAndFoldingMixin):
r"""
The folded geometric mechanism, where values outside a pre-described range are folded back toward the domain around
the closest point within the domain.
Half-integer bounds are permitted.
Parameters
----------
epsilon : float
Privacy parameter :math:`\epsilon` for the mechanism. Must be in (0, ∞].
sensitivity : float, default: 1
The sensitivity of the mechanism. Must be in [0, ∞).
lower : int or float
The lower bound of the mechanism. Must be integer or half-integer -valued.
upper : int or float
The upper bound of the mechanism. Must be integer or half-integer -valued.
random_state : int or RandomState, optional
Controls the randomness of the mechanism. To obtain a deterministic behaviour during randomisation,
``random_state`` has to be fixed to an integer.
"""
def __init__(self, *, epsilon, sensitivity=1, lower, upper, random_state=None):
super().__init__(epsilon=epsilon, sensitivity=sensitivity, random_state=random_state)
TruncationAndFoldingMixin.__init__(self, lower=lower, upper=upper)
@classmethod
def _check_bounds(cls, lower, upper):
if not np.isclose(2 * lower, np.round(2 * lower)) or not np.isclose(2 * upper, np.round(2 * upper)):
raise ValueError("Bounds must be integer or half-integer floats")
return super()._check_bounds(lower, upper)
def _fold(self, value):
return super()._fold(int(np.round(value)))
@copy_docstring(DPMechanism.bias)
def bias(self, value):
raise NotImplementedError
@copy_docstring(DPMechanism.bias)
def variance(self, value):
raise NotImplementedError
def _check_all(self, value):
super()._check_all(value)
TruncationAndFoldingMixin._check_all(self, value)
return True
@copy_docstring(Geometric.randomise)
def randomise(self, value):
self._check_all(value)
noisy_value = super().randomise(value)
return int(np.round(self._fold(noisy_value)))