Xbootstrap
Nested bootstrap resampling of xarray objects
Install / Use
/learn @dougiesquire/XbootstrapREADME
[!NOTE]
This repo has been archived.xbootstrapcode and functionality has been moved into thescorespackage and will continue to be maintained and developed there.
xbootstrap
xbootstrap is a simple package for performing nested<sup>*</sup> circular<sup>†</sup> block<sup>‡</sup> bootstrapping<sup>¶</sup> of xarray objects.
- <sup>¶</sup> Bootstrapping is random resampling with replacement.
- <sup>‡</sup> Block boostrapping is a simple way to account for autocorrelation in the data being randomly resampled.
- <sup>†</sup> Circular block bootstrapping avoids undersampling data values near the beginning and end of the dimension(s) being resampled. This is optional in
xbootstrap. - <sup>*</sup> 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)
Related Skills
node-connect
339.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
339.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.8kCommit, push, and open a PR
