Skip to content

Instantly share code, notes, and snippets.

View Shoeboxam's full-sized avatar

Michael Shoemate Shoeboxam

View GitHub Profile
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
)
@Shoeboxam
Shoeboxam / 0.11.py
Last active August 19, 2024 15:13
Code to accompany OpenDP Library 0.11 Blog
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)
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,
@Shoeboxam
Shoeboxam / pca.py
Created April 9, 2024 19:39
OpenDP PCA
import opendp.prelude as dp
model = dp.sklearn.PCA(
epsilon=1.,
row_norm=1.,
n_samples=num_rows,
n_features=4,
)
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.,
@Shoeboxam
Shoeboxam / dp_grouping_columns.py
Created April 9, 2024 17:10
OpenDP Select Grouping Columns
import opendp.prelude as dp
import pandas as pd
import faker
import random
# first, write constructors that will be used to build the mechanism
def make_grouping_cols_score(candidates, min_bin_contributions):
r"""Create a transformation that assesses the utility of each candidate in `candidates`.
@Shoeboxam
Shoeboxam / dp_quantile.py
Last active April 9, 2024 15:46
OpenDP Quantile
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]
@Shoeboxam
Shoeboxam / bench.py
Created February 1, 2023 23:48
simple memory+time benchmark
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()
@Shoeboxam
Shoeboxam / mock_measurement.py
Last active July 5, 2022 17:27
Mock Measurement
from dataclasses import dataclass
from typing import Callable, Any
@dataclass
class MockMeasurement(object):
input_domain: Any
output_domain: Any
function: Callable
input_metric: Any
@Shoeboxam
Shoeboxam / make_stable_constructor.py
Last active October 22, 2021 02:07
Pseudocode of an OpenDP combinator for privatizing a certain class of vector-valued transformations
# 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)}