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
rsomics-binom-test --successes K --trials N [--p 0.5]
[--alternative two-sided|less|greater]
[--ci-method exact|wilson|wilsoncc]
[--confidence 0.95]
--alternative—greaterisbinom.sf(k−1, n, p),lessisbinom.cdf(k, n, p),two-sidedsums the binomial pmf over every outcome no more likely than the observed one (scipy's exact two-sided rule).--ci-method—exactis the Clopper-Pearson interval (Beta quantiles),wilson/wilsonccare the Wilson score interval without / with continuity correction.--jsonemits one machine-readable envelope.--batch FILEruns manyk<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-tthreads).
rsomics-binom-test --batch tests.tsv --alternative two-sided -t8
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.
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.
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 itsrerr = 1 + 1e-7relative-tolerance guard and the implicit binary search that brackets the opposite tail, plus the Clopper-Pearson and Wilson confidence-interval definitions. - The Cephes
incbetregularized incomplete beta integral (the binomial cdf/sf and the Clopper-Pearson Beta-quantile inverse), and Cephesndtri(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.