/resample

Randomization-based inference in Python

Primary LanguagePythonBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

https://readthedocs.org/projects/resample/badge/?version=stable https://img.shields.io/pypi/l/resample

Link to full documentation

Resampling-based inference in Python based on data resampling and permutation.

This package was created by Daniel Saxton and is now maintained by Hans Dembinski.

Features

  • Bootstrap resampling: ordinary or balanced with optional stratification
  • Extended bootstrap resampling: also varies sample size
  • Parametric resampling: Gaussian, Poisson, gamma, etc.)
  • Jackknife estimates of bias and variance of any estimator
  • Compute bootstrap confidence intervals (percentile or BCa) for any estimator
  • Permutation-based variants of traditional statistical tests (USP test of independence and others)
  • Tools for working with empirical distributions (CDF, quantile, etc.)
  • Depends only on numpy and scipy

Example

We bootstrap the uncertainty of the arithmetic mean, an estimator for the expectation. In this case, we know the formula to compute this uncertainty and can compare it to the bootstrap result. More complex examples can be found in the documentation.

from resample.bootstrap import variance
import numpy as np

# data
d = [1, 2, 6, 3, 5]

# this call is all you need
stdev_of_mean = variance(np.mean, d) ** 0.5

print(f"bootstrap {stdev_of_mean:.2f}")
print(f"exact {np.std(d) / len(d) ** 0.5:.2f}")
# bootstrap 0.82
# exact 0.83

The amazing thing is that the bootstrap works as well for arbitrarily complex estimators. The bootstrap often provides good results even when the sample size is small.

Installation

You can install with pip.

pip install resample