org.apache.pulsar.shade.com.yahoo.sketches.BoundsOnRatiosInThetaSketchedSets Maven / Gradle / Ivy
/*
* Copyright 2015-16, Yahoo! Inc.
* Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms.
*/
package org.apache.pulsar.shade.com.yahoo.sketches;
import org.apache.pulsar.shade.com.yahoo.sketches.theta.Sketch;
/**
* This class is used to compute the bounds on the estimate of the ratio B / A, where:
*
* - A is a Theta Sketch of population PopA.
* - B is a Theta Sketch of population PopB that is a proper subset of A,
* obtained by an intersection of A with some other Theta Sketch C,
* which acts like a predicate or selection clause.
* - The estimate of the ratio PopB/PopA is
* BoundsOnRatiosInThetaSketchedSets.getEstimateOfBoverA(A, B).
* - The Upper Bound estimate on the ratio PopB/PopA is
* BoundsOnRatiosInThetaSketchedSets.getUpperBoundForBoverA(A, B).
* - The Lower Bound estimate on the ratio PopB/PopA is
* BoundsOnRatiosInThetaSketchedSets.getLowerBoundForBoverA(A, B).
*
* Note: The theta of A cannot be greater than the theta of B.
* If B is formed as an intersection of A and some other set C,
* then the theta of B is guaranteed to be less than or equal to the theta of B.
*
* @author Kevin Lang
* @author Lee Rhodes
*/
public final class BoundsOnRatiosInThetaSketchedSets {
private BoundsOnRatiosInThetaSketchedSets() {}
/**
* Gets the approximate lower bound for B over A based on a 95% confidence interval
* @param sketchA the sketch A
* @param sketchB the sketch B
* @return the approximate lower bound for B over A
*/
public static double getLowerBoundForBoverA(final Sketch sketchA, final Sketch sketchB) {
final double thetaA = sketchA.getTheta();
final double thetaB = sketchB.getTheta();
checkThetas(thetaA, thetaB);
final int countB = sketchB.getRetainedEntries(true);
final int countA = (thetaB == thetaA) ? sketchA.getRetainedEntries(true)
: sketchA.getCountLessThanTheta(thetaB);
if (countA <= 0) { return 0; }
return BoundsOnRatiosInSampledSets.getLowerBoundForBoverA(countA, countB, thetaB);
}
/**
* Gets the approximate upper bound for B over A based on a 95% confidence interval
* @param sketchA the sketch A
* @param sketchB the sketch B
* @return the approximate upper bound for B over A
*/
public static double getUpperBoundForBoverA(final Sketch sketchA, final Sketch sketchB) {
final double thetaA = sketchA.getTheta();
final double thetaB = sketchB.getTheta();
checkThetas(thetaA, thetaB);
final int countB = sketchB.getRetainedEntries(true);
final int countA = (thetaB == thetaA) ? sketchA.getRetainedEntries(true)
: sketchA.getCountLessThanTheta(thetaB);
if (countA <= 0) { return 1.0; }
return BoundsOnRatiosInSampledSets.getUpperBoundForBoverA(countA, countB, thetaB);
}
/**
* Gets the estimate for B over A
* @param sketchA the sketch A
* @param sketchB the sketch B
* @return the estimate for B over A
*/
public static double getEstimateOfBoverA(final Sketch sketchA, final Sketch sketchB) {
final double thetaA = sketchA.getTheta();
final double thetaB = sketchB.getTheta();
checkThetas(thetaA, thetaB);
final int countB = sketchB.getRetainedEntries(true);
final int countA = (thetaB == thetaA) ? sketchA.getRetainedEntries(true)
: sketchA.getCountLessThanTheta(thetaB);
if (countA <= 0) { return 0.5; }
return (double) countB / (double) countA;
}
static void checkThetas(final double thetaA, final double thetaB) {
if (thetaB > thetaA) {
throw new SketchesArgumentException("ThetaB cannot be > ThetaA.");
}
}
}