Skip to content

STATISTICS-104: Fix comparisons with rounding errors - #142

Merged
aherbert merged 1 commit into
apache:masterfrom
aherbert:statistics-104-tolerance
Dec 4, 2026
Merged

aherbert merged 1 commit into
apache:masterfrom
aherbert:statistics-104-tolerance

Conversation

@aherbert

@aherbert aherbert commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Rounding errors in values used for comparisons create incorrect p-value computations in inference tests: BinomialTest; FisherExactTest; and UnconditionedExactTest.

Update the binomial distribution log probability computation. This now handles symmetry in x or (n - x); sums terms in magnitude order; and detects a bimodal distribution and returns only 1 value for both modes. This impacts both the binomial distribution, and the hypergeometric distribution.

The binomial distribution is now symmetric at the mode for bimodal distributions; and is symmetric when the probability of success is 0.5.

The hypergeometric distribution is defined by the log probability function for its PMF and cumulative probability functions. It sums 3 binomial log probabilities. This is now done in magnitude order. A bimodal distribution is detected and the value of the upper mode in this case is always used. The PMF now exploits parameter symmetry in the number of successes (K) and sample size (n):

f(k; N, K, n) = f(k; N, n, K)

where N is the population size. The K or n that chooses a binomial probability closest to 0.5 is chosen. This ensures the distribution is symmetric when p=0.5.

A bug in the BinomialTest and FisherExactTest two-sided p-value computation is now fixed at the mode. When k=mode then all other values have equal or lower probability and the significance result is 1.0.

A bug in the BinomialTest where one side of a symmetric binomial distribution computed values p(n-k) != p(k) is now fixed.

The FisherExactTest computes p-values for all possible 2x2 contingency tables using the hypergeometric distribution. Because this now supports parameter symmetry two symmetric contingency tables have the same p-value.

This also fixes Boschloo's method in the UnconditionedExactTest which computes Fisher p-values as a score to compare candidate contingency tables.

The Z pooled/unpooled method of the UnconditionedExactTest has been updated to use rational numbers for p-values and exploit symmetry to compute the Z scores. Two scores from symmetric contingency tables now have the same magnitude.


  • [x ] Read the contribution guidelines for this project.
  • [x ] Read the ASF Generative Tooling Guidance if you use Artificial Intelligence (AI).
  • I used AI to create any part of, or all of, this pull request. Which AI tool was used to create this pull request, and to what extent did it contribute?
  • [x ] Run a successful build using the default Maven goal with mvn; that's mvn on the command line by itself.
  • [x ] Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
  • [x ] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • [x ] Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.

@codecov-commenter

codecov-commenter commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.71%. Comparing base (d524259) to head (7fe52db).
⚠️ Report is 341 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #142      +/-   ##
============================================
+ Coverage     99.62%   99.71%   +0.09%     
+ Complexity     2441      659    -1782     
============================================
  Files           116      122       +6     
  Lines          6087     6785     +698     
  Branches        951     1043      +92     
============================================
+ Hits           6064     6766     +702     
+ Misses           10        6       -4     
  Partials         13       13              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Rounding errors in values used for comparisons create incorrect p-value
computations in inference tests: BinomialTest; FisherExactTest; and
UnconditionedExactTest.

Update the binomial distribution log probability computation. This now
handles symmetry in x or (n - x); sums terms in magnitude order; and
detects a bimodal distribution and returns only 1 value for both modes.
This impacts both the binomial distribution, and the hypergeometric
distribution.

The binomial distribution is now symmetric at the mode for bimodal
distributions; and is symmetric when the probability of success is 0.5.

The hypergeometric distribution is defined by the log probability
function for its PMF and cumulative probability functions. It sums 3
binomial log probabilities. This is now done in magnitude order. A
bimodal distribution is detected and the value of the upper mode in this
case is always used. The PMF now exploits parameter symmetry in the
number of successes (K) and sample size (n):

f(k; N, K, n) = f(k; N, n, K)

where N is the population size. The K or n that chooses a binomial
probability closest to 0.5 is chosen. This ensures the distribution is
symmetric when p=0.5.

A bug in the BinomialTest and FisherExactTest two-sided p-value
computation is now fixed at the mode. When k=mode then all other values
have equal or lower probability and the significance result is 1.0.

A bug in the BinomialTest where one side of a symmetric binomial
distribution computed values p(n-k) != p(k) is now fixed.

The FisherExactTest computes p-values for all possible 2x2 contingency
tables using the hypergeometric distribution. Because this now supports
parameter symmetry two symmetric contingency tables have the same
p-value.

This also fixes Boschloo's method in the UnconditionedExactTest which
computes Fisher p-values as a score to compare candidate contingency
tables.

The Z pooled/unpooled method of the UnconditionedExactTest has been
updated to use rational numbers for p-values and exploit symmetry to
compute the Z scores. Two scores from symmetric contingency tables now
have the same magnitude.
@aherbert
aherbert force-pushed the statistics-104-tolerance branch from 2c01287 to 7fe52db Compare December 4, 2026 07:41
@aherbert
aherbert merged commit 28a6268 into apache:master Dec 4, 2026
7 checks passed
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.

2 participants