Example #1
0
 def test_anonymize_proportion_with_budget_multiple(self):
     expected_values = np.array([87.0586455, 435.2701297])
     n = 10.0
     epsilon = 1.0
     self.set_seed()
     anonymized = DiffPrivLaplaceMechanism.anonymize_proportion_with_budget(
         [87.0, 435.0], n, epsilon)
     np.testing.assert_almost_equal(anonymized, expected_values)
Example #2
0
 def test_anonymize_proportion_with_budget_single(self):
     expected_value = 87.05864551385037
     n = 10.0
     epsilon = 1.0
     self.set_seed()
     anonymized = DiffPrivLaplaceMechanism.anonymize_proportion_with_budget(
         87.0, n, epsilon)
     np.testing.assert_almost_equal(anonymized, expected_value)
Example #3
0
 def test_anonymize_proportion_with_budget_single_many(self):
     expected_values = np.array([87.0586455, 87.2701297, 86.9451988])
     n = 10.0
     epsilon = 1.0
     self.set_seed()
     anonymized = DiffPrivLaplaceMechanism.anonymize_proportion_with_budget(
         87.0, n, epsilon, size=3)
     np.testing.assert_almost_equal(anonymized, expected_values)
Example #4
0
    def proportion(cls,
                   data,
                   epsilon,
                   condition=None,
                   axis=None,
                   postprocess=True):
        """
        Performs the proportion operation and anonymizes the value(s) using the
        provided privacy budget.

        The operation counts non-zero or non-False values. When a condition
        function is provided, it is invoked so that the data can be transformed
        before counting. Afterwords, the proportion is calculated using the total
        amount of values.

        Parameters
        ----------
        data : list|ndarray
            The data to retrieve the proportion(s) from.
        epsilon : float
            The privacy budget.
        [condition] : function
            A condition function that receives the data as a parameter and returns
            a list or ndarray of the same size in which for each element is either
            a zero or False when it should not be counted and non-zero or True when
            it should.
        [axis] : int|tuple
            Axis or tuple of axes along which to count non-zeros or non-False value(s).
        [postprocess] : bool
            Indicates whether or not to post-process the proportion values so that the
            returned value is within the [0.0, 1.0] range.

        Returns
        -------
        float|ndarray
            The anonymized count(s).

        """
        n = np.size(data, axis=axis)
        if condition:
            data = condition(data)

        value = np.count_nonzero(data, axis=axis)
        value = np.divide(value, n)
        anonymized = DiffPrivLaplaceMechanism.anonymize_proportion_with_budget(
            value, n, epsilon)

        if postprocess:
            anonymized = np.clip(anonymized, 0.0, 1.0)

        return anonymized