cswork / libraryOpen practice workspace

Coding questions · Pinterest

Calculate 95% Bootstrap Confidence Interval for Order Values

This question evaluates a data scientist's competence in statistical inference using bootstrap resampling, proficiency with numerical computing for large sample operations, and attention to performance optimization.

Scenario

An e-commerce firm wants a 95% confidence interval for the average order value but only has a single historical sample of order amounts.

Question

Given an array of past order values, write efficient Python code to return the 95% bootstrap confidence interval using 10,000 resamples. Explain your approach and any performance optimizations.

Hints

Use vectorized resampling (np.random.choice) and percentile bounds; avoid Python loops.

Bootstrap 95% CI for Order Mean

Given a non-empty list of historical order values (floats), compute a two-sided 95% bootstrap confidence interval for the mean using exactly 10,000 resamples with replacement. Use NumPy's Generator-based RNG for reproducibility: numpy.random.default_rng(seed).choice. Return the 2.5th and 97.5th percentile bounds of the bootstrap sample means as a list [low, high], rounded to 6 decimal places. If the list has one unique value, the interval is that value for both bounds.

Constraints

  • 1 <= len(order_values) <= 5000
  • Order values are finite floats (can be zero or positive)
  • Use exactly B = 10,000 bootstrap resamples with replacement
  • RNG must be numpy.random.default_rng(seed) for determinism
  • Percentile bounds are [2.5, 97.5]
  • Return a list of two floats rounded to 6 decimals

Examples

Example 1

Input: order_values = [100.25, 200.75], seed = 42
Output: [100.25, 200.75]

Explanation: With two values, bootstrap means concentrate at 100.25, 150.5, and 200.75; the 2.5th and 97.5th percentiles equal the extremes.

Example 2

Input: order_values = [50.0]
Output: [50.0, 50.0]

Explanation: All resampled means equal 50.0, so the interval is degenerate.

Hints

  1. Use numpy.random.default_rng(seed).choice to generate resamples in a vectorized way.
  2. Compute means along axis=1 and then np.percentile at [2.5, 97.5].
  3. To limit memory, generate resamples in batches (e.g., 1000 at a time) while keeping vectorization within each batch.

Approach

Convert the input to a NumPy array. Use default_rng(seed) for reproducible sampling. Generate bootstrap resamples with replacement in vectorized batches to manage memory. For each batch, compute row-wise means and store them. After collecting 10,000 bootstrap means, compute the 2.5th and 97.5th percentiles to form the two-sided 95% confidence interval. Round both bounds to 6 decimals before returning.

  • Time complexity: O(B * n)
  • Space complexity: O(B + batch * n)