Evaluate multiple guardrail metrics for non-inferiority breaches in pandas
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: medium
Interview Round: Other
Implement `guardrail_evaluation_multi(df)` in Python with pandas. The input has one row per guardrail metric with columns `metric` (object), `control_value` (float64, non-zero), `treatment_value` (float64), `direction` (`'increase_bad'` or `'decrease_bad'`) and `threshold` (float64, a non-negative relative bound). Compute each metric's relative change and flag a breach when the change moves past the threshold in that metric's harmful direction.
```python
def guardrail_evaluation_multi(df: pd.DataFrame) -> pd.DataFrame:
...
```
Requirements:
- `rel_change = (treatment_value - control_value) / control_value` for each row.
- For `'increase_bad'`, `breached` is true if and only if `rel_change > threshold`.
- For `'decrease_bad'`, `breached` is true if and only if `rel_change < -threshold`.
- Return the columns `['metric', 'rel_change', 'breached']` in input row order (do not sort), with the index reset to `0..n-1`.
- Dtypes: `metric` object, `rel_change` float64, `breached` bool.
Example:
```python
df = pd.DataFrame({
'metric': ['latency', 'ctr'],
'control_value': [100.0, 0.20],
'treatment_value': [108.0, 0.19],
'direction': ['increase_bad', 'decrease_bad'],
'threshold': [0.05, 0.10],
})
guardrail_evaluation_multi(df)
# metric rel_change breached
# 0 latency 0.08 True
# 1 ctr -0.05 False
```
Latency rose 8%, which is past its 5% bound in the harmful direction. CTR fell 5%, which is inside its 10% bound, so it does not breach.
```hint Avoid a row loop
The comparison depends on each row's direction. Build one boolean condition per direction and combine them.
```
```hint Boundaries and alignment
Check what your code returns when a change lands exactly on the threshold, and when the input index is not `0..n-1`.
```
### Constraints & Assumptions
- `control_value` is non-zero in every row, and `threshold` is non-negative.
- `direction` takes only the two listed values.
- The input index can be arbitrary; the output index must be `0..n-1`.
### What a Strong Answer Covers
- A vectorized computation of the relative change.
- The correct one-sided, strict comparison for each direction.
- The exact columns, row order, index and dtypes requested.
- Inputs the specification does not cover cleanly, such as negative baselines, missing values or unknown direction labels.
- The difference between a point-estimate check and a statistical non-inferiority test.
### Follow-up Questions
- How would you add statistical significance to the breach decision?
- How would you handle many guardrails and the multiple-comparisons problem?
- What goes wrong with this relative change when `control_value` is negative?
Overview: Write a pandas function that computes the relative change of several guardrail metrics between control and treatment and flags a breach when a metric moves past its threshold in its harmful direction. It tests vectorized conditional logic, exact output contracts, and the limits of point-estimate guardrail checks.