Rank lags by autocorrelation to detect seasonality in a daily series
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Write `autocorr_lag_scan(s, lags, threshold)` taking a `pd.Series` of floats on a daily `DatetimeIndex`, a list of int `lags`, and a float `threshold`. Return a table ranking each lag by its lag-k autocorrelation, so the strongest candidate seasonal period is on top.
Requirements:
- Columns, in this order: `lag` (int64), `autocorr` (float64, rounded to 4 decimal places), `significant` (bool).
- `autocorr` for lag k is the Pearson correlation of the series with itself shifted by k, computed over the overlapping points.
- `significant` is True where `abs(autocorr) >= threshold`, using the rounded value shown in the table.
- Sort by `autocorr` descending with `lag` ascending as the tie-breaker, then `reset_index(drop=True)`.
- An undefined (NaN) autocorrelation, from too few overlapping points or a constant series or overlap, sorts after all non-NaN rows and has `significant` False. NaN rows are ordered among themselves by `lag` ascending.
Example:
```python
idx = pd.date_range("2024-01-01", periods=21, freq="D")
s = pd.Series([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0] * 3, index=idx) # a spike every Sunday
autocorr_lag_scan(s, lags=[1, 7, 14, 30], threshold=0.5)
# lag autocorr significant
# 0 7 1.00 True
# 1 14 1.00 True
# 2 1 -0.14 False
# 3 30 NaN False
```
At lags 7 and 14 the overlapping segments are identical, so both correlations are exactly 1 and the tie goes to the smaller lag. Lag 1 pairs each spike with a zero, giving a small negative value. Lag 30 is longer than the series, so there is no overlap and the value is undefined.
```hint Undefined lags
Work out which lags can produce no correlation at all, and check what your sort does with those NaN values when the main key is descending.
```
```hint Round once
Decide where the rounding happens. The ranking, the tie-break and the `significant` flag should all agree with the numbers printed in the table.
```
### Constraints & Assumptions
- The input index is already daily, complete and sorted, so no resampling is needed and a shift of k positions is a shift of k days.
- `lags` is a list of integers and `threshold` is a float.
### What a Strong Answer Covers
- Computing the lag-k Pearson correlation over the overlapping points for each requested lag.
- Producing the exact columns and dtypes, with rounding applied consistently to the ranking and the `significant` flag.
- Ordering rows deterministically, including ties and undefined correlations.
- The time and space cost in terms of the number of lags and the series length.
- Edge cases such as lags close to or beyond the series length, constant stretches, missing days and NaN values in the series.
### Follow-up Questions
- How would you flag statistically significant autocorrelation in place of a fixed `threshold`?
- Autocorrelation at lag 14 is often inflated by the lag 7 signal. How would you isolate the true period?
- Why might your numbers differ from the ACF values a time series library reports for the same series?
Overview: Write a pandas function that scores candidate lags of a daily time series by autocorrelation, flags the strong ones and ranks them in a deterministic order. It tests Series.autocorr, handling undefined correlations, and sorting with ties and NaN values when screening for seasonality.