The Laplace mechanism
- Laplace mechanism: For continuous data, noise satisfying Laplace distribution is added to the original data set, and the output result is denoised data.
- Index mechanism: For discrete data, returns a certain data in a discrete set with a certain probability.
Index mechanism Python code implementation
- A set of discrete data {R1,R2,.. Rn}. Different from Laplace mechanism, I want to output an exact value Ri in this range. The final output depends on the probability of each data, and the probability is related to privacy budget (EPslion), sensitivity and scoring function (q().
Here’s an example:
Now there will be a sporting event with four optional sports: football, volleyball, basketball and tennis. The audience voted for the four events, and then decided which one to host based on the results.
- Problem analysis: in this scenario, the number of votes of users can be used as a scoring function, assuming that the votes of the current four projects are {30,25,8,2} respectively.
- Input: scoring function {30,25,8,2}, privacy budget (epslion), sensitivity.
- Output: The probability of each project being selected as the final project.
- Python code
import numpy as np
def expMechanism(scores,eps,sensitivity) :
for e in eps:
probability0 = []
probability1 = []
for score in scores:
probability0.append(np.exp(0.5*e*score/sensitivity))
sum = np.sum(probability0)
# Normalized processing
for i in range(len(probability0)):
probability1.append(probability0[i]/sum)
print("when epslion={},probability1:".format(e))
print(probability1)
if __name__ == '__main__':
# The value of the scoring function
scores = [30.25.8.2]
# Privacy Budget EPslion
eps = [0.0.1.1]
sensitivity = 1
expMechanism(scores,eps,sensitivity)
Copy the code
- The output