Flag level shifts in a daily series using a trailing rolling mean and standard deviation
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Implement `level_shift_flags(s, window, k)`, where `s` is a `pd.Series` of floats on a daily `DatetimeIndex`, `window` is an int, and `k` is a threshold in standard deviations. Return a boolean `pd.Series` that flags level shifts, meaning points that sit far from their recent history.
The function must follow these rules:
- For each point, compute the mean and the sample standard deviation (ddof=1) of the `window` points that precede it. The current point is excluded from its own window (shift the rolling statistics by 1).
- Flag the point when `abs(value - rolling_mean) > k * rolling_std`.
- Points without a full window of history must be `False`, never `NaN`.
- Points where the trailing std is `0` or `NaN` must be `False`.
- The result has dtype `bool` throughout, the same index and length as `s`, and `name=None`.
Example:
```python
s = pd.Series([10.0, 10.0, 10.0, 12.0, 50.0, 11.0],
index=pd.date_range('2024-01-01', periods=6))
level_shift_flags(s, window=3, k=2.0)
# 2024-01-01 False # no full window
# 2024-01-02 False # no full window
# 2024-01-03 False # no full window
# 2024-01-04 False # trailing window 10, 10, 10 has std 0
# 2024-01-05 True # trailing mean 10.67, std 1.15, so 50 is far outside
# 2024-01-06 False # trailing mean 24, std 22.54, and |11 - 24| = 13 < 45.08
```
On 2024-01-04 the value 12 differs from the trailing mean of 10, but the trailing std is 0, so the rule forces `False`. On 2024-01-06 the spike of 50 is inside the trailing window and inflates the std, so 11 is not flagged.
```hint Which points the window covers
By default, the rolling window at a position ends at that position. Check whether the value you are testing ends up inside the statistics it is compared against.
```
```hint Masks you actually need
Work out what a pandas comparison against NaN returns, and what the threshold becomes when the std is exactly 0, before deciding which masks to add.
```
### Constraints & Assumptions
- `window` is an integer of at least 2, since the sample std of a single point is undefined.
- `k` is non-negative.
- The window counts observations, not calendar days.
### What a Strong Answer Covers
- Building the statistics from the `window` points strictly before each point, so the tested value never sits in its own window.
- Using the sample std (ddof=1) and the strict `>` comparison as specified.
- Forcing `False` for short history and for a zero or NaN std, with a reason why zero std needs its own mask.
- Returning a bool Series on the original index with the same length and `name=None`, in linear time.
### Follow-up Questions
- How would you make the screen resistant to outliers that drive the rolling mean and std?
- How would you flag a sustained level shift, as opposed to a single spike?
- What changes if the daily index has missing dates?
- If the series were stationary Gaussian noise, roughly what fraction of points would this flag with `k = 2`, and how does the window size change that?
Overview: Implement a pandas screen that flags points in a daily series whose deviation from the trailing rolling mean exceeds k rolling standard deviations. It tests look-ahead leakage in rolling windows, the sample standard deviation, NaN and zero-variance handling, and returning a clean boolean Series.