Skip to content

Fix group-order dependence in Java Fisher exact test - #12502

Open
jiteshkhatri11 wants to merge 3 commits into
cBioPortal:masterfrom
jiteshkhatri11:fix-12501-fisher-group-order-invariance
Open

jiteshkhatri11 wants to merge 3 commits into
cBioPortal:masterfrom
jiteshkhatri11:fix-12501-fisher-group-order-invariance

Conversation

@jiteshkhatri11

@jiteshkhatri11 jiteshkhatri11 commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

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

  1. Compare two groups, each containing 300 profiled samples.
  2. Use alteration counts of 2 and 1.
  3. Swap the order of the groups.

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

  • Added a relative tolerance of 1e-7.
  • Applied it to both probability-comparison loops.
  • Added regression tests for AURKA, ABL1 and EGFR.
  • Used full-precision expected p-values in regression tests.
  • Added upper-bound checks and clamped the final two-tailed p-value using Math.min(1.0, p).
  • Increased the ABL1 assertion tolerance to 1e-10 to reduce potential cross-JVM floating-point flakiness.

getCumulativePValue is unaffected because it does not compare candidate probabilities against baseP.

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.

@Pradyuman-aviator

Copy link
Copy Markdown
Contributor

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.
The frontend's FisherExactTestCalculator.ts has the same strict <=, so I'll keep #12501 open for that side.

@jiteshkhatri11

Copy link
Copy Markdown
Contributor Author

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 Math.min(1.0, p). The focused tests and Spotless checks pass locally. I've pushed the changes to the same PR.

@arieleven arieleven left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. The test asserts against the expected values with delta = 1e-12. My independent recomputation of the ABL1 case came out at 0.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 or Math.log intrinsic could push it over. Something like 1e-10 would keep the test meaningful without the flakiness risk.
  2. getCumulativePValue doesn't compare against baseP, so it isn't affected. Might be worth a sentence in the description so reviewers don't wonder.

@jiteshkhatri11

jiteshkhatri11 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for checking the calculations independently!

I've updated the ABL1 test tolerance to 1e-10 to avoid potential cross-JVM floating-point precision issues.

I've also clarified in the PR description that getCumulativePValue is unaffected, since it does not compare candidate probabilities against baseP.

The changes are pushed, and the focused tests and Spotless checks pass locally.

@alisman

alisman commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

@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.

@jiteshkhatri11

Copy link
Copy Markdown
Contributor Author

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 FisherExactTest with two-sided and one-sided alternatives. However, it would introduce a new dependency because it isn't currently declared in the backend pom.xml.

One thing worth checking is that its two-sided implementation also compares hypergeometric probabilities using Pr(X = i) <= Pr(X = k). I haven't established whether it handles the floating-point group-order issue we're fixing here.

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.

@jiteshkhatri11

Copy link
Copy Markdown
Contributor Author

I tested the existing Apache Commons Math 3.6.1 dependency using its HypergeometricDistribution API.

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 1e-7. Commons Math produced differences only at floating-point rounding level (approximately 1e-16) in two cases.

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.

@Pradyuman-aviator

Pradyuman-aviator commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

@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.

tables that change with group order p-values off R (of 60,946)
master 4,861 4,862
this PR 0 0
Commons Statistics 1.3 FisherExactTest 0 96 (48 tables × 2 orders)
Datumbox 0.8.2 Fisher (already in pom.xml) 9,789 most
  • Datumbox is the Fisher test already in the project, but it uses raw factorials and overflows from about 50 samples per group. It returns 0.0 for all three regression tables in this PR.
  • Commons Statistics gets your three cases right and never depends on group order, but it has no tolerance. It misses ties when exactly half of all samples are altered and the groups differ in size. Example: 27/33 vs 23/67 altered, R gives 1.30e-5 and Commons Statistics gives 7.2e-6.

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

@jiteshkhatri11

Copy link
Copy Markdown
Contributor Author

@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.

@onursumer
onursumer force-pushed the fix-12501-fisher-group-order-invariance branch from 45e10b8 to b4c7d86 Compare November 24, 2026 22:13

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants