Skip to main content

Nested circular block bootstrap resampling for xarray objects

Project description

xbootstrap

PyPI tests build codecov License: MIT Code style: black

xbootstrap is a simple package for performing nested* circular block bootstrapping of xarray objects.

  • Bootstrapping is random resampling with replacement.
  • Block boostrapping is a simple way to account for autocorrelation in the data being randomly resampled.
  • Circular block bootstrapping avoids undersampling data values near the beginning and end of the dimension(s) being resampled. This is optional in xbootstrap.
  • * Here, nested bootstrapping means that, when multiple dimensions are specified, the first dimension is randomly resampled, then for each resampled element along that dimension the second dimension is randomly resampled, then for each resampled element along that dimension the third dimension is randomly resampled etc.

Installation

To install this package from PyPI:

pip install xbootstrap

Example usage

xbootstrap currently comprises a single function called block_bootstrap that can be used as follows:

import numpy as np
import xarray as xr
from xbootstrap import block_bootstrap

# Generate some example data
n_time = 100
n_ensemble = 10
ds1 = xr.DataArray(
    np.random.random((n_time, n_ensemble)),
    coords={"time": range(n_time), "ensemble": range(n_ensemble)},
)
ds2 = xr.DataArray(
    np.random.random((n_time, n_ensemble)),
    coords={"time": range(n_time), "ensemble": range(n_ensemble)},
)
ds3 = xr.DataArray(np.random.random((n_time)), coords={"time": range(n_time)})

# Create 1000 circularly bootstrapped resamples of ds1, ds2 and ds3
# using a blocksize of 5 for the time dimension and 1 for the ensemble
# dimension, and only bootstrapping the time dimension for ds2
ds1_bs, ds2_bs, ds3_bs = block_bootstrap(
    ds1,
    ds2,
    ds3,
    blocks={"time": 5, "ensemble": 1},
    n_iteration=1000,
    exclude_dims=[[], ["ensemble"], []],
    circular=True,
)

block_bootstrap also operates lazily with dask-backed xarray objects, but this requires dask to be installed:

ds1_bs, ds2_bs, ds3_bs = block_bootstrap(
    ds1.chunk(),
    ds2.chunk(),
    ds3.chunk(),
    blocks={"time": 5, "ensemble": 1},
    n_iteration=10,
    exclude_dims=[[], ["ensemble"], []],
    circular=True,
)

Contributing

Contributions are very welcome, particularly in the form of reporting bugs and writing tests. Please open an issue and check out the contributor guide.

References

Wilks, D.S., 2011. Statistical methods in the atmospheric sciences (Vol. 100). Academic press. (particularly Chapter 5.3)

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

xbootstrap-0.0.6.tar.gz (29.1 kB view details)

Uploaded Source

Built Distribution

xbootstrap-0.0.6-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file xbootstrap-0.0.6.tar.gz.

File metadata

  • Download URL: xbootstrap-0.0.6.tar.gz
  • Upload date:
  • Size: 29.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for xbootstrap-0.0.6.tar.gz
Algorithm Hash digest
SHA256 cee796f156a03fc970804a883a17a79f671b2d9cd7e36fb700007831de3226d4
MD5 41412ab4cc8d8e6c20701d5840506942
BLAKE2b-256 124b924a93cb77616b006d8d241b1d21d28986dc4945bf6a09e2d01e9a3124ed

See more details on using hashes here.

File details

Details for the file xbootstrap-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: xbootstrap-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for xbootstrap-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 3be52ff2f49f247b3120eff831f27f0526024462ad173f7aa9b85259b3cb8961
MD5 a78a2239d20f0a535585363c04084aa4
BLAKE2b-256 b99f42ecb57d6ea8fbe8e136be843f1e8eddccaaf3bdb224614979fd62c50761

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page