Differential privacy using Laplace mechanism documentation

The Laplace mechanism consists of adding noise, generated through the Laplace distribution and the privacy budget, to a value. The derived value is said to be “anonymized” if the privacy budget used is good enough.

Privacy budget

The privacy budget epsilon defines how much privacy protection to apply.

  • If epsilon is large less noise will be added and therefore more information leakage exists, so less privacy protection will be present.
  • If epsilon is small (must be larger than zero) more noise will be added and therefore less information leakage exists, so more privacy protection will be present.

Sequential composition

When using a data set one tends to issue multiple statistical queries such that one output might be correlated with another. In doing so we reveal more, therefore leak more information so less privacy protection will be present. In order to address this problem, we divide the provided privacy budget into the amount of queries performed creating a query privacy budget. This query privacy budget is then used for each statistic query in order to strengthen the privacy protection.

class diffpriv_laplace.DiffPrivSequentialStatisticsQuery

The sequential composition statistics query class.

classmethod calculate_query_epsilon(kinds, epsilon)

Calculates the privacy budget to use for each query.

Parameters:
  • kinds (list) – The list of DiffPrivStatisticKind to calculate the queries from.
  • epsilon (float) – The privacy budget to calculate the query privacy budget from.
Returns:

The calculated privacy budget to use for each query.

Return type:

float

classmethod query(data, kinds, epsilon, axis=None)

Performs sequential composition by decomposing a multiple statistic queries into sub-queries (each subset assigned to each data slice) which use a portion of the defined privacy budget.

Parameters:
  • data (list|ndarray) – The data to retrieve the anonymized statistic value(s) from.
  • kinds (DiffPrivStatisticKind|list) – The kind of statistics to perform on each data slice. If a None value is provided the corresponding statistics calculation for the data slice is skipped.
  • epsilon (float) – The privacy budget.
  • [axis] (int|tuple) – Axis or tuple of axes along which to obtain the anonymized statistic value(s).
Returns:

The list of anonymized statistics requested.

Return type:

list

Raises:
  • DiffPrivInvalidDimensions – The exception is raised when the data dimension is invalid.
  • DiffPrivSizeMismatch – The exception is raised when the length of the kinds list is of different size than the amount of data slices in data defined through axis.
classmethod query_count(kinds)

Determines the amount of queries to perform.

Parameters:kinds (list) – The list of DiffPrivStatisticKind to calculate the queries from.
Returns:The amount of queries to perform.
Return type:float

Examples:

  • Perform mean and variance statistic queries for single data slice:
import numpy as np
from diffpriv_laplace import DiffPrivSequentialStatisticsQuery, DiffPrivStatisticKind


epsilon = 0.1
data = np.array(list(range(0, 20)) + [100.0])
kinds = DiffPrivStatisticKind.mean | DiffPrivStatisticKind.variance
results = DiffPrivSequentialStatisticsQuery.query(data, kinds, epsilon)
  • Perform mean and variance statistic queries for multiple data slices:
import numpy as np
from diffpriv_laplace import DiffPrivSequentialStatisticsQuery, DiffPrivStatisticKind


epsilon = 0.1
data = np.array([list(range(0, 20)) + [100.0]] * 3)
kinds = [DiffPrivStatisticKind.mean | DiffPrivStatisticKind.variance] * 3
results = DiffPrivSequentialStatisticsQuery.query(data, kinds, epsilon, axis=1)

Parallel composition

Unlike the pessimistic approach of sequential composition, when using disjoint data sets we assume there isn’t any correlation between statistical queries. Therefore, if we have a privacy budget for each query we choose the maximum one and use it for all queries.

class diffpriv_laplace.DiffPrivParallelStatisticsQuery

The parallel composition statistics query class.

classmethod query(data, kinds, epsilon, axis=None)

Performs parallel composition by decomposing a multiple statistic queries into sub-queries (each subset assigned to each data slice) which use the defined privacy budget which should correspond to the maximum budget of the queries.

Parameters:
  • data (list|ndarray) – The data to retrieve the anonymized statistic value(s) from.
  • kinds (DiffPrivStatisticKind|list) – The kind of statistics to perform on each data slice. If a None value is provided the corresponding statistics calculation for the data slice is skipped.
  • epsilon (list) – The privacy budget.
  • [axis] (int|tuple) – Axis or tuple of axes along which to obtain the anonymized statistic value(s).
Returns:

The list of anonymized statistics requested.

Return type:

list

Raises:
  • DiffPrivInvalidDimensions – The exception is raised when the data dimension is invalid.
  • DiffPrivSizeMismatch – The exception is raised when the length of the kind list is of different size than the amount of data slices in data defined through axis.

Examples:

  • Perform mean and variance statistic queries for single data slice:
import numpy as np
from diffpriv_laplace import DiffPrivParallelStatisticsQuery, DiffPrivStatisticKind


epsilon = 0.1
data = np.array(list(range(0, 20)) + [100.0])
kinds = DiffPrivStatisticKind.mean | DiffPrivStatisticKind.variance
results = DiffPrivParallelStatisticsQuery.query(data, kinds, epsilon)
  • Perform mean and variance statistic queries for multiple data slices:
import numpy as np
from diffpriv_laplace import DiffPrivParallelStatisticsQuery, DiffPrivStatisticKind


epsilon = 0.1
data = np.array([list(range(0, 20)) + [100.0]] * 3)
kinds = [DiffPrivStatisticKind.mean | DiffPrivStatisticKind.variance] * 3
results = DiffPrivParallelStatisticsQuery.query(data, kinds, epsilon, axis=1)

Laplace sanitizer

The Laplace sanitizer is an extension to the Laplace mechanism that is usable if it’s possible to decompose categorical data into disjoint/independent subsets (e.g. a histogram or a contingency table). Under these circumstances it’s possible to use parallel composition statistical queries.

class diffpriv_laplace.DiffPrivLaplaceSanitizer

Differential privacy Laplace sanitizer.

classmethod constrain_anonymized_counts(counts, n)

Constrain the anonymized counts by a defined quantity.

Parameters:
  • counts (ndarray) – The anonymized counts.
  • n (float) – The quantity to constrain the values by.
Returns:

The constrained anonymized counts.

Return type:

ndarray

classmethod count(data, selectors, epsilon, axis=None, postprocess=True)

Performs Laplace sanitizer counting by selecting the data into independent/disjoint data slice subsets and then applying parallel composition on each using the defined privacy budget which should correspond to the maximum budget of all the count queries. This process creates a histogram for each data slice where the counts correspond to the frequency for each category.

Parameters:
  • data (list|ndarray) – The data to retrieve the anonymized statistic value(s) from.
  • selectors (list) – The list of lists of selector functions to apply to the data such that each collection of functions in index i is applied to its corresponding data slice i. If an entry is is None, no selecting is performed.
  • epsilon (list) – The privacy budget.
  • [axis] (int|tuple) – Axis or tuple of axes along which to obtain the anonymized statistic value(s).
  • [postprocess] (bool) – Indicates whether or not to constrain the anonymized count values.
Returns:

The list of anonymized statistics requested subject to the decomposed data.

Return type:

ndarray

Raises:
  • DiffPrivInvalidDimensions – The exception is raised when the data dimension is invalid.
  • DiffPrivSizeMismatch – The exception is raised when the length of the selectors list is of different size than the amount of data slices in data defined through axis.
  • DiffPrivInvalidDecomposition – The exception is raised when there are overlapping elements in the decomposed data slice subsets meaning that there is at least one element which is present in other subsets. This means that the data slice subsets are not independent/disjoint with respect to each other which is a requirement for the Laplace sanitizer.
classmethod decompose_data_slice(data, selectors)

Decomposes a data slice into a category subsets of independent/disjoint data derived from the provided selectors.

Parameters:
  • data (list|ndarray) – The data slice to split.
  • selectors (list) – The list of selector functions to apply to the data such that the data slice is decomposed into n subsets of the same size, where n is the number of decomposing functions. Each selector function should return a boolean ndarray or list where the value is True if it belongs to that category and False otherwise.
Returns:

The decomposed data slice.

Return type:

ndarray

Raises:

DiffPrivInvalidDecomposition – The exception is raised when there are overlapping elements in the decomposed data slice subsets meaning that there is at least one element which is present in other subsets. This means that the data slice subsets are not independent/disjoint with respect to each other which is a requirement for the Laplace sanitizer.

Examples:

  • Perform categorical anonymized count:
import numpy as np
from diffpriv_laplace import DiffPrivLaplaceSanitizer


epsilon = 0.1
data = np.array([0.01, -0.01, 0.03, -0.001, 0.1] * 2)

def selector_positive(data):
    return data >= 0.0

def selector_negative(data):
    return data < 0.0

selectors = [selector_positive, selector_negative]
value = DiffPrivLaplaceSanitizer.count(data, selectors, epsilon)

Statistics

class diffpriv_laplace.DiffPrivStatistics

Differential privacy Laplace mechanism statistics.

classmethod apply_kind_on_data_slice(data, kind, epsilon, axis=None)

Performs the statistic operations for its corresponding data slice using the provided privacy budget. The statistics defined in kind at index i is only applied to data slice i. Therefore, the length of the kind list should be the same as the amount of data slices that are derived from axis.

Parameters:
  • data (list|ndarray) – The data to retrieve the anonymized statistic(s) from.
  • kind (DiffPrivStatisticKind|list) – The kind of statistics to perform on each data slice. If a None value is provided the corresponding statistics calculation for the data slice is skipped.
  • epsilon (float) – The privacy budget.
  • [axis] (int|tuple) – Axis or tuple of axes along which to obtain the anonymized statistic value(s).
Returns:

The list of anonymized statistics requested and calculated for each data slice.

Return type:

list

Raises:
  • DiffPrivInvalidDimensions – The exception is raised when the data dimension is invalid.
  • DiffPrivSizeMismatch – The exception is raised when the length of the kind list is of different size than the amount of data slices in data defined through axis.
classmethod calculate_data_slice_statistics(data_slice, kind, epsilon)

Calculates the statistics for a given data slice using a provided privacy budget.

Parameters:
  • data (ndarray) – The data slice to calculate the anonymized statistic(s) for.
  • kind (DiffPrivStatisticKind) – The kind of statistics to perform on the data slice.
  • epsilon (float) – The privacy budget.
Returns:

The dictionary of calculated statistics where keys are of type DiffPrivStatisticKind and values are of type float.

Return type:

dict

classmethod count(data, epsilon, condition=None, axis=None, postprocess=True)

Performs the count operation and anonymizes the value(s) using the provided privacy budget.

The operation counts non-zero or non-False values. When a condition function is provided, it is invoked so that the data can be transformed before counting.

Parameters:
  • data (list|ndarray) – The data to retrieve the count(s) from.
  • epsilon (float) – The privacy budget.
  • [condition] (function) – A condition function that receives the data as a parameter and returns a list or ndarray of the same size in which for each element is either a zero or False when it should not be counted and non-zero or True when it should.
  • [axis] (int|tuple) – Axis or tuple of axes along which to count non-zeros or non-False value(s).
  • [postprocess] (bool) – Indicates whether or not to post-process the count values so that the returned value is rounded and within the [0, n] range, where n is the total number of observations.
Returns:

The anonymized count(s).

Return type:

float|ndarray

classmethod max(data, epsilon, axis=None)

Performs the max operation and anonymizes the value(s) using the provided privacy budget.

Parameters:
  • data (list|ndarray) – The data to retrieve the max value(s) from.
  • epsilon (float) – The privacy budget.
  • [axis] (int|tuple) – Axis or tuple of axes along which to obtain the max value(s).
Returns:

The anonymized max value(s).

Return type:

float|ndarray

classmethod mean(data, epsilon, axis=None)

Performs the mean operation and anonymizes the value(s) using the provided privacy budget.

Parameters:
  • data (list|ndarray) – The data to retrieve the mean value(s) from.
  • epsilon (float) – The privacy budget.
  • [axis] (int|tuple) – Axis or tuple of axes along which to obtain the mean value(s).
Returns:

The anonymized mean value(s).

Return type:

float|ndarray

classmethod median(data, epsilon, axis=None)

Performs the median operation and anonymizes the value(s) using the provided privacy budget.

Parameters:
  • data (list|ndarray) – The data to retrieve the median value(s) from.
  • epsilon (float) – The privacy budget.
  • [axis] (int|tuple) – Axis or tuple of axes along which to obtain the median value(s).
Returns:

The anonymized median value(s).

Return type:

float|ndarray

classmethod min(data, epsilon, axis=None)

Performs the min operation and anonymizes the value(s) using the provided privacy budget.

Parameters:
  • data (list|ndarray) – The data to retrieve the min value(s) from.
  • epsilon (float) – The privacy budget.
  • [axis] (int|tuple) – Axis or tuple of axes along which to obtain the min value(s).
