Class BoundedSum
- java.lang.Object
-
- com.google.privacy.differentialprivacy.BoundedSum
-
public class BoundedSum extends java.lang.ObjectCalculates a differentially private sum 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). Multiple contributions from a single privacy unit should be pre-aggregated before they are passed to this class.
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 bounded sum meaning that the expected value of the differentially private bounded sum is equal to the raw bounded sum.
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 classBoundedSum.Params
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddEntries(java.util.Collection<java.lang.Double> e)Clamps the input values and adds them to the sum.voidaddEntry(double e)Clamps the input value and adds it to the sum.static BoundedSum.Params.Builderbuilder()ConfidenceIntervalcomputeConfidenceInterval(double alpha)Computes a confidence interval that contains the trueBoundedSumwith a probability greater or equal to 1 - alpha using the noisedBoundedSumcomputed bycomputeResult().doublecomputeResult()Computes and returns a differentially private sum of the elements added viaaddEntry(double)andaddEntries(java.util.Collection<java.lang.Double>).byte[]getSerializableSummary()Returns a serializable summary of the current state of thisBoundedSuminstance and its parameters.voidmergeWith(byte[] otherBoundedSumSummary)Merges the output ofgetSerializableSummary()from a different instance ofBoundedSumwith this instance.
-
-
-
Method Detail
-
builder
public static BoundedSum.Params.Builder builder()
-
addEntry
public void addEntry(double e)
Clamps the input value and adds it to the sum.- Throws:
java.lang.IllegalStateException- if this this instance ofBoundedSumhas already been queried or serialized.
-
addEntries
public void addEntries(java.util.Collection<java.lang.Double> e)
Clamps the input values and adds them to the sum.- Throws:
java.lang.IllegalStateException- if this this instance ofBoundedSumhas already been queried or serialized.
-
computeResult
public double computeResult()
Computes and returns a differentially private sum of the elements added viaaddEntry(double)andaddEntries(java.util.Collection<java.lang.Double>). 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 bounded sum.
The returned value may sometimes be outside the set of possible raw bounded sums, e.g., the differentially private bounded sum may be positive although neither the lower nor the upper bound are positive. This can be corrected by the caller of this method, e.g., by snapping the result to the closest value representing a bounded sum that is possible. Note that such post processing introduces bias to the result.
- Throws:
java.lang.IllegalStateException- if this this instance ofBoundedSumhas already been queried or serialized.
-
computeConfidenceInterval
public ConfidenceInterval computeConfidenceInterval(double alpha)
Computes a confidence interval that contains the trueBoundedSumwith a probability greater or equal to 1 - alpha using the noisedBoundedSumcomputed bycomputeResult().Refer to this doc for more information.
- Throws:
java.lang.IllegalStateException- if this this instance ofBoundedSumhas not been queried yet.
-
getSerializableSummary
public byte[] getSerializableSummary()
Returns a serializable summary of the current state of thisBoundedSuminstance and its parameters. The summary can be used to merge this instance with another instance ofBoundedSum.This method cannot be invoked if the sum has already been queried, i.e.,
computeResult()has been called. Moreover, after this instance ofBoundedSumhas been serialized once, no further modification, queries or serialization is possible anymore.- Throws:
java.lang.IllegalStateException- if this this instance ofBoundedSumhas already been queried or serialized.
-
mergeWith
public void mergeWith(byte[] otherBoundedSumSummary)
Merges the output ofgetSerializableSummary()from a different instance ofBoundedSumwith 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 ofBoundedSumhas already been queried or serialized.
-
-