Metadata-Version: 2.4
Name: exact-tests
Version: 0.11.1
Summary: Accurate and efficient binomial, Poisson, Hardy-Weinberg equilibrium, and Fisher's exact tests.
Home-page: https://github.com/chrchang/stats
Author: Christopher Chang
Author-email: Christopher Chang <chrchang@alumni.caltech.edu>
License-Expression: GPL-3.0-only
Project-URL: Homepage, https://github.com/chrchang/stats
Project-URL: Issues, https://github.com/chrchang/stats/issues
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.21.3
Dynamic: author
Dynamic: home-page
Dynamic: license-file
Dynamic: requires-python

exact-tests
===========

This package implements the binomial, Poisson, Hardy-Weinberg equilibrium, and
Fisher's (2xk and 3xk so far) exact tests, along with corresponding
discrete distributions (binomial, hypergeometric, Poisson).  scipy- and R-style
interfaces are provided.  cdf-like functions offer a fast 'approx' mode.

As of this writing, these functions (even in 'approx' mode) are more accurate,
and often simultaneously more efficient, than their scipy counterparts: see the
MPFR-comparison and benchmark scripts under utils/ .  E.g.

    $ utils/pbinom_accuracy.py
    n in [2^5, 2^6): errRMS=0  approxErrRMS=7.31e-17  scipyErrRMS=0
    n in [2^20, 2^21): errRMS=0  approxErrRMS=0  scipyErrRMS=2.18e-15
    n in [2^35, 2^36): errRMS=0  approxErrRMS=0  scipyErrRMS=1.15e-14
    $ utils/pbinom_accuracy.py -p 0.1  # scipy accuracy degrades more quickly with most values of p != 0.5, or k further from np
    n in [2^5, 2^6): errRMS=0  approxErrRMS=1.06e-16  scipyErrRMS=4.22e-16
    n in [2^20, 2^21): errRMS=0  approxErrRMS=0  scipyErrRMS=1.23e-11
    n in [2^35, 2^36): errRMS=0  approxErrRMS=0  scipyErrRMS=3.47e-07
    $ utils/pbinom_accuracy.py --z-score -2
    n in [2^5, 2^6): errRMS=6.51e-17  approxErrRMS=2.48e-16  scipyErrRMS=1.96e-16
    n in [2^20, 2^21): errRMS=0  approxErrRMS=0  scipyErrRMS=1.01e-13
    n in [2^35, 2^36): errRMS=0  approxErrRMS=4.82e-17  scipyErrRMS=1.66e-11
    $ utils/pbinom_benchmark.py
    n=(2^5)-1: base=7.54e-07  approx=3.12e-07  scipy=2.91e-05 sec/iter
    n=(2^20)-1: base=2.94e-06  approx=1.24e-06  scipy=3.06e-05 sec/iter
    n=(2^35)-1: base=1.63e-06  approx=1.12e-06  scipy=9.54e-05 sec/iter

    $ utils/binomtest_accuracy.py --z-score 1
    n in [2^5, 2^6): errRMS=1.44e-16  scipyErrRMS=1.96e-16
    n in [2^20, 2^21): errRMS=3.03e-16  scipyErrRMS=6e-14
    n in [2^35, 2^36): errRMS=7.32e-16  scipyErrRMS=7.53e-12
    $ utils/binomtest_benchmark.py --z-score 1  # ~100-250x speedup, better accuracy
    n=(2^5)-1: base=1.02e-06  scipy=0.00023 sec/iter
    n=(2^20)-1: base=4.14e-06  scipy=0.00053 sec/iter
    n=(2^35)-1: base=5.98e-06  scipy=0.000852 sec/iter

    $ utils/phyper_accuracy.py --z-score -0.5  # scipy accuracy degrades quickly
    n in [2^5, 2^6): errRMS=0  approxErrRMS=1.72e-16  scipyErrRMS=2.01e-16
    n in [2^20, 2^21): errRMS=0  approxErrRMS=2.75e-15  scipyErrRMS=1.22e-10
    n in [2^35, 2^36): errRMS=0  approxErrRMS=4.13e-14  scipyErrRMS=7.4e-06
    $ utils/phyper_benchmark.py --z-score -0.5  # scipy speed is ok, except...
    n=(2^5)-1: base=7.29e-07  approx=2.17e-07  scipy=3.53e-05 sec/iter
    n=(2^20)-1: base=6.94e-05  approx=5.35e-06  scipy=4.42e-05 sec/iter
    n=(2^35)-1: base=0.0146  approx=0.000853  scipy=0.00194 sec/iter
    $ utils/phyper_benchmark.py --z-score 0.0001  # ...it blows up for large n when z approaches 0.
    n=(2^5)-1: base=7.5e-07  approx=2.42e-07  scipy=3.52e-05 sec/iter
    n=(2^20)-1: base=6.92e-05  approx=5.32e-06  scipy=0.000489 sec/iter
    n=(2^35)-1: base=0.0146  approx=0.000908  scipy=3.71 sec/iter
    $ utils/phyper_benchmark.py
    n=(2^5)-1: base=7.29e-07  approx=2.13e-07  scipy=3.54e-05 sec/iter
    n=(2^20)-1: base=6.91e-05  approx=5.33e-06  scipy=0.000494 sec/iter
    n=(2^35)-1: base=0.0147  approx=0.000864  scipy=14.8 sec/iter

    $ utils/fisher_exact_22_accuracy.py --z-score -1
    n in [2^5, 2^6): errRMS=9.71e-17  scipyErrRMS=2.39e-16
    n in [2^20, 2^21): errRMS=1.33e-15  scipyErrRMS=1.85e-10
    n in [2^35, 2^36): errRMS=4.52e-13
    $ utils/fisher_exact_22_benchmark.py --z-score -1  # ~100-600x speedup, better accuracy
    n=(2^5)-1: base=4.5e-07  scipy=0.000331 sec/iter
    n=(2^20)-1: base=5.97e-06  scipy=0.000986 sec/iter
    n=(2^35)-1: base=0.000906 sec/iter

    $ utils/odds_ratio_accuracy.py --z-score 1
    n in [2^5, 2^6): estErrRMS=5.25e-16  ciLowErrRMS=7.12e-16  ciHighErrRMS=7.23e-16  scipyEstErrRMS=1.3e-15  scipyCiLowErrRMS=1.25e-14  scipyCiHighErrRMS=1.42e-15
    n in [2^10, 2^11): estErrRMS=1.43e-16  ciLowErrRMS=2e-15  ciHighErrRMS=1.88e-15  scipyEstErrRMS=1.2e-14  scipyCiLowErrRMS=2.33e-14  scipyCiHighErrRMS=3.13e-14
    n in [2^15, 2^16): estErrRMS=8.46e-17  ciLowErrRMS=7.79e-15  ciHighErrRMS=7.73e-15  scipyEstErrRMS=1.52e-12  scipyCiLowErrRMS=1.2e-11  scipyCiHighErrRMS=6.64e-13
    n in [2^35, 2^36): estErrRMS=7.56e-17  ciLowErrRMS=8.35e-12  ciHighErrRMS=8.35e-12
    $ utils/odds_ratio_benchmark.py --z-score 1  # 200x and larger speedups, better accuracy
    n=(2^5)-1: est=8.75e-07  ci=8.21e-07  scipy_est=0.000456  scipy_ci=0.00254 sec/iter
    n=(2^10)-1: est=7.71e-07  ci=1.62e-06  scipy_est=0.000512  scipy_ci=0.0273 sec/iter
    n=(2^15)-1: est=3.03e-06  ci=6.29e-06  scipy_est=0.000702  scipy_ci=2.24 sec/iter
    n=(2^35)-1: est=0.000943  ci=0.00172 sec/iter

    $ utils/HWE_accuracy.py --maf 0.05 --z-score 1
    n in [2^10, 2^11): errRMS=1.95e-16  snphweErrRMS=2.04e-16
    n in [2^15, 2^16): errRMS=2.9e-16  snphweErrRMS=6.48e-16
    n in [2^20, 2^21): errRMS=6.25e-16  snphweErrRMS=1.6e-15
    $ utils/HWE_benchmark.py --maf 0.05 --z-score 1  # snphwe.snphwe() is accurate, but slow for biobank-scale cases (we're >200x as fast for n~=1m)
    n=(2^10)-1: base=2.67e-07  snphwe=6.17e-07 sec/iter
    n=(2^15)-1: base=4.08e-07  snphwe=1.28e-05 sec/iter
    n=(2^20)-1: base=1.56e-06  snphwe=0.00038 sec/iter

The central building block is a high-precision log-factorial function utilizing
the QD library (https://github.com/BL-highprecision/QD ).

### Build instructions
PyPI:
```
python -m pip install 'pip>=20.3'
python -m pip install exact_tests
```

GitHub:
```
python -m pip install 'pip>=20.3'
python -m pip install -e 'git+https://github.com/chrchang/stats.git#egg=exact_tests&subdirectory=Python'
```

Or install from a cloned copy:
```
# clone repo
git clone https://github.com/chrchang/stats
# go to python folder
cd stats/Python
# install the package
python -m pip install -e .
```

You can test the package with `pytest`.

##### Example usage:
```
import exact_tests
from fractions import Fraction

exact_tests.binomtest(3, n=15, p=0.1, alternative="greater")
exact_tests.binomtest(2, 29, 0.1)
exact_tests.binomtest(2, 29, "0.1")
exact_tests.binomtest(2, 29, "0.1", midp=True)
exact_tests.binomtest(100, 10000, "0.000001", logp=True)
exact_tests.poissontest(137, t=24.19893)  # R poisson.test(137, 24.19893)
exact_tests.poissontest(1000123456, r=1e9, alternative="greater")
exact_tests.poissontest(0, t=3, r=Fraction(1, 3), midp=True)  # t*r exactly 1
exact_tests.poissontest2(11, 21, t1=800, t2=3011)  # R poisson.test(c(11, 21), c(800, 3011))
exact_tests.ppois(950, 1000.5, logp=True)
exact_tests.poisson.sf([1003000, 1004000], 1e6)
exact_tests.fisher_exact([[4, 0], [0, 4]], alternative="greater", midp=True)
exact_tests.fisher_exact([[10000, 20000], [30000, 40000], [50000, 60000]], logp=True)
exact_tests.fisher_exact([[3, 10, 4, 7], [9, 2, 4, 1]], midp=True)
exact_tests.fisher_exact([[400, 520, 580], [510, 470, 520], [600, 480, 420]], logp=True)
exact_tests.fisher_exact([[25, 15, 18, 12, 15], [20, 22, 12, 18, 10], [15, 20, 10, 8, 5]])
exact_tests.fisher_exact([[25, 15, 18, 12, 15], [20, 22, 12, 18, 10], [15, 20, 10, 8, 5]], time_limit=60)
```

fisher_exact() running time on larger 2xk and 3xk tables is hard to predict
in advance (it can vary by orders of magnitude between tables of similar
shape and total), so it raises TimeoutError once the exact computation has
run for time_limit seconds (default 3600; pass None for no limit).  These
computations can also be interrupted with Ctrl-C.