Returns:

The anonymized min value(s).

Return type:

float|ndarray

classmethod proportion(data, epsilon, condition=None, axis=None, postprocess=True)

Performs the proportion operation and anonymizes the value(s) using the provided privacy budget.

The operation counts non-zero or non-False values. When a condition function is provided, it is invoked so that the data can be transformed before counting. Afterwords, the proportion is calculated using the total amount of values.

Parameters:
  • data (list|ndarray) – The data to retrieve the proportion(s) from.
  • epsilon (float) – The privacy budget.
  • [condition] (function) – A condition function that receives the data as a parameter and returns a list or ndarray of the same size in which for each element is either a zero or False when it should not be counted and non-zero or True when it should.
  • [axis] (int|tuple) – Axis or tuple of axes along which to count non-zeros or non-False value(s).
  • [postprocess] (bool) – Indicates whether or not to post-process the proportion values so that the returned value is within the [0.0, 1.0] range.
Returns:

The anonymized count(s).

Return type:

float|ndarray

classmethod sum(data, epsilon, axis=None)

Performs the sum operation and anonymizes the value(s) using the provided privacy budget.

Parameters:
  • data (list|ndarray) – The data to retrieve the sum value(s) from.
  • epsilon (float) – The privacy budget.
  • [axis] (int|tuple) – Axis or tuple of axes along which to obtain the sum value(s).
Returns:

The anonymized sum value(s).

Return type:

float|ndarray

classmethod variance(data, epsilon, axis=None)

Performs the variance operation and anonymizes the value(s) using the provided privacy budget.

Parameters:
  • data (list|ndarray) – The data to retrieve the variance value(s) from.
  • epsilon (float) – The privacy budget.
  • [axis] (int|tuple) – Axis or tuple of axes along which to obtain the variance value(s).
Returns:

The anonymized variance value(s).

Return type:

float|ndarray

Examples:

  • Perform anonymized count:
import numpy as np
from diffpriv_laplace import DiffPrivStatistics


epsilon = 0.1
data = np.array([True, False, True, False, True] * 2)
value = DiffPrivStatistics.count(data, epsilon)
  • Perform anonymized count with condition function:
import numpy as np
from diffpriv_laplace import DiffPrivStatistics


epsilon = 0.1
data = np.array([0.01, -0.01, 0.03, -0.001, 0.1] * 2)

def condition(data):
    return data >= 0.0

value = DiffPrivStatistics.count(data, epsilon, condition=condition)
  • Perform anonymized min:
import numpy as np
from diffpriv_laplace import DiffPrivStatistics


epsilon = 0.1
data = np.array(list(range(1, 101)))
value = DiffPrivStatistics.min(data, epsilon)
  • Perform anonymized max:
import numpy as np
from diffpriv_laplace import DiffPrivStatistics


epsilon = 0.1
data = np.array(list(range(1, 101)))
value = DiffPrivStatistics.max(data, epsilon)
  • Perform anonymized median:
import numpy as np
from diffpriv_laplace import DiffPrivStatistics


epsilon = 0.1
data = np.array(list(range(1, 101)))
value = DiffPrivStatistics.median(data, epsilon)
  • Perform anonymized proportion:
import numpy as np
from diffpriv_laplace import DiffPrivStatistics


epsilon = 0.1
data = np.array([True, False, True, False, True] * 2)
value = DiffPrivStatistics.proportion(data, epsilon)
  • Perform anonymized proportion with condition function:
import numpy as np
from diffpriv_laplace import DiffPrivStatistics


epsilon = 0.1
data = np.array([0.01, -0.01, 0.03, -0.001, 0.1] * 2)

def condition(data):
    return data >= 0.0

value = DiffPrivStatistics.proportion(data, epsilon, condition=condition)
  • Perform anonymized sum:
import numpy as np
from diffpriv_laplace import DiffPrivStatistics


epsilon = 0.1
data = np.array(list(range(1, 101)))
value = DiffPrivStatistics.sum(data, epsilon)
  • Perform anonymized mean:
import numpy as np
from diffpriv_laplace import DiffPrivStatistics


epsilon = 0.1
data = np.array(list(range(1, 101)))
value = DiffPrivStatistics.mean(data, epsilon)
  • Perform anonymized variance:
import numpy as np
from diffpriv_laplace import DiffPrivStatistics


