KatInterview

Delivery Experiment Design: Sample Size and Error Control

Arrays & HashingAuto-graded · 2 test cases
MediumAuto-gradedArrays & Hashingstatisticsexperimental-designhypothesis-testing

Last updated

Tests ability to calculate sample sizes for superiority and non-inferiority tests, apply multiple-testing corrections, adjust for clustering, and explain sequential monitoring.

You are designing a two-arm randomized experiment to evaluate a new delivery routing feature. The control arm uses the existing system, and the treatment arm uses the new feature. Calculate the required sample sizes and describe error-control strategies under the following conditions.

Part 1: Main Metric (Delivery Duration)

The current average delivery duration is 42 minutes with a standard deviation of 15 minutes. You want to detect a 5% relative reduction (2.1 minutes absolute improvement). Use a two-sided significance level of 0.05 and power of 0.80.

Calculate the required sample size per arm for a two-sample t-test comparing means.

Part 2: Safety Metric (Cancellation Rate)

The baseline cancellation rate is 6%. You need to ensure the new feature does not increase cancellations by more than 0.5 percentage points (non-inferiority margin). Use a one-sided significance level of 0.05 and power of 0.80.

Calculate the required sample size per arm for a one-sided non-inferiority test on proportions.

Part 3: Multiple Hypothesis Testing

Your experiment tracks:

  • 1 primary metric (delivery duration)
  • 2 guardrail metrics (cancellation rate, estimated time accuracy)
  • 1 secondary metric (deliveries per courier-hour)

Describe an error-control strategy that accounts for testing multiple hypotheses. Specify the effective significance level for each metric family and explain how you would construct confidence intervals.

Part 4: Clustered Randomization

Randomization occurs at the zone-day level (all deliveries in a zone on a given day receive the same treatment). Each zone-day contains an average of 300 deliveries, and the intracluster correlation coefficient (ICC) is 0.03 for the primary metric.

Calculate the design effect, the adjusted sample size per arm, and the number of zone-days needed per arm.

Part 5: Sequential Testing

You plan to conduct 4 interim analyses at equally spaced intervals using the O'Brien-Fleming alpha spending function.

Explain how the critical values at early looks compare to the final analysis, describe the impact on experiment duration and minimum detectable effect, and outline how you would implement boundary checks in practice.

Output Format

Return a dictionary with five keys: "primary_mean", "cancellation_noninferiority", "error_control", "cluster_randomization", and "sequential_monitoring".

Each key should map to a nested dictionary containing the calculated values and explanations as shown in the sample output structure.

Constraints

  • Use normal approximations for sample size calculations
  • Round sample sizes up to the nearest integer
  • Use z = 1.96 for two-sided alpha = 0.05
  • Use z = 1.645 for one-sided alpha = 0.05
  • Use z = 0.842 for power = 0.80
  • Assume equal allocation between arms
  • For the proportion test, use conservative variance under the null hypothesis

Examples

Example 1

Input: calculate_experiment_design()

Output: {'primary_mean': {'formula': 'n_per_arm = 2 * (z_(1-alpha/2) + z_(power))^2 * sigma^2 / delta^2', 'delta_minutes': 2.1, 'z_alpha': 1.959963984540054, 'z_power': 0.8416212335729143, 'sample_size_per_arm': 801, 'note': 'Normal-approximation sample size for a two-sample t-test; rounding up.'}, 'cancellation_noninferiority': {'formula': 'n_per_arm = 2 * (z_(1-alpha) + z_(power))^2 * p0*(1-p0) / margin^2', 'absolute_margin': 0.005, 'z_alpha': 1.6448536269514722, 'z_power': 0.8416212335729143, 'sample_size_per_arm': 27896, 'note': 'Conservative planning approximation using the null-boundary variance; test H0: p_biker - p_control >= 0.005 versus H1: difference < 0.005.'}, 'error_control': {'approach': 'Hierarchical gatekeeping with Holm control within the guardrail family', 'primary_family_alpha': 0.05, 'guardrails_family_alpha': 0.05, 'guardrail_holm_procedure': 'Sort the two guardrail p-values; compare the smaller with 0.025 and the larger with 0.05, stopping at the first non-rejection.', 'secondary_family_alpha': 0.05, 'gatekeeping': 'Test the secondary orders-per-courier-hour metric confirmatorily only after the primary succeeds and both guardrails pass; otherwise report it as exploratory.', 'confidence_intervals': 'Report two-sided 95% CI for the primary. For non-inferiority guardrails report one-sided 95% upper confidence bounds for Biker minus control against +0.5 percentage points; provide Holm-adjusted simultaneous intervals (equivalently, sequential Holm-compatible intervals) for the two guardrails. Label exploratory intervals unadjusted.'}, 'cluster_randomization': {'design_effect_formula': 'DE = 1 + (m - 1)*ICC', 'design_effect': 9.969999999999999, 'adjusted_orders_per_arm': 7986, 'zone_days_per_arm': 27, 'orders_provided_by_whole_clusters_per_arm': 8100, 'note': 'Multiply the individually randomized primary sample size by DE, then round cluster count up; this assumes equal cluster size and ignores small-cluster degrees-of-freedom corrections.'}, 'sequential_monitoring': {'looks': 4, 'spending': 'O\'Brien-Fleming alpha spending is very small at early information fractions, so early efficacy boundaries are much more extreme than the fixed-horizon boundary; the final boundary is close to the ordinary two-sided 0.05 boundary. This protects type-I error while allowing early stopping only for unusually strong evidence. The stricter early boundary generally increases expected runtime and can require more information to stop early; the planned maximum-horizon MDE is not inherently changed when the design is powered using the sequential critical value.', 'final_look_spending_approximation': 'At information fraction 1, cumulative two-sided alpha spent is 0.05 (approximately 0.025 per tail); with four discrete looks the actual final increment is less than 0.05 because some alpha was spent at prior looks. The final critical value is computed from the chosen O\'Brien-Fleming spending function and joint boundary calibration, not by assigning 0.05 anew.', 'implementation': 'Prespecify the four information fractions, spending function, test statistic, and boundary algorithm. At each scheduled look compute information fraction and standardized statistic, evaluate the corresponding calibrated efficacy/futility boundaries, and stop only according to those boundaries. Use a validated group-sequential package and retain the prespecified analysis population and missing-data rules.'}}

Hints

Hint 1

For the mean comparison, apply the standard two-sample formula with pooled variance and normal quantiles

Hint 2

Non-inferiority tests use a one-sided alternative; the margin shifts the null hypothesis boundary

Hint 3

Hierarchical testing preserves familywise error by testing secondary metrics only after primary success; guardrails can use intersection-union logic

Hint 4

The design effect multiplies the variance by 1 + (m-1)×ICC, which directly inflates the required sample size