1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| import numpy as np import matplotlib.pyplot as plt from scipy.stats import gaussian_kde
def add_noise(data, epsilon, delta, sensitivity): beta = sensitivity / epsilon noise = np.random.laplace(0, beta / delta, len(data)) return data + noise
original_data = np.random.normal(loc=50, scale=100, size=500)
epsilon = 0.8
delta = 0.01
sensitivity = 0.2 noisy_data = add_noise(original_data, epsilon, delta, sensitivity)
print("Original data:", original_data) print("Noisy data:", noisy_data) print(sum(original_data), sum(noisy_data))
original_density = gaussian_kde(original_data) original_x = np.linspace(np.min(original_data), np.max(original_data), 1000) original_y = original_density(original_x)
noisy_density = gaussian_kde(noisy_data) noisy_x = np.linspace(np.min(noisy_data), np.max(noisy_data), 1000) noisy_y = noisy_density(noisy_x)
plt.figure(figsize=(8, 6)) plt.plot(original_x, original_y, label='Original Data') plt.plot(noisy_x, noisy_y, label='Noisy Data') plt.xlabel('Value') plt.ylabel('Probability Density') plt.title('Data Distribution') plt.legend() plt.show()
|