Class Count
- java.lang.Object
-
- com.google.privacy.differentialprivacy.Count
-
public class Count extends java.lang.ObjectCalculates a differentially private count for a collection of values using the Laplace or Gaussian mechanism.This class allows a single privacy unit (e.g., an individual) to contribute data to multiple different partitions. The class does not check whether the number of partitions is within the specified bounds. This is the responsibility of the caller.
This class assumes that each privacy unit may contribute to a single partition only once (i.e., only one data contribution per privacy unit per partition), it doesn't do clamping. For datasets with multiple contributions from the same user to a single partition
BoundedSumshould be used instead.The user can provide a
Noiseinstance which will be used to generate the noise. If no instance is specified,LaplaceNoiseis applied.This class provides an unbiased estimator for the raw count meaning that the expected value of the differentially private count is equal to the raw count.
Note: this class is not thread-safe.
For more implementation details, see
computeResult().For general details and key definitions, see this introduction to Differential Privacy.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCount.Params
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static Count.Params.Builderbuilder()ConfidenceIntervalcomputeConfidenceInterval(double alpha)Computes aConfidenceIntervalwith integer bounds that contains the trueCountwith a probability greater or equal to 1 - alpha using the noisedCountcomputed bycomputeResult().longcomputeResult()Calculates and returns a differentially private count of elements added usingincrement()andincrementBy(long).java.util.Optional<java.lang.Long>computeThresholdedResult(double thresholdDelta)Returns either ofcomputeResult()orOptional.empty().byte[]getSerializableSummary()Returns a serializable summary of the current state of thisCountinstance and its parameters.voidincrement()Increments count by one.voidincrementBy(long count)Increments count by the given value.voidmergeWith(byte[] otherCountSummary)Merges the output ofgetSerializableSummary()from a different instance ofCountwith this instance.
-
-
-
Method Detail
-
builder
public static Count.Params.Builder builder()
-
increment
public void increment()
Increments count by one.- Throws:
java.lang.IllegalStateException- if this this instance ofCounthas already been queried or serialized.
-
incrementBy
public void incrementBy(long count)
Increments count by the given value. Note, that this shouldn't be used to count multiple contributions to a partition from the same user.- Throws:
java.lang.IllegalStateException- if this this instance ofCounthas already been queried or serialized.
-
computeResult
public long computeResult()
Calculates and returns a differentially private count of elements added usingincrement()andincrementBy(long). The method can be called only once for a given collection of elements. All subsequent calls will throw an exception.The returned value is an unbiased estimate of the raw count.
The returned value may sometimes be negative. This can be corrected by setting negative results to 0. Note that such post processing introduces bias to the result.
- Throws:
java.lang.IllegalStateException- if this this instance ofCounthas already been queried or serialized.
-
computeConfidenceInterval
public ConfidenceInterval computeConfidenceInterval(double alpha)
Computes aConfidenceIntervalwith integer bounds that contains the trueCountwith a probability greater or equal to 1 - alpha using the noisedCountcomputed bycomputeResult().Refer to this doc for more information.
- Throws:
java.lang.IllegalStateException- if this this instance ofCounthas not been queried yet.
-
computeThresholdedResult
public java.util.Optional<java.lang.Long> computeThresholdedResult(double thresholdDelta)
Returns either ofcomputeResult()orOptional.empty(). The result is (epsilon, noiseDelta + thresholdDelta)-differentially private assuming that empty counts are not published. The method can be called only once for a given collection of elements. All subsequent calls will throw an exception.To ensure that the boolean signal of a count's publication satisfies (0, thresholdDelta)-differential privacy, noised counts smaller than an appropriately set threshold k > 0 are returned as
Optional.empty(). It is the responsibility of the caller of this method to ensure that a count that returned empty is not published.- Parameters:
thresholdDelta- the privacy budget spent on publishing non-empty counts.
-
getSerializableSummary
public byte[] getSerializableSummary()
Returns a serializable summary of the current state of thisCountinstance and its parameters. The summary can be used to merge this instance with another instance ofCount.This method cannot be invoked if the count has already been queried, i.e.,
computeResult()has been called. Moreover, after this instance ofCounthas been serialized once, no further modification, queries or serialization is possible anymore.- Throws:
java.lang.IllegalStateException- if this this instance ofCounthas already been queried or serialized.
-
mergeWith
public void mergeWith(byte[] otherCountSummary)
Merges the output ofgetSerializableSummary()from a different instance ofCountwith this instance. Intended to be used in the context of distributed computation.- Throws:
java.lang.IllegalArgumentException- if the parameters of the two instances (epsilon, delta, contribution bounds, etc.) do not match or if the passed serialized summary is invalid.java.lang.IllegalStateException- if this this instance ofCounthas already been queried or serialized.
-
-