Created
April 9, 2022 21:21
-
-
Save j2kun/c431d227fff5ba1b68adab9ca31e1416 to your computer and use it in GitHub Desktop.
Differential Privacy Test Util, extracted from https://github.com/google/differential-privacy/blob/c2376f0daaf406e1524b462accaa9cbb548fd6d1/java/main/com/google/privacy/differentialprivacy/testing/StatisticalTestsUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static <T> boolean verifyApproximateDp( | |
T[] samplesA, T[] samplesB, double epsilon, double delta, double deltaTolerance) { | |
Map<T, Long> histogramA = buildHistogram(samplesA); | |
Map<T, Long> histogramB = buildHistogram(samplesB); | |
double testValueA = computeAproximateDpTestValue(histogramA, histogramB, epsilon, samplesA.length); | |
double testValueB = computeAproximateDpTestValue(histogramB, histogramA, epsilon, samplesA.length); | |
return testValueA < delta + deltaTolerance && testValueB < delta + deltaTolerance; | |
} | |
private static <T> double computeAproximateDpTestValue( | |
Map<T, Long> histogramA, Map<T, Long> histogramB, double epsilon, int numOfSamples) { | |
double testValue = 0; | |
for (T sample : histogramA.keySet()) { | |
double sampleCountA = histogramA.get(sample); | |
if (histogramB.containsKey(sample)) { | |
double sampleCountB = histogramB.get(sample); | |
testValue += max(0.0, (sampleCountA - Math.exp(epsilon) * sampleCountB) / numOfSamples); | |
} else { | |
testValue += sampleCountA / numOfSamples; | |
} | |
} | |
return testValue; | |
} | |
private static <T> Map<T, Long> buildHistogram(T[] samples) { | |
Map<T, Long> histogram = new HashMap<>(); | |
for (T sample : samples) { | |
if (histogram.containsKey(sample)) { | |
histogram.put(sample, histogram.get(sample) + 1); | |
} else { | |
histogram.put(sample, 1L); | |
} | |
} | |
return histogram; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment