Skip to main content

🧪 pytest-bootstrap

Build status. Documentation status. PyPI package version.

Scientific software development often relies on stochasticity, e.g. for Monte Carlo integration or simulating the Ising model. Testing non-deterministic code is difficult. This package offers a bootstrap test to validate stochastic algorithms, including multiple hypothesis correction for vector statistics. It can be installed by running pip install pytest-bootstrap.

Example

Suppose we want to implement the expected value of log-normal distribution with location parameter \mu and scale parameter \sigma.

>>> import numpy as np
>>>
>>> def lognormal_expectation(mu, sigma):
...   return np.exp(mu + sigma ** 2 / 2)
>>>
>>> def lognormal_expectation_wrong(mu, sigma):
...   return np.exp(mu + sigma ** 2)

We can validate our implementation by simulating from a lognormal distribution and comparing with the bootstrapped mean.

>>> from pytest_bootstrap import bootstrap_test
>>>
>>> mu = -1
>>> sigma = 1
>>> reference = lognormal_expectation(mu, sigma)
>>> x = np.exp(np.random.normal(mu, sigma, 1000))
>>> result = bootstrap_test(x, np.mean, reference)

This returns a summary of the test, such as the bootstrapped statistics.

>>> result.keys()
dict_keys(['alpha', 'alpha_corrected', 'reference', 'lower', 'upper',
           'z_score', 'median', 'iqr', 'tol', 'statistics'])

Comparing with our incorrect implementation reveals the bug.

>>> reference_wrong = lognormal_expectation_wrong(mu, sigma)
>>> result = bootstrap_test(x, np.mean, reference_wrong)
Traceback (most recent call last):
  ...
pytest_bootstrap.BootstrapTestError: the reference value 1.0 lies outside
  the 1 - (alpha = 0.01) interval ...

Visualising the bootstrapped distribution using pytest_bootstrap.result_hist can help identify discrepancies between the bootstrapped statistics and the theoretical reference value. Note that you need to install matplotlib separately or install pytest-bootstrap using pip install pytest-bootstrap[plot].

.. plot:: examples/lognormal.py
:caption: Histogram of bootstrapped means reveals the erroneous implementation of the log-normal mean.

A comprehensive set of examples can be found in the tests.

Interface

.. automodule:: pytest_bootstrap
:members:

Metadata

Release files for pytest-bootstrap 0.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-bootstrap 0.2.3
File Size Uploaded
pytest-bootstrap-0.2.3.tar.gz 5.3 kB Details

Release files / pytest-bootstrap-0.2.3.tar.gz

Download URL pytest-bootstrap-0.2.3.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
7064ffa1d3647ba97b7f3c5835cc7d37e42867d65cf79b1d36823e2de3b7044f
BLAKE2b-256 checksum
How to use checksums
041125bd6329825ce71d753c8ee87f1ee0038cbb335367ecd8d54bf9db8bd8b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

Release history Release notifications | RSS feed

This release

0.2.3 This release

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page