Detect a novelty effect from daily A/B test lift in pandas
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: medium
Interview Round: Other
Implement `def detect_novelty(daily: pd.DataFrame, early_days: int, late_days: int) -> dict` in `solution.py`. `daily` has columns `'day'` (int, 1-based, contiguous) and `'lift'` (float, the treatment-minus-control relative lift for that day). Compare the early and late mean lift and flag a suspected novelty decay.
Requirements:
- `early_lift`: unweighted mean of `'lift'` where `day <= early_days`.
- `late_lift`: unweighted mean of `'lift'` where `day > max(day) - late_days`.
- The two windows are selected independently. Days between them are ignored, and if `early_days + late_days` is larger than the number of days, the windows overlap and a day counts in both.
- `novelty_suspected = (early_lift > 0) and (late_lift < 0.5 * early_lift)`, written without division.
- Return `{'early_lift': float, 'late_lift': float, 'novelty_suspected': bool}`.
Example, with lifts declining from 0.20 to -0.01 over 14 days:
```python
daily = pd.DataFrame({
'day': list(range(1, 15)),
'lift': [0.20, 0.18, 0.15, 0.12, 0.10, 0.08, 0.07,
0.05, 0.04, 0.03, 0.02, 0.01, 0.00, -0.01],
})
detect_novelty(daily, early_days=3, late_days=3)
# -> {'early_lift': 0.17666666666666667,
# 'late_lift': 0.0,
# 'novelty_suspected': True}
```
The early window is days 1 to 3 (mean 0.53 / 3), the late window is days 12 to 14 (mean 0.0), and 0.0 is below half of the early lift.
```hint Window by day value
Derive the late window's cutoff from the largest `day` value. Code that relies on row positions breaks when the rows arrive out of order.
```
```hint Return types
Pandas and NumPy return their own scalar types. Check what `mean()` and a comparison give you before you put them in the result.
```
### Constraints & Assumptions
- `daily` has one row per day, and `day` runs from 1 to `max(day)` with no gaps. Rows are not guaranteed to be sorted.
- `early_days` and `late_days` are non-negative integers.
- Lifts can be negative, zero or positive.
### What a Strong Answer Covers
- Selecting both windows by day value, with the late cutoff at `max(day) - late_days`.
- Unweighted means returned as plain floats, and the flag returned as a plain bool.
- Writing the decay rule as a multiplication guarded by `early_lift > 0`, with a reason for both choices.
- Behavior when a window is empty or the windows overlap.
- The limits of a fixed threshold as evidence of a novelty effect.
### Follow-up Questions
- How would you distinguish a novelty effect from a genuine decline in treatment value?
- Would you add a significance test instead of relying on a raw threshold?
- When can an unweighted mean of daily lifts mislead you, and what would you change?
Overview: Implement a pandas function that compares the mean daily lift in an early and a late window of an A/B test and flags a suspected novelty effect when a positive early lift drops below half its value. It tests window selection by day, safe threshold comparisons, and how to tell novelty decay from a real decline.