Skip to main content

Production-grade bootstrap uncertainty estimation: 15+ methods, sklearn CV, pandas accessor.

Project description

bootstrapx

Production-grade bootstrap uncertainty estimation for Python.

CI PyPI Downloads Python Coverage License: MIT Docs

15 bootstrap methods · sklearn-compatible · pandas accessor · memory-safe batching


Why bootstrapx?

scipy.stats.bootstrap covers 3 CI types and only iid data.
The R boot package is comprehensive but not Pythonic.
bootstrapx bridges this gap.

Feature scipy arch bootstrapx
BCa interval
Studentized (bootstrap-t)
Bayesian bootstrap
Poisson / Bernoulli weights
MBB / CBB / Stationary block
Sieve (AR-based)
Wild bootstrap
Cluster / Stratified
scikit-learn CV API
pandas .bootstrap accessor
Reproducible (seeded RNG) partial
Constant memory (batched)

Installation

pip install bootstrapx-lib                  # core (numpy + scipy only)
pip install "bootstrapx-lib[pandas]"        # + pandas accessor
pip install "bootstrapx-lib[numba]"         # + Numba JIT acceleration
pip install "bootstrapx-lib[pandas,numba]"  # everything

Quick Start

Basic usage

import numpy as np
from bootstrapx import bootstrap

data = np.random.default_rng(42).normal(5, 2, size=300)

result = bootstrap(data, np.mean)
print(result)
# BootstrapResult(method='bca', theta_hat=4.97, se=0.11, CI=[4.75, 5.19])

print(result.confidence_interval.low, result.confidence_interval.high)
print(5.0 in result.confidence_interval)  # True

pandas accessor

import pandas as pd
import numpy as np
import bootstrapx  # registers .bootstrap accessor

s = pd.Series(np.random.default_rng(0).exponential(scale=2, size=500))

# On a Series
r = s.bootstrap.bca(np.mean)
print(r)

# On a DataFrame — column-wise summary
df = pd.DataFrame({"control": s, "treatment": s * 1.1 + 0.3})
print(df.bootstrap.summary(np.mean))
#              theta_hat    ci_low   ci_high        se method
# column
# control       1.9973    1.8215    2.1862    0.0941    bca
# treatment     2.4970    2.3036    2.7048    0.1035    bca

scikit-learn cross-validation

from bootstrapx import BootstrapCV
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import cross_val_score
from sklearn.datasets import load_breast_cancer

X, y = load_breast_cancer(return_X_y=True)

cv = BootstrapCV(n_splits=200, random_state=42)
scores = cross_val_score(
    GradientBoostingClassifier(n_estimators=100),
    X, y, cv=cv, scoring="roc_auc"
)
print(f"AUC: {scores.mean():.4f} ± {scores.std():.4f}")
# AUC: 0.9921 ± 0.0071

Time-series bootstrap

import numpy as np
from bootstrapx import bootstrap

# AR(1) process
rng = np.random.default_rng(0)
y = np.zeros(500)
for t in range(1, 500):
    y[t] = 0.7 * y[t-1] + rng.normal()

# Moving Block Bootstrap — preserves serial correlation
result = bootstrap(y, np.mean, method="mbb", block_length=15, n_resamples=4999)
print(result)

# Sieve Bootstrap — fits AR(p) model to residuals (16x faster than 0.2.0)
result = bootstrap(y, np.mean, method="sieve", n_resamples=9999)
print(result)

A/B test with clustered data

import numpy as np
from bootstrapx import bootstrap

# Users clustered by session (standard iid bootstrap underestimates variance)
n_clusters = 50
cluster_ids = np.repeat(np.arange(n_clusters), 20)  # 1000 obs, 50 clusters
rng = np.random.default_rng(1)
data = rng.normal(loc=cluster_ids * 0.1, scale=1.0)

result = bootstrap(
    data, np.mean,
    method="cluster",
    cluster_ids=cluster_ids,
    n_resamples=4999,
)
print(result)
# Correctly wider CI that accounts for within-cluster correlation

Performance

Measured on Intel Core i7-12700, Python 3.11, n_resamples=9 999.
See benchmarks/ for reproducible scripts.

N Method scipy bootstrapx Speedup
1 000 BCa 0.18s 0.09s 2.0×
5 000 BCa 0.80s 0.27s 3.0×
50 000 Percentile 7.29s 2.01s 3.6×
100 000 Percentile 54.34s 3.99s 13.6×
300 Sieve 0.19s scipy N/A

Documentation

📖 Full docs: artyerokhin.github.io/bootstrapx


All supported methods

Method method= Use case
BCa "bca" General purpose, best coverage accuracy
Percentile "percentile" Simple, fast
Basic (Hall) "basic" Symmetric distributions
Studentized "studentized" Known variance structure
Bayesian "bayesian" Bayesian UQ, non-parametric posterior
Poisson weights "poisson" Weighted bootstrap, survey data
Bernoulli weights "bernoulli" Subsampling variant
Subsampling "subsampling" Heavy tails, no finite variance
Moving Block (MBB) "mbb" Stationary time series
Circular Block (CBB) "cbb" Stationary TS, edge-effect free
Stationary "stationary" Politis & Romano (1994)
Tapered Block "tapered" Paparoditis & Politis (2001)
Sieve "sieve" AR(p) time series (Bühlmann 1997)
Wild "wild" Heteroscedastic residuals (Wu 1986)
Cluster "cluster" Multi-level / panel data
Stratified "strata" Stratified sampling designs

Contributing

git clone https://github.com/artyerokhin/bootstrapx.git
cd bootstrapx
pip install -e ".[dev,pandas]"
pytest tests/ -v

Citation

If you use bootstrapx in academic work:

@software{bootstrapx,
  author  = {Erokhin, Artem},
  title   = {bootstrapx: Production-grade bootstrap uncertainty estimation},
  url     = {https://github.com/artyerokhin/bootstrapx},
  version = {0.3.0},
  year    = {2026},
}

License

MIT — see LICENSE.

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

bootstrapx_lib-0.3.0.tar.gz (23.9 kB view details)

Uploaded Source

Built Distribution

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

bootstrapx_lib-0.3.0-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

Details for the file bootstrapx_lib-0.3.0.tar.gz.

File metadata

  • Download URL: bootstrapx_lib-0.3.0.tar.gz
  • Upload date:
  • Size: 23.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for bootstrapx_lib-0.3.0.tar.gz
Algorithm Hash digest
SHA256 7d9eb4662510c4db093fec30ee03cae6127dcaa7775e35a8a6f05c5dfbbb5c26
MD5 424896493b1085199ef0907f6fca0641
BLAKE2b-256 12c32e73f5571a0e2a28443552337696f51f6ac1bf9dc16739dc46560ecd74b2

See more details on using hashes here.

Provenance

The following attestation bundles were made for bootstrapx_lib-0.3.0.tar.gz:

Publisher: publish.yml on artyerokhin/bootstrapx

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

File details

Details for the file bootstrapx_lib-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: bootstrapx_lib-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 21.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for bootstrapx_lib-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 54773d3ba727a5f02f2b4da45fb796535f44726138fddd0b475d8f1467fffc13
MD5 43d1b39d9d14e69dac4b188871abd3f9
BLAKE2b-256 756df49ddc5c514cfa0a61b8c303cf37c4a3f3fbcc9d2887a73cee246ff2fa81

See more details on using hashes here.

Provenance

The following attestation bundles were made for bootstrapx_lib-0.3.0-py3-none-any.whl:

Publisher: publish.yml on artyerokhin/bootstrapx

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