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_sum_with_budget_multiple(self): expected_values = np.array([145.0590587, 702.4284063]) lower = 10.0 upper = 99.0 epsilon = 1.0 self.set_seed() anonymized = DiffPrivLaplaceMechanism.anonymize_sum_with_budget( [87.0, 435.0], lower, upper, epsilon) np.testing.assert_almost_equal(anonymized, expected_values)
def test_anonymize_sum_with_budget_single_many(self): expected_values = np.array([145.0590587, 354.4284063, 32.746848]) lower = 10.0 upper = 99.0 epsilon = 1.0 self.set_seed() anonymized = DiffPrivLaplaceMechanism.anonymize_sum_with_budget( 87.0, lower, upper, epsilon, size=3) np.testing.assert_almost_equal(anonymized, expected_values)
def test_anonymize_sum_with_budget_single(self): expected_value = 145.05905871186388 lower = 10.0 upper = 99.0 epsilon = 1.0 self.set_seed() anonymized = DiffPrivLaplaceMechanism.anonymize_sum_with_budget( 87.0, lower, upper, epsilon) np.testing.assert_almost_equal(anonymized, expected_value)