Skip to main content

Exact confidence intervals for odds ratios

Project description

ExactCIs: Exact Confidence Intervals for Odds Ratios

Provides five methods to compute confidence intervals for the odds ratio of a 2×2 table (\bigl[\begin{smallmatrix}a&b\c&d\end{smallmatrix}\bigr]). It validates inputs, computes exact‑conditional probabilities via the noncentral hypergeometric distribution, and inverts p‑value functions by robust root‑finding. Small, single‑purpose functions are chained by an orchestrator compute_all_cis.

Documentation

Installation

Using pip

pip install exactcis

Optional dependencies

# For data manipulation with pandas
pip install "exactcis[pandas]"

# For symbolic mathematics
pip install "exactcis[sympy]"

# For visualization
pip install "exactcis[viz]"

# For all optional dependencies
pip install "exactcis[full]"

Development installation

# Clone the repository
git clone https://github.com/yourusername/exactcis.git
cd exactcis

# Install with development dependencies using uv
uv pip install -e ".[dev]"

# Run tests
uv run pytest

# Run all tests including slow ones
uv run pytest --run-slow

Command Line Interface

ExactCIs provides a command-line interface for quick calculations:

# Basic usage
exactcis-cli 12 5 8 10 --method blaker

# With additional options
exactcis-cli 12 5 8 10 --method unconditional --alpha 0.01 --grid-size 30 --apply-haldane --verbose

For help and all available options:

exactcis-cli --help

Methods & When to Use Them

Method How It Works When to Use
conditional
(Fisher)
Inverts the noncentral hypergeometric CDF at (\alpha/2) in each tail. • Very small samples or rare events
• Regulatory/safety‑critical studies requiring guaranteed ≥ 1–α coverage
• Fixed‑margin designs (case–control)
midp
(Mid‑P adjusted)
Same inversion but gives half‑weight to the observed table in the tail p‑value, reducing conservatism. • Epidemiology or surveillance where conservative Fisher intervals are too wide
• Moderate samples where slight undercoverage is tolerable for tighter intervals
blaker
(Blaker's exact)
Uses the acceptability function (f(k)=\min[P(K≤k),P(K≥k)]) and inverts it exactly for monotonic, non‑flip intervals. • Routine exact inference when Fisher is overly conservative
• Fields standardized on Blaker (e.g. genomics, toxicology)
• Exact coverage with minimal over‑coverage
unconditional
(Barnard's)
Treats both margins as independent binomials, optimizes over nuisance (p_1) via grid (or NumPy) search, and inverts the worst‑case p‑value. • Small clinical trials or pilot studies with unfixed margins
• Need maximum power and narrowest exact CI
• Compute budget allows optimization or vectorized acceleration
wald_haldane
(Haldane–Anscombe)
Adds 0.5 to each cell and applies the standard log‑OR ± z·SE formula; includes a pure‑Python normal quantile fallback if SciPy is absent. • Large samples where asymptotic Wald is reasonable
• Quick, approximate intervals for routine reporting
• When speed and convenience outweigh strict exactness

For a detailed guide on selecting the appropriate method, see the Method Selection Guide.


Quick Start Examples

from exactcis import compute_all_cis

# 2×2 table:   Cases   Controls
#   Exposed      a=12     b=5
#   Unexposed    c=8      d=10

results = compute_all_cis(12, 5, 8, 10, alpha=0.05, grid_size=500)
for method, (lo, hi) in results.items():
    print(f"{method:12s} CI: ({lo:.3f}, {hi:.3f})")

This prints:

conditional  CI: (1.059, 8.726)
midp         CI: (1.205, 7.893)
blaker       CI: (1.114, 8.312)
unconditional CI: (1.132, 8.204)
wald_haldane CI: (1.024, 8.658)

For more detailed examples, see the Quick Start Notebook and Method Comparison Notebook.

Method-Specific Usage

You can also use each method individually:

from exactcis.methods import (
    exact_ci_conditional,
    exact_ci_midp,
    exact_ci_blaker,
    exact_ci_unconditional,
    ci_wald_haldane
)

# Example: Compute a 99% confidence interval using Fisher's method
lower, upper = exact_ci_conditional(12, 5, 8, 10, alpha=0.01)
print(f"99% CI: ({lower:.3f}, {upper:.3f})")

Running Tests

The package includes a comprehensive test suite. By default, tests marked as "slow" are skipped:

uv run pytest -v

To run all tests, including slow ones:

uv run pytest --run-slow -v

Performance Considerations

For optimal performance with the unconditional method:

  • Install with NumPy acceleration: pip install "exactcis[numpy]"
  • Use appropriate grid sizes based on your precision requirements
  • Set reasonable timeouts for large or imbalanced tables

For detailed performance information, see the Performance Documentation.

Comparison with Other Implementations

ExactCIs has been benchmarked against:

  • R's exact2x2 package
  • SciPy's fisher_exact function
  • StatXact

For benchmark results, see the Implementation Comparison.

Citation

If you use ExactCIs in your research, please cite:

ExactCIs: A Python package for exact confidence intervals for odds ratios.
https://github.com/yourusername/exactcis

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

exactcis-0.1.0.tar.gz (342.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

exactcis-0.1.0-py3-none-any.whl (64.2 kB view details)

Uploaded Python 3

File details

Details for the file exactcis-0.1.0.tar.gz.

File metadata

  • Download URL: exactcis-0.1.0.tar.gz
  • Upload date:
  • Size: 342.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for exactcis-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e691e67e104cfeea3fae42e3137d11545d0590531937ed34951b95ed50d836b8
MD5 e7cc93b4f80e3983dfc981ed80db511e
BLAKE2b-256 7826955d1090c4cea26ab088f0fb6e501ea18ab8b9061c33a43f9abf53a7a481

See more details on using hashes here.

File details

Details for the file exactcis-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: exactcis-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 64.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for exactcis-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 36e02c7736c7bcbafdb20fbba960ec24ce32ce714fc11a705b6081ee7e7a0c7d
MD5 ae9e67e8b7eb58d11d68dcf50f1b6e29
BLAKE2b-256 46e86ff0fc695c9a357dea0bc1ecb435d9c1995d8629206d3ca1140eecbd8fba

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page