L5Easy★ Core Essential · Industrial BedrockPart L · Classical ML & Statistical Simulation

Bootstrap Resampling & 95% Confidence Interval

⚡Flashcard Drill➔

Non-parametric Bootstrap resampling with replacement to construct empirical 95% confidence intervals.

⏱️ Time: O(B * N)
💾 Space: O(B) 保存重采样统计量
🎯 Interactive Self-Practice Loop (Attempt Before Peeking)
Practice in 4 steps: recall mechanism, complete core lines, check edge cases, and log mastery locally.
Step 1 · Mechanism Recall & Stability Self-Check
Step 2 · Complete Key Implementation Lines
import numpy as np
def bootstrap_ci(
data: np.ndarray,
n_bootstraps: int = 1000,
ci: float = 0.95,
stat_fn = np.mean
) -> tuple:
N = len(data)
boot_stats = np.empty(n_bootstraps, dtype=np.float64)
for b in range(n_bootstraps):
# 有放回随机抽取 N 个样本
resample_idx =
boot_stats[b] = stat_fn(data[resample_idx])
# 计算双侧分位数
alpha = (1.0 - ci) / 2.0
lower_bound = np.percentile(boot_stats, alpha * 100)
upper_bound =
point_est = float(stat_fn(data))
return point_est, float(lower_bound), float(upper_bound)
Step 3 · Verify Boundary Cases & Assertions
Step 4 · Follow-Up Drill & Review Outcome
Q1: What fraction of original data points are left out (Out-Of-Bag, OOB) on average in each bootstrap draw?
Next: Welford's Algorithm for Online Mean & Variance →
💡

Core Mental Anchor / Mnemonic

Resample N items with replacement B times and read [2.5%, 97.5%] empirical quantiles.

📐 Mathematical Derivation & Core Formula

X∗(b)∼Sample(X,N,replace=True),CI95%=[Percentile2.5%,Percentile97.5%]X^{*(b)} \sim \mathrm{Sample}(X, N, \text{replace}=\text{True}), \quad \mathrm{CI}_{95\%} = [\text{Percentile}_{2.5\%}, \text{Percentile}_{97.5\%}]
### Non-Parametric Resampling Confidence Intervals
Many estimators (medians, quantiles, AUC lifts, ratio metrics) lack simple closed-form variance formulas.
Bootstrap treats the empirical sample of size NN as a proxy population: draw BB bootstrap datasets (e.g., B=1000B = 1000) of size NN with replacement, recompute θ∗(b)\theta^{*(b)} on each, and take the [2.5%,97.5%][2.5\%, 97.5\%] empirical percentiles as a two-sided 95% confidence interval.

🔄 Tensor Dimensions & Shape Flow

sample (N,) -> B resamples of size N w/ replacement -> stat distribution (B,) -> [2.5%, 97.5%] percentiles

🛡️ Numerical Stability & Pitfalls Checklist

  • Keep each bootstrap resample size equal to original sample size N with replacement (replace=True).
  • Effective for metrics lacking simple parametric variance formulas (e.g., medians, P99 latency, AUC).

💻 Reference Code Implementation

import numpy as np

def bootstrap_ci(
    data: np.ndarray,
    n_bootstraps: int = 1000,
    ci: float = 0.95,
    stat_fn = np.mean
) -> tuple:
    N = len(data)
    boot_stats = np.empty(n_bootstraps, dtype=np.float64)
    
    for b in range(n_bootstraps):
        # 有放回随机抽取 N 个样本
        resample_idx = np.random.choice(N, size=N, replace=True)
        boot_stats[b] = stat_fn(data[resample_idx])
        
    # 计算双侧分位数
    alpha = (1.0 - ci) / 2.0
    lower_bound = np.percentile(boot_stats, alpha * 100)
    upper_bound = np.percentile(boot_stats, (1.0 - alpha) * 100)
    point_est = float(stat_fn(data))
    
    return point_est, float(lower_bound), float(upper_bound)

🧪 Runnable Assertions & Validation

Copy and run directly in Python / Jupyter:

import numpy as np
np.random.seed(42)
data = np.random.normal(loc=10.0, scale=2.0, size=200)
est, low, high = bootstrap_ci(data, n_bootstraps=500, ci=0.95)
# 均值点估计应在 9.5 ~ 10.5 之间,且真值 10 必被置信区间包含
assert 9.5 < est < 10.5
assert low < 10.0 < high
print("✓ Bootstrap 重采样自测通过")

🎯 Core Follow-up Inquiries & Reference Answers

Q1:What fraction of original data points are left out (Out-Of-Bag, OOB) on average in each bootstrap draw?
Each point has probability 1−1/N1 - 1/N of being skipped on a single draw, so across NN draws (1−1/N)N→e−1≈36.8%(1 - 1/N)^N \to e^{-1} \approx 36.8\% of observations stay out-of-bag on average.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔
← Prev: Principal Component Analysis (PCA)Back to Part L & All 105Next: Welford's Algorithm for Online Mean & Variance →