SmartNoise SDK

repository·main·Indexed 43 days ago

https://github.com/opendp/smartnoise-sdk

A suite of tools for differential privacy on tabular data, featuring smartnoise-sql for differentially private SQL queries and smartnoise-synth for synthetic data generation using MWEM and PATE-CTGAN. Includes smartnoise-eval (v0.3.1) for assessing privacy and accuracy through dataset analysis and result evaluation, requiring PySpark.

Tokens
21.8K
Snippets
67
Records
90
Agent score
55%

What's inside SmartNoise SDK

  1. Overview of SmartNoise SDK

    main

    The SmartNoise SDK provides tools for applying differential privacy to tabular data. It is composed of two primary packages:

    • smartnoise-sql: Used to run differentially private SQL queries on datasets.
    • smartnoise-synth: Used to generate differentially private synthetic data.

    Both packages support Python 3.10 through 3.14.

  2. Overview of SmartNoise Evaluator

    main

    The SmartNoise Evaluator is a toolset for assessing the privacy and accuracy of differentially private queries. It consists of two primary capabilities:

    1. Analyze: Used to inspect a dataset to understand its characteristics (cardinality, data types, independencies, etc.), which helps in designing an effective privacy pipeline.
    2. Evaluate: Used to compare privatized results against true (original) results to measure accuracy and bias.

    Requirement: These tools currently require PySpark to function.

  3. Use the SmartNoise Eval API components

    main

    The SmartNoise Eval library provides three main classes for managing data and performing analysis/evaluation:

    • sneval.Analyze: The entry point for performing data analysis to inform your synthesis strategy.
    • sneval.Dataset: A class used to represent and manage the datasets being analyzed or evaluated.
    • sneval.Evaluate: The entry point for comparing original data with synthetic/private data to assess quality.
  4. Monitor and estimate Privacy Cost

    main

    Privacy parameters (epsilon and delta) apply to each computed column for the life of the session.

    • Accrual: Privacy cost accrues as queries are executed. The total spent is available via reader.odometer.spent.
    • Estimation: Use reader.get_privacy_cost(query_string) to estimate the cost of a query before executing it. This does not advance the odometer.
    • Odometer Behavior: The odometer is advanced immediately before the result is returned. Note that the total session cost (via odometer.spent) may accrue at a slower rate than the sum of individual get_privacy_cost calls due to how mechanisms are composed.
    # Estimating cost without executing
    cost = reader.get_privacy_cost('SELECT AVG(age), AVG(income) FROM PUMS.PUMS')
    
    # Checking total spent after execution
    print(reader.odometer.spent)
  5. Choose the right SmartNoise component for your use case

    main

    SmartNoise provides different tools depending on your data type and privacy requirements. Use the following rules of thumb to select a component:

    • OpenDP: Use directly if you are creating Jupyter notebooks, performing reproducible research, or require fine-grained control over processing and privacy budgets.
    • SmartNoise SQL: Use if you are generating reports or data cubes over tabular data stored in SQL databases or Spark, especially when dealing with very large datasets.
    • SmartNoise Synthesizers: Use if you cannot predict the workload in advance and want to share synthetic data that mimics the properties of the original data with collaborators.
  6. Manage and track privacy budget with Odometer

    main

    Privacy parameters epsilon and delta are set when instantiating a Privacy object and apply to each computed column for the session. Privacy cost accumulates as queries are executed. You can track the total accumulated cost using the spent property of the connection's odometer.

    Key behaviors:

    • Accrual: The odometer is advanced immediately before the differentially private result is returned.
    • Column Scaling: Privacy cost increases with the number of columns in a query.
    • Session vs. Sum: The total session cost accrues at a slower rate than the sum of individual query costs obtained via get_privacy_cost because the odometer tracks all mechanism invocations for the life of the session.
    • Sharing Odometers: You can share an odometer across multiple readers to track a single privacy budget across different workloads.
    from snsql import Privacy, from_connection
    
    # Initialize privacy
    privacy = Privacy(epsilon=0.1, delta=10e-7)
    reader = from_connection(conn, metadata=metadata, privacy=privacy)
    print(reader.odometer.spent)  # (0.0, 0.0)
    
    # Execute query and see cost increase
    result = reader.execute('SELECT COUNT(*) FROM PUMS.PUMS')
    print(reader.odometer.spent)  # approximately (0.1, 10e-7)
  7. Evaluate privacy results with SmartNoise Evaluator

    main

    The Evaluate capability compares an original data file against one or more comparison (privatized) files. It can compare single-file metrics produced by Analyze or perform two-dataset comparisons. If multiple comparison datasets are provided, the tool provides comparisons for each against the original, allowing you to aggregate results (e.g., by averaging).

    Metrics provided include:

    • Suppression & Fabrication: Counts of dimension combinations that were suppressed or fabricated.
    • Redaction: Counts of redacted rows (distinguishing between fully redacted and partly redacted).
    • Error Metrics:
      • Mean error in count across categories (by 1-way, 2-way, etc.).
      • Mean absolute error (by 1-way, 2-way, etc., up to a reporting length; supports user-specified dimension combinations and reporting by bin size, e.g., < 1000, >= 1000).
      • Mean proportional error (by 1-way, 2-way, etc.).
  8. Analyze a dataset with SmartNoise Evaluator

    main

    The Analyze capability provides detailed metrics about a single dataset to help prepare for differential privacy workflows. Key metrics provided include:

    • Uniqueness & Cardinality: Percent of dimension combinations that are unique, or have low cardinality ($k < 5$ or $k < 10$). It also reports individual cardinalities.
    • Linkability: Identifies which columns are "most linkable."
    • Marginal Histograms: Generates $n$-way marginal histograms (defaults to 20 marginals with 10 per marginal, though these are configurable). Labels are trimmed and encoded.
    • Descriptive Statistics: Number of rows, number of distinct rows, and for each marginal: Count, Mean, Variance, Min, Max, Median, and Percentiles.
    • Data Structure: Dimensionality, Sparsity, and Independencies.
    • Classification: Classification AUC.
  9. Configure MWEM query workloads and marginals

    main

    MWEM's performance depends on the quality of its candidate query workload. By default, it generates workloads consisting of all one-way and two-way marginals. You can tune this behavior using the following parameters:

    • marginal_width: Controls the complexity of marginals. Increasing this ensures three-way or higher marginals are included as candidates. Setting marginal_width=1 suppresses two-way marginals.
    • add_ranges: When used (especially with ordinal data or binned continuous values), this ensures candidate queries include range queries.
    • q_count: Specifies a fixed number of candidate queries to generate, regardless of the number of iterations. This is useful if you want to prioritize range queries over marginals.
    • measure_only: If set to True, the algorithm skips the query selection step and samples uniformly from the workload. This doubles the epsilon budget available for measuring queries but is generally only recommended for very small datasets.
  10. Data requirements for MWEM and MST

    main

    When using the MWEM or MST synthesizers, input columns must be categorical.

    If your dataset contains continuous values, you must discretize them before fitting the model. Warning: Ensure that the discretization process itself is performed in a way that does not reveal information about the underlying data distribution.

  11. Understand Private Synopsis

    main
    A private synopsis is a pre-computed set of differentially private aggregates. Because the synopsis itself is differentially private, you can generate new reports from it using standard, non-private SQL or tools like Excel Pivot Tables without incurring additional privacy loss. This allows for safe distribution of data summaries.