拉普拉斯分布(也称双指数分布)
绘制laplace pdf 曲线:
import matplotlib.pyplot as plt
import numpy as np
def laplace_pdf(x, b, μ):
return (1 / (2 * b)) * np.e ** (-1 * (np.abs(x - μ) / b))
x = np.linspace(-10, 10, 10000)
y1 = [laplace_pdf(x_, 1, 0) for x_ in x]
y2 = [laplace_pdf(x_, 2, 0) for x_ in x]
y3 = [laplace_pdf(x_, 4, 0) for x_ in x]
y4 = [laplace_pdf(x_, 4, -5) for x_ in x]
plt.plot(x, y1, label="μ=0,b=1")
plt.plot(x, y2, label="μ=0,b=2")
plt.plot(x, y3, label="μ=0,b=4")
plt.plot(x, y4, label="μ=-5,b=4")
plt.title("Laplace pdf curves")
plt.legend()
plt.show()
生成拉普拉斯噪声后的数据:
import numpy as np
def add_laplace_noise(data_list, μ=0, b=1):
laplace_noise = np.random.laplace(μ, b, len(data_list)) # 为原始数据添加μ为0,b为1的噪声
return laplace_noise + data_list
data = np.random.uniform(0, 100, 400)
print("原始无噪声数据|均值:" + str(data.mean()) + " 方差:" + str(data.std()))
noise_list = add_laplace_noise(data)
print("加噪声后的数据|均值:" + str(noise_list.mean()) + " 方差:" + str(noise_list.std()))