Exemple #1
0
 def test_anonymize_count_with_budget_multiple(self):
     expected_values = np.array([87.5864551, 437.701297])
     epsilon = 1.0
     self.set_seed()
     anonymized = DiffPrivLaplaceMechanism.anonymize_count_with_budget(
         [87.0, 435.0], epsilon)
     np.testing.assert_almost_equal(anonymized, expected_values)
Exemple #2
0
 def test_anonymize_count_with_budget_single(self):
     expected_value = 87.58645513850368
     epsilon = 1.0
     self.set_seed()
     anonymized = DiffPrivLaplaceMechanism.anonymize_count_with_budget(
         87.0, epsilon)
     np.testing.assert_almost_equal(anonymized, expected_value)
Exemple #3
0
 def test_anonymize_count_with_budget_single_many(self):
     expected_values = np.array([87.5864551, 89.701297, 86.4519884])
     epsilon = 1.0
     self.set_seed()
     anonymized = DiffPrivLaplaceMechanism.anonymize_count_with_budget(
         87.0, epsilon, size=3)
     np.testing.assert_almost_equal(anonymized, expected_values)
Exemple #4
0
    def count(cls, data, epsilon, condition=None, axis=None, postprocess=True):
        """
        Performs the count 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.

        Parameters
        ----------
        data : list|ndarray
            The data to retrieve the count(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 count values so that the
            returned value is rounded and within the [0, n] range, where n is the total
            number of observations.

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

        """
        if condition:
            data = condition(data)

        value = np.count_nonzero(data, axis=axis)
        anonymized = DiffPrivLaplaceMechanism.anonymize_count_with_budget(
            value, epsilon)

        if postprocess:
            n = np.size(data, axis=axis)
            anonymized = np.clip(anonymized, 0.0, n)
            anonymized = np.round(anonymized)

        return anonymized