def test_sensitivity_norm_list_of_arrays(): class ReshapeToList(Query): def get(self, data): return list(np.reshape(data, (20, 30, 40))) distribution = NormalDistribution(0, 1.5) sampler = SensitivitySampler() # L1 norm: s_max, s_mean = sampler.sample_sensitivity(ReshapeToList(), L1SensitivityNorm(), distribution, n=20 * 30 * 40, m=285, gamma=0.33) assert isinstance(s_max, list) assert isinstance(s_mean, list) for i in range(len(s_max)): assert s_max[i].sum() < 2 * 1.5 assert s_mean[i].sum() < 2 * 1.5 # L2 norm: s_max, s_mean = sampler.sample_sensitivity(ReshapeToList(), L2SensitivityNorm(), distribution, n=20 * 30 * 40, m=285, gamma=0.33) assert isinstance(s_max, list) assert isinstance(s_mean, list) for i in range(len(s_max)): assert s_max[i].sum() < 2 * 1.5 assert s_mean[i].sum() < 2 * 1.5
def test_sample_sensitivity_gamma(): distribution = NormalDistribution(0, 1) sampler = SensitivitySampler() _, mean = sampler.sample_sensitivity(Mean(), L1SensitivityNorm(), distribution, n=100, gamma=0.33) assert np.abs(mean - 0) < 0.5
def test_laplace_mechanism(): data_size = 1000 array = NormalDistribution(175, 7).sample(data_size) federated_array = shfl.private.federated_operation.federate_array( array, data_size) federated_array.configure_data_access(LaplaceMechanism(1, 1)) result = federated_array.query() differences = 0 for i in range(data_size): if result[i] != array[i]: differences = differences + 1 assert differences == data_size assert np.mean(array) - np.mean(result) < 5
def test_normal_distribution(): data_size = 1000 array = NormalDistribution(175, 7).sample(data_size) assert len(array) == 1000 assert np.abs(np.mean(array) - 175) < 5