FastPrepBootstrap Mean and Confidence Interval

Bootstrap Mean and Confidence Interval

Amazon logoAmazon● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Compute the observed sample mean and a two-sided percentile bootstrap interval from the supplied resampling draws.

data is a nonempty sample of n numeric observations. Each row of resampleIndices records n draws with replacement from that sample: an index i selects data[i]. Repeated indices and repeated rows are valid. Use the recorded draws exactly as supplied.

  1. Compute the arithmetic mean of the original data.
  2. Compute the mean of each resample and sort those b means into y.
  3. Let tail = (1 - confidence) / 2. The interval endpoints are Q(tail) and Q(1 - tail).

For this exercise, assume linear quantiles: for 0 <= q <= 1, let h = q * (b - 1), lo = floor(h), hi = ceil(h), and w = h - lo. Then Q(q) = (1 - w) * y[lo] + w * y[hi]. This also defines the one-resample case.

Return [observedMean, lowerEndpoint, upperEndpoint]. The first value is the original sample mean, rather than the average of the bootstrap means. Confidence zero returns the bootstrap median twice; confidence one returns the minimum and maximum bootstrap means.

The supplied indices make the resampling reproducible. Do not generate new random draws. Use standard arithmetic and collection operations for the implementation.

Interview follow-ups

  • Explain how a numerical-array library can compute the resample means without Python-level loops, and discuss its memory cost.
  • Discuss when bootstrap estimates are appropriate for different statistics; explain why the applicability depends on the statistic and sampling assumptions.

Function

bootstrapMeanInterval(data: double[], resampleIndices: int[][], confidence: double) → double[]

Examples

Example 1

data = [1,3]resampleIndices = [[0,0],[0,1],[1,0],[1,1]]confidence = 0.5return = [2,1.75,2.25]

The four resample means are 1,2,2,3. The 25th and 75th percentiles interpolate to 1.75 and 2.25; the original mean is 2.

Example 2

data = [-5]resampleIndices = [[0],[0],[0]]confidence = 0.95return = [-5,-5,-5]

Every resample and the original data have the same mean -5, so both interval endpoints are -5.

Example 3

data = [0,10,20]resampleIndices = [[0,1,2],[0,0,0],[2,2,2],[1,1,1],[2,0,1]]confidence = 1return = [10,0,20]

The resample means sort to 0,10,10,10,20. Confidence 1 selects their minimum and maximum; the observed mean remains 10.

Constraints

  • 1 <= n <= 200 and 1 <= b <= 200, with n * b <= 4000.
  • Every resample row has length n and contains indices in 0..n-1.
  • Every observation is finite and between -1000000 and 1000000.
  • 0 <= confidence <= 1.
  • Use double precision. Absolute error of at most 10^-7 is accepted for each output coordinate.

More Amazon problems

See Amazon hiring insights
public double[] bootstrapMeanInterval(double[] data, int[][] resampleIndices, double confidence) {
    // Write your code here
}
data[1,3]
resampleIndices[[0,0],[0,1],[1,0],[1,1]]
confidence0.5
expected[2,1.75,2.25]
Checking account…