-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathuniform.py
98 lines (75 loc) · 3.5 KB
/
uniform.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
# 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 uniform mechanism in differential privacy.
"""
from numbers import Real
from diffprivlib.mechanisms.base import DPMechanism
from diffprivlib.mechanisms.laplace import Laplace
from diffprivlib.utils import copy_docstring
class Uniform(DPMechanism):
r"""
The Uniform mechanism in differential privacy.
This emerges as a special case of the :class:`.LaplaceBoundedNoise` mechanism when epsilon = 0.
Paper link: https://arxiv.org/pdf/1810.00877.pdf
Parameters
----------
delta : float
Privacy parameter :math:`\delta` for the mechanism. Must be in (0, 0.5].
sensitivity : float
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, *, delta, sensitivity, random_state=None):
super().__init__(epsilon=0.0, delta=delta, random_state=random_state)
self.sensitivity = self._check_sensitivity(sensitivity)
@classmethod
def _check_epsilon_delta(cls, epsilon, delta):
if not epsilon == 0:
raise ValueError("Epsilon must be strictly zero.")
if not 0 < delta <= 0.5:
raise ValueError("Delta must be in the half-open interval (0, 0.5]")
return super()._check_epsilon_delta(epsilon, delta)
@classmethod
def _check_sensitivity(cls, sensitivity):
if not isinstance(sensitivity, Real):
raise TypeError("Sensitivity must be numeric")
if sensitivity < 0:
raise ValueError("Sensitivity must be non-negative")
return float(sensitivity)
@copy_docstring(Laplace.bias)
def bias(self, value):
return 0.0
@copy_docstring(Laplace.variance)
def variance(self, value):
self._check_all(value)
return (self.sensitivity / self.delta) ** 2 / 12
def _check_all(self, value):
super()._check_all(value)
self._check_sensitivity(self.sensitivity)
if not isinstance(value, Real):
raise TypeError("Value to be randomised must be a number")
return True
@copy_docstring(Laplace.randomise)
def randomise(self, value):
self._check_all(value)
unif_rv = 2 * self._rng.random() - 1
unif_rv *= self.sensitivity / self.delta / 2
return value + unif_rv