epsilon = 0.1
data = np.array(list(range(1, 101)))
value = DiffPrivStatistics.variance(data, epsilon)

Laplace mechanism

class diffpriv_laplace.DiffPrivLaplaceMechanism(epsilon)

Differential privacy Laplace mechanism.

__init__(epsilon)

Initialize the Laplace mechanism with a privacy budget.

Parameters:epsilon (float) – The privacy budget value to use.
__weakref__

list of weak references to the object (if defined)

anonymize_count(value, size=None)

Anonymizes one or many count value(s).

Parameters:
  • value (float|list|ndarray) – The count value(s).
  • [size] (int|tuple) – Output shape.
Returns:

The anonymized count(s).

Return type:

float|list|ndarray

classmethod anonymize_count_with_budget(value, epsilon, size=None)

Anonymizes one or many count value(s) for a given privacy budget.

Parameters:
  • value (float|list|ndarray) – The count value(s).
  • epsilon (float) – The privacy budget.
  • [size] (int|tuple) – Output shape.
Returns:

The anonymized count(s).

Return type:

float|list|ndarray

anonymize_max(value, size=None)

Anonymizes one or many max value(s).

Parameters:
  • value (float|list|ndarray) – The max value(s).
  • [size] (int|tuple) – Output shape.
Returns:

The anonymized max value(s).

Return type:

float|list|ndarray

classmethod anonymize_max_with_budget(value, epsilon, size=None)

Anonymizes one or many max value(s) for a given privacy budget.

Parameters:
  • value (float|list|ndarray) – The max value(s).
  • epsilon (float) – The privacy budget.
  • [size] (int|tuple) – Output shape.
Returns:

The anonymized max value(s).

Return type:

float|list|ndarray

anonymize_mean(value, lower, upper, n, size=None)

Anonymizes one or many mean value(s).

Parameters:
  • value (float|list|ndarray) – The mean value(s).
  • lower (float) – The lower bound of the data.
  • upper (float) – The upper bound of the data.
  • n (float) – The total number of observations.
  • [size] (int|tuple) – Output shape.
Returns:

The anonymized mean(s).

Return type:

float|list|ndarray

classmethod anonymize_mean_with_budget(value, lower, upper, n, epsilon, size=None)

Anonymizes one or many mean value(s) for a given privacy budget.

Parameters:
  • value (float|list|ndarray) – The mean value(s).
  • lower (float) – The lower bound of the data.
  • upper (float) – The upper bound of the data.
  • n (float) – The total number of observations.
  • epsilon (float) – The privacy budget.
  • [size] (int|tuple) – Output shape.
Returns:

The anonymized mean(s).

Return type:

float|list|ndarray

anonymize_median(value, size=None)

Anonymizes one or many ,median value(s).

Parameters:
  • value (float|list|ndarray) – The median value(s).
  • [size] (int|tuple) – Output shape.
Returns:

The anonymized median value(s).

Return type:

float|list|ndarray

classmethod anonymize_median_with_budget(value, epsilon, size=None)

Anonymizes one or many median value(s) for a given privacy budget.

Parameters:
  • value (float|list|ndarray) – The median value(s).
  • epsilon (float) – The privacy budget.
  • [size] (int|tuple) – Output shape.
Returns:

The anonymized median value(s).

Return type:

float|list|ndarray

anonymize_min(value, size=None)

Anonymizes one or many min value(s).

Parameters:
  • value (float|list|ndarray) – The min value(s).
  • [size] (int|tuple) – Output shape.
Returns:

The anonymized min value(s).

Return type:

float|list|ndarray

classmethod anonymize_min_with_budget(value, epsilon, size=None)

Anonymizes one or many min value(s) for a given privacy budget.

Parameters:
  • value (float|list|ndarray) – The min value(s).
  • epsilon (float) – The privacy budget.
  • [size] (int|tuple) – Output shape.
Returns:

The anonymized min value(s).

Return type:

float|list|ndarray

anonymize_proportion(value, n, size=None)

Anonymizes one or many proportion value(s).

Parameters:
  • value (float|list|ndarray) – The proportion value(s).
  • n (float) – The total number of observations.
  • [size] (int|tuple) – Output shape.
Returns:

The anonymized proportion(s).

Return type:

float|list|ndarray

classmethod anonymize_proportion_with_budget(value, n, epsilon, size=None)

