com.yahoo.sketches.BoundsOnRatiosInSampledSets 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 com.yahoo.sketches;
import static com.yahoo.sketches.BoundsOnBinomialProportions.approximateLowerBoundOnP;
import static com.yahoo.sketches.BoundsOnBinomialProportions.approximateUpperBoundOnP;
/**
* This class is used to compute the bounds on the estimate of the ratio |B| / |A|, where:
*
* - |A| is the unknown size of a set A of unique identifiers.
* - |B| is the unknown size of a subset B of A.
* - a = |SA| is the observed size of a sample of A
* that was obtained by Bernoulli sampling with a known inclusion probability f.
* - b = |SA ∩ B| is the observed size of a subset
* of SA.
*
*
* @author Kevin Lang
*/
public final class BoundsOnRatiosInSampledSets {
private static final double NUM_STD_DEVS = 2.0; //made a constant to simplify interface.
private BoundsOnRatiosInSampledSets() {}
/**
* Return the approximate lower bound based on a 95% confidence interval
* @param a See class javadoc
* @param b See class javadoc
* @param f the inclusion probability used to produce the set with size a and should
* generally be less than 0.5. Above this value, the results not be reliable.
* When f = 1.0 this returns the estimate.
* @return the approximate upper bound
*/
public static double getLowerBoundForBoverA(long a, long b, double f) {
checkInputs(a, b, f);
if (a == 0) { return 0.0; }
if (f == 1.0) { return (double) b / a; }
return approximateLowerBoundOnP(a, b, NUM_STD_DEVS * hackyAdjuster(f));
}
/**
* Return the approximate upper bound based on a 95% confidence interval
* @param a See class javadoc
* @param b See class javadoc
* @param f the inclusion probability used to produce the set with size a.
* @return the approximate lower bound
*/
public static double getUpperBoundForBoverA(long a, long b, double f) {
checkInputs(a, b, f);
if (a == 0) { return 1.0; }
if (f == 1.0) { return (double) b / a; }
return approximateUpperBoundOnP(a, b, NUM_STD_DEVS * hackyAdjuster(f));
}
/**
* Return the estimate of b over a
* @param a See class javadoc
* @param b See class javadoc
* @return the estimate of b over a
*/
public static double getEstimateOfBoverA(long a, long b) {
checkInputs(a, b, 0.3);
if (a == 0) { return 0.5; }
return (double) b / a;
}
/**
* This hackyAdjuster is tightly coupled with the width of the confidence interval normally
* specified with number of standard deviations. To simplify this interface the number of
* standard deviations has been fixed to 2.0, which corresponds to a confidence interval of
* 95%.
* @param f the inclusion probability used to produce the set with size a.
* @return the hacky Adjuster
*/
private static double hackyAdjuster(double f) {
double tmp = Math.sqrt(1.0 - f);
return (f <= 0.5) ? tmp : tmp + (0.01 * (f - 0.5));
}
static void checkInputs(long a, long b, double f) {
if ( ( (a - b) | (a) | (b) ) < 0) { //if any group goes negative
throw new SketchesArgumentException(
"a must be >= b and neither a nor b can be < 0: a = " + a + ", b = " + b);
}
if ((f > 1.0) || (f <= 0.0)) {
throw new SketchesArgumentException("Required: ((f <= 1.0) && (f > 0.0)): " + f);
}
}
/**
* Return the estimate of A. See class javadoc.
* @param a See class javadoc
* @param f the inclusion probability used to produce the set with size a.
* @return the approximate lower bound
*/
public static double getEstimateOfA(long a, double f) {
checkInputs(a, 1, f);
return a / f;
}
/**
* Return the estimate of B. See class javadoc.
* @param b See class javadoc
* @param f the inclusion probability used to produce the set with size a.
* @return the approximate lower bound
*/
public static double getEstimateOfB(long b, double f) {
checkInputs(b + 1, b, f);
return b / f;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy