### Run Privacy Accounting Example via setup.py Source: https://github.com/google/differential-privacy/blob/main/python/dp_accounting/README.md Install dependencies and execute the basic privacy loss distribution example using standard Python setup tools. ```bash pip install --upgrade setuptools ``` ```bash python setup.py install python dp_accounting/privacy_loss_distribution_basic_example.py ``` -------------------------------- ### Install and Run DP-Auditorium Example via Pip Source: https://github.com/google/differential-privacy/blob/main/python/dp_auditorium/README.md Install the library using pip and then run the mean mechanism example script. Ensure setuptools is installed and sqlite3 is available. ```bash python -m pip install . ``` ```bash python dp_auditorium/examples/run_mean_mechanism_example.py ``` -------------------------------- ### Run Example via setup.py Source: https://github.com/google/differential-privacy/blob/main/learning/clustering/README.md Install the library and run the clustering demo using setup.py. ```bash python setup.py install ``` ```bash python clustering/demo/clustering_demo.py ``` -------------------------------- ### Build Example Code with Bazel Source: https://github.com/google/differential-privacy/blob/main/examples/pipelinedp4j/README.md This command builds the example code using Bazel. It assumes you have cloned the repository and have the necessary Bazel setup. ```shell bazelisk build ... ``` -------------------------------- ### Build and Run Example with Bazel Source: https://github.com/google/differential-privacy/blob/main/cc/accounting/README.md Use these commands to build and run the differential privacy example using Bazel. Ensure Bazel is installed. ```bash bazel build :all ``` ```bash bazel run :example ``` -------------------------------- ### Install Setuptools Source: https://github.com/google/differential-privacy/blob/main/learning/clustering/README.md Ensure the setuptools package is installed or upgraded for running setup.py. ```bash pip install --upgrade setuptools ``` -------------------------------- ### Build PipelineDP4j example Source: https://github.com/google/differential-privacy/blob/main/examples/pipelinedp4j/README.md Compiles the project using Maven clean install. ```shell cd pipelinedp4j && mvn clean install ``` -------------------------------- ### Run Beam Example with Bazel Source: https://github.com/google/differential-privacy/blob/main/examples/pipelinedp4j/README.md This command builds and runs the Beam example using Bazel. Ensure you have Bazel installed and are in the examples/pipelinedp4j directory. The input and output file paths can be adjusted as needed. ```shell bazel-bin/beam/src/main/java/com/google/privacy/differentialprivacy/pipelinedp4j/examples/BeamExample --inputFilePath=netflix_data.csv --outputFilePath=output.txt ``` -------------------------------- ### Build and Run DP-Auditorium with Bazelisk Source: https://github.com/google/differential-privacy/blob/main/python/dp_auditorium/README.md Use Bazelisk to build all targets and then run the mean mechanism example. Ensure Bazelisk is installed. ```bash bazelisk build dp_auditorium:all ``` ```bash bazelisk run dp_auditorium/examples:run_mean_mechanism_example ``` -------------------------------- ### Run Pre-aggregation Example via Bazel Source: https://github.com/google/differential-privacy/blob/main/examples/java/README.md Commands to build and execute the pre-aggregation example code. ```shell $ cd examples/java $ bazel build … $ bazel-bin/Main SUM_REVENUE_PER_DAY_WITH_PREAGGREGATION ``` -------------------------------- ### Install and import dp_auditorium dependencies Source: https://github.com/google/differential-privacy/blob/main/python/dp_auditorium/dp_auditorium/examples/run_mean_mechanism_example.ipynb Installs required libraries, clones the repository, and sets up the system path for imports. ```python #@title Install and import dp_auditorium and all necessary libraries. !pip install google-vizier equinox !git clone https://github.com/google/differential-privacy.git import sys sys.path.append('differential-privacy/python/dp_auditorium') from dp_auditorium import interfaces from dp_auditorium import privacy_test_runner from dp_auditorium.generators import vizier_dataset_generator from dp_auditorium.configs import dataset_generator_config from dp_auditorium.configs import privacy_property from dp_auditorium.configs import privacy_test_runner_config import numpy as np import dataclasses ``` -------------------------------- ### Run Privacy Accounting Example with Bazel Source: https://github.com/google/differential-privacy/blob/main/python/dp_accounting/README.md Build and execute the basic privacy loss distribution example using the Bazel build system. ```bash bazel build dp_accounting:all bazel run dp_accounting:privacy_loss_distribution_basic_example ``` -------------------------------- ### Run Differential Privacy Example Source: https://github.com/google/differential-privacy/blob/main/examples/cc/README.md Navigate to the C++ examples directory and run the `report_the_carrots` bazel target to execute the differential privacy example. ```shell cd examples/cc bazel run report_the_carrots ``` -------------------------------- ### Install PostgreSQL extension Source: https://github.com/google/differential-privacy/blob/main/cc/postgres/README.md Execute the installation script from the cc directory of the differential library repository. ```bash ./postgres/install_extension.sh ``` -------------------------------- ### Execute PipelineDP4j example with Maven Source: https://github.com/google/differential-privacy/blob/main/examples/pipelinedp4j/README.md Runs the Beam example locally using Maven. Ensure input and output paths are correctly specified. ```shell mvn compile exec:java -Dexec.mainClass=com.google.privacy.differentialprivacy.pipelinedp4j.examples.BeamExample -Dexec.args="--inputFilePath=/netflix_data.csv --outputFilePath=output.txt" ``` -------------------------------- ### Install and Import Dependencies Source: https://github.com/google/differential-privacy/blob/main/python/dp_auditorium/dp_auditorium/examples/pipelinedp_mean_mechanism_example.ipynb Installs required libraries and imports the necessary modules for running DP-auditorium tests. ```python #@title Install and import dp_auditorium and all necessary libraries. !pip install google-vizier equinox pipeline_dp !git clone https://github.com/google/differential-privacy.git import sys sys.path.append('differential-privacy/python/dp_auditorium') from dp_auditorium import privacy_test_runner from dp_auditorium.generators import pipeline_dp_vizier_dataset_generator from dp_auditorium.configs import dataset_generator_config from dp_auditorium.configs import privacy_property from dp_auditorium.configs import privacy_test_runner_config from dp_auditorium.configs import property_tester_config from dp_auditorium.mechanisms.pipeline_dp import aggregation as pipeline_dp_mechanism from dp_auditorium.testers import hockey_stick_tester import pipeline_dp import tensorflow as tf tf.compat.v1.enable_eager_execution() ``` -------------------------------- ### Build and Run SumRevenuePerDay Source: https://github.com/google/differential-privacy/blob/main/examples/java/README.md Commands to build the Java example project and execute the SumRevenuePerDay logic. ```shell $ cd examples/java $ bazel build ... $ bazel-bin/Main SUM_REVENUE_PER_DAY ``` -------------------------------- ### Build and Run Differential Privacy Java Example Source: https://github.com/google/differential-privacy/blob/main/examples/java/README.md Commands to navigate to the Java examples directory, build the project using Bazel, and execute the CountVisitsPerHour logic. ```shell $ cd examples/java $ bazel build ... $ bazel-bin/Main COUNT_VISITS_PER_HOUR ``` -------------------------------- ### Install system dependencies Source: https://github.com/google/differential-privacy/blob/main/cc/postgres/README.md Required packages for building the extension on Ubuntu systems. ```bash sudo apt-get install make libreadline-dev bison flex ``` -------------------------------- ### Run CountVisitsPerDay Example Source: https://github.com/google/differential-privacy/blob/main/examples/java/README.md Build and run the Java example for counting visits per day using Bazel. This command triggers the logic for both non-private and private count calculations. ```shell $ cd examples/java $ bazel build ... $ bazel-bin/Main COUNT_VISITS_PER_DAY ``` -------------------------------- ### Run the SumRevenuePerDay scenario Source: https://github.com/google/differential-privacy/blob/main/examples/go/README.md Execute the Go codelab example to compute raw and differentially private sums of visitor spending. ```shell $ cd examples/go $ go run --mod=mod ./main --scenario=SumRevenuePerDay --input_file=data/week_data.csv --non_private_output_file=non_private.csv --private_output_file=private.csv ``` -------------------------------- ### Initialize mean mechanism example Source: https://github.com/google/differential-privacy/blob/main/python/dp_auditorium/dp_auditorium/examples/run_mean_mechanism_example.ipynb Imports necessary typing and timing utilities for the mean mechanism test. ```python #@title Mean mechanism example from typing import Callable import time ``` -------------------------------- ### Construct BoundedMean for int64_t Source: https://github.com/google/differential-privacy/blob/main/cc/docs/algorithms/bounded-mean.md Minimal construction example for BoundedMean with int64_t type. Ensure epsilon, lower, and upper bounds are set. ```cpp absl::StatusOr>> bounded_mean = BoundedMean::Builder.SetEpsilon(1) .SetLower(-10) .SetUpper(10) .Build(); ``` -------------------------------- ### Construct BoundedVariance Source: https://github.com/google/differential-privacy/blob/main/cc/docs/algorithms/bounded-variance.md Minimal construction example for BoundedVariance. Ensure epsilon, lower, and upper bounds are set. ```c++ absl::StatusOr>> bounded_var = BoundedVariance::Builder.SetEpsilon(1) .SetLower(-10) .SetUpper(10) .Build(); ``` -------------------------------- ### Run the Privacy on Beam codelab Source: https://github.com/google/differential-privacy/blob/main/privacy-on-beam/README.md Execute the codelab example with specific input and output file parameters. ```shell cd codelab/main go run -mod=mod . -example=count -input_file=day_data.csv -output_stats_file=stats.csv -output_chart_file=chart.png ``` -------------------------------- ### Install DP-Auditorium with CFLAGS on MacOS Source: https://github.com/google/differential-privacy/blob/main/python/dp_auditorium/README.md When encountering issues linking Sqlite3 with Python on MacOS during pip installation, pass C++ options to pip. ```bash CFLAGS=-Wno-error=implicit-function-declaration pip install . ``` -------------------------------- ### Troubleshoot missing extension files Source: https://github.com/google/differential-privacy/blob/main/cc/postgres/README.md Examples of errors indicating that the extension control files or binary files are not in the expected directory. ```text could not access file "anon_func": No such file or directory ``` ```text could not open extension control file "/usr/local/share/postgresql/extension/anon_func.control": No such file or directory ``` -------------------------------- ### Troubleshoot pg_config error Source: https://github.com/google/differential-privacy/blob/main/cc/postgres/README.md Example of the error message encountered when the pg_config utility is missing from the system path. ```text postgres/install_extension.sh: line 26: pg_config: command not found ``` -------------------------------- ### Run Mean Mechanism Example Source: https://github.com/google/differential-privacy/blob/main/python/dp_auditorium/dp_auditorium/examples/run_mean_mechanism_example.ipynb Executes the example code for a mean mechanism with specified privacy parameters (epsilon, delta) and a random seed. It configures a non-private mean mechanism, a Hockey-Stick property tester, and a dataset generator, then uses a privacy test runner to evaluate the mechanism. ```python def default_generator_factory( config: dataset_generator_config.VizierDatasetGeneratorConfig, ) -> vizier_dataset_generator.VizierScalarDataAddRemoveGenerator: return vizier_dataset_generator.VizierScalarDataAddRemoveGenerator( config=config ) def mean_mechanism_report( epsilon: float, delta: float, seed: int, generator_factory: Callable[ [dataset_generator_config.VizierDatasetGeneratorConfig], vizier_dataset_generator.VizierScalarDataAddRemoveGenerator, ] = default_generator_factory, ) -> privacy_test_runner_config.PrivacyTestRunnerResults: """Runs the example code for a mean mechanism. Args: epsilon: standard approximate DP parmaeter. delta: standard approximate DP parameter. seed: seed to initialize the random number generator. generator_factory: factory to create a generator; to be replaced in tests Returns: The result of the example code as PrivacyTestRunnerResults. """ rng = np.random.default_rng(seed=seed) tf.random.set_seed(seed) # Configuration for a non-private mean mechanism that uses the true number of # points to calculate the average and the scale of the noise. mech_config = mechanism_config.MeanMechanismConfig( epsilon=epsilon, delta=delta, use_noised_counts_for_calculating_mean=False, use_noised_counts_for_calculating_noise_scale=False, min_value=0.0, max_value=1.0, ) # Initialize the mechanism. mechanism = mean.MeanMechanism(mech_config, rng) # Configuration for a Hockey-Stick property tester. Given arrays s1 and s2 # with samples two distributions it will estimate the hockey-stick divergence # from the underlying distributions. It checks if the divergence is bounded by # delta. tester_config = property_tester_config.HockeyStickPropertyTesterConfig( training_config=hockey_stick_tester.make_default_hs_training_config(), approximate_dp=privacy_property.ApproximateDp( epsilon=epsilon, delta=delta, ), ) # Initialize a classifier model for the Hockey-Stick property tester. # This classifier will learn to distinguish between samples of the mechanism # on adjacent datasets. Its accuracy level should be controlled by the privacy # guarantee. base_model = hockey_stick_tester.make_default_hs_base_model() # Initialize a property tester. property_tester = hockey_stick_tester.HockeyStickPropertyTester( config=tester_config, base_model=base_model, ) # Configuration for dataset generator. It generates neighboring datasets under # the add/remove definition. Unique study name prevents using cached results # from previous runs. generator_config = dataset_generator_config.VizierDatasetGeneratorConfig( study_name=str(time.time()), study_owner="owner", num_vizier_parameters=2, data_type=dataset_generator_config.DataType.DATA_TYPE_FLOAT, min_value=-1.0, max_value=1.0, search_algorithm="RANDOM_SEARCH", metric_name="hockey_stick_divergence", ) # Initialize the dataset generator. dataset_generator = generator_factory(generator_config) # Configuration for the test runner. # The test runner coordinates how the test is evaluated. It receives a # dataset generator, a property tester and a configuration (see base class for # details on these parameters), and runs privacy tests using the property # tester on datasets generated by the dataset generator. test_runner_config = privacy_test_runner_config.PrivacyTestRunnerConfig( property_tester=privacy_test_runner_config.PropertyTester.HOCKEY_STICK_TESTER, max_num_trials=10, failure_probability=0.05, num_samples=10_000, # Apply a hyperbolic tangent function to the output of the mechanism post_processing=privacy_test_runner_config.PostProcessing.TANH, ) # Initialize the test runner. test_runner = privacy_test_runner.PrivacyTestRunner( config=test_runner_config, dataset_generator=dataset_generator, property_tester=property_tester, ) return test_runner.test_privacy(mechanism, "non-private-mean-mechanism") EPSILON = 1.0 DELTA = 1e-5 SEED = 1 # The results indicate whether a privacy violation was identified within the # designated number of trials defined in the configuration. In the absence of a # violation, a message is returned indicating that the limit of the number of # trials has been reached. For reference, all computed divergences across all # trials are also reported. results = mean_mechanism_report(EPSILON, DELTA, SEED) print(f" Results: {results}") ``` -------------------------------- ### Run PipelineDP4j on Dataflow Source: https://github.com/google/differential-privacy/blob/main/examples/pipelinedp4j/README.md Executes the Beam example on Google Cloud Dataflow using the dataflow-runner profile. ```shell cd beam && mvn -Pdataflow-runner compile exec:java -Dexec.mainClass=com.google.privacy.differentialprivacy.pipelinedp4j.examples.BeamExample -Dexec.args="--project= --gcpTempLocation=gs:///temp/ --inputFilePath=gs:///netflix_data.csv --outputFilePath=gs:///output.txt --runner=DataflowRunner --region=us-central1" -Pdataflow-runner ``` -------------------------------- ### Manually move extension files Source: https://github.com/google/differential-privacy/blob/main/cc/postgres/README.md Commands to manually relocate extension files if the default installation paths differ. ```bash mv $PG_DIR/share/extension/anon_func.control /usr/local/share/postgresql/extension/ ``` ```bash mv $PG_DIR/lib/anon_func.so `pg_config --pkglibdir` ``` -------------------------------- ### Import Data into PostgreSQL Source: https://github.com/google/differential-privacy/blob/main/cc/postgres/docker/README.md Import data from a CSV file into a PostgreSQL table named FruitEaten. This setup is used for demonstrating aggregate functions. ```sql CREATE TABLE FruitEaten ( uid integer, fruit character varying(20) ); COPY fruiteaten(uid, fruit) FROM '/fruiteaten.csv' DELIMITER ',' CSV HEADER; ``` -------------------------------- ### Run CountVisitsPerDuration scenario in Go Source: https://github.com/google/differential-privacy/blob/main/examples/go/README.md Executes the differential privacy codelab example for counting visits per duration using the provided input data. ```shell $ cd examples/go $ go run --mod=mod ./main --scenario=CountVisitsPerDuration --input_file=data/outliers_week_data.csv --non_private_output_file=non_private.csv --private_output_file=private.csv ``` -------------------------------- ### SQL Table Creation and Data Copy Source: https://github.com/google/differential-privacy/blob/main/cc/postgres/README.md Defines a 'Shirts' table and copies data from a CSV file. Ensure the file path is correct for your setup. ```sql CREATE TABLE Shirts ( uid integer, color character varying(20) ); COPY shirts(uid, color) FROM 'shirts.csv' DELIMITER ',' CSV HEADER; ``` -------------------------------- ### Build Differential Privacy Library with Bazel Source: https://github.com/google/differential-privacy/blob/main/cc/README.md Build the C++ differential privacy library and its dependencies using Bazel. Ensure you have bazelisk installed. ```shell cd cc bazelisk build ... ``` -------------------------------- ### Run Clustering Demo with Bazel Source: https://github.com/google/differential-privacy/blob/main/learning/clustering/README.md Commands to build and execute the clustering demonstration using Bazel. ```bash bazel build clustering:all bazel run clustering/demo:clustering_demo -- [options] ``` -------------------------------- ### Build the CLI Source: https://github.com/google/differential-privacy/blob/main/examples/zetasql/README.md Compile the ZetaSQL DP CLI and its dependencies using Bazelisk. ```shell bazelisk build //:execute_query ``` -------------------------------- ### Install Dependencies for CMake Build Source: https://github.com/google/differential-privacy/blob/main/cc/README.md Install necessary dependencies for building the differential privacy library using CMake on Ubuntu 22.04. This includes cmake, protobuf-compiler, and libssl-dev. ```shell sudo apt install -y cmake protobuf-compiler libssl-dev ``` -------------------------------- ### Build Go packages Source: https://github.com/google/differential-privacy/blob/main/go/README.md Builds all packages in the library using the go command with module support enabled. ```shell go build -mod=mod ./... ``` -------------------------------- ### Connect to PostgreSQL Source: https://github.com/google/differential-privacy/blob/main/cc/postgres/docker/README.md Connect to the running PostgreSQL instance using psql. Ensure you use the correct username, host, and port. ```bash psql -U postgres -h localhost -p 5432 ``` -------------------------------- ### Build BoundedMean Algorithm Source: https://github.com/google/differential-privacy/blob/main/cc/docs/protos.md Instantiates the BoundedMean algorithm with specified parameters for integer inputs. Ensure the epsilon, lower, and upper bounds are appropriately set for your data. ```c++ absl::StatusOr>> bounded_mean = BoundedMean::Builder.SetEpsilon(1) .SetLower(-10) .SetUpper(10) .Build(); ``` -------------------------------- ### Configure CMake and Fetch Dependencies Source: https://github.com/google/differential-privacy/blob/main/CMakeLists.txt Sets the C++ standard and uses FetchContent to retrieve and build the Protocol Buffers library. ```cmake cmake_minimum_required(VERSION 3.16) project(DifferentialPrivacyAlgorithms LANGUAGES CXX) # C++ Standard set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) # Fetch protobuf include(FetchContent) FetchContent_Declare( protobuf GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git GIT_TAG v3.21.12 # For older CMake versions, you might need SOURCE_DIR for protobuf SOURCE_SUBDIR cmake FIND_PACKAGE_ARGS NAMES protobuf ) # CMake variables to control the protobuf build set(protobuf_BUILD_TESTS OFF CACHE BOOL "" FORCE) # Usually good to disable tests of dependencies FetchContent_MakeAvailable(protobuf) include(FindProtobuf) find_package(protobuf CONFIG REQUIRED) ``` -------------------------------- ### Execute a SQL query Source: https://github.com/google/differential-privacy/blob/main/examples/zetasql/README.md Run the CLI to execute a SQL statement on sample data. ```shell bazelisk run //:execute_query -- ``` ```shell bazelisk run //:execute_query -- ' SELECT WITH DIFFERENTIAL_PRIVACY OPTIONS( epsilon=1, delta=1e-10, max_groups_contributed=1, privacy_unit_column=VisitorId) TIME_TRUNC(PARSE_TIME("%I:%M%p", `Time entered`), HOUR) AS `Hour entered`, AVG(CAST (`Time spent (mins)` AS INT32), contribution_bounds_per_group => (10, 120)) AS `DP average time spent (mins)`, SUM(CAST (`Money spent (euros)` AS INT32), contribution_bounds_per_group => (10, 50)) AS `DP total money spent (euros)` FROM day_data GROUP BY `Hour entered`' ``` -------------------------------- ### Prepare Netflix Sample Dataset Source: https://github.com/google/differential-privacy/blob/main/examples/pipelinedp4j/README.md This shell command prepares a sample dataset from the Netflix Prize data. It reformats the data and selects the first 10,000 lines, saving it to netflix_data.csv. ```shell awk -v OFS=',' '/^[0-9]+:$/ {movie_id=substr($1, 1, length($1)-1)} /^[0-9]+,[0-9]+/ {print movie_id, $0}' combined_data_2.txt | \ head -n 10000 > netflix_data.csv ``` -------------------------------- ### Getting Complete Results Source: https://github.com/google/differential-privacy/blob/main/cc/docs/algorithms/algorithm.md Adds entries from a given range and then retrieves the complete result. This method can only be called once before epsilon and delta are exhausted. ```APIDOC ## template ## Output Result(Iterator begin, Iterator end) ### Description Add the entries from `begin` to `end`, and then get the result. ### Method Output ### Endpoint Result(Iterator begin, Iterator end) ### Parameters - **begin** (Iterator) - Iterator pointing to the beginning of the data range. - **end** (Iterator) - Iterator pointing to the end of the data range. ### Request Example None ### Response #### Success Response (200) - **Output** (proto) - The complete result from the algorithm, potentially containing accuracy and mechanism data. #### Response Example None ``` -------------------------------- ### Getting Partial Results Source: https://github.com/google/differential-privacy/blob/main/cc/docs/algorithms/algorithm.md Retrieves a partial result based on the current state of the Algorithm. This method can only be called once before epsilon and delta are exhausted. ```APIDOC ## Output PartialResult() ### Description Get a result based on the current state of the `Algorithm`. ### Method Output ### Endpoint PartialResult() ### Parameters None ### Request Example None ### Response #### Success Response (200) - **Output** (proto) - The partial result from the algorithm. #### Response Example None ``` -------------------------------- ### Build PipelineDP4j with Bazelisk Source: https://github.com/google/differential-privacy/blob/main/pipelinedp4j/README.md Use this command to compile the library and its dependencies from the source directory. ```shell cd pipelinedp4j bazelisk build ... ``` -------------------------------- ### Run Go tests Source: https://github.com/google/differential-privacy/blob/main/go/README.md Executes all tests within the library packages. ```shell go test -mod=mod ./... ``` -------------------------------- ### Create and Load FruitEaten Table Source: https://github.com/google/differential-privacy/blob/main/cc/postgres/README.md Defines the schema for the FruitEaten table and loads data from a CSV file. Ensure the file path is correct. ```sql CREATE TABLE FruitEaten ( uid integer, fruit character varying(20) ); COPY fruiteaten(uid, fruit) FROM 'fruiteaten.csv' DELIMITER ',' CSV HEADER; ``` -------------------------------- ### Download source code archive Source: https://github.com/google/differential-privacy/blob/main/examples/zetasql/README.md Download the source code as a ZIP file. ```shell https://github.com/google/differential-privacy/archive/main.zip ``` -------------------------------- ### Construct QuantileTree Algorithm Source: https://github.com/google/differential-privacy/blob/main/cc/docs/algorithms/quantiles.md Constructs a QuantileTree algorithm instance. Requires explicit setting of lower and upper bounds. Tree height and branching factor are optional parameters with default values. ```c++ absl::StatusOr>> quantile_tree = QuantileTree::Builder() .SetLower(lower) .SetUpper(upper) .SetTreeHeight(tree_height) // optional .SetBranchingFactor(branching_factor) .Build(); ``` -------------------------------- ### AddToOutput Utility Function Source: https://github.com/google/differential-privacy/blob/main/cc/docs/protos.md Adds an element to an existing Output proto. The field set depends on the type of the template parameter T; for example, AddToOutput(&output, 7.0) adds an element and sets float_value. ```c++ template void AddToOutput(Output* output, T value); ``` -------------------------------- ### MakeOutput Utility Function Source: https://github.com/google/differential-privacy/blob/main/cc/docs/protos.md Creates an Output proto with a single element. The field set in the Output depends on the type of the template parameter T; for example, MakeOutput(7) sets int_value, while MakeOutput(7.0) sets float_value. ```c++ template MakeOutput(T value); ``` -------------------------------- ### Initialize dpagg.Count for Daily Visits Source: https://github.com/google/differential-privacy/blob/main/examples/go/README.md Initializes `dpagg.Count` objects for each day of the week to calculate differentially private visit counts. It configures privacy parameters such as epsilon, noise mechanism, and the maximum number of partitions a visitor can contribute to. ```go const ( // An upper limit of visits per week for a single person. // Visits over this // limit will be dropped and ignored in the final statistics. // // Higher values result into less visits being dropped (and therefore less // bias) at the cost of higher noise. A good value should estimate how many // times a typical visitor may enter the restaurant per week. Getting // this right helps adding optimal amount of noise without losing too much // data. maxThreeVisitsPerWeek = 3 ) for _, day := range weekDays { // Construct dpagg.Count objects, which will be used to calculate DP counts. // One Count is created for every work hour. dayToDpCount[day], err = dpagg.NewCount(&dpagg.CountOptions{ Epsilon: ln3, // The data was pre-processed so that // each visitor may visit the restaurant up to maxThreeVisitsPerWeek times per week. // Hence, each visitor may contribute to up to maxThreeVisitsPerWeek daily counts. // Note: while the library accepts this limit as a configurable parameter, // it doesn't pre-process the data to ensure this limit is respected. // It is responsibility of the caller to ensure the data passed to the library // is capped for getting the correct privacy guarantee. // TODO: Clarify the note above. MaxPartitionsContributed: maxThreeVisitsPerWeek, Noise: noise.Laplace(), }) if err != nil { // Handle errors } } ``` -------------------------------- ### Construct Quantiles Algorithm Source: https://github.com/google/differential-privacy/blob/main/cc/docs/algorithms/quantiles.md Constructs a Quantiles algorithm instance. Requires explicit setting of lower and upper bounds, and the quantiles to calculate. Input type T can be any numeric type. ```c++ absl::StatusOr>> quantile = Quantiles::Builder() .SetLower(upper) .SetUpper(lower) .SetQuantiles(quantiles) .Build(); ``` -------------------------------- ### Run codelab scenario Source: https://github.com/google/differential-privacy/blob/main/go/README.md Executes a specific scenario from the codelab using the provided input and output file paths. ```shell cd examples/go/main go run -mod=mod . -scenario=CountVisitsPerHour -input_file=../data/day_data.csv -non_private_output_file=out1.csv -private_output_file=out2.csv ``` -------------------------------- ### Construct Bounded Algorithm with Builder Source: https://github.com/google/differential-privacy/blob/main/cc/docs/algorithms/bounded-algorithm.md Constructs a bounded algorithm using BoundedAlgorithmBuilder. You can either set bounds directly or let the builder infer them automatically. Use this when you need to create a bounded algorithm instance. ```cpp BoundedAlgorithmBuilder builder = BoundedAlgorithmBuilder().SetEpsilon(double epsilon) // Option 1: Set bounds directly. absl::StatusOr>> bounded_algorithm = builder.SetLower(T lower) .SetUpper(T upper) .Build(); // Option 2: Automatically infer bounds. absl::StatusOr>> bounded_algorithm = builder.Build(); ``` -------------------------------- ### Configure BoundedSum with Pre-aggregated Bounds Source: https://github.com/google/differential-privacy/blob/main/examples/java/README.md Setting up the BoundedSum builder with adjusted upper bounds to account for pre-aggregated data. ```java private static final int MAX_EUROS_SPENT = 65; ... BoundedSum dpSum = BoundedSum.builder() .epsilon(LN_3) .maxPartitionsContributed(MAX_CONTRIBUTED_DAYS) .lower(MIN_EUROS_SPENT) .upper(MAX_EUROS_SPENT) .build(); ``` -------------------------------- ### Construct a BoundedSum instance Source: https://github.com/google/differential-privacy/blob/main/cc/docs/algorithms/bounded-sum.md Initializes a BoundedSum object for int64_t values with specified epsilon and bounds. ```cpp absl::StatusOr>> bounded_sum = BoundedSum::Builder.SetEpsilon(1) .SetLower(-10) .SetUpper(10) .Build(); ``` -------------------------------- ### Build and Submit Spark Jobs Source: https://github.com/google/differential-privacy/blob/main/examples/pipelinedp4j/README.md Commands to package the application and submit it to a Dataproc batch cluster. ```shell mvn clean install && cd spark && mvn package assembly:single ``` ```shell gcloud dataproc batches submit spark --region=us-central1 --jars=./target/spark-1.0-SNAPSHOT-jar-with-dependencies.jar --class=com.google.privacy.differentialprivacy.pipelinedp4j.examples.SparkExample --deps-bucket=gs:// -- --inputFilePath=gs:///netflix_data.csv --outputFolder=gs:///output ``` -------------------------------- ### Instantiate Stochastic Tester Source: https://github.com/google/differential-privacy/blob/main/cc/testing/README.md Creates a StochasticTester instance with the algorithm, sequence, and parameters for dataset checks and sample generation. Higher numbers increase detection likelihood but also test duration. ```cpp StochasticTester tester( std::move(algorithm), std::move(sequence), /*num_datasets=*/500, /*num_samples_per_histogram=*/20000); ``` -------------------------------- ### Calculate DP sums with BoundedSumInt64 Source: https://github.com/google/differential-privacy/blob/main/examples/go/README.md Configure and use BoundedSumInt64 to aggregate spending data while applying privacy constraints. ```go const ( // TODO: Clarify why we're assuming 4 average visits per week, and why we're calling it a max // An estimate of how many times on average a visitor may enter the restaurant per week maxFourVisitsPerWeek = 4 minEurosSpent = 0 // Maximum amount of money we expect a visitor to spend on a single visit. // This limit is used to choose the amount of noise required to "mask" // participation of a single user. This is required to ensure that // differential privacy guarantees hold for all the users in the dataset, // including outliers. // // Higher limits result into less values being "cut-off" (and therefore less // bias) at the cost of higher noise. maxEurosSpent = 50 ) for _, day := range weekDays { // Construct dpagg.BoundedSumInt64 objects which will be used to calculate DP sums. // One dpagg.BoundedSumInt64 is created for every day. dayToBoundedSum[day], err = dpagg.NewBoundedSumInt64(&dpagg.BoundedSumInt64Options{ Epsilon: ln3, // The data was pre-processed so that // each visitor may visit the restaurant up to maxFourVisitsPerWeek times per week. // Hence, each visitor may contribute to up to maxFourVisitsPerWeek daily counts. // Note: while the library accepts this limit as a configurable parameter, // it doesn't pre-process the data to ensure this limit is respected. // It is responsibility of the caller to ensure the data passed to the library // is capped for getting the correct privacy guarantee. // TODO: Clarify the note above. MaxPartitionsContributed: maxFourVisitsPerWeek, // No need to pre-process the data: BoundedSumInt64 will clamp the input values. Lower: minEurosSpent, Upper: maxEurosSpent, Noise: noise.Laplace(), }) if err != nil { // Handle errors } privateSums[day] = 0 } // Pre-process the data set by limiting the number of visits to // maxFourVisitsPerWeek per VisitorID. boundedVisits := boundVisits(visits, maxFourVisitsPerWeek) for _, visit := range boundedVisits { dayToBoundedSum[visit.Day].Add(visit.EurosSpent) } // Calculate DP result. for day, boundedSum := range dayToBoundedSum { privateSums[day], err = boundedSum.Result() if err != nil { // Handle errors } } ``` -------------------------------- ### Pre-aggregation Partition Selection in Go Source: https://github.com/google/differential-privacy/blob/main/examples/go/README.md This Go code snippet demonstrates how to implement pre-aggregation partition selection. It iterates through aggregated data, checks if a partition should be kept based on privacy unit count, and then calculates the private result if the partition is retained. Ensure proper error handling for the Result() call. ```go for duration, boundedSum := range durationToBoundedSum { // Pre-aggregation partition selection. // If there are enough visitors within this duration, // then it will appear in the result statistics table. // Otherwise, the duration's partition is simply dropped and excluded from the result. keepPartition := durationToSelectPartition[duration].ShouldKeepPartition() if keepPartition { privateSums[duration], err = boundedSum.Result() if err != nil { // Handle errors } } } ``` -------------------------------- ### Generate and Link Protobuf Library Source: https://github.com/google/differential-privacy/blob/main/CMakeLists.txt Defines the proto files, generates C++ code from them, and creates a linkable library target. ```cmake # Include directories for the project include_directories(${CMAKE_CURRENT_SOURCE_DIR}) set(PROTO_FILES proto/summary.proto proto/data.proto proto/confidence-interval.proto proto/numerical-mechanism.proto ) message(STATUS ${PROTO_FILES}) # Generate C++ sources and headers from .proto files set(Protobuf_IMPORT_DIRS "${protobuf_SOURCE_DIR}/src" .) add_library(dp_protos) protobuf_generate(TARGET dp_protos PROTOS ${PROTO_FILES} IMPORT_DIRS ${Protobuf_IMPORT_DIRS} PROTOC_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR}) # Add a library for the generated protobuf files target_include_directories(dp_protos PUBLIC ${CMAKE_CURRENT_BINARY_DIR} "${CMAKE_CURRENT_BINARY_DIR}/.." ..) target_link_libraries(dp_protos PRIVATE protobuf::libprotobuf ) # Add cc subdirectories add_subdirectory(cc) ```