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 |
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
# Pseudocode of an OpenDP combinator for privatizing a certain class of vector-valued transformations | |
def make_some_transformation_stable(trans_query: Transformation, scale, threshold): | |
assert trans_query.input_metric == SymmetricDistance | |
assert trans_query.output_metric == L1Distance | |
# must be equivalent to representation 2 in https://arxiv.org/pdf/1709.05396.pdf, Section 2.2.1 | |
assert trans_query.output_domain == HashMapDomain[AllDomain[KeyType], AllDomain[FloatCountType]] | |
def function(data): | |
noised = {k: v + sample_laplace(scale) for k, v in trans_query.invoke(data)} |
NewerOlder