Created
January 13, 2026 00:26
-
-
Save thiagobutignon/2bc2763e01d4d000daa9ac15d574c199 to your computer and use it in GitHub Desktop.
example-1-la-place-algorithm-differential-privacy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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