Skip to content

omics-rust/rsomics-binom-test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rsomics-binom-test

Exact binomial test — a value-exact, faster scipy.stats.binomtest.

Tests the null hypothesis that the success probability of a Bernoulli experiment is p, given k successes in n trials. Reports the exact p-value, the proportion estimate k/n, and a confidence interval (Clopper-Pearson exact or Wilson).

rsomics-binom-test --successes 3 --trials 15 --p 0.1 --alternative two-sided
# pvalue<TAB>proportion<TAB>ci_low<TAB>ci_high
0.18406106910639083	0.2	0.04331200510583664	0.48089113380685317

Usage

rsomics-binom-test --successes K --trials N [--p 0.5]
                   [--alternative two-sided|less|greater]
                   [--ci-method exact|wilson|wilsoncc]
                   [--confidence 0.95]
  • --alternativegreater is binom.sf(k−1, n, p), less is binom.cdf(k, n, p), two-sided sums the binomial pmf over every outcome no more likely than the observed one (scipy's exact two-sided rule).
  • --ci-methodexact is the Clopper-Pearson interval (Beta quantiles), wilson/wilsoncc are the Wilson score interval without / with continuity correction.
  • --json emits one machine-readable envelope.
  • --batch FILE runs many k<TAB>n[<TAB>p] rows from a TSV in a single process, one result line per row — the throughput path against a per-call scipy loop (and parallel across -t threads).
rsomics-binom-test --batch tests.tsv --alternative two-sided -t8

Performance

200 000 (k, n, p) triples (n ∈ [50, 5000]), Apple M2, single-thread each:

mean wall relative
rsomics-binom-test -t1 2.63 s 1.00×
scipy 1.17.1 loop (1 thread) 77.75 s 29.55×

29.55 ± 0.83× faster single-threaded (hyperfine, 5 runs). The --batch mode also scales across threads.

Accuracy

Value-exact against scipy.stats.binomtest (scipy 1.17.1). Over the 200k-row fixture, for every p-value scipy can represent (≥ 1e-15) the worst relative error is 1.4e-11; confidence-interval bounds agree to 1e-11. The only larger discrepancies are p-values below ~1e-250, where scipy's pmf summation underflows to exactly 0.0 while the log-space computation here keeps a nonzero subnormal value — a regime with no practical meaning.

Origin

This crate is an independent Rust reimplementation of scipy.stats.binomtest based on:

  • SciPy's scipy/stats/_binomtest.py (BSD-3-Clause): the exact two-sided pmf-sum rule with its rerr = 1 + 1e-7 relative-tolerance guard and the implicit binary search that brackets the opposite tail, plus the Clopper-Pearson and Wilson confidence-interval definitions.
  • The Cephes incbet regularized incomplete beta integral (the binomial cdf/sf and the Clopper-Pearson Beta-quantile inverse), and Cephes ndtri (the Wilson interval's z-multiplier).
  • C. J. Clopper and E. S. Pearson, "The use of confidence or fiducial limits illustrated in the case of the binomial," Biometrika 26(4):404–413 (1934).
  • E. B. Wilson, "Probable inference, the law of succession, and statistical inference," J. Amer. Stat. Assoc. 22:209–212 (1927); continuity correction per R. G. Newcombe, Statistics in Medicine 17:857–872 (1998).

License: MIT OR Apache-2.0. Upstream credit: SciPy (https://scipy.org, BSD-3-Clause); Cephes Mathematical Library, Stephen L. Moshier.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages