Пример #1
0
 def test_anonymize_variance_with_budget_multiple(self):
     expected_values = np.array([133.4531115, 648.969738])
     lower = 10.0
     upper = 99.0
     n = 100.0
     epsilon = 1.0
     self.set_seed()
     anonymized = DiffPrivLaplaceMechanism.anonymize_variance_with_budget(
         [87.0, 435.0], lower, upper, n, epsilon)
     np.testing.assert_almost_equal(anonymized, expected_values)
Пример #2
0
 def test_anonymize_variance_with_budget_single(self):
     expected_value = 133.45311152087612
     lower = 10.0
     upper = 99.0
     n = 100.0
     epsilon = 1.0
     self.set_seed()
     anonymized = DiffPrivLaplaceMechanism.anonymize_variance_with_budget(
         87.0, lower, upper, n, epsilon)
     np.testing.assert_almost_equal(anonymized, expected_value)
Пример #3
0
 def test_anonymize_variance_with_budget_single_many(self):
     expected_values = np.array([133.4531115, 300.969738, 43.5919983])
     lower = 10.0
     upper = 99.0
     n = 100.0
     epsilon = 1.0
     self.set_seed()
     anonymized = DiffPrivLaplaceMechanism.anonymize_variance_with_budget(
         87.0, lower, upper, n, epsilon, size=3)
     np.testing.assert_almost_equal(anonymized, expected_values)
Пример #4
0
    def variance(cls, data, epsilon, axis=None):
        """
        Performs the variance operation and anonymizes the value(s) using the provided
        privacy budget.

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

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

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

        return anonymized