KatInterview

Bootstrap Confidence Interval for Mean Order Value

Statistics & ExperimentsModel answer
MediumModel answerStatistics & Experimentsbootstrapconfidence-intervalsresampling

Last updated

Implement vectorized bootstrap resampling to estimate a 95% confidence interval for the mean of a sample, handling memory constraints efficiently.

You're analyzing transaction data for an online retailer. Given a single sample of historical order amounts, you need to estimate the uncertainty around the average order value.

Write a Python function that computes a 95% confidence interval for the mean using bootstrap resampling with 10,000 iterations.

Input:

  • orders: a NumPy array or list of numeric order values (floats or integers)

Output:

  • A tuple (lower_bound, upper_bound) representing the 95% confidence interval

Requirements:

  • Use the percentile method: the interval should span the 2.5th to 97.5th percentiles of the bootstrap distribution
  • Each bootstrap sample should be the same size as the original data, drawn with replacement
  • Optimize for performance: the solution should handle arrays with tens of thousands of elements without excessive memory usage
  • Handle edge cases appropriately (empty arrays, invalid values, etc.)

Example:

orders = [45.2, 67.8, 23.1, 89.4, 56.7, ...]  # thousands of values
lower, upper = compute_bootstrap_ci(orders)
# Returns something like (52.3, 58.9)

Explain your approach, including how you handle the memory/performance tradeoff and any assumptions you make.

Hints

Hint 1

Bootstrap resampling means drawing n samples with replacement from your n observations, computing the statistic of interest, and repeating many times to build an empirical distribution

Hint 2

Vectorize the resampling step using NumPy's random integer generation and fancy indexing rather than Python loops—but watch out for memory when creating a 10,000 × n matrix

Hint 3

Process resamples in chunks to bound memory usage: each chunk handles as many iterations as fit within a fixed element budget, keeping vectorization benefits without allocating gigabytes