expt_lmdk_sel: Testing the Pareto principle
This commit is contained in:
		@ -62,6 +62,47 @@ def exponential(x, R, u, delta, epsilon):
 | 
			
		||||
    return np.array([]), pr
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
'''
 | 
			
		||||
  The exponential mechanism.
 | 
			
		||||
 | 
			
		||||
  Parameters:
 | 
			
		||||
    x - The data.
 | 
			
		||||
    R - The possible outputs.
 | 
			
		||||
    u - The scoring function.
 | 
			
		||||
    delta - The sensitivity of the scoring function.
 | 
			
		||||
    epsilon - The privacy budget.
 | 
			
		||||
  Returns:
 | 
			
		||||
    res - A randomly sampled output.
 | 
			
		||||
    pr - The PDF of all possible outputs.
 | 
			
		||||
'''
 | 
			
		||||
def exponential_pareto(x, R, u, delta, epsilon):
 | 
			
		||||
  # Calculate the score for each element of R
 | 
			
		||||
  scores = [u(x, r) for r in R]
 | 
			
		||||
  # Keep the top 20%
 | 
			
		||||
  n = int(len(scores)*.2)
 | 
			
		||||
  scores = np.sort(scores)[-n : ]
 | 
			
		||||
  # Normalize the scores between 0 and 1
 | 
			
		||||
  # (the higher, the better the utility)
 | 
			
		||||
  scores = 1 - (scores - np.min(scores))/(np.max(scores) - np.min(scores))
 | 
			
		||||
 | 
			
		||||
  # Calculate the probability for each element, based on its score
 | 
			
		||||
  pr = [np.exp(epsilon*score/(2*delta)) for score in scores]
 | 
			
		||||
 | 
			
		||||
  # Normalize the probabilities so that they sum to 1
 | 
			
		||||
  pr = pr/np.linalg.norm(pr, ord = 1)
 | 
			
		||||
 | 
			
		||||
  # Debugging
 | 
			
		||||
  # print(R[np.argmax(pr)], pr.max(), scores[np.argmax(pr)])
 | 
			
		||||
  # print(R[np.argmin(pr)], pr.min(), scores[np.argmin(pr)])
 | 
			
		||||
  # print(abs(pr.max() - pr.min()), abs(scores[np.argmax(pr)] - scores[np.argmin(pr)]))
 | 
			
		||||
 | 
			
		||||
  # Choose an element from R based on the probabilities
 | 
			
		||||
  if len(pr) > 0:
 | 
			
		||||
    return R[np.random.choice(range(n), 1, p = pr)[0]], pr
 | 
			
		||||
  else:
 | 
			
		||||
    return np.array([]), pr
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def main():
 | 
			
		||||
  start, end = 1.0, 10.0
 | 
			
		||||
  scale = 1.0
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user