Switchback Test Design for Reducing Cold Deliveries by Bike
Statistics & ExperimentsModel answerLast updated
Design and analyze a two-week, city-level switchback test of a courier feature: slot length, randomization, ITT versus exposure, clustered regression, power, diagnostics and stopping rules.
A food-delivery marketplace has built a feature meant to cut the share of orders that reach customers cold when delivered by bike couriers in dense urban zones. Because couriers and demand are shared across orders, you can't randomize individual orders. Instead you will run a two-week switchback test: within each city, the feature alternates ON and OFF across equal-length time slots.
Facts you can use: an order takes about 45 minutes from dispatch to delivery, and courier positioning from the previous slot carries over for about 30 minutes.
Answer each part precisely.
A. Randomization. Pick a slot length L and justify it against the contamination sources above. Describe a block-randomization scheme that balances day-of-week and peak hours while keeping the schedule unpredictable.
B. Assignment vs. exposure. Distinguish slot-level assignment (intention-to-treat) from realized exposure, for example when OFF-slot couriers pick up spillover demand from nearby ON slots. State the numerator and denominator of the primary metric (cold-food rate among bike deliveries). Show two denominator variants: (1) all deliveries, condition_label in {0, 1}; (2) only deliveries with condition_label = 1.
C. Analysis model. Write the exact regression, with city fixed effects, slot-of-week fixed effects and standard errors clustered at city×slot. Explain how pre-period baselines and covariates (weather, surge, courier mix) could add precision, and what risks they carry.
D. Power. Baseline cold-food rate is 6%, the target is a 10% relative reduction (0.6 percentage points), there are 120 eligible orders per slot on average, slot-level ICC is 0.02, and the test lasts 14 days. Estimate how many slots, cities and ON↔OFF switchbacks you need for 80% power at α = 0.05. State assumptions and show the calculation or code.
E. Diagnostics. List concrete randomization and balance checks, and describe how you would test for carryover (leading indicators, excluding boundary periods, and so on).
F. Robustness. Explain how you would handle partial compliance, missing telemetry and mid-test shocks such as a major event. Give a principled rule for when to stop, extend or rerun.
Hints
Hint 1
Slot length must exceed the order lifecycle plus the courier carryover horizon, but each extra hour you add shrinks the number of clusters, and clusters are where your power comes from.
Hint 2
For the power calculation, first compute the order count for a plain two-proportion test, then multiply by the design effect 1 + (m − 1)ρ and divide by orders per slot to get slots. Convert slots to cities using the number of slots one city offers in 14 days.
Hint 3
Keep the randomized assignment as the primary analysis (ITT). Treat anything that conditions on realized exposure or on post-assignment covariates as secondary, because both can reintroduce bias. Pre-register the decision rules before looking at outcomes.
Model answer
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Framing
Couriers, demand and kitchens are shared, so treating one order can change the outcome of another. Randomizing by order would therefore be biased. A switchback randomizes whole time windows, and the slot (city × time window) is the cluster. All design, analysis and power choices follow from that.
A. Randomization
Slot length. Two things leak across slot boundaries:
- Orders dispatched late in a slot are delivered during the next one (about 45 min).
- Courier positions at the start of a slot were shaped by the previous slot's policy (about 30 min).
Taken together, the contaminated stretch is up to about 75 minutes, so a slot must be clearly longer than that. I'd choose L = 2 hours: 12 slots per day and 168 per city over 14 days. A 90-minute slot would be mostly contaminated. A 4-hour slot would halve the cluster count and hurt power. Assign each order to a slot by dispatch time, because that is when the feature acts. Pre-register a sensitivity analysis that drops the first 30 minutes of each slot.
Block randomization.
- Stratify by city × day-of-week × daypart (morning, lunch peak, afternoon, dinner peak, late).
- Within each stratum, form blocks of two adjacent slots and assign one ON and one OFF in random order. This guarantees balance on weekday and peak exposure.
- Randomize each city independently, so at any hour some cities are ON and others OFF. That decorrelates treatment from citywide shocks such as weather or events.
- For unpredictability, use a seeded RNG with the seed stored in a log, never strict alternation, keep the schedule hidden from operations staff and couriers, and never let assignment depend on live conditions.
B. Assignment vs. exposure
- Assignment (ITT): an order is treated if its dispatch falls in an ON slot. This is random by construction, so it estimates the effect of the policy as actually deployed.
- Exposure: what the order really experienced. It diverges from assignment when an OFF-slot courier completes a batch accepted under ON logic, when demand spills over from neighboring ON slots or zones, or when the flag fails to apply.
Let condition_label = 1 mean the order was cleanly exposed to its assigned condition and 0 mean mixed or spillover.
Metric: cold-food rate among bike deliveries. The numerator is bike deliveries with a cold incident. The denominator depends on the variant:
- Variant 1, ITT (primary): all bike deliveries in the slot, labels 0 and 1. Inclusion depends only on the random assignment, so the comparison is unbiased.
- Variant 2, per-protocol (secondary): only
condition_label = 1deliveries, in both numerator and denominator. This can be biased, because the treatment itself influences who gets labeled 0. The dropped orders tend to be the longest or boundary-crossing trips, which are the ones most prone to going cold.
The metric is a ratio of slot totals, so analyze it at slot level, using the delta method or the clustered regression below.
C. Analysis model
cold_i = α + τ·ON_{c,s} + γ_c + δ_{slot-of-week} + θ·X_{c,s} + ε_i
SE: clustered by city×slot
- τ is the ITT effect in percentage points.
- City FE (γ) remove level differences. Slot-of-week FE (84 levels: 7 days × 12 slots) remove daily and weekly seasonality.
- Precision: include a CUPED-style covariate, the cold rate for the same city and slot-of-week over the four pre-test weeks. Add weather, which is exogenous.
- Caution: realized surge and courier mix are post-treatment, since the feature can move them. Adjusting for them biases τ. Use only pre-slot or forecast versions, such as scheduled supply at slot start.
- Robustness: randomization inference. Re-draw the block assignments about 10,000 times and compare the observed τ to the permuted distribution. This avoids reliance on asymptotic cluster SEs.
D. Power
Assumptions: two-sided α = 0.05, power 0.80, p₀ = 0.06, p₁ = 0.054, m = 120, ρ = 0.02, equal allocation, and fixed effects and covariates ignored (conservative).
- Orders per arm without clustering: n = (1.96 + 0.8416)² × [0.06·0.94 + 0.054·0.946] / 0.006² = 7.849 × 0.1075 / 0.000036 ≈ 23,400.
- Design effect: 1 + (120 − 1)(0.02) = 3.38.
- Adjusted orders: about 79,200 per arm, which is about 660 slots per arm and about 1,320 slots in total.
from scipy.stats import norm
import math
p0, p1, m, icc = 0.06, 0.054, 120, 0.02
z = norm.ppf(0.975) + norm.ppf(0.80)
n_arm = z**2 * (p0*(1-p0) + p1*(1-p1)) / (p0-p1)**2
de = 1 + (m-1)*icc
slots_arm = math.ceil(n_arm * de / m) # ~660
cities = math.ceil(2*slots_arm / (14*12)) # ~8
One city offers only 168 slots, so about 8 comparable cities are needed (8 × 168 = 1,344 ≥ 1,320). That is about 84 ON and 84 OFF slots per city. Under paired-block randomization, expect roughly 80–110 ON↔OFF transitions per city. If fewer cities are available, extend the duration, restrict to dense zones where the event rate is higher, or accept a larger MDE. Trimming burn-in orders lowers m, so rerun the calculation with the trimmed value.
E. Diagnostics
- Schedule vs. flag logs: report the share of slots where the applied state differs from the assigned state.
- Sample-ratio mismatch: chi-square test on ON/OFF slot counts and order counts (alert at p < 0.001).
- Balance: compare pre-period cold rate, order volume, courier supply, weather and daypart mix across arms. A joint F-test on covariates predicting assignment should be unremarkable.
- Volume check: orders per slot should not differ by arm. If they do, suspect demand-side interference or logging bugs.
- Carryover: add the previous slot's assignment as a regressor (it should be near zero). Add a lead (next slot's assignment) as a placebo that must be zero. Estimate the effect by minutes since the switch to see whether it ramps up. Compare results with and without the first 30 minutes of each slot, and with and without slots that follow a state change.
F. Robustness and decision rules
- Partial compliance: keep ITT as the primary result. For a secondary estimate of the effect on compliers, scale the ITT by the difference in compliance rates, using assignment as an instrument. Avoid comparing only
condition_label = 1orders naively. - Missing telemetry: check whether missingness differs by arm. If not, analyze available data. If so, report worst-case bounds and treat the result as inconclusive.
- Shocks: randomization already spreads shocks across arms. Pre-register exclusion rules for affected city-days, applied blind to arm and symmetric across arms, and report results both ways.
- Decision rule: fix the horizon in advance and avoid peeking, or use alpha-spending for interim looks. At the end:
- Stop and decide if the CI is clearly above or below the MDE and diagnostics pass.
- Extend if diagnostics pass but the CI is too wide to decide.
- Rerun if SRM, flag failures or significant carryover invalidate the design, for example with longer slots.
Common mistakes
- Randomizing by order, or by slots shorter than the contamination horizon.
- Ignoring clustering in power or standard errors, which understates the sample needed by over 3×.
- Adjusting for post-treatment covariates.
- Using the per-protocol variant as the headline result.
- Peeking and stopping on a favorable interim result.