Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] Avoid very low p-values if the term is only a tiny fraction of the foreground set #76764

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,48 +113,50 @@ public double getScore(long subsetFreq, long subsetSize, long supersetFreq, long
return 0.0;
}

// casting to `long` to round down to nearest whole number
double epsAllDocsInClass = (long)eps(allDocsInClass);
double epsAllDocsNotInClass = (long)eps(allDocsNotInClass);

docsContainTermInClass += epsAllDocsInClass;
docsContainTermNotInClass += epsAllDocsNotInClass;
allDocsInClass += epsAllDocsInClass;
allDocsNotInClass += epsAllDocsNotInClass;

// Adjust counts to ignore ratio changes which are less than 5%
// casting to `long` to round down to nearest whole number
docsContainTermNotInClass = (long)(Math.min(
1.05 * docsContainTermNotInClass,
docsContainTermInClass / allDocsInClass * allDocsNotInClass
) + 0.5);

// casting to `long` to round down to nearest whole number
double epsAllDocsInClass = (long)eps(allDocsInClass);
double epsAllDocsNotInClass = (long)eps(allDocsNotInClass);

if ((allDocsInClass + epsAllDocsInClass) > Long.MAX_VALUE
|| (docsContainTermInClass + epsAllDocsInClass) > Long.MAX_VALUE
|| (allDocsNotInClass + epsAllDocsNotInClass) > Long.MAX_VALUE
|| (docsContainTermNotInClass + epsAllDocsNotInClass) > Long.MAX_VALUE) {
if (allDocsInClass > Long.MAX_VALUE
|| docsContainTermInClass > Long.MAX_VALUE
|| allDocsNotInClass > Long.MAX_VALUE
|| docsContainTermNotInClass > Long.MAX_VALUE) {
throw new AggregationExecutionException(
"too many documents in background and foreground sets, further restrict sets for execution"
);
}

double v1 = new LongBinomialDistribution(
(long)(allDocsInClass + epsAllDocsInClass),
(docsContainTermInClass + epsAllDocsInClass)/(allDocsInClass + epsAllDocsInClass)
).logProbability((long)(docsContainTermInClass + epsAllDocsInClass));
(long)allDocsInClass, docsContainTermInClass / allDocsInClass
).logProbability((long)docsContainTermInClass);

double v2 = new LongBinomialDistribution(
(long)(allDocsNotInClass + epsAllDocsNotInClass),
(docsContainTermNotInClass + epsAllDocsNotInClass)/(allDocsNotInClass + epsAllDocsNotInClass)
).logProbability((long)(docsContainTermNotInClass + epsAllDocsNotInClass));
(long)allDocsNotInClass, docsContainTermNotInClass / allDocsNotInClass
).logProbability((long)docsContainTermNotInClass);

double p2 = (docsContainTermInClass + docsContainTermNotInClass + epsAllDocsNotInClass + epsAllDocsInClass)
/ (allDocsInClass + allDocsNotInClass + epsAllDocsNotInClass + epsAllDocsInClass);
double p2 = (docsContainTermInClass + docsContainTermNotInClass) / (allDocsInClass + allDocsNotInClass);

double v3 = new LongBinomialDistribution((long)(allDocsInClass + epsAllDocsInClass), p2)
.logProbability((long)(docsContainTermInClass + epsAllDocsInClass));
double v3 = new LongBinomialDistribution((long)allDocsInClass, p2)
.logProbability((long)docsContainTermInClass);

double v4 = new LongBinomialDistribution((long)(allDocsNotInClass + epsAllDocsNotInClass), p2)
.logProbability((long)(docsContainTermNotInClass + epsAllDocsNotInClass));
double v4 = new LongBinomialDistribution((long)allDocsNotInClass, p2)
.logProbability((long)docsContainTermNotInClass);

double logLikelihoodRatio = v1 + v2 - v3 - v4;
double pValue = CHI_SQUARED_DISTRIBUTION.survivalFunction(2.0 * logLikelihoodRatio);
return -FastMath.log(FastMath.max(pValue, Double.MIN_NORMAL));
return FastMath.max(-FastMath.log(FastMath.max(pValue, Double.MIN_NORMAL)), 0.0);
}

private double eps(double value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.lessThanOrEqualTo;

Expand Down Expand Up @@ -110,31 +109,31 @@ public void testPValueScore() {
);
assertThat(
FastMath.exp(-new PValueScore(false).getScore(10, 100, 10, 1000)),
closeTo(0.002988594884934073, eps)
closeTo(0.003972388976814195, eps)
);
assertThat(
FastMath.exp(-new PValueScore(false).getScore(10, 100, 200, 1000)),
closeTo(1.0, eps)
);
assertThat(
FastMath.exp(-new PValueScore(false).getScore(20, 10000, 5, 10000)),
closeTo(0.6309430298306147, eps)
closeTo(1.0, eps)
);
}

public void testSmallChanges() {
assertThat(
FastMath.exp(-new PValueScore(false).getScore(1, 4205, 0, 821496)),
closeTo(0.9572480202044421, eps)
closeTo(0.9999037287868853, eps)
);
// Same(ish) ratios
assertThat(
FastMath.exp(-new PValueScore(false).getScore(10, 4205, 195, 82149)),
closeTo(0.9893886454928338, eps)
closeTo(0.9995943820612134, eps)
);
assertThat(
FastMath.exp(-new PValueScore(false).getScore(10, 4205, 1950, 821496)),
closeTo(0.9867689169546193, eps)
closeTo(0.9999942565428899, eps)
);

// 4% vs 0%
Expand All @@ -145,12 +144,12 @@ public void testSmallChanges() {
// 4% vs 2%
assertThat(
FastMath.exp(-new PValueScore(false).getScore(168, 4205, 16429, 821496)),
closeTo(4.78464746423625e-06, eps)
closeTo(8.542608559219833e-5, eps)
);
// 4% vs 3.5%
assertThat(
FastMath.exp(-new PValueScore(false).getScore(168, 4205, 28752, 821496)),
closeTo(0.4728938449949742, eps)
closeTo(0.8833950526957098, eps)
);
}

Expand Down Expand Up @@ -186,7 +185,7 @@ public void testIncreasedSubsetIncreasedScore() {
for (int j = 1; j < 11; j++) {
double nextScore = getScore.apply(j*10L);
assertThat(nextScore, greaterThanOrEqualTo(0.0));
assertThat(nextScore, greaterThan(priorScore));
assertThat(nextScore, greaterThanOrEqualTo(priorScore));
priorScore = nextScore;
}
}
Expand Down