Check Covariate Balance Between Experiment Arms with Standardized Mean Differences
Quick Overview
Implement a pandas function that computes the standardized mean difference of each covariate between the control and treatment arms of an experiment. It tests sample standard deviations, the choice of pooled denominator and how to read covariate balance after randomization.
Check Covariate Balance Between Experiment Arms with Standardized Mean Differences
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: easy
Interview Round: Other
Implement `def covariate_smd(df: pd.DataFrame, covariates: list[str]) -> dict`. `df` has a `'group'` column (`'control'` or `'treatment'`) plus numeric covariate columns. Return the standardized mean difference (SMD) between arms for each name in `covariates`.
Requirements:
- `smd = (mean_treatment - mean_control) / sp` (treatment minus control).
- `sp = sqrt((s_control**2 + s_treatment**2) / 2)`, the plain average of the two variances, not the n-weighted pooled form.
- Both per-arm SDs use `ddof=1` (the pandas `.std()` default).
- Return a plain `dict` of `{covariate: float}` in input order.
For example:
```python
df = pd.DataFrame({
'group': ['control', 'control', 'control', 'treatment', 'treatment'],
'age': [30, 32, 34, 40, 44],
'prior_sessions': [1, 2, 3, 1, 3],
})
covariate_smd(df, ['age', 'prior_sessions'])
# -> {'age': 4.08248..., 'prior_sessions': 0.0}
```
Control ages average 32 and treatment ages average 42. Both arms average 2 prior sessions.
```hint Standard deviation defaults
Split the frame by arm once, then watch which `ddof` each library uses: pandas `.std()` and NumPy `np.std` do not share a default.
```
### What a Strong Answer Covers
- Computes per-arm means and sample standard deviations with `ddof=1` after splitting the data by `group`.
- Uses the average-of-variances denominator and keeps the treatment-minus-control sign.
- Returns a plain dict of floats in the order of `covariates`.
- Checks the result by hand on a small example, including a covariate whose means are equal.
- Handles edge cases such as an arm with one row, a covariate that is constant in both arms, and missing values.
### Follow-up Questions
- How would you flag which covariates are imbalanced?
- What if an arm has zero variance for a covariate?
- Why average the two variances instead of using the n-weighted pooled variance, and when do the two give different answers?
- Randomization balances covariates in expectation. Why check SMDs at all, and why not run a t-test on each covariate?
Overview: Implement a pandas function that computes the standardized mean difference of each covariate between the control and treatment arms of an experiment. It tests sample standard deviations, the choice of pooled denominator and how to read covariate balance after randomization.
Implement def covariate_smd(df: pd.DataFrame, covariates: list[str]) -> dict. df has a 'group' column ('control' or 'treatment') plus numeric covariate columns. Return the standardized mean difference (SMD) between arms for each name in covariates.
Requirements:
smd = (mean_treatment - mean_control) / sp
(treatment minus control).
sp = sqrt((s_control**2 + s_treatment**2) / 2)
, the plain average of the two variances, not the n-weighted pooled form.
Both per-arm SDs use
ddof=1
(the pandas
.std()
default).
Return a plain
dict
of
{covariate: float}
in input order.