Edit the
python source
for this recipe, or download the generated
.ipynb notebook.
Just adding Laplace and Gaussian noise#
Using higher-level APIs helps to ensure the correctness of your analysis, but sometimes you just want to add noise.
The Laplace and Gaussian mechanisms below are “contrib” features. Any features that have not completed the vetting process are marked as “contrib”, and require explicit opt-in.
[1]:
import opendp.prelude as dp
dp.enable_features("contrib")
[2]:
space = dp.atom_domain(T=float, nan=False), dp.absolute_distance(T=float)
laplace_mechanism = space >> dp.m.then_laplace(scale=1.0)
dp_value = laplace_mechanism(100.0)
print(dp_value)
100.45653031548228
Ta-da! We’ve added calibrated Laplace noise to 100.0. Gaussian noise is also possible:
[3]:
gaussian_mechanism = space >> dp.m.then_gaussian(scale=1.0)
dp_value = gaussian_mechanism(100.0)
print(dp_value)
99.87182007510107
If you have any doubts about whether Laplace or Gaussian noise is appropriate, or how to choose an appropriate scale, the Context API will handle those details for you.