Bootstrap
bootstrap
Non-parametric bootstrap resampling and confidence interval estimation (Block 54).
This module provides the run_bootstrap_ci function, which performs computer-intensive resampling
to estimate the sampling distribution of arbitrary statistical estimators, yielding robust confidence intervals
without relying on asymptotic parametric assumptions.
| FUNCTION | DESCRIPTION |
|---|---|
run_bootstrap_ci |
Computes non-parametric bootstrap confidence intervals for arbitrary complex metrics. |
run_block_bootstrap_ci |
Computes non-parametric block bootstrap confidence intervals for dependent/autocorrelated data. |
run_bootstrap_ci
run_bootstrap_ci(
data_group: ndarray,
num_resamples: int = 2000,
confidence_level: float = 0.95,
method: str = "bca",
random_seed: Optional[int] = None,
) -> tuple
Computes non-parametric bootstrap confidence intervals for arbitrary complex metrics.
Bootstrap resampling (Efron, 1979) is a non-parametric method used to estimate the standard error and confidence intervals of an estimator (such as means, medians, ratios, or quantiles). It is particularly valuable when the underlying metric distribution is highly non-normal (e.g., bi-modal, zero-inflated, or power-law) or when the estimator's mathematical variance cannot be easily derived analytically.
Mathematical and Algorithmic Formulation
Let \mathbf{x} = (x_1, x_2, \dots, x_n) be the observed sample of size \(n\), and let \hat{\theta} = s(\mathbf{x}) be the point estimate of interest.
The bootstrap sampling distribution is constructed as follows: 1. Draw a bootstrap sample \mathbf{x}^{b} of size \(n\) by sampling uniformly with replacement from the original sample \mathbf{x}\(. 2. Calculate the bootstrap replication of the estimator: \\hat{\\theta}^{*b} = s(\\mathbf{x}^{*b})\). 3. Repeat steps 1-2 a large number of times \(B\) (where \(B = \\text{num\\_resamples}\), typically \(B \\ge 2000\)), generating a set of replicates: \{\hat{\theta}^{1}, \hat{\theta}^{2}, \dots, \hat{\theta}^{B}\}.
Confidence Interval Methods
- Percentile Bootstrap (Simple and intuitive): Sorts the bootstrap replicates in ascending order: \hat{\theta}^{(1)} \le \hat{\theta}^{(2)} \le \dots \le \hat{\theta}^{(B)}. For a confidence level of \(1 - \\alpha\) (e.g., \(0.95\) with \(\\alpha = 0.05\)), the interval endpoints are the \(\\alpha/2\) and \(1 - \\alpha/2\) percentiles of the empirical bootstrap distribution: $$ \left[ \hat{\theta}^{(\lfloor B \cdot \alpha/2 \rfloor)}, \ \hat{\theta}^{*(\lfloor B \cdot (1 - \alpha/2) \rfloor)} \right] $$
- Bias-Corrected and Accelerated (BCa) Bootstrap (Robust and accurate): Adjusts the percentile endpoints to correct for both median bias (displacement of the bootstrap distribution from the point estimate) and skewness (non-constant variance, represented by acceleration \(a\)).
- The bias-correction factor \(z_0\) is: $$ z_0 = \Phi^{-1} \left( \frac{\#\{\hat{\theta}^{*b} < \hat{\theta}\}}{B} \right) $$ where \(\\Phi^{-1}\) is the inverse cumulative distribution function of the standard normal distribution.
- The acceleration parameter \(a\) is computed using jackknife (leave-one-out) estimators: $$ a = \frac{\sum_{i=1}^{n} (\bar{\theta}{(\cdot)} - \theta{(i)})^3}{6 \left[ \sum_{i=1}^{n} (\bar{\theta}{(\cdot)} - \theta{(i)})^2 \right]^{3/2}} $$ where \(\\theta_{(i)}\) is the estimate of \(\\theta\) calculated by omitting the \(i\)-th observation, and \(\\bar{\\theta}_{(\\cdot)}\) is the average of these jackknife estimates.
- Transformed confidence percentiles are then mapped back to the sorted replicates to construct the interval.
| PARAMETER | DESCRIPTION |
|---|---|
data_group
|
The raw 1D array of observed values.
TYPE:
|
num_resamples
|
The number of bootstrap iterations (\(B\)). Defaults to 2000.
TYPE:
|
confidence_level
|
The desired confidence interval width (\(1 - \\alpha\)). Defaults to 0.95.
TYPE:
|
method
|
The bootstrap method to utilize (
TYPE:
|
random_seed
|
Random seed for numpy generator reproducibility. Defaults to None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple
|
A tuple of floats
TYPE:
|
Source code in src\xpyrment\analyze\inference\bootstrap.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
run_block_bootstrap_ci
run_block_bootstrap_ci(
data_group: ndarray,
block_size: int,
num_resamples: int = 2000,
confidence_level: float = 0.95,
bootstrap_method: str = "moving",
ci_method: str = "percentile",
random_seed: Optional[int] = None,
) -> tuple
Computes non-parametric block bootstrap confidence intervals for dependent/autocorrelated data.
Block bootstrap methods (Moving Block and Circular Block) resample contiguous blocks of data to preserve the temporal dependence structure within the time-series or sequence.
Mathematical Formulation of Block Bootstrap
Let \(\mathbf{x} = (x_1, x_2, \dots, x_n)\) be a time series of length \(n\), and let \(b\) be the block size.
-
Moving Block Bootstrap (MBB): We define \(N = n - b + 1\) overlapping blocks of length \(b\): $$ B_i = (x_i, x_{i+1}, \dots, x_{i+b-1}), \quad \text{for } i = 1, \dots, N $$ We resample \(k = \lceil n/b \rceil\) blocks with replacement from \(\{B_1, \dots, B_N\}\), concatenate them, and truncate the final sequence to length \(n\).
-
Circular Block Bootstrap (CBB): We define \(n\) overlapping blocks of length \(b\) by wrapping the time series circularly: $$ B_i = (x_i, x_{i+1}, \dots, x_{i+b-1}), \quad \text{for } i = 1, \dots, n $$ where index wrap-around uses modulo arithmetic: \(x_j = x_{((j - 1) \bmod n) + 1}\). We resample \(k = \lceil n/b \rceil\) blocks with replacement from \(\{B_1, \dots, B_n\}\), concatenate them, and truncate the final sequence to length \(n\).
| PARAMETER | DESCRIPTION |
|---|---|
data_group
|
The raw 1D array of observed values.
TYPE:
|
block_size
|
The size of contiguous blocks (\(b\)).
TYPE:
|
num_resamples
|
The number of bootstrap iterations (\(B\)). Defaults to 2000.
TYPE:
|
confidence_level
|
The desired confidence interval width (\(1 - \alpha\)). Defaults to 0.95.
TYPE:
|
bootstrap_method
|
The block bootstrap method (
TYPE:
|
ci_method
|
The confidence interval estimation method (
TYPE:
|
random_seed
|
Random seed for numpy generator reproducibility. Defaults to None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple
|
A tuple of floats
TYPE:
|
Source code in src\xpyrment\analyze\inference\bootstrap.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | |