]> git.armaanb.net Git - python_dp.git/blob - Incomplete/renyi.ipynb
Initial commit
[python_dp.git] / Incomplete / renyi.ipynb
1 {
2  "cells": [
3   {
4    "cell_type": "markdown",
5    "id": "49e2a702",
6    "metadata": {},
7    "source": [
8     "# Renyi Differential Privacy\n",
9     "\n",
10     "By [Armaan Bhojwani](https://armaanb.net) under [Praneeth Vepakomma](https://praneeth.mit.edu/)\n",
11     "\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",
13     "\n",
14     "### Dependencies\n",
15     "- matplotlib\n",
16     "- dp_accounting\n",
17     "\n",
18     "### Status\n",
19     "Incomplete"
20    ]
21   },
22   {
23    "cell_type": "markdown",
24    "id": "378ba024",
25    "metadata": {},
26    "source": [
27     "## Setup"
28    ]
29   },
30   {
31    "cell_type": "code",
32    "execution_count": 171,
33    "id": "062e3b39",
34    "metadata": {},
35    "outputs": [],
36    "source": [
37     "import numpy as np\n",
38     "\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",
42     "\n",
43     "sensitivity = 99  # Sensitivity of the function\n",
44     "epsilon = 2    # Privacy garuntee\n",
45     "delta = 10e-7\n",
46     "\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",
51     "\n",
52     "# Initialize Numpy RNG\n",
53     "rng = np.random.default_rng()\n",
54     "\n",
55     "# Increment data_high so that it includes the value specified\n",
56     "data_high += 1\n",
57     "\n",
58     "# Create dataset as defined by above parameters\n",
59     "x = rng.integers(low=data_low, high=data_high, size=(data_len))"
60    ]
61   },
62   {
63    "cell_type": "markdown",
64    "id": "25510bc4",
65    "metadata": {},
66    "source": [
67     "## Implementation"
68    ]
69   },
70   {
71    "cell_type": "code",
72    "execution_count": 117,
73    "id": "a74d9ba6",
74    "metadata": {},
75    "outputs": [
76     {
77      "name": "stdout",
78      "output_type": "stream",
79      "text": [
80       "found noise multiplier: 2.382578592961857\n"
81      ]
82     }
83    ],
84    "source": [
85     "import dp_accounting as dpa\n",
86     "import dp_accounting.rdp as rdpa\n",
87     "\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",
92     "                                              delta)\n",
93     "\n",
94     "print(f\"found noise multiplier: {noise_multiplier}\")"
95    ]
96   },
97   {
98    "cell_type": "code",
99    "execution_count": 118,
100    "id": "7451e486",
101    "metadata": {},
102    "outputs": [
103     {
104      "name": "stdout",
105      "output_type": "stream",
106      "text": [
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"
112      ]
113     }
114    ],
115    "source": [
116     "from common import *\n",
117     "\n",
118     "accountant = rdpa.RdpAccountant(\n",
119     "    orders=custom_orders) if custom_orders else rdpa.RdpAccountant()\n",
120     "\n",
121     "event = dpa.GaussianDpEvent(noise_multiplier)\n",
122     "accountant = accountant.compose(event)\n",
123     "\n",
124     "t_epsilon, alpha = accountant.get_epsilon_and_optimal_order(delta)\n",
125     "t_delta = accountant.get_delta(t_epsilon)\n",
126     "\n",
127     "alpha_idx = np.where(accountant._orders == alpha)\n",
128     "rho = accountant._rdp[alpha_idx][0]\n",
129     "\n",
130     "print(f\"found epsilon: {t_epsilon}\")\n",
131     "print(f\"found delta:   {t_delta}\")\n",
132     "print_hline(40)\n",
133     "print(f\"found alpha:   {alpha}\")\n",
134     "print(f\"found rho:     {rho}\")"
135    ]
136   },
137   {
138    "cell_type": "code",
139    "execution_count": 170,
140    "id": "6c295418",
141    "metadata": {},
142    "outputs": [
143     {
144      "name": "stdout",
145      "output_type": "stream",
146      "text": [
147       "Using sigma: 2.382578592961857\n",
148       "non-private sum: 75550\n",
149       "private sum:     75548\n"
150      ]
151     }
152    ],
153    "source": [
154     "import matplotlib.pyplot as plt\n",
155     "\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",
160     "\n",
161     "# https://programming-dp.com/ch6.html#vector-valued-functions-and-their-sensitivities\n",
162     "l2_sensitivity = sensitivity ** 0.5\n",
163     "\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",
167     "\n",
168     "print(f\"non-private sum: {sum_x}\")\n",
169     "print(f\"private sum:     {sum_X}\")"
170    ]
171   }
172  ],
173  "metadata": {
174   "kernelspec": {
175    "display_name": "Python 3 (ipykernel)",
176    "language": "python",
177    "name": "python3"
178   },
179   "language_info": {
180    "codemirror_mode": {
181     "name": "ipython",
182     "version": 3
183    },
184    "file_extension": ".py",
185    "mimetype": "text/x-python",
186    "name": "python",
187    "nbconvert_exporter": "python",
188    "pygments_lexer": "ipython3",
189    "version": "3.9.7"
190   }
191  },
192  "nbformat": 4,
193  "nbformat_minor": 5
194 }