Skip to content

Instantly share code, notes, and snippets.

@Shoeboxam
Last active August 19, 2024 15:13
Show Gist options
  • Save Shoeboxam/3d3c9d6b7d0d6b470109d9f4ba3c0b62 to your computer and use it in GitHub Desktop.
Save Shoeboxam/3d3c9d6b7d0d6b470109d9f4ba3c0b62 to your computer and use it in GitHub Desktop.
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)
# release counts of the number of pets per-species
query = context.query().group_by("species").agg(
pl.len().dp.noise()
)
# view calibrated scale parameters per-query, accuracy estimate and censoring threshold
print(query.summarize(alpha=0.05))
# ┌────────┬───────────┬─────────────────┬───────┬──────────┬───────────┐
# │ column ┆ aggregate ┆ distribution ┆ scale ┆ accuracy ┆ threshold │
# │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
# │ str ┆ str ┆ str ┆ f64 ┆ f64 ┆ u32 │
# ╞════════╪═══════════╪═════════════════╪═══════╪══════════╪═══════════╡
# │ len ┆ Len ┆ Integer Laplace ┆ 2.0 ┆ 6.429605 ┆ 33 │
# └────────┴───────────┴─────────────────┴───────┴──────────┴───────────┘
print(query.release().collect())
# ┌─────────┬─────┐
# │ species ┆ len │
# │ --- ┆ --- │
# │ str ┆ u32 │
# ╞═════════╪═════╡
# │ cat ┆ 112 │
# │ dog ┆ 142 │
# │ mouse ┆ 45 │
# └─────────┴─────┘
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment