Switchback
switchback
Switchback and temporal crossover Design of Experiments (DoE) matrices.
This module provides the SwitchbackDesign class, which constructs time-series crossover designs.
Switchback experiments are the standard for marketplace, dispatch, and matching networks (e.g., ride-sharing,
on-demand delivery) where standard user-level randomization is corrupted by market-wide network spillovers.
| CLASS | DESCRIPTION |
|---|---|
SwitchbackDesign |
Generates time/geo crossover switchback experimental designs for marketplace tests. |
SwitchbackDesign
Bases: DesignMatrix
Generates time/geo crossover switchback experimental designs for marketplace tests.
In standard user-split A/B testing, a treatment that increases supply utilization in a local market indirectly deprives the control group of supply (market interference/spillover bias). This violates the Stable Unit Treatment Value Assumption (SUTVA). Switchback designs resolve this by randomizing the entire marketplace over sequential time blocks.
Temporal Crossover and Balance
The marketplace switches back and forth between control and treatment configurations over a series of discrete time blocks of length \(W\) (e.g., 2 hours). $$ \text{Schedule}: W_1 \to \text{Control}, \ W_2 \to \text{Treatment}, \ W_3 \to \text{Treatment}, \ \dots $$ To prevent systematic time-of-day or day-of-week biases (e.g., treatment always running during rush hour), the assignments are structured using balanced crossover patterns: - Markovian Transitions: Ensuring equal transition probabilities between states (\(C \to T\), \(T \to C\), \(C \to C\), \(T \to T\)) to model and subtract temporal carryover. - Multi-region Crossover: If multiple geographic markets are available, we cross them over simultaneously: $$ \text{Region A}: C \to T \quad \text{vs.} \quad \text{Region B}: T \to C $$
Carryover and Washout Periods: A major challenge in switchbacks is the carryover effect — supply/demand states from a treatment period spilling over into the subsequent control period. To solve this, the algorithm configures a "washout" parameter \(\omega\) (e.g., 30 minutes) at the start of each window. Data collected during the first \(\omega\) minutes of each transition is excluded from statistical evaluation.
Pseudocode for the Algorithm
function generate_switchback_schedule(regions, start_time, end_time, window_hours, washout_minutes):
1. Partition the experimental timeframe [start_time, end_time] into N discrete blocks of size window_hours.
2. For each region:
Generate a balanced crossover assignment sequence (using Latin Squares or randomized block schedules).
3. For each block in the schedule:
Mark the first washout_minutes as "washout_active = True" (telemetry exclusion flag).
Assign the active variant label.
4. Compile regional time-block rows into a structured DataFrame.
5. Return DataFrame.
| ATTRIBUTE | DESCRIPTION |
|---|---|
unit_window_hours |
The duration in hours of each discrete experimental block. Defaults to 2.
TYPE:
|
Examples:
Example
| PARAMETER | DESCRIPTION |
|---|---|
factors
|
Mapping of the market-level factor to its variant options.
TYPE:
|
unit_window_hours
|
Duration of each switch window in hours. Defaults to 2.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
generate |
Generates the Switchback design schedule. |
optimize_washout |
Evaluates AR(p) error structures across candidate washout times to stabilize covariance. |
Source code in src\xpyrment\design\doe\switchback.py
generate
Generates the Switchback design schedule.
Divides the temporal horizons into balanced blocks, schedules treatment crossovers, marks washout segments, and outputs the operational assignment ledger.
| PARAMETER | DESCRIPTION |
|---|---|
regions
|
List of geographic or logical market regions to crossover.
Defaults to
TYPE:
|
num_periods
|
Total number of sequential time block windows. Defaults to 12.
TYPE:
|
washout_minutes
|
Transition period length to discard temporal carryover. Defaults to 30.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
pd.DataFrame: A pandas DataFrame representing the temporal switchback schedule. |
Source code in src\xpyrment\design\doe\switchback.py
optimize_washout
optimize_washout(
df: DataFrame,
time_col: str,
metric_col: str,
treatment_col: str,
max_p: int = 3,
stability_threshold: float = 0.05,
) -> int
Evaluates AR(p) error structures across candidate washout times to stabilize covariance.
| PARAMETER | DESCRIPTION |
|---|---|
df
|
Experimental telemetry dataset.
TYPE:
|
time_col
|
Minute/second elapsed column since block transition.
TYPE:
|
metric_col
|
Target evaluation metric.
TYPE:
|
treatment_col
|
Column indicating binary/categorical treatment assignment.
TYPE:
|
max_p
|
Maximum autoregressive lag order. Defaults to 3.
TYPE:
|
stability_threshold
|
Variance/autocorrelation index threshold representing stability. Defaults to 0.05.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
The optimal washout period in minutes.
TYPE:
|