Anonymizes one or many proportion value(s) for a given privacy budget.

Parameters:
  • value (float|list|ndarray) – The proportion value(s).
  • n (float) – The total number of observations.
  • epsilon (float) – The privacy budget.
  • [size] (int|tuple) – Output shape.
Returns:

The anonymized proportion(s).

Return type:

float|list|ndarray

anonymize_sum(value, lower, upper, size=None)

Anonymizes one or many sum value(s).

Parameters:
  • value (float|list|ndarray) – The sum value(s).
  • lower (float) – The lower bound of the data.
  • upper (float) – The upper bound of the data.
  • [size] (int|tuple) – Output shape.
Returns:

The anonymized sum value(s).

Return type:

float|list|ndarray

classmethod anonymize_sum_with_budget(value, lower, upper, epsilon, size=None)

Anonymizes one or many sum value(s) for a given privacy budget.

Parameters:
  • value (float|list|ndarray) – The sum value(s).
  • lower (float) – The lower bound of the data.
  • upper (float) – The upper bound of the data.
  • epsilon (float) – The privacy budget.
  • [size] (int|tuple) – Output shape.
Returns:

The anonymized sum value(s).

Return type:

float|list|ndarray

anonymize_variance(value, lower, upper, n, size=None)

Anonymizes one or many variance value(s).

Parameters:
  • value (float|list|ndarray) – The variance value(s).
  • lower (float) – The lower bound of the data.
  • upper (float) – The upper bound of the data.
  • n (float) – The total number of observations.
  • [size] (int|tuple) – Output shape.
Returns:

The anonymized variance(s).

Return type:

float|list|ndarray

classmethod anonymize_variance_with_budget(value, lower, upper, n, epsilon, size=None)

Anonymizes one or many variance value(s) for a given privacy budget.

Parameters:
  • value (float|list|ndarray) – The variance value(s).
  • lower (float) – The lower bound of the data.
  • upper (float) – The upper bound of the data.
  • n (float) – The total number of observations.
  • epsilon (float) – The privacy budget.
  • [size] (int|tuple) – Output shape.
Returns:

The anonymized variance(s).

Return type:

float|list|ndarray

classmethod create_count_anonymizer(epsilon)

Creates a count anonymizer instance.

Parameters:epsilon (float) – The privacy budget.
Returns:A count anonymizer with the provided privacy budget.
Return type:DiffPrivCountAnonymizer
classmethod create_counting_anonymizer(epsilon)

Creates a counting anonymizer instance.

Parameters:epsilon (float) – The privacy budget.
Returns:A counting anonymizer with the provided privacy budget.
Return type:DiffPrivCountingAnonymizer
classmethod create_max_anonymizer(epsilon)

Creates a max anonymizer instance.

Parameters:epsilon (float) – The privacy budget.
Returns:A max anonymizer with the provided privacy budget.
Return type:DiffPrivMaxAnonymizer
classmethod create_mean_anonymizer(epsilon, lower, upper, n)

Creates a mean anonymizer instance.

Parameters:
  • epsilon (float) – The privacy budget.
  • lower (float) – The lower bound of the data.
  • upper (float) – The upper bound of the data.
  • n (float) – The total number of observations.
Returns:

A mean anonymizer with the provided privacy budget.

Return type:

DiffPrivMeanAnonymizer

classmethod create_median_anonymizer(epsilon)

Creates a median anonymizer instance.

Parameters:epsilon (float) – The privacy budget.
Returns:A median anonymizer with the provided privacy budget.
Return type:DiffPrivMedianAnonymizer
classmethod create_min_anonymizer(epsilon)

Creates a min anonymizer instance.

Parameters:epsilon (float) – The privacy budget.
Returns:A min anonymizer with the provided privacy budget.
Return type:DiffPrivMinAnonymizer
classmethod create_proportion_anonymizer(epsilon, n)

Creates a proportion anonymizer instance.

Parameters:
  • epsilon (float) – The privacy budget.
  • n (float) – The total number of observations.
Returns:

A proportion anonymizer with the provided privacy budget.

Return type:

DiffPrivProportionAnonymizer

classmethod create_sum_anonymizer(epsilon, lower, upper)

Creates a sum anonymizer instance.

Parameters:
  • epsilon (float) – The privacy budget.
  • lower (float) – The lower bound of the data.
  • upper (float) – The upper bound of the data.
Returns:

A sum anonymizer with the provided privacy budget.

Return type:

DiffPrivSumAnonymizer

classmethod create_variance_anonymizer(epsilon, lower, upper, n)

Creates a variance anonymizer instance.

Parameters:
  • epsilon (float) – The privacy budget.
  • lower (float) – The lower bound of the data.
  • upper (float) – The upper bound of the data.
  • n (float) – The total number of observations.
Returns:

A variance anonymizer with the provided privacy budget.

Return type:

DiffPrivVarianceAnonymizer

epsilon

The privacy budget value.

Returns:The privacy budget value.
Return type:float

Examples:

  • Create an instance with a defined privacy budget:
from diffpriv_laplace import DiffPrivLaplaceMechanism


epsilon = 0.1
anonymizer = DiffPrivLaplaceMechanism(epsilon)
  • Anonymize a count value:
from diffpriv_laplace import DiffPrivLaplaceMechanism


epsilon = 0.1
value = 32.0

# Using the class method
anonymized = DiffPrivLaplaceMechanism.anonymize_count_with_budget(value, epsilon)

# Using an instance
anonymizer = DiffPrivLaplaceMechanism(epsilon)
anonymized = anonymizer.anonymize_count(value)
  • Anonymize a min value:
from diffpriv_laplace import DiffPrivLaplaceMechanism


epsilon = 0.1
value = 32.0

# Using the class method
anonymized = DiffPrivLaplaceMechanism.anonymize_min_with_budget(value, epsilon)

# Using an instance
anonymizer = DiffPrivLaplaceMechanism(epsilon)
anonymized = anonymizer.anonymize_min(value)
  • Anonymize a max value:
from diffpriv_laplace import DiffPrivLaplaceMechanism


epsilon = 0.1
value = 32.0

# Using the class method
anonymized = DiffPrivLaplaceMechanism.anonymize_max_with_budget(value, epsilon)

# Using an instance
anonymizer = DiffPrivLaplaceMechanism(epsilon)
anonymized = anonymizer.anonymize_max(value)

-Anonymize a median value:

from diffpriv_laplace import DiffPrivLaplaceMechanism


epsilon = 0.1
value = 32.0

# Using the class method
anonymized = DiffPrivLaplaceMechanism.anonymize_median_with_budget(value, epsilon)

# Using an instance
anonymizer = DiffPrivLaplaceMechanism(epsilon)
anonymized = anonymizer.anonymize_median(value)
  • Anonymize a proportion value:
from diffpriv_laplace import DiffPrivLaplaceMechanism


epsilon = 0.1
n = 50.0
value = 32.0

# Using the class method
anonymized = DiffPrivLaplaceMechanism.anonymize_proportion_with_budget(value, n, epsilon)

# Using an instance
anonymizer = DiffPrivLaplaceMechanism(epsilon)
anonymized = anonymizer.anonymize_proportion(value, n)
  • Anonymize a sum value:
from diffpriv_laplace import DiffPrivLaplaceMechanism


epsilon = 0.1
lower = 0.1
upper = 100.3
value = 32.0

# Using the class method
anonymized = DiffPrivLaplaceMechanism.anonymize_sum_with_budget(value, lower, upper, epsilon)

# Using an instance
anonymizer = DiffPrivLaplaceMechanism(epsilon)
anonymized = anonymizer.anonymize_sum(value, lower, upper)
  • Anonymize a mean value:
from diffpriv_laplace import DiffPrivLaplaceMechanism


epsilon = 0.1
lower = 0.1
upper = 100.3
n = 50.0
value = 32.0

# Using the class method
anonymized = DiffPrivLaplaceMechanism.anonymize_mean_with_budget(value, lower, upper, n, epsilon)

# Using an instance
anonymizer = DiffPrivLaplaceMechanism(epsilon)
anonymized = anonymizer.anonymize_mean(value, lower, upper, n)
  • Anonymize a variance value:
from diffpriv_laplace import DiffPrivLaplaceMechanism


epsilon = 0.1
lower = 0.1
upper = 100.3
n = 50.0
value = 32.0

# Using the class method
anonymized = DiffPrivLaplaceMechanism.anonymize_variance_with_budget(value, lower, upper, n, epsilon)

# Using an instance
anonymizer = DiffPrivLaplaceMechanism(epsilon)
anonymized = anonymizer.anonymize_variance(value, lower, upper, n)

Index