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 Distribution

grecov-0.2.0.tar.gz (37.6 kB view details)

Uploaded Source

Built Distributions

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

grecov-0.2.0-cp313-cp313-win_amd64.whl (66.6 kB view details)

Uploaded CPython 3.13Windows x86-64

grecov-0.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (84.3 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

grecov-0.2.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (80.6 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

grecov-0.2.0-cp313-cp313-macosx_11_0_arm64.whl (67.1 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

grecov-0.2.0-cp313-cp313-macosx_10_13_x86_64.whl (75.9 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

grecov-0.2.0-cp312-cp312-win_amd64.whl (66.6 kB view details)

Uploaded CPython 3.12Windows x86-64

grecov-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (84.3 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

grecov-0.2.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (80.6 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

grecov-0.2.0-cp312-cp312-macosx_11_0_arm64.whl (67.1 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

grecov-0.2.0-cp312-cp312-macosx_10_13_x86_64.whl (75.9 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

File details

Details for the file grecov-0.2.0.tar.gz.

File metadata

  • Download URL: grecov-0.2.0.tar.gz
  • Upload date:
  • Size: 37.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for grecov-0.2.0.tar.gz
Algorithm Hash digest
SHA256 50d8d5172e33c0c136c83fad56d2c5baf307bd5bbe2e0b8463f55e7432c13db1
MD5 02aa353a296ef2890273ec4406df9702
BLAKE2b-256 ac1a41c6b403d990e0860fd1e1ebf928865ca967bfaa21652de5e1359251422b

See more details on using hashes here.

Provenance

The following attestation bundles were made for grecov-0.2.0.tar.gz:

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.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: grecov-0.2.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 66.6 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for grecov-0.2.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 664144ea0e9c70abc5636a562cb365090fc3a216eceb072e1c251589721a6300
MD5 7ed284846ecee6c757c5af00bbee8773
BLAKE2b-256 09be02a84ed075a1f42bf6b7d46fe02547c28703f6d65e36d6437c4a4dac5c56

See more details on using hashes here.

Provenance

The following attestation bundles were made for grecov-0.2.0-cp313-cp313-win_amd64.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.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for grecov-0.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d830e529f7386478d4965ef2bcffb333f93b85292b7a2beb47aa4712da33422a
MD5 50827b93c2437e0a93a5f4e762665a16
BLAKE2b-256 5f16266ebc9e345cdd1131087feb02cdec85e98163000abe0505a9a84643c2d4

See more details on using hashes here.

Provenance

The following attestation bundles were made for grecov-0.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_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.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for grecov-0.2.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 01a885a8fb668bc2d10bc7088924a3abddd390aaa06dbb381e700f7ea69ff87c
MD5 147be394d19780f14ded0539ac34fc41
BLAKE2b-256 ca2593394a1550bea85a3c60de3bd397ca859fc2257c9c49189ce12f47fc38b5

See more details on using hashes here.

Provenance

The following attestation bundles were made for grecov-0.2.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_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.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for grecov-0.2.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f7446cd22747a45524c4189b5d5b8917b1f3202738be603e625d7faaad163ce8
MD5 b46c37817c5651507075789442a89769
BLAKE2b-256 9e8c8704e72f3b4cfe818561aae10325b92da98a8ae0b86b31a71e23a634ada9

See more details on using hashes here.

Provenance

The following attestation bundles were made for grecov-0.2.0-cp313-cp313-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.0-cp313-cp313-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for grecov-0.2.0-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 205cb39dbe98b8bcf930cf562dc62a0be9a460674769e892f2fcf47f556b3b37
MD5 8b6b7d0e2c2caa116997b2eebf0e4ee0
BLAKE2b-256 fb13634502d545d877f1cd3e7b281057be00fe5f6826409dbb3db50d058dfece

See more details on using hashes here.

Provenance

The following attestation bundles were made for grecov-0.2.0-cp313-cp313-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.

File details

Details for the file grecov-0.2.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: grecov-0.2.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 66.6 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for grecov-0.2.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 a095fdd635005ed041a38811989c0110289fd914be38ee7191d4945a3979608e
MD5 18b4dbcd74bbac9be0158dfdebcece8b
BLAKE2b-256 f03d47948e6a067bb95adf60685d8dfb7dbb68f18123c6a73b5fbfdc51ff1d78

See more details on using hashes here.

Provenance

The following attestation bundles were made for grecov-0.2.0-cp312-cp312-win_amd64.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.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for grecov-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d923aea3ab82380d8d2d78b4c91de09214029ba65f2e806247b2065c1b5b8258
MD5 aa0e22e91c740c58c989ea079cb2284b
BLAKE2b-256 6b710bd30d293980896a3aefc1b5957fe83ccfbc43aa409c57eb615eb03689d8

See more details on using hashes here.

Provenance

The following attestation bundles were made for grecov-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_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.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for grecov-0.2.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 03bbc0a0247c16c942c8555c0af6e0dd6d40a1e6dfe19e72d76d8034bb90ae17
MD5 e33cb05e794e72084ebf4941a0025586
BLAKE2b-256 5fa782bffdcc0600ede07070a21a4b3ce4ff6a984e383bfba3edaefd66ebd0fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for grecov-0.2.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_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.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for grecov-0.2.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5554c7559c3ce77e3c0114582ef99210111b44244245eba2767857700870d817
MD5 2abcc64d9f47f307db82a93c5ae8fbeb
BLAKE2b-256 553b634187e72f31795a17330c845051a3720ea8e342e4269282deacf1a1b9fc

See more details on using hashes here.

Provenance

The following attestation bundles were made for grecov-0.2.0-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.0-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for grecov-0.2.0-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 ee77d8b7f367ed53db55e1885d9dc0d983fa12b8cadba4f55526f6abc02c12e8
MD5 1a19a3b6ad6dc576af820476942cc154
BLAKE2b-256 ae9237edea4090165bf5438638187cf53ecce952ea10068921e43907941c074c

See more details on using hashes here.

Provenance

The following attestation bundles were made for grecov-0.2.0-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