Situation: We ran an A/B campaign but randomization had slight geographic imbalance (e.g., more treatment traffic from Region A). We need a credible estimate of the campaign effect after adjusting for that imbalance.Approach (summary):- Quantify imbalance, adjust for geography and other covariates, estimate treatment effect with robust CIs, run diagnostics and sensitivity checks.Step-by-step with examples:1) Measure imbalance- Quick SQL to compare assignment and outcomes by region:sql
SELECT region,
SUM(CASE WHEN variant='treatment' THEN 1 ELSE 0 END) AS n_treat,
SUM(CASE WHEN variant='control' THEN 1 ELSE 0 END) AS n_ctrl,
AVG(metric) FILTER (WHERE variant='treatment') AS avg_treat,
AVG(metric) FILTER (WHERE variant='control') AS avg_ctrl
FROM experiment_table
GROUP BY region;
Look for standardized differences >0.1.2) Covariate adjustment (recommended)- Fit regression of outcome on treatment + region + other covariates (age, device, pre-period metric) to control confounding and increase precision. Use robust SE or cluster by region if correlation exists.python
import statsmodels.formula.api as smf
# df has columns: outcome, variant (0/1), region, pre_metric, device
model = smf.ols('outcome ~ variant + C(region) + pre_metric + C(device)', data=df).fit(cov_type='cluster', cov_kwds={'groups': df['region']})
print(model.params['variant'], model.conf_int().loc['variant'])
Interpretation: coefficient on variant = adjusted average treatment effect (ATE).3) Alternative / robustness- Propensity score weighting if many covariates: estimate P(treatment|covariates), then IPW or overlap weights and estimate weighted difference.- Stratify by region and compute weighted average of within-region ATEs (meta-analysis weighting by inverse variance).4) Diagnostics & sensitivity- Check residual balance after adjustment; plot treatment effect by region (interaction term) to detect heterogeneity:python
model_inter = smf.ols('outcome ~ variant*C(region) + pre_metric', data=df).fit()
- Bootstrap or cluster-robust CIs; run falsification tests on pre-period metrics.5) Communication- Report adjusted ATE, unadjusted ATE, confidence intervals, and note assumptions (no unobserved confounders varying by region). If treatment effect heterogeneous, provide region-specific estimates and recommend targeted actions.Why this works:- Regression/IPW controls observed imbalances and improves precision; stratification respects the non-random assignment structure; diagnostics and sensitivity checks build stakeholder trust in conclusions.