def test_anonymize_median_single_many(self): expected_values = np.array([87.5864551, 89.701297, 86.4519884]) epsilon = 1.0 anonymizer = DiffPrivLaplaceMechanism(epsilon) self.set_seed() anonymized = anonymizer.anonymize_median(87.0, size=3) np.testing.assert_almost_equal(anonymized, expected_values)
def test_anonymize_median_multiple(self): expected_values = np.array([87.5864551, 437.701297]) epsilon = 1.0 anonymizer = DiffPrivLaplaceMechanism(epsilon) self.set_seed() anonymized = anonymizer.anonymize_median([87.0, 435.0]) np.testing.assert_almost_equal(anonymized, expected_values)
def test_anonymize_median_single(self): expected_value = 87.58645513850368 epsilon = 1.0 anonymizer = DiffPrivLaplaceMechanism(epsilon) self.set_seed() anonymized = anonymizer.anonymize_median(87.0) np.testing.assert_almost_equal(anonymized, expected_value)
def sum(cls, data, epsilon, axis=None): """ Performs the sum operation and anonymizes the value(s) using the provided privacy budget. Parameters ---------- data : list|ndarray The data to retrieve the sum value(s) from. epsilon : float The privacy budget. [axis] : int|tuple Axis or tuple of axes along which to obtain the sum value(s). Returns ------- float|ndarray The anonymized sum value(s). """ lower = np.min(data, axis=axis) upper = np.max(data, axis=axis) value = np.sum(data, axis=axis) if np.isscalar(lower): anonymized = DiffPrivLaplaceMechanism.anonymize_sum_with_budget( value, lower, upper, epsilon) else: size = lower.size anonymized = np.zeros(size) for index in range(0, size): anonymized[ index] = DiffPrivLaplaceMechanism.anonymize_sum_with_budget( value[index], lower[index], upper[index], epsilon) return anonymized
def test_anonymize_proportion_single(self): expected_value = 87.05864551385037 n = 10.0 epsilon = 1.0 anonymizer = DiffPrivLaplaceMechanism(epsilon) self.set_seed() anonymized = anonymizer.anonymize_proportion(87.0, n) np.testing.assert_almost_equal(anonymized, expected_value)
def test_anonymize_proportion_multiple(self): expected_values = np.array([87.0586455, 435.2701297]) n = 10.0 epsilon = 1.0 anonymizer = DiffPrivLaplaceMechanism(epsilon) self.set_seed() anonymized = anonymizer.anonymize_proportion([87.0, 435.0], n) np.testing.assert_almost_equal(anonymized, expected_values)
def test_anonymize_proportion_single_many(self): expected_values = np.array([87.0586455, 87.2701297, 86.9451988]) n = 10.0 epsilon = 1.0 anonymizer = DiffPrivLaplaceMechanism(epsilon) self.set_seed() anonymized = anonymizer.anonymize_proportion(87.0, n, size=3) np.testing.assert_almost_equal(anonymized, expected_values)
def test_anonymize_sum_multiple(self): expected_values = np.array([145.0590587, 702.4284063]) lower = 10.0 upper = 99.0 epsilon = 1.0 anonymizer = DiffPrivLaplaceMechanism(epsilon) self.set_seed() anonymized = anonymizer.anonymize_sum([87.0, 435.0], lower, upper) np.testing.assert_almost_equal(anonymized, expected_values)
def test_anonymize_sum_single_many(self): expected_values = np.array([145.0590587, 354.4284063, 32.746848]) lower = 10.0 upper = 99.0 epsilon = 1.0 anonymizer = DiffPrivLaplaceMechanism(epsilon) self.set_seed() anonymized = anonymizer.anonymize_sum(87.0, lower, upper, size=3) np.testing.assert_almost_equal(anonymized, expected_values)
def test_anonymize_sum_single(self): expected_value = 145.05905871186388 lower = 10.0 upper = 99.0 epsilon = 1.0 anonymizer = DiffPrivLaplaceMechanism(epsilon) self.set_seed() anonymized = anonymizer.anonymize_sum(87.0, lower, upper) np.testing.assert_almost_equal(anonymized, expected_value)
def test_anonymize_mean_multiple(self): expected_values = np.array([87.5219451, 437.4041544]) lower = 10.0 upper = 99.0 n = 100.0 epsilon = 1.0 anonymizer = DiffPrivLaplaceMechanism(epsilon) self.set_seed() anonymized = anonymizer.anonymize_mean([87.0, 435.0], lower, upper, n) np.testing.assert_almost_equal(anonymized, expected_values)
def test_anonymize_mean_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 anonymizer = DiffPrivLaplaceMechanism(epsilon) self.set_seed() anonymized = anonymizer.anonymize_mean(87.0, lower, upper, n, size=3) np.testing.assert_almost_equal(anonymized, expected_values)
def test_anonymize_mean_single(self): expected_value = 87.52194507326827 lower = 10.0 upper = 99.0 n = 100.0 epsilon = 1.0 anonymizer = DiffPrivLaplaceMechanism(epsilon) self.set_seed() anonymized = anonymizer.anonymize_mean(87.0, lower, upper, n) np.testing.assert_almost_equal(anonymized, expected_value)
def test_anonymize_variance_single(self): expected_value = 133.45311152087612 lower = 10.0 upper = 99.0 n = 100.0 epsilon = 1.0 anonymizer = DiffPrivLaplaceMechanism(epsilon) self.set_seed() anonymized = anonymizer.anonymize_variance(87.0, lower, upper, n) np.testing.assert_almost_equal(anonymized, expected_value)
def test_anonymize_variance_multiple(self): expected_values = np.array([133.4531115, 648.969738]) lower = 10.0 upper = 99.0 n = 100.0 epsilon = 1.0 anonymizer = DiffPrivLaplaceMechanism(epsilon) self.set_seed() anonymized = anonymizer.anonymize_variance([87.0, 435.0], lower, upper, n) np.testing.assert_almost_equal(anonymized, expected_values)
def test_anonymize_variance_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 anonymizer = DiffPrivLaplaceMechanism(epsilon) self.set_seed() anonymized = anonymizer.anonymize_variance(87.0, lower, upper, n, size=3) np.testing.assert_almost_equal(anonymized, expected_values)
def test_anonymize_max_with_budget_single_many(self): expected_values = np.array([87.5864551, 89.701297, 86.4519884]) epsilon = 1.0 self.set_seed() anonymized = DiffPrivLaplaceMechanism.anonymize_max_with_budget( 87.0, epsilon, size=3) np.testing.assert_almost_equal(anonymized, expected_values)
def test_anonymize_max_with_budget_multiple(self): expected_values = np.array([87.5864551, 437.701297]) epsilon = 1.0 self.set_seed() anonymized = DiffPrivLaplaceMechanism.anonymize_max_with_budget( [87.0, 435.0], epsilon) np.testing.assert_almost_equal(anonymized, expected_values)
def test_anonymize_median_with_budget_single(self): expected_value = 87.58645513850368 epsilon = 1.0 self.set_seed() anonymized = DiffPrivLaplaceMechanism.anonymize_median_with_budget( 87.0, epsilon) np.testing.assert_almost_equal(anonymized, expected_value)
def test_create_proportion_anonymizer(self): epsilon = 0.1 n = 10.0 anonymizer = DiffPrivLaplaceMechanism.create_proportion_anonymizer( epsilon, n) self.assertIsInstance(anonymizer, DiffPrivProportionAnonymizer) self.assertEqual(anonymizer.epsilon, epsilon) self.assertEqual(anonymizer.n, n)
def test_create_sum_anonymizer(self): epsilon = 1.0 lower = 10.0 upper = 99.0 anonymizer = DiffPrivLaplaceMechanism.create_sum_anonymizer( epsilon, lower, upper) self.assertIsInstance(anonymizer, DiffPrivSumAnonymizer) self.assertEqual(anonymizer.epsilon, epsilon) self.assertEqual(anonymizer.lower, lower) self.assertEqual(anonymizer.upper, upper)
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
def min(cls, data, epsilon, axis=None): """ Performs the min operation and anonymizes the value(s) using the provided privacy budget. Parameters ---------- data : list|ndarray The data to retrieve the min value(s) from. epsilon : float The privacy budget. [axis] : int|tuple Axis or tuple of axes along which to obtain the min value(s). Returns ------- float|ndarray The anonymized min value(s). """ value = np.min(data, axis=axis) anonymized = DiffPrivLaplaceMechanism.anonymize_min_with_budget( value, epsilon) return anonymized
def test_epsilon_getter(self): epsilon = 0.1 anonymizer = DiffPrivLaplaceMechanism(epsilon) self.assertEqual(anonymizer.epsilon, epsilon)
def test_create_median_anonymizer(self): epsilon = 0.1 anonymizer = DiffPrivLaplaceMechanism.create_median_anonymizer(epsilon) self.assertIsInstance(anonymizer, DiffPrivMedianAnonymizer) self.assertEqual(anonymizer.epsilon, epsilon)