Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# 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 Laplace mechanism in differential privacy, and its derivatives.
"""
from numbers import Real
import numpy as np
from numpy.random import random
from diffprivlib.mechanisms.base import DPMechanism, TruncationAndFoldingMixin
from diffprivlib.utils import copy_docstring
class Laplace(DPMechanism):
r"""
The classic Laplace mechanism in differential privacy, as first proposed by Dwork, McSherry, Nissim and Smith.
Paper link: https://link.springer.com/content/pdf/10.1007/11681878_14.pdf
Includes extension to (relaxed) :math:`(\epsilon,\delta)`-differential privacy, as proposed by Holohan et al.
Paper link: https://arxiv.org/pdf/1402.6124.pdf
"""
def __init__(self):
super().__init__()
self._sensitivity = None
def __repr__(self):
output = super().__repr__()
@copy_docstring(DPMechanism.get_variance)
def get_variance(self, value):
pass
# 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 binary mechanism for differential privacy.
"""
import numpy as np
from numpy.random import random
from diffprivlib.mechanisms.base import DPMechanism
class Binary(DPMechanism):
"""The classic binary mechanism in differential privacy.
Given a binary input value, the mechanism randomly decides to flip to the other binary value or not, in order to
satisfy differential privacy.
Paper link: https://arxiv.org/pdf/1612.05568.pdf
Notes
-----
* The binary attributes, known as `labels`, must be specified as strings. If non-string labels are required (e.g.
integer-valued labels), a :class:`.DPTransformer` can be used (e.g. :class:`.IntToString`).
"""
def __init__(self):
super().__init__()
self._value0 = None
self._value1 = None
# 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 vector mechanism in differential privacy, for producing perturbed objectives
"""
from numbers import Real
import numpy as np
from diffprivlib.mechanisms.base import DPMechanism
class Vector(DPMechanism):
"""
The vector mechanism in differential privacy.
The vector mechanism is used when perturbing convex objective functions.
Full paper: http://www.jmlr.org/papers/volume12/chaudhuri11a/chaudhuri11a.pdf
"""
def __init__(self):
super().__init__()
self._function_sensitivity = None
self._data_sensitivity = 1
self._vector_dim = None
self._alpha = 0.01
def __repr__(self):
output = super().__repr__()
output += ".set_alpha(" + str(self._alpha) + ")" if self._alpha != 0.01 else ""
# 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 numpy.random import random
from diffprivlib.mechanisms.base import DPMechanism, TruncationAndFoldingMixin
from diffprivlib.utils import copy_docstring
class Geometric(DPMechanism):
"""
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
"""
def __init__(self):
super().__init__()
self._sensitivity = None
self._scale = None
def __repr__(self):
output = super().__repr__()
output += ".set_sensitivity(" + str(self._sensitivity) + ")" if self._sensitivity is not None else ""
@copy_docstring(DPMechanism.get_bias)
def get_bias(self, value):
pass
# 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 numpy.random import random
from diffprivlib.mechanisms.base import DPMechanism
from diffprivlib.mechanisms.laplace import Laplace
from diffprivlib.utils import copy_docstring
class Uniform(DPMechanism):
"""
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
"""
def __init__(self):
super().__init__()
self._sensitivity = None
def __repr__(self):
output = super().__repr__()
output += ".set_sensitivity(" + str(self._sensitivity) + ")" if self._sensitivity is not None else ""
return output
# 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 Wishart mechanism in differential privacy, for producing positive semi-definite perturbed second-moment matrices
"""
from numbers import Real
import numpy as np
from diffprivlib.mechanisms.base import DPMechanism
class Wishart(DPMechanism):
"""
The Wishart mechanism in differential privacy.
Used to achieve differential privacy on 2nd moment matrices.
Paper link: https://ieeexplore.ieee.org/abstract/document/7472095/
"""
def __init__(self):
super().__init__()
self._sensitivity = None
def __repr__(self):
output = super().__repr__()
output += ".set_sensitivity(" + str(self._sensitivity) + ")" if self._sensitivity is not None else ""
return output
delta : float
The value of delta for achieving :math:`(\epsilon,\delta)`-differential privacy with the mechanism. Must
have `0 < delta < 0.5`.
Returns
-------
self : class
"""
if epsilon == 0:
raise ValueError("Epsilon must be strictly positive. For zero epsilon, use :class:`.Uniform`.")
if isinstance(delta, Real) and not 0 < delta < 0.5:
raise ValueError("Delta must be strictly in (0,0.5). For zero delta, use :class:`.Laplace`.")
return DPMechanism.set_epsilon_delta(self, epsilon, delta)
# SOFTWARE.
"""
The classic Gaussian mechanism in differential privacy, and its derivatives.
"""
from math import erf
from numbers import Real
import numpy as np
from numpy.random import random
from diffprivlib.mechanisms.base import DPMechanism
from diffprivlib.mechanisms.laplace import Laplace
from diffprivlib.utils import copy_docstring
class Gaussian(DPMechanism):
"""The Gaussian mechanism in differential privacy.
As first proposed by Dwork and Roth in "The algorithmic foundations of differential privacy".
Paper link: https://www.nowpublishers.com/article/DownloadSummary/TCS-042
"""
def __init__(self):
super().__init__()
self._sensitivity = None
self._scale = None
self._stored_gaussian = None
def __repr__(self):
output = super().__repr__()
output += ".set_sensitivity(" + str(self._sensitivity) + ")" if self._sensitivity is not None else ""