[
https://issues.apache.org/jira/browse/STATISTICS-104?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18153109#comment-18153109
]
Alex Herbert commented on STATISTICS-104:
-----------------------------------------
I have fixed this issue by adding tolerances to the inference tests that
compare values that are inexact.
h2. BinomialTest
{code:java}
BinomialTest.withRelativeTolerance(eps) // default 1e-15
{code}
The BinomialTest uses the log probability from the binomial distribution. This
is very accurate. But a few ULP error in probabilities that should be exactly
the same lead to issues. For example:
{noformat}
BinomialDistribution d = BinomialDistribution.of(11, 0.75)
d.probability(8) = 0.258103609085083
d.probability(9) = 0.25810360908508295{noformat}
Here a lack of tolerance in comparisons can cause a difference in the p-value
for the binomial test at k=9 of 0.258 because p(k=8) is larger by 1 ULP when it
should be the same.
h2. FisherExactTest
Similar to the BinomialTest. This uses probabilities from the hypergeometric
distribution. These are 3 log probabilities from the binomial distribution
added together with cancellation. So this requires a higher default relative
tolerance. The API is the same:
{code:java}
FisherExactTest.withDefaults()
.withRelativeTolerance(eps) // default 1e-14
{code}
h2. UnconditionedExactTest
This is more involved. This compares a 2x2 contingency table to all other
tables using a score. The score can be a Z statistic, or the Fisher p-value for
the table, i.e. the p-value computed in the FisherExactTest. This is Boschloo's
test.
For Boschloo's test the Fisher p-values are computed as per the
FisherExactTest. So a 1e-14 releative tolerance works. Once the p-values are
computed these are then compared for all candidate tables against the Fisher
p-value for the input table. Since each p-value may be inexact I have added a
tolerance for this too. Using 1e-14 worked on the current test suite.
This is the configuration for Boschloo's two-sided test:
{code:java}
UnconditionedExactTest.withDefaults()
.withBoschlooRelativeTolerance(eps) // default 1e-14
.withFisherRelativeTolerance(eps) // default 1e-14
{code}
The other score to test if tables are more extreme is the Z score. This can be
pooled or unpooled. For a table [[a, b], [c, d]]:
{noformat}
m = a + c
n = b + d
p0 = a / m
p1 = b / n
Z = (p0 - p1) / sqrt(v)
v = variance
pooled:
p = (a+b) / (m+n)
v = p * (1-p) * (1/m + 1/n)
= [ (a+b) * ((m+n)-(a+b)) / (m+n)^2 ] * ((m+n) / (m*n))
= [ (a+b) * ((m+n)-(a+b)) ] / ((m+n) * (m*n))
unpooled:
v = p0 * (1 - p0) / m + p1 * (1 - p1) / n
= (m*a - a*a) / m^3 + (n*b - b*b) / n^3{noformat}
The 1.1 implementation for computing Z used doubles to represent p0 and p1, and
computed the variance in double. This has a number of places for rounding error
in the fractional terms. I updated the computation to use rational numbers
where possible with exact numerators and then rounding error from division by
denominators that may be inexact. The Z used for comparison is:
{noformat}
p0 = n * a / (m * n)
p1 = m * b / (m * n)
(p0 - p1) / sqrt(v) = (n*a - m*b) / (m*n) / sqrt(v)
= (n*a - m*b) / sqrt(v)
{noformat}
The denominator (m*n) is constant and does not changes comparison of Z values.
So it can be removed to create a scaled Z value. The variance can be computed
using rational expansions of the terms. This reduces the places for rounding
error but does not eliminate them
I compared the various cases in the test suite to a BigFraction implementation
of the Z score. BigFraction does not have a sqrt so the computation returns Z
squared with a sign. The closest representable Z can then be found using:
{code:java}
BigFraction zz = ...
Math.copySign(Math.sqrt(zz.abs().doubleValue()), zz.signum())
{code}
The current code equals this to 1 ULP. The old code was worse. I then used the
BigFraction zSquared to exactly separate tables into those that are more
extreme using BigFraction's compareTo method for exact <= testing. Across all
the test suite the new implementation for Z makes the same identification of
more extreme tables with 0 ULP tolerance. The old method required a 3 ULP
equality tolerance to match the result. Without the 3 ULP tolerance the
UnconditionedExactTest p-value differs in the 3rd decimal place. It is not as
big an issue as for Boschloo's test but is easily observable.
I think the new Z computation is more robust. But It will have rounding error
on much larger tables when the rational numbers have numerators and
denominators that are not representable doubles. To avoid Z comparisons raising
this issue again I added a ULP tolerance for Z comparisons with a default 2 ULP
setting:
{code:java}
UnconditionedExactTest.withDefaults()
.withZUlpTolerance(n) // default 2
{code}
h2. Behaviour Change
This changes the behaviour to the inference tests. But it fixes bugs that are
difficult to observe unless looked for. In typical usage this is simply an
admission that the underlying values used to make choices in the inference
tests are not computed to full double precision. So the choice must have a
small tolerance to allow for almost equal values to be treated as equal.
The old behaviour can be partially restored by setting all the tolerances back
to zero. However that is not recommend. Doing so will activate the bug that
this fixes. Also note that it is not possible to restore the old behaviour for
Z since the computation is now more precise. This change makes the
UnconditionedExactTest to be more accurate than the R and SciPy reference
implementations that provide results for the test suite. So tests have been
updated as required.
h2. No tolerance reporting
Note that the inference tests currently have no way to signal that values being
compared were very close. So it is not obvious when changing the tolerance
would make a difference. It would be possible to store the closest possible
result above the comparison threshold and provide this as part of the
SignificanceResult. For example the BinomialTest example above when run with
relative tolerance 0 would have a result property that returned 2^-52 (1 ULP)
for the closest result above the threshold being used for comparisons.
I've not made this change. It would require more methods in the API, and also
requires a user to inspect the value and decide if they should re-run the
inference test. E.g. first excluded value at 0.16 relative = OK; first excluded
value at 1e-15 relative = Re-run with a higher threshold.
Ideally all underlying values used for comparisons would be exact. Then this is
not an issue. The next ideal is that the error in values used for comparisons
is known and the comparison can identify values as equal using their error
bound. The current solution knows that the values are inexact, but not by how
much. It has settings that match the typical error observed in these values.
But it provides a way to adjust those tolerance values. This is better than
implementing the tolerances and not informing the user, or not allowing them to
be changed.
> Inference p-values are incorrect when less-than-or-equal probability
> comparisons have rounding errors in the p-values being compared
> ------------------------------------------------------------------------------------------------------------------------------------
>
> Key: STATISTICS-104
> URL: https://issues.apache.org/jira/browse/STATISTICS-104
> Project: Commons Statistics
> Issue Type: Bug
> Components: inference
> Affects Versions: 1.1
> Reporter: Alex Herbert
> Priority: Minor
>
> The FisherExactTest computes the two-sided probability using the probability
> that a random table has probability equal to or less than the input table.
> Both the computation for the input table, and the computation for other
> tables are inexact. Their p-values can vary by a few ULP. In this case the
> test may not identify random tables that have an equal probability to the
> input table. Use of {{p(other) <= p(input)}} is too strict and tables with
> the same probability can be missed.
> FisherExactTest:
> {noformat}
> TWO_SIDED
> {{0, 2}, {2, 3}} : 0.5238095238095237 -> 1.0
> {{2, 8}, {13, 7}} : 0.027686156921539203 -> 502/10005 =
> 0.050174912543728{noformat}
> This also affects the UnconditionedExactTest and the BinomialTest where
> searches for p-values {{<=}} target are used.
> UnconditionedExactTest:
> {noformat}
> BOSCHLOO
> TWO_SIDED
> {{0,2},{2,3}} 0.4375 -> 1.0
> {{1,0},{10,11}} 0.3340 -> 1
> {{2,8},{13,7}} 0.0174 -> 0.0234
> {{0,7},{4,4}} 0.0376 -> 0.0510
> {{3,1},{4,13}} 0.0660 -> 0.0475
> LESS_THAN
> {{3,4},{4,3}} 0.2876 -> 0.3953
> {{3,8},{5,2}} 0.0470 -> 0.0550
> Z_POOLED
> TWO_SIDED
> {{1,1},{0,2}}: 0.3125 -> 0.5
> {{7,12},{8,3}}: 0.0681534 -> 0.0682183{noformat}
> BinomialTest
> {noformat}
> TWO_SIDED (n, k, probability=0.5)
> (3, 2) 0.625 -> 1
> (21, 10) 0.832 -> 1
> (13, 3) 0.0574 -> 0.0923
> (19, 5) 0.0414 -> 0.0636{noformat}
> The suggested fix is to add a tolerance for equality. This can raise the
> threshold for {{<=}} to a higher value:
> {noformat}
> p = p + p * eps{noformat}
> Where eps is a small relative epsilon. This is effectively zero in the
> current code. It could be configured using a setter method:
> {code:java}
> FisherExactTest.withDefaults().withRelativeTolerance(tol) // tol is small,
> e.g. 1e-13
> {code}
> This bug was identified and the error cases provided by Eran Shaham.
> The tolerance should be ~1e-14 or higher. It is noted that the Boschloo test
> may require a higher threshold than the other tests.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)