Skip to content

Batch resampling for one-dimensional bootstrap means - #4046

Open
soodoku wants to merge 1 commit into
mwaskom:masterfrom
finite-sample:perf/batched-mean-bootstrap
Open

soodoku wants to merge 1 commit into
mwaskom:masterfrom
finite-sample:perf/batched-mean-bootstrap

Conversation

@soodoku

@soodoku soodoku commented Nov 21, 2026

Copy link
Copy Markdown

The default 95% interval on a grouped mean calls the bootstrap statistic 1,000 times per group. This patch generates resampling indices for several draws at a time and computes one mean per row. It uses the same random-number stream as the individual draws, with temporary indices and values bounded to about 4 MiB or one complete draw.

The change applies to unstructured means of one real-valued, one-dimensional array. Arbitrary estimators, structured resampling, and named NaN-aware reductions take the existing path. Floating-point inputs with enough range to overflow an intermediate sum also take that path, so an exception leaves the caller’s generator at the same draw. The tests compare the individual estimates, output dtype, and next random bytes to an independent scalar loop for both Generator and RandomState.

Timings

Median elapsed time per complete call on macOS arm64, Python 3.14, NumPy 2.3.5, pandas 2.3.3 and Matplotlib 3.10.8. Each chart created a figure, drew to an Agg canvas and closed the figure. Each revision had 12 warmed calls in four alternating process blocks. Baseline is c1cb287.

Plot Groups × observations each Resamples per group Before After
barplot 2 × 9 25 13.71 ms 13.16 ms
barplot 18 × 73 1,000 100.58 ms 32.49 ms
pointplot 18 × 73 1,000 110.43 ms 32.07 ms
barplot 5 × 999 1,000 57.35 ms 30.36 ms
barplot 18 × 73 Intervals off 26.91 ms 25.90 ms

All individual bootstrap draws, plot coordinates and rendered RGBA images matched byte for byte. On the 18-group bar plot, the bootstrap calculation alone went from 69.51 ms to 3.61 ms. The estimate for the first group was −0.30619 with interval [−0.61281, −0.00818] in both versions.

Reproduce the fully rendered bar plot benchmark on either checkout
import timeit
from statistics import median
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

groups, n = 18, 73
rng = np.random.default_rng(20261121)
y = rng.standard_t(6, (groups, n)) + np.arange(groups)[:, None] / 15
df = pd.DataFrame({"group": np.arange(groups).repeat(n), "response": y.ravel()})

def render():
    fig, ax = plt.subplots(figsize=(6, 4), dpi=80)
    try:
        sns.barplot(df, x="group", y="response", n_boot=1000, seed=13579, ax=ax)
        fig.canvas.draw()
    finally:
        plt.close(fig)

render()
print(median(timeit.repeat(render, number=1, repeat=12)) * 1000, "ms")

Local checks: make test (2,376 passed, 14 skipped, six expected failures), make lint, and make typecheck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant