Use scipy for Fisher combination and contingency chi-square - #6
Merged
Conversation
Replace two hand-rolled statistics with their vetted scipy equivalents (scipy is already a dependency): - Fisher's method (-2*sum(ln p) + chi2 tail), written out at four sites (fisher_bh_meta, fft_domain_battery, and both battery verdicts) -> scipy.stats.combine_pvalues(pvals, method="fisher"). The statistic and p-value are identical; two of the sites used `1 - chi2.cdf`, so they also gain tail accuracy (combine_pvalues uses chi2.sf). - block_homogeneity's hand-rolled contingency chi-square -> scipy.stats.chi2_contingency(obs, correction=False). Empty bins/blocks are dropped first (chi2_contingency requires positive margins); this also fixes a latent detail -- the former code counted empty bins toward the degrees of freedom, which chi2_contingency correctly does not. Suite unchanged: 134 passed, 1 skipped. Note: scipy.stats.monte_carlo_test was considered for mc_calibrate/mc_pvalue but deliberately NOT adopted -- it needs a custom rvs + non-vectorized statistic (no net code saving), and it would change the seeded MC p-value stream for no correctness gain (the current (1+count)/(B+1) estimator is already correct). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace hand-rolled statistics with vetted scipy equivalents (scipy is already a dependency). Part of the code-quality pass.
Changes
-2·Σln(p)+ chi² tail, written out at four sites (fisher_bh_meta,fft_domain_battery, and both battery verdicts) →scipy.stats.combine_pvalues(pvals, method="fisher"). Statistic and p-value are identical; two of the sites used1 - chi2.cdf, so they also gain tail accuracy (combine_pvaluesuseschi2.sf).block_homogeneity's hand-rolled row/col-totals loop →scipy.stats.chi2_contingency(obs, correction=False). Empty bins/blocks are dropped first (it requires positive margins); this also fixes a latent detail — the former code counted empty bins toward the degrees of freedom, whichchi2_contingencycorrectly does not.Deliberately not swapped
scipy.stats.monte_carlo_testformc_calibrate/mc_pvalue: it needs a customrvs+ non-vectorizedstatistic(no net code saving) and would change the seeded MC p-value stream for no correctness gain — the current(1+count)/(B+1)estimator is already correct. A lateral move, so skipped.Verification
Suite unchanged: 134 passed, 1 skipped.
Independent of #5 (dedup); touches different regions of
univariate_tests.py, so the two merge cleanly in either order.🤖 Generated with Claude Code