Flag Imbalanced Covariates in an Experiment by Standardized Mean Difference
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: easy
Interview Round: Other
Implement `imbalanced_covariates(df, covariates, threshold=0.1)`. `df` has a `'group'` column (`'control'` or `'treatment'`) plus numeric covariate columns. Return the covariates whose absolute standardized mean difference (SMD) exceeds `threshold`.
Requirements:
- Per-arm means and sample SDs (`ddof=1`).
- `smd = (mean_treatment - mean_control) / sqrt((s_control**2 + s_treatment**2)/2)`.
- Flag when `|SMD| > threshold` (strict).
- Return a `list[str]` in the same order as `covariates`.
For example:
```python
df = pd.DataFrame({
'group': ['control'] * 3 + ['treatment'] * 3,
'sessions_last_week': [4, 6, 8, 3, 5, 7],
'prior_sessions': [1, 2, 3, 3, 2, 1],
'age': [30, 32, 34, 40, 42, 44],
'tenure_months': [0, 10, 20, 1, 11, 21],
})
# SMDs -> {sessions_last_week: -0.5, prior_sessions: 0.0, age: 5.0, tenure_months: 0.1}
imbalanced_covariates(df, ['sessions_last_week', 'prior_sessions', 'age', 'tenure_months'])
# -> ['sessions_last_week', 'age']
```
`sessions_last_week` is flagged because the rule uses the absolute value. `tenure_months` is not, because 0.1 is not strictly above the threshold.
```hint Undefined values
Before writing the comparison, work out what your SMD becomes when the denominator is 0 or a standard deviation is NaN, and what `abs(x) > threshold` returns for that value.
```
### What a Strong Answer Covers
- Computes each covariate's SMD from per-arm means, sample SDs with `ddof=1` and the average-of-variances denominator.
- Flags on the absolute SMD with a strict comparison against `threshold`.
- Returns the flagged names as a list in the order of `covariates`.
- Checks the result on an example that includes a negative SMD and a value on the threshold.
- Explains what an undefined SMD does to the flag and picks an explicit policy for it.
### Follow-up Questions
- How would you handle a covariate with zero pooled variance?
- Would you use a pooled SD or the treatment-arm SD in the denominator?
- You check 30 pre-experiment covariates and two are flagged. What do you conclude, and what do you do next?
Overview: Write a pandas function that returns the covariates whose absolute standardized mean difference between the control and treatment arms exceeds a threshold. It tests sample standard deviations, the strict comparison rule, output ordering and how to treat an undefined SMD.