def test_compute_dp_sum(self): params = dp_computations.MeanVarParams( eps=0.5, delta=1e-10, min_value=2, max_value=3, max_partitions_contributed=1, max_contributions_per_partition=1, noise_kind=NoiseKind.LAPLACE) l0_sensitivity = params.l0_sensitivity() linf_sensitivity = params.max_contributions_per_partition * max( params.min_value, params.max_value) # Laplace Mechanism l1_sensitivity = dp_computations.compute_l1_sensitivity( l0_sensitivity, linf_sensitivity) results = [ dp_computations.compute_dp_sum(sum=10, dp_params=params) for _ in range(N_ITERATIONS) ] self._test_laplace_noise(results, 10, params.eps, l1_sensitivity) # Gaussian Mechanism params.noise_kind = NoiseKind.GAUSSIAN l2_sensitivity = dp_computations.compute_l2_sensitivity( l0_sensitivity, linf_sensitivity) results = [ dp_computations.compute_dp_sum(sum=10, dp_params=params) for _ in range(N_ITERATIONS) ] self._test_gaussian_noise(results, 10, params.eps, params.delta, l2_sensitivity)
def test_compute_dp_var(self): params = dp_computations.MeanVarParams( eps=10, delta=1e-10, min_value=1, max_value=20, max_partitions_contributed=1, max_contributions_per_partition=1, noise_kind=NoiseKind.LAPLACE) (count_eps, count_delta), (_, _), (_, _) = dp_computations.equally_split_budget( params.eps, params.delta, 3) l0_sensitivity = params.l0_sensitivity() count_linf_sensitivity = params.max_contributions_per_partition # Laplace Mechanism results = [ dp_computations.compute_dp_var(count=100000, sum=1000000, sum_squares=20000000, dp_params=params) for _ in range(N_ITERATIONS) ] count_values, sum_values, sum_squares_values, var_values = zip( *results) self._test_laplace_noise( count_values, 100000, count_eps, dp_computations.compute_l1_sensitivity(l0_sensitivity, count_linf_sensitivity)) self.assertAlmostEqual(np.mean(sum_values), 1000000, delta=1) self.assertAlmostEqual(np.mean(sum_squares_values), 20000000, delta=2) self.assertAlmostEqual(np.mean(var_values), 100, delta=0.1) # Gaussian Mechanism params.noise_kind = NoiseKind.GAUSSIAN results = [ dp_computations.compute_dp_var(count=100000, sum=1000000, sum_squares=20000000, dp_params=params) for _ in range(N_ITERATIONS) ] count_values, sum_values, sum_squares_values, var_values = zip( *results) self._test_gaussian_noise( count_values, 100000, count_eps, count_delta, dp_computations.compute_l2_sensitivity(l0_sensitivity, count_linf_sensitivity)) self.assertAlmostEqual(np.mean(sum_values), 1000000, delta=5) self.assertAlmostEqual(np.mean(sum_squares_values), 20000000, delta=5) self.assertAlmostEqual(np.mean(var_values), 100, delta=0.5)
def test_compute_dp_sum(self, bound_per_partition): min_value = max_value = min_sum_per_partition = max_sum_per_partition = None if bound_per_partition: min_sum_per_partition, max_sum_per_partition = 2, 3 else: min_value, max_value = 2, 3 params = dp_computations.ScalarNoiseParams( eps=0.5, delta=1e-10, min_value=min_value, max_value=max_value, min_sum_per_partition=min_sum_per_partition, max_sum_per_partition=max_sum_per_partition, max_partitions_contributed=1, max_contributions_per_partition=1, noise_kind=NoiseKind.LAPLACE) l0_sensitivity = params.l0_sensitivity() if bound_per_partition: linf_sensitivity = params.max_contributions_per_partition * max( params.min_sum_per_partition, params.max_sum_per_partition) else: # bound per contribution linf_sensitivity = params.max_contributions_per_partition * max( params.min_value, params.max_value) # Laplace Mechanism l1_sensitivity = dp_computations.compute_l1_sensitivity( l0_sensitivity, linf_sensitivity) results = [ dp_computations.compute_dp_sum(sum=10, dp_params=params) for _ in range(N_ITERATIONS) ] self._test_laplace_noise(results=results, num_trials=N_ITERATIONS, expected_mean=10, eps=params.eps, l1_sensitivity=l1_sensitivity) # Gaussian Mechanism params.noise_kind = NoiseKind.GAUSSIAN l2_sensitivity = dp_computations.compute_l2_sensitivity( l0_sensitivity, linf_sensitivity) results = [ dp_computations.compute_dp_sum(sum=10, dp_params=params) for _ in range(N_ITERATIONS) ] self._test_gaussian_noise(results=results, expected_mean=10, num_trials=N_ITERATIONS, eps=params.eps, delta=params.delta, l2_sensitivity=l2_sensitivity)
def test_compute_dp_count(self): params = dp_computations.ScalarNoiseParams( eps=0.5, delta=1e-10, min_value=0, max_value=0, min_sum_per_partition=None, max_sum_per_partition=None, max_partitions_contributed=1, max_contributions_per_partition=1, noise_kind=NoiseKind.LAPLACE) l0_sensitivity = params.l0_sensitivity() linf_sensitivity = params.max_contributions_per_partition # Laplace Mechanism l1_sensitivity = dp_computations.compute_l1_sensitivity( l0_sensitivity, linf_sensitivity) results = [ dp_computations.compute_dp_count(count=10, dp_params=params) for _ in range(N_ITERATIONS) ] self._test_laplace_noise(results=results, num_trials=N_ITERATIONS, expected_mean=10, eps=params.eps, l1_sensitivity=l1_sensitivity) # Gaussian Mechanism params.noise_kind = NoiseKind.GAUSSIAN l2_sensitivity = dp_computations.compute_l2_sensitivity( l0_sensitivity, linf_sensitivity) results = [ dp_computations.compute_dp_count(count=10, dp_params=params) for _ in range(N_ITERATIONS) ] self._test_gaussian_noise(results=results, num_trials=N_ITERATIONS, expected_mean=10, eps=params.eps, delta=params.delta, l2_sensitivity=l2_sensitivity)
def test_l2_sensitivity(self): self.assertAlmostEqual(dp_computations.compute_l2_sensitivity( l0_sensitivity=4.5, linf_sensitivity=12.123), 25.716766525, delta=0.1)
def test_compute_dp_var(self): params = dp_computations.ScalarNoiseParams( eps=10, delta=1e-10, min_value=1, max_value=20, min_sum_per_partition=None, max_sum_per_partition=None, max_partitions_contributed=1, max_contributions_per_partition=1, noise_kind=NoiseKind.LAPLACE) (count_eps, count_delta), (_, _), (_, _) = dp_computations.equally_split_budget( params.eps, params.delta, 3) l0_sensitivity = params.l0_sensitivity() count_linf_sensitivity = params.max_contributions_per_partition expected_count = 100000 expected_sum = 1000000 expected_mean = 10 expected_var = 100 normalized_sum = -50000 normalized_sum_squares = 10025000 # sum of squares = 20000000 # Laplace Mechanism results = [ dp_computations.compute_dp_var( count=expected_count, normalized_sum=normalized_sum, normalized_sum_squares=normalized_sum_squares, dp_params=params) for _ in range(N_ITERATIONS) ] count_values, sum_values, mean_values, var_values = zip(*results) self._test_laplace_noise( results=count_values, num_trials=N_ITERATIONS, expected_mean=100000, eps=count_eps, l1_sensitivity=dp_computations.compute_l1_sensitivity( l0_sensitivity, count_linf_sensitivity)) self.assertAlmostEqual(np.mean(sum_values), expected_sum, delta=1) self.assertAlmostEqual(np.mean(mean_values), expected_mean, delta=0.00003) self.assertAlmostEqual(np.mean(var_values), expected_var, delta=0.1) # Gaussian Mechanism params.noise_kind = NoiseKind.GAUSSIAN results = [ dp_computations.compute_dp_var( count=expected_count, normalized_sum=normalized_sum, normalized_sum_squares=normalized_sum_squares, dp_params=params) for _ in range(N_ITERATIONS) ] count_values, sum_values, mean_values, var_values = zip(*results) self._test_gaussian_noise( results=count_values, num_trials=N_ITERATIONS, expected_mean=100000, eps=count_eps, delta=count_delta, l2_sensitivity=dp_computations.compute_l2_sensitivity( l0_sensitivity, count_linf_sensitivity)) self.assertAlmostEqual(np.mean(sum_values), expected_sum, delta=5) self.assertAlmostEqual(np.mean(mean_values), expected_mean, delta=0.0002) self.assertAlmostEqual(np.mean(var_values), expected_var, delta=0.5)