Check an A/B test latency guardrail for a relative mean regression
Quick Overview
Write a pandas check that compares mean request latency between control and treatment and reports whether the relative regression exceeds a guardrail threshold. Tests groupby aggregation, relative versus absolute differences, strict boundary handling and returning a native Python bool.
Check an A/B test latency guardrail for a relative mean regression
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: easy
Interview Round: Other
Implement the function below. `df` has one row per request with columns `variant` (object, `'control'` or `'treatment'`) and `latency_ms` (float64). Return whether treatment's relative latency regression versus control breaches `threshold`. Higher latency is worse.
```python
def guardrail_latency_regressed(df: pd.DataFrame, threshold: float = 0.01) -> bool:
```
Requirements:
- Aggregate by `variant` and take the mean of `latency_ms`.
- Relative regression: `(mean_treatment - mean_control) / mean_control`, a fraction of the control mean (not an absolute ms difference).
- Return `True` only if the relative regression is strictly `>` `threshold`. Exactly at `threshold` is not a breach.
- Return a native Python `bool`, not a NumPy bool.
Example:
```python
df = pd.DataFrame({
"variant": ["control", "control", "treatment", "treatment"],
"latency_ms": [90.0, 110.0, 100.0, 104.0],
})
# mean_control = 100.0, mean_treatment = 102.0
# relative = (102 - 100) / 100 = 0.02
guardrail_latency_regressed(df, threshold=0.01) # -> True (0.02 > 0.01)
```
```hint Check the return type
Scalars pulled out of a pandas Series are NumPy types, and so is the result of comparing them. Look at what type your comparison produces before you return it.
```
### Constraints & Assumptions
- One row per request.
- `variant` takes the values `'control'` and `'treatment'`; `latency_ms` is float64.
### What a Strong Answer Covers
- Grouping by variant and taking the mean latency for each arm
- Measuring the regression relative to the control mean, with the sign set so that higher treatment latency counts as worse
- The strict comparison at the threshold and the native `bool` return type
- What happens when a variant is missing, the control mean is zero, or latencies are missing
### Follow-up Questions
- A raw mean comparison ignores noise. How would you make this decision statistically sound?
- Why might the mean be the wrong statistic for a latency guardrail, and what would you compare instead?
- The data has one row per request, but experiments usually randomize users. How does that affect the uncertainty of the comparison?
Overview: Write a pandas check that compares mean request latency between control and treatment and reports whether the relative regression exceeds a guardrail threshold. Tests groupby aggregation, relative versus absolute differences, strict boundary handling and returning a native Python bool.
Implement the function below. df has one row per request with columns variant (object, 'control' or 'treatment') and latency_ms (float64). Return whether treatment's relative latency regression versus control breaches threshold. Higher latency is worse.