Skip to content

Instantly share code, notes, and snippets.

@j2kun
Created April 9, 2022 21:21
Show Gist options
  • Save j2kun/c431d227fff5ba1b68adab9ca31e1416 to your computer and use it in GitHub Desktop.
Save j2kun/c431d227fff5ba1b68adab9ca31e1416 to your computer and use it in GitHub Desktop.
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