Questions or feedback?

Measurement example#

Use make_user_measurement() to construct a measurement for your own mechanism.

Note

This requires a looser trust model, as we cannot verify any privacy or stability properties of user-defined functions.

>>> import opendp.prelude as dp
>>> dp.enable_features("honest-but-curious", "contrib")

enable_features("honest-but-curious", "contrib")

This example mocks the typical API of the OpenDP library to make the most private DP mechanism ever!

>>> def make_base_constant(constant):
...     """Construct a Measurement that only returns a constant value."""
...     def function(_arg: int):
...         return constant
...     def privacy_map(d_in: int) -> float:
...         return 0.0
...     return dp.m.make_user_measurement(
...         input_domain=dp.atom_domain(T=int),
...         input_metric=dp.absolute_distance(T=int),
...         output_measure=dp.max_divergence(),
...         function=function,
...         privacy_map=privacy_map,
...         TO=type(constant),
...     )
...

make_base_constant <- function(input_domain, input_metric, constant) {
  function_ <- function(.arg) constant
  privacy_map <- function(d_in) 0.

  make_user_measurement(
    input_domain = input_domain,
    input_metric = input_metric,
    output_measure = max_divergence(),
    function_ = function_,
    privacy_map = privacy_map,
    .TO = String
  )
}
then_base_constant <- to_then(make_base_constant)

The resulting Measurement may be used interchangeably with those constructed via the library:

>>> meas = (
...     (
...         dp.vector_domain(dp.atom_domain((0, 10))),
...         dp.symmetric_distance(),
...     )
...     >> dp.t.then_sum()
...     >> make_base_constant("denied")
... )
>>> meas([2, 3, 4])
'denied'
>>> meas.map(1)
0.0

space <- c(
  vector_domain(atom_domain(bounds = c(0L, 10L))),
  symmetric_distance()
)
meas <- space |>
  then_sum() |>
  then_base_constant("denied")
meas(arg = c(2L, 3L, 4L))
meas(d_in = 1L)

While this mechanism clearly has no utility, the code snip may form a basis for you to create own measurements, or even incorporate mechanisms from other libraries.