Repository navigation
Fix group-order dependence in Java Fisher exact test - #12502
jiteshkhatri11 wants to merge 3 commits into
Conversation
|
Two small suggestions: The expected values are R's 7-digit printout (exact ABL1 is 0.6859270045884647, so 0.685927 sits close to the 1e-8 delta). Values from print(..., digits = 17) would make the tests sturdier. R also clamps the result with min(1, ...). Adding Math.min(1.0, p) would avoid a p-value slightly above 1 from rounding. |
|
Thanks for the feedback! I've updated the PR with full-precision expected values, added the upper-bound checks, and clamped the final two-tailed p-value using |
arieleven
left a comment
There was a problem hiding this comment.
Nice catch. I re-implemented the old and new comparison in a few lines of Python to check the three cases in the test, and the numbers line up:
| table (a,b,c,d) | old, original order | old, swapped | new, both orders |
|---|---|---|---|
| 299,1,298,2 | 0.6244 | 1.0000 | 1.0000 |
| 296,4,298,2 | 0.4512 | 0.6859 | 0.6859 |
| 265,35,285,15 | 0.00313 | 0.00458 | 0.00458 |
So before the fix, the EGFR-like case could flip between p=0.003 and p=0.005 just from which group was listed first, which matters near the usual thresholds. The tolerance also matches what R's fisher.test does (relErr <- 1 + 10^(-7) when it collects tables at least as extreme), so the results should now agree with R as well.
Two small things:
- The test asserts against the expected values with
delta = 1e-12. My independent recomputation of the ABL1 case came out at0.68592700458799, about 5e-13 away from the constant in the test. That's still inside the delta, but it's close enough that a different JVM orMath.logintrinsic could push it over. Something like1e-10would keep the test meaningful without the flakiness risk. getCumulativePValuedoesn't compare againstbaseP, so it isn't affected. Might be worth a sentence in the description so reviewers don't wonder.
|
Thanks for checking the calculations independently! I've updated the ABL1 test tolerance to I've also clarified in the PR description that The changes are pushed, and the focused tests and Spotless checks pass locally. |
|
@jiteshkhatri11 thanks for this. could you research whether we there are statistics packages that could handle? we might even have something as part of the project already. |
|
I looked into the existing dependencies and a possible alternative. cBioPortal already includes Apache Commons Math 3.6.1, but I couldn't find a direct Fisher exact test implementation in its API. I found Apache Commons Statistics, which provides One thing worth checking is that its two-sided implementation also compares hypergeometric probabilities using I'll compare its results against the three regression cases in this PR, including swapped group order, before recommending whether we should adopt it or keep the current implementation. |
|
I tested the existing Apache Commons Math 3.6.1 dependency using its It provides individual hypergeometric probabilities, but not a complete two-sided Fisher exact test, so we would still need custom enumeration logic. I also compared it against the current PR implementation using the three regression tables, including swapped group order. The PR implementation produced identical results for all three cases with the relative tolerance of Based on these tests, I don't see a clear advantage in replacing the current implementation with Commons Math. It would still require custom two-sided logic. Apache Commons Statistics has a dedicated Fisher exact test implementation, but it would introduce a new dependency. I haven't yet verified its behavior against these regression cases, so I can't conclude that it would resolve the group-order issue. My recommendation is to keep the current implementation for now rather than add a dependency without a demonstrated benefit. |
|
@jiteshkhatri11 adding to your check, since #12501 is mine. I ran a wider sweep: 30,473 tables, each in both group orders, compared with R's rule computed exactly.
So I agree with keeping this implementation. @alisman on your question: the Fisher test already in the project (Datumbox) breaks on large groups, and Commons Statistics would add a dependency and still miss some ties, so this PR's version is the one that matches R. Code: https://gist.github.com/Pradyuman-aviator/baeab391dec1e069a42caf82b4c03bfe |
|
@Pradyuman-aviator Thanks for running the wider comparison! This confirms the results from my regression tests and supports keeping the current implementation. I agree that there's no clear benefit in introducing another dependency here. |
45e10b8 to
b4c7d86
Compare
Description
Fixes group-order-dependent p-value calculations in the Java two-sided Fisher exact test used by alteration enrichment calculations.
A relative tolerance is now applied when comparing table probabilities to reduce inconsistent inclusion of probabilities caused by floating-point precision.
Related Issue
Related to #12501
How to reproduce this issue
Before the fix, the Java calculator returned different p-values for the two orders (approximately 0.6244 and 1.0).
Root Cause
The calculator used strict floating-point comparisons (
candidate <= baseP). Small numerical differences could cause equivalent probabilities to be treated differently when the group order changed.Fix
1e-7.Math.min(1.0, p).1e-10to reduce potential cross-JVM floating-point flakiness.getCumulativePValueis unaffected because it does not compare candidate probabilities againstbaseP.This PR changes the Java implementation only; equivalent implementations in other languages are not modified here.
Testing
FisherExactTestCalculatorTest— 3 tests passed.AlterationEnrichmentUtilTest— 1 test passed.mvn spotless:check— passed.git diff --check— passed.