Skip to main content

A software to estimate medians through Bootstrapping

Project description

Bootmedian

Bootmedian estimates robust statistics (median, mean, sum, std) via bootstrapping and returns confidence intervals for the estimates.

This package provides a small set of utilities centered on the bootmedian() function in bootmedian/main.py to compute bootstrapped statistics and an API to run bootstrap-based linear fits.

Highlights

  • Robust medians: estimate medians using bootstrap resampling.
  • Confidence intervals: returns 1σ, 2σ and 3σ up/down limits for estimates.
  • Multiple modes: compute median, mean, std or sum via the mode argument.
  • Weighted resampling: supports sample weights in resampling.

Table of Contents

  • Overview: short summary and behavior
  • Installation: dependencies and install
  • Quickstart: minimal usage examples
  • API Reference: main functions and parameters
  • Notes & Contactn+ Overview

bootmedian() takes a 1-D array-like input and performs nsimul bootstrap resamples. By default it computes the median of each resample and returns the median of that distribution together with percentile-based confidence intervals for 1σ, 2σ and 3σ. NaN values in the input are ignored.

Typical return (dictionary):

  • median: median of the bootstrap distribution (or mean/std/sum depending on mode).
  • s1_up, s1_down: 1σ upward and downward limits (percentiles).
  • s2_up, s2_down: 2σ limits.
  • s3_up, s3_down: 3σ limits.
  • std1_up, std1_down: optional percentiles of the raw sample (if std provided).
  • sims: the full bootstrap simulation array (useful for diagnostics).

Installation

Requirements: Python 3.8+ and the dependencies listed in setup.py / pyproject.toml.

Install editable (dev) mode from the project root:

pip install -e .

Or install via pip (from PyPI when published):

pip install Bootmedian

Quickstart

Example: compute the bootstrapped median and 1/2/3σ intervals for a sample:

import numpy as np
from bootmedian import bootmedian

data = np.array([1.0, 2.1, 2.3, np.nan, 3.5, 2.0])
result = bootmedian(data, nsimul=2000, errors=1, verbose=True)
print(result)
# -> dict with keys: 'median','s1_up','s1_down',...,'sims'

Using weights:

weights = np.array([1, 1, 2, 1, 1, 1.5])
result_w = bootmedian(data, nsimul=2000, weights=weights)

Change the statistic with mode ("median", "mean", "std", "sum"):

# Bootmedian

Bootmedian estimates robust statistics (median, mean, sum, std) via
bootstrapping and returns confidence intervals for the estimates.

This package provides utilities centered on the `bootmedian()` function in
`bootmedian/bootmedian.py` to compute bootstrapped statistics and an API to run
bootstrap-based linear fits.

Highlights

- Robust medians: estimate medians using bootstrap resampling.
- Confidence intervals: returns 1σ, 2σ and 3σ up/down limits for estimates.
- Multiple modes: compute `median`, `mean`, `std` or `sum` via the `mode` argument.
- Weighted resampling: supports sample weights in resampling.

Table of contents

- Overview
- Installation
- Quickstart
- API reference
- Notes & contact

Overview

`bootmedian()` takes a 1-D array-like input and performs `nsimul` bootstrap
resamples. By default it computes the median of each resample and returns the
median of that distribution together with percentile-based confidence intervals
for 1σ, 2σ and 3σ. NaN values in the input are ignored.

Typical return (dictionary)

- `median`: median of the bootstrap distribution (or mean/std/sum depending on `mode`).
- `s1_up`, `s1_down`: 1σ upward and downward limits (percentiles).
- `s2_up`, `s2_down`: 2σ limits.
- `s3_up`, `s3_down`: 3σ limits.
- `std1_up`, `std1_down`: optional percentiles of the raw sample (if `std` provided).
- `sims`: the full bootstrap simulation array (useful for diagnostics).

Installation

Requirements: Python 3.8+ and the dependencies listed in `setup.py` / `pyproject.toml`.

Install in editable (development) mode from the project root:

```bash
pip install -e .

Quickstart

Example: compute the bootstrapped median and 1/2/3σ intervals for a sample:

import numpy as np
from bootmedian import bootmedian

data = np.array([1.0, 2.1, 2.3, np.nan, 3.5, 2.0])
result = bootmedian(data, nsimul=2000, errors=1, verbose=True)
print(result)
# -> dict with keys: 'median','s1_up','s1_down',...,'sims'

Using weights:

weights = np.array([1, 1, 2, 1, 1, 1.5])
result_w = bootmedian(data, nsimul=2000, weights=weights)

Change the statistic with mode ("median", "mean", "std", "sum"):

mean_result = bootmedian(data, nsimul=1500, mode="mean")

Bootstrap linear fit

Use bootfit(x, y, nsimul) to obtain bootstrap distributions for slope (m) and intercept (b). The function returns a dictionary with medians and confidence percentiles for m and b.

from bootmedian import bootfit

x = np.linspace(0, 10, 20)
y = 2.3*x + 1.5 + np.random.normal(scale=0.5, size=x.size)
fit = bootfit(x, y, nsimul=1000)
print(fit['m_median'], fit['b_median'])

API reference (short)

  • bootstrap_resample(X, weights=False, seed=None) — Resamples an array-like X with optional weights (weighted sampling). Returns a flattened numpy array with one bootstrap resample.
  • median_bootstrap(argument) / mean_bootstrap(argument) / sum_bootstrap(argument) / std_bootstrap(argument) — Internal helpers used by bootmedian when running parallel workers. argument is a tuple/list: (sample, weights, std) where std is optional.
  • boot_polyfit(x, y, seed) — Performs a single resampled linear fit and returns [slope, intercept].
  • bootfit(x, y, nsimul, errors=1) — Runs nsimul bootstrap fits (currently single-threaded loop with progress). Returns a dict with medians and percentile confidence intervals for m and b.
  • bootmedian(sample_input, nsimul=1000, weights=False, errors=1, std=False, verbose=False, nthreads=7, mode="median") — Main function; see docstring in code for parameter details.

Notes & recommendations

  • The implementation uses bottleneck and pandas.DataFrame.sample for resampling.
  • For reproducibility you can set numpy.random.seed(...) before calling routines that internally use randomness. Some helper functions accept a seed.
  • nsimul controls accuracy vs runtime: start with a few hundred simulations, then increase to a few thousand if you need tighter percentiles.
  • If your input contains many NaNs ensure weights (if provided) align with non-NaN entries.

Development & tests

  • See setup.py and pyproject.toml for declared dependencies.
  • A small example is available at examples/simple_example.py — run it after installing dependencies with pip install -e ..

License & contact

This project is released under the BSD license (see setup.py for metadata). Author: Alejandro S. Borlaff a.s.borlaff@nasa.gov


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

bootmedian-1.1.3.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

bootmedian-1.1.3-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file bootmedian-1.1.3.tar.gz.

File metadata

  • Download URL: bootmedian-1.1.3.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for bootmedian-1.1.3.tar.gz
Algorithm Hash digest
SHA256 232503ec3d09fd5dc16450613864d09671515a66d7ea870bc6eeedd23bf9be1f
MD5 390eff2b5a28332a05a5c4456dc2e541
BLAKE2b-256 41ec4aa54d0cfe0984cfe71babc5344e05ef7022bd872d14cb6fa233934dfb91

See more details on using hashes here.

File details

Details for the file bootmedian-1.1.3-py3-none-any.whl.

File metadata

  • Download URL: bootmedian-1.1.3-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for bootmedian-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ca21d23b56cb442f74c3a685411560897fff1cb638c200c611a522dda6d77096
MD5 218cefaeaf596b7b06f00913c8b1816a
BLAKE2b-256 83a989f9eb233361ee9a40e5219cd612d677fa246b6e866b430f74541d881e49

See more details on using hashes here.

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