Skip to content

Instantly share code, notes, and snippets.

@tcbegley
Created September 1, 2022 12:38
Show Gist options
  • Save tcbegley/13483859eb07488d711368c982af5ded to your computer and use it in GitHub Desktop.
Save tcbegley/13483859eb07488d711368c982af5ded to your computer and use it in GitHub Desktop.
# Compute privacy curves for Gaussian mechanism
# cf. https://github.com/microsoft/prv_accountant/blob/main/notebooks/validate-gaussian.ipynb
import matplotlib.pyplot as plt
import numpy as np
from opacus.accountants import create_accountant
from scipy.stats import norm
MAX_COMPOSITIONS = 10_000
NOISE_MULTIPLIER = 100.0
SAMPLING_PROBABILITY = 1.0
DELTA_ERROR = 1e-10
def delta_exact(eps):
mu = np.sqrt(MAX_COMPOSITIONS) / NOISE_MULTIPLIER
return norm.cdf(mu / 2 - eps / mu) - np.exp(eps) * norm.cdf(-mu / 2 - eps / mu)
def compute_delta(accountant, eps, eps_error):
f_n = accountant._get_dprv(eps_error=eps_error, delta_error=DELTA_ERROR)
delta_lower = f_n.compute_delta_estimate(eps + eps_error) - DELTA_ERROR
delta_estim = f_n.compute_delta_estimate(eps)
delta_upper = f_n.compute_delta_estimate(eps - eps_error) + DELTA_ERROR
return (delta_lower, delta_estim, delta_upper)
if __name__ == "__main__":
epss = np.linspace(0.1, 5.0, 10)
accountant = create_accountant("prv")
accountant.history = [(NOISE_MULTIPLIER, SAMPLING_PROBABILITY, MAX_COMPOSITIONS)]
delta_01 = [compute_delta(accountant, eps, 0.1) for eps in epss]
delta_05 = [compute_delta(accountant, eps, 0.5) for eps in epss]
delta_1 = [compute_delta(accountant, eps, 1.0) for eps in epss]
f, ax = plt.subplots(figsize=(14, 8))
ax.fill_between(
epss,
[d_low for d_low, _, _ in delta_01],
[d_high for _, _, d_high in delta_01],
label="eps_error=0.1",
alpha=0.1,
)
ax.fill_between(
epss,
[d_low for d_low, _, _ in delta_05],
[d_high for _, _, d_high in delta_05],
label="eps_error=0.5",
alpha=0.1,
)
ax.fill_between(
epss,
[d_low for d_low, _, _ in delta_1],
[d_high for _, _, d_high in delta_1],
label="eps_error=1.0",
alpha=0.1,
)
ax.plot(epss, delta_exact(epss), color="tab:red", label="Exact")
ax.set_yscale("log")
ax.legend()
f.savefig("privacy-curve.png", dpi=100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment