Skip to main content

Exact confidence intervals for multinomial distributions via Neyman construction

Project description

grecov

PyPI Tests Build License: MIT

Efficient Neyman construction for multinomial distributions with the Greedy Coverage algorithm.

Installation

For the IPOPT optimizer (recommended for best results):

pip install grecov[ipopt]

Development

Install in editable mode with test dependencies:

pip install -e ".[test]"

After modifying C++ code in src/ext/, rebuild with:

pip install -e .

Install pre-commit hooks to run ruff and clang-format on every commit:

pip install pre-commit
pre-commit install

Usage

from grecov import multinomial_ci

result = multinomial_ci(
    counts=[10, 10, 20, 60],
    values=[1, 2, 3, 4],
    alpha=0.05,
)
print(f"95% CI: [{result['lower']:.4f}, {result['upper']:.4f}]")

R (via reticulate)

An R package is available in r-package/ that wraps grecov using reticulate.

Installation

# Install reticulate if needed
install.packages("reticulate")

# Install the R package from the local directory
install.packages("r-package", repos = NULL, type = "source")

# Install the grecov Python package into reticulate's Python environment
grecov::install_grecov()

Usage

library(grecov)

result <- multinomial_ci(
  counts = c(10, 10, 20, 60),
  values = c(1, 2, 3, 4),
  alpha = 0.05
)
cat(sprintf("95%% CI: [%.4f, %.4f]\n", result$lower, result$upper))

The low-level BFS function is also available:

bfs <- grecov_bfs(
  p = c(0.1, 0.1, 0.2, 0.6),
  v = c(1, 2, 3, 4),
  s_obs = 330,
  n = 100L
)
cat(sprintf("P(S <= 330) = %.6f\n", bfs$prob_left))

Options

All parameters from the Python API are available:

result <- multinomial_ci(
  counts = c(10, 10, 20, 60),
  values = c(1, 2, 3, 4),
  alpha = 0.10,
  method = "greedy",
  verbose = 1L
)

Pyodide (browser / WebAssembly)

grecov compiles to WebAssembly and runs in the browser via Pyodide. The C++ BFS extension compiles unchanged; cyipopt is not included (the scipy trust-constr fallback is used automatically).

Using in a webpage

The pyodide wheel is attached to each GitHub Release. The following snippet automatically fetches the latest one:

<script src="https://cdn.jsdelivr.net/pyodide/v0.27.7/full/pyodide.js"></script>
<script type="module">
const pyodide = await loadPyodide();
await pyodide.loadPackage(["micropip", "numpy", "scipy"]);

// Find and install the latest pyodide wheel from GitHub Releases
const res = await fetch("https://api.github.com/repos/louisabraham/grecov/releases/latest");
const release = await res.json();
const wheel = release.assets.find(a => a.name.includes("pyodide"));
await pyodide.runPythonAsync(`
    import micropip
    await micropip.install("${wheel.browser_download_url}")
`);

pyodide.runPython(`
from grecov import multinomial_ci
result = multinomial_ci(
    counts=[10, 10, 20, 60],
    values=[1, 2, 3, 4],
    alpha=0.05,
)
print(f"95% CI: [{result['lower']:.4f}, {result['upper']:.4f}]")
`);
</script>

Building the Pyodide wheel locally

pip install pyodide-build
pyodide xbuildenv install
pyodide build

This requires the matching Emscripten version (pyodide config get emscripten_version). Install it via emsdk and source emsdk_env.sh before running pyodide build. The wheel is written to dist/.

Performance

Pyodide/wasm performance is on par with native CPython (~1.0x). Run the comparison benchmark with:

python profiling/bench_pyodide.py

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

grecov-0.2.3-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (66.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

grecov-0.2.3-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (63.4 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

grecov-0.2.3-cp312-cp312-macosx_11_0_arm64.whl (67.8 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

grecov-0.2.3-cp312-cp312-macosx_10_13_x86_64.whl (76.6 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

File details

Details for the file grecov-0.2.3-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for grecov-0.2.3-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 37efc7f12d18431e42f8dc621d105cf72fffb7d98035037857815510c2673432
MD5 247da50cfa43da4c1fa2e107d3b3c388
BLAKE2b-256 de8df7f99eb30bf9f78b3ef9f5b488b693ee808632259656259b13e5a22e1788

See more details on using hashes here.

Provenance

The following attestation bundles were made for grecov-0.2.3-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: build.yml on louisabraham/grecov

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file grecov-0.2.3-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for grecov-0.2.3-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 b6f5d20a46cd605623de45f99a6e95bffd3f82c203a0b128653e1966339690a7
MD5 103d389a03bae587c090022bdbdd9c92
BLAKE2b-256 92980dca4177a3b87bcb23eb3e925277dd86dc201bfb873fe1076cf1384453ef

See more details on using hashes here.

Provenance

The following attestation bundles were made for grecov-0.2.3-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl:

Publisher: build.yml on louisabraham/grecov

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file grecov-0.2.3-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for grecov-0.2.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1d0e8c13ccd1d8c87f0f587382be3444bed743f22b76020bda34f560406eacc7
MD5 e4110be2af52e487ab11257a8d34a2f0
BLAKE2b-256 ab7dd32aea6f66d28b722016bd0ad02fb870b6918d5590d4085a3c872304dbc3

See more details on using hashes here.

Provenance

The following attestation bundles were made for grecov-0.2.3-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: build.yml on louisabraham/grecov

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file grecov-0.2.3-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for grecov-0.2.3-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 47ceb1f0a0b99ae620528c492416add9f8e1fdbebe296b3f8beba152b71a10c0
MD5 72ece65e7f5fc2b1e24490404a0fceff
BLAKE2b-256 284b17055822587a984a1b8bc61da24d8dd5180dda554113f5450572a55afa0b

See more details on using hashes here.

Provenance

The following attestation bundles were made for grecov-0.2.3-cp312-cp312-macosx_10_13_x86_64.whl:

Publisher: build.yml on louisabraham/grecov

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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