4 "cell_type": "markdown",
8 "# Renyi Differential Privacy\n",
10 "By [Armaan Bhojwani](https://armaanb.net) under [Praneeth Vepakomma](https://praneeth.mit.edu/)\n",
12 "This notebook features some examples of using the dp_accounting Renyi accountant to translate between epsilon, delta and alpha, rho interpretations of Renyi DP, and for usk in compositions.\n",
23 "cell_type": "markdown",
32 "execution_count": 171,
37 "import numpy as np\n",
39 "# Privacy parameters\n",
40 "# (https://github.com/google/differential-privacy/blob/main/python/dp_accounting/rdp/rdp_privacy_accountant.py#L781)\n",
41 "custom_orders = False # Set to array of custom orders, otherwise use defaults\n",
43 "sensitivity = 99 # Sensitivity of the function\n",
44 "epsilon = 2 # Privacy garuntee\n",
47 "# Data parameters\n",
48 "data_len = 1500 # Length of dataset\n",
49 "data_low = 0 # Lowest value of dataset\n",
50 "data_high = 99 # Highest value of dataset\n",
52 "# Initialize Numpy RNG\n",
53 "rng = np.random.default_rng()\n",
55 "# Increment data_high so that it includes the value specified\n",
58 "# Create dataset as defined by above parameters\n",
59 "x = rng.integers(low=data_low, high=data_high, size=(data_len))"
63 "cell_type": "markdown",
72 "execution_count": 117,
78 "output_type": "stream",
80 "found noise multiplier: 2.382578592961857\n"
85 "import dp_accounting as dpa\n",
86 "import dp_accounting.rdp as rdpa\n",
88 "# noise_multiplier is the ratio of the standard deviation of the Gaussian\n",
89 "# noise to the l2-sensitivity of the function to which it is added\n",
90 "noise_multiplier = dpa.calibrate_dp_mechanism(rdpa.RdpAccountant,\n",
91 " dpa.GaussianDpEvent, epsilon,\n",
94 "print(f\"found noise multiplier: {noise_multiplier}\")"
99 "execution_count": 118,
105 "output_type": "stream",
107 "found epsilon: 1.9999992100966923\n",
108 "found delta: 1.0000000000000023e-06\n",
109 "────────────────────────────────────────\n",
110 "found alpha: 12.0\n",
111 "found rho: 1.0569556863430245\n"
116 "from common import *\n",
118 "accountant = rdpa.RdpAccountant(\n",
119 " orders=custom_orders) if custom_orders else rdpa.RdpAccountant()\n",
121 "event = dpa.GaussianDpEvent(noise_multiplier)\n",
122 "accountant = accountant.compose(event)\n",
124 "t_epsilon, alpha = accountant.get_epsilon_and_optimal_order(delta)\n",
125 "t_delta = accountant.get_delta(t_epsilon)\n",
127 "alpha_idx = np.where(accountant._orders == alpha)\n",
128 "rho = accountant._rdp[alpha_idx][0]\n",
130 "print(f\"found epsilon: {t_epsilon}\")\n",
131 "print(f\"found delta: {t_delta}\")\n",
133 "print(f\"found alpha: {alpha}\")\n",
134 "print(f\"found rho: {rho}\")"
139 "execution_count": 170,
145 "output_type": "stream",
147 "Using sigma: 2.382578592961857\n",
148 "non-private sum: 75550\n",
149 "private sum: 75548\n"
154 "import matplotlib.pyplot as plt\n",
156 "def gaussian_mech_RDP(x, sensitivity, alpha, rho, sigma=0):\n",
157 " sigma = np.sqrt((sensitivity**2 * alpha) / (2 * rho)) if sigma == 0 else sigma\n",
158 " print(f\"Using sigma: {sigma}\")\n",
159 " return x + np.random.normal(loc=0, scale=sigma)\n",
161 "# https://programming-dp.com/ch6.html#vector-valued-functions-and-their-sensitivities\n",
162 "l2_sensitivity = sensitivity ** 0.5\n",
164 "sum_x = np.sum(x)\n",
165 "sigma = noise_multiplier * l2_sensitivity\n",
166 "sum_X = round(gaussian_mech_RDP(sum_x, l2_sensitivity, alpha, rho, sigma=sigma))\n",
168 "print(f\"non-private sum: {sum_x}\")\n",
169 "print(f\"private sum: {sum_X}\")"
175 "display_name": "Python 3 (ipykernel)",
176 "language": "python",
184 "file_extension": ".py",
185 "mimetype": "text/x-python",
187 "nbconvert_exporter": "python",
188 "pygments_lexer": "ipython3",