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.prelude as dp | |
| def approx_zcdp_to_approx_dp(rho: float, delta: float, delta_prime: float): | |
| """ | |
| Convert a (rho, delta) approx-zCDP guarantee into an (eps, delta + delta_prime) approx-DP guarantee | |
| using OpenDP's measure casting and fix-delta combinators. | |
| Returns: | |
| eps (float): epsilon after conversion |
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 numpy as np | |
| import opendp.prelude as dp | |
| dp.enable_features("contrib") | |
| # Create the randomized response mechanism | |
| m_rr = dp.m.make_randomized_response_bitvec( | |
| dp.bitvector_domain(max_weight=4), dp.discrete_distance(), f=0.95 | |
| ) |
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 polars as pl | |
| import opendp.prelude as dp | |
| dp.enable_features("contrib") | |
| # set up your analysis | |
| context = dp.Context.compositor( | |
| data=pl.scan_csv("pet_species.csv"), | |
| privacy_unit=dp.unit_of(contributions=1), | |
| privacy_loss=dp.loss_of(epsilon=1., delta=1e-7), | |
| split_evenly_over=2) |
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 polars as pl | |
| import opendp.prelude as dp | |
| dp.enable_features("contrib") | |
| # set up your analysis | |
| context = dp.Context.compositor( | |
| data=pl.scan_csv("grade_pets.csv"), | |
| privacy_unit=dp.unit_of(contributions=1), | |
| privacy_loss=dp.loss_of(epsilon=1.), | |
| split_evenly_over=3, |
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.prelude as dp | |
| model = dp.sklearn.PCA( | |
| epsilon=1., | |
| row_norm=1., | |
| n_samples=num_rows, | |
| n_features=4, | |
| ) |
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 faker | |
| import opendp.prelude as dp | |
| counter = dp.t.make_count_by( | |
| dp.vector_domain(dp.atom_domain(T=str)), | |
| dp.symmetric_distance(), | |
| MO=dp.L1Distance[int]) | |
| alp_meas = counter >> dp.m.then_alp_queryable( | |
| scale=1., |
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
| # WARNING: | |
| # This script works with OpenDP 0.9. | |
| # For a version compatible with 0.13, see: | |
| # https://docs.opendp.org/en/v0.13.0/api/user-guide/plugins/selecting-grouping-columns.html | |
| import opendp.prelude as dp | |
| import pandas as pd | |
| import faker | |
| import random |
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.prelude as dp | |
| dp.enable_features("contrib") | |
| # define privacy guarantee | |
| max_contributions = 1 | |
| epsilon = 0.1 | |
| # public information | |
| candidates = [10, 30, 50, 70, 90] |
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
| def bench(function, iterations): | |
| import time | |
| elapsed_times = [] | |
| import tracemalloc | |
| tracemalloc.start() | |
| for _ in range(iterations): | |
| prev_snap = tracemalloc.take_snapshot() | |
| prev_time = time.time() |
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
| from dataclasses import dataclass | |
| from typing import Callable, Any | |
| @dataclass | |
| class MockMeasurement(object): | |
| input_domain: Any | |
| output_domain: Any | |
| function: Callable | |
| input_metric: Any |
NewerOlder