{ "cells": [ { "cell_type": "markdown", "id": "49e2a702", "metadata": {}, "source": [ "# Renyi Differential Privacy\n", "\n", "By [Armaan Bhojwani](https://armaanb.net) under [Praneeth Vepakomma](https://praneeth.mit.edu/)\n", "\n", "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", "\n", "### Dependencies\n", "- matplotlib\n", "- dp_accounting\n", "\n", "### Status\n", "Incomplete" ] }, { "cell_type": "markdown", "id": "378ba024", "metadata": {}, "source": [ "## Setup" ] }, { "cell_type": "code", "execution_count": 171, "id": "062e3b39", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "# Privacy parameters\n", "# (https://github.com/google/differential-privacy/blob/main/python/dp_accounting/rdp/rdp_privacy_accountant.py#L781)\n", "custom_orders = False # Set to array of custom orders, otherwise use defaults\n", "\n", "sensitivity = 99 # Sensitivity of the function\n", "epsilon = 2 # Privacy garuntee\n", "delta = 10e-7\n", "\n", "# Data parameters\n", "data_len = 1500 # Length of dataset\n", "data_low = 0 # Lowest value of dataset\n", "data_high = 99 # Highest value of dataset\n", "\n", "# Initialize Numpy RNG\n", "rng = np.random.default_rng()\n", "\n", "# Increment data_high so that it includes the value specified\n", "data_high += 1\n", "\n", "# Create dataset as defined by above parameters\n", "x = rng.integers(low=data_low, high=data_high, size=(data_len))" ] }, { "cell_type": "markdown", "id": "25510bc4", "metadata": {}, "source": [ "## Implementation" ] }, { "cell_type": "code", "execution_count": 117, "id": "a74d9ba6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "found noise multiplier: 2.382578592961857\n" ] } ], "source": [ "import dp_accounting as dpa\n", "import dp_accounting.rdp as rdpa\n", "\n", "# noise_multiplier is the ratio of the standard deviation of the Gaussian\n", "# noise to the l2-sensitivity of the function to which it is added\n", "noise_multiplier = dpa.calibrate_dp_mechanism(rdpa.RdpAccountant,\n", " dpa.GaussianDpEvent, epsilon,\n", " delta)\n", "\n", "print(f\"found noise multiplier: {noise_multiplier}\")" ] }, { "cell_type": "code", "execution_count": 118, "id": "7451e486", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "found epsilon: 1.9999992100966923\n", "found delta: 1.0000000000000023e-06\n", "────────────────────────────────────────\n", "found alpha: 12.0\n", "found rho: 1.0569556863430245\n" ] } ], "source": [ "from common import *\n", "\n", "accountant = rdpa.RdpAccountant(\n", " orders=custom_orders) if custom_orders else rdpa.RdpAccountant()\n", "\n", "event = dpa.GaussianDpEvent(noise_multiplier)\n", "accountant = accountant.compose(event)\n", "\n", "t_epsilon, alpha = accountant.get_epsilon_and_optimal_order(delta)\n", "t_delta = accountant.get_delta(t_epsilon)\n", "\n", "alpha_idx = np.where(accountant._orders == alpha)\n", "rho = accountant._rdp[alpha_idx][0]\n", "\n", "print(f\"found epsilon: {t_epsilon}\")\n", "print(f\"found delta: {t_delta}\")\n", "print_hline(40)\n", "print(f\"found alpha: {alpha}\")\n", "print(f\"found rho: {rho}\")" ] }, { "cell_type": "code", "execution_count": 170, "id": "6c295418", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using sigma: 2.382578592961857\n", "non-private sum: 75550\n", "private sum: 75548\n" ] } ], "source": [ "import matplotlib.pyplot as plt\n", "\n", "def gaussian_mech_RDP(x, sensitivity, alpha, rho, sigma=0):\n", " sigma = np.sqrt((sensitivity**2 * alpha) / (2 * rho)) if sigma == 0 else sigma\n", " print(f\"Using sigma: {sigma}\")\n", " return x + np.random.normal(loc=0, scale=sigma)\n", "\n", "# https://programming-dp.com/ch6.html#vector-valued-functions-and-their-sensitivities\n", "l2_sensitivity = sensitivity ** 0.5\n", "\n", "sum_x = np.sum(x)\n", "sigma = noise_multiplier * l2_sensitivity\n", "sum_X = round(gaussian_mech_RDP(sum_x, l2_sensitivity, alpha, rho, sigma=sigma))\n", "\n", "print(f\"non-private sum: {sum_x}\")\n", "print(f\"private sum: {sum_X}\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }