Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save thiagobutignon/2bc2763e01d4d000daa9ac15d574c199 to your computer and use it in GitHub Desktop.

Select an option

Save thiagobutignon/2bc2763e01d4d000daa9ac15d574c199 to your computer and use it in GitHub Desktop.
example-1-la-place-algorithm-differential-privacy
import opendp as dp
import pandas as pd
import numpy as np
# Create sample customer dataset
customers = pd.DataFrame({
'age': [25, 34, 45, 29, 52, 38, 41, 33, 47, 36],
'purchase_amount': [150, 320, 890, 200, 450, 275, 680, 180, 520, 340]
})
# Configure privacy parameters
epsilon = 1.0 # Privacy budget
sensitivity = 1 # For count queries
# Create differentially private count query
def private_count(data, epsilon, sensitivity):
true_count = len(data)
noise_scale = sensitivity / epsilon
noise = np.random.laplace(0, noise_scale)
private_result = true_count + noise
return max(0, round(private_result))
# Execute private count query
true_customer_count = len(customers)
private_customer_count = private_count(customers, epsilon, sensitivity)
print(f"True count: {true_customer_count}")
print(f"Private count: {private_customer_count}")
print(f"Privacy budget consumed: {epsilon}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment