Beispiel #1
0
 def test_anonymize_mean_with_budget_multiple(self):
     expected_values = np.array([87.5219451, 437.4041544])
     lower = 10.0
     upper = 99.0
     n = 100.0
     epsilon = 1.0
     self.set_seed()
     anonymized = DiffPrivLaplaceMechanism.anonymize_mean_with_budget(
         [87.0, 435.0], lower, upper, n, epsilon)
     np.testing.assert_almost_equal(anonymized, expected_values)
Beispiel #2
0
 def test_anonymize_mean_with_budget_single(self):
     expected_value = 87.52194507326827
     lower = 10.0
     upper = 99.0
     n = 100.0
     epsilon = 1.0
     self.set_seed()
     anonymized = DiffPrivLaplaceMechanism.anonymize_mean_with_budget(
         87.0, lower, upper, n, epsilon)
     np.testing.assert_almost_equal(anonymized, expected_value)
Beispiel #3
0
 def test_anonymize_mean_with_budget_single_many(self):
     expected_values = np.array([87.5219451, 89.4041544, 86.5122696])
     lower = 10.0
     upper = 99.0
     n = 100.0
     epsilon = 1.0
     self.set_seed()
     anonymized = DiffPrivLaplaceMechanism.anonymize_mean_with_budget(
         87.0, lower, upper, n, epsilon, size=3)
     np.testing.assert_almost_equal(anonymized, expected_values)
Beispiel #4
0
    def mean(cls, data, epsilon, axis=None):
        """
        Performs the mean operation and anonymizes the value(s) using the provided
        privacy budget.

        Parameters
        ----------
        data : list|ndarray
            The data to retrieve the mean value(s) from.
        epsilon : float
            The privacy budget.
        [axis] : int|tuple
            Axis or tuple of axes along which to obtain the mean value(s).

        Returns
        -------
        float|ndarray
            The anonymized mean value(s).

        """
        n = np.size(data, axis=axis)
        lower = np.min(data, axis=axis)
        upper = np.max(data, axis=axis)
        value = np.mean(data, axis=axis)
        if np.isscalar(lower):
            anonymized = DiffPrivLaplaceMechanism.anonymize_mean_with_budget(
                value, lower, upper, n, epsilon)
        else:
            size = lower.size
            anonymized = np.zeros(size)
            for index in range(0, size):
                mean_value = DiffPrivLaplaceMechanism.anonymize_mean_with_budget(
                    value[index], lower[index], upper[index], n, epsilon)
                anonymized[index] = mean_value

        return anonymized