Here’s a quick rundown of what was going on and a more robust version of your “find_seasonal_lag” helper.
-
Index‐offset mix-up
- You sliced
acf_values[2:…] but then did +1, which means you were really picking up lags 3,4,… instead of 2,3,…
- If you want to skip only lag-0 (and optionally lag-1), it’s clearer to slice at
[1:] (or [2:]), and then add the same number back.
-
“Significance” by hard threshold is arbitrary
- It’s often better to use the built-in confidence intervals from
statsmodels.tsa.stattools.acf(…, alpha=…)
- Or at least expose the threshold as a parameter.
-
No minimum lag parameter
- Sometimes you want to ignore lag-1 altogether (short-term autocorrelation).
-
Missing imports / docstring
Revised Implementation
import numpy as np
import pandas as pd
from statsmodels.tsa.stattools import acf
def find_seasonal_lag(
series: pd.Series,
max_lag: int = 53,
threshold: float = 0.3,
min_lag: int = 1,
use_confint: bool = False,
alpha: float = 0.05
) -> int | None:
"""
Scan the ACF up to max_lag and return the first lag >= min_lag
whose autocorrelation is 'significantly' high.
Parameters
----------
series : pd.Series
The time series (will dropna() internally).
max_lag : int
Maximum lag to consider for seasonality.
threshold : float
Absolute ACF threshold if not using confint.
min_lag : int
Minimum lag to consider (e.g. skip lag-1 by setting min_lag=2).
use_confint : bool
If True, use ACF confidence intervals (alpha) instead of threshold.
alpha : float
Significance level for ACF confidence intervals.
Returns
-------
int or None
The first “seasonal” lag, or None if none found.
"""
# 1) Prepare data
x = series.dropna().values
if x.size < max_lag + 1:
raise ValueError("Series too short for requested max_lag")
# 2) Compute ACF (and optional confints)
if use_confint:
acf_vals, confint = acf(x, nlags=max_lag, fft=True, alpha=alpha)
else:
acf_vals = acf(x, nlags=max_lag, fft=True)
# 3) Scan lags
for lag in range(min_lag, max_lag + 1):
val = acf_vals[lag]
if use_confint:
lo, hi = confint[lag]
# if outside the band, it’s significant
if (val < lo) or (val > hi):
return lag
else:
if abs(val) >= threshold:
return lag
# nothing found
return None
How it fixes your original version
| Issue | Original | Fix in Revised Version |
|---|
| Indexing offset | acf_values[2: max_lag+1] + +1 → actually picks lags 3,4,… | Loop for lag in range(min_lag, max_lag+1) and directly index acf_vals[lag]. |
| Hard threshold only | No way to know if an ACF spike is statistically significant | Optionally use use_confint=True to leverage statsmodels’ confidence intervals. |
| No min-lag control | Implicitly skipped lags 0 and 1 in a confusing way | Explicit min_lag parameter (e.g. set to 2 to skip lag-1). |
| Missing imports | References to acf and np but no imports | import numpy as np; from statsmodels.tsa.stattools import acf. |
Example usage
# 1) Simple threshold method, skipping only lag-0
season = find_seasonal_lag(my_ts, max_lag=52, threshold=0.4, min_lag=1)
print("Seasonal lag by threshold:", season)
# 2) Statistically significant autocorrelation only
season = find_seasonal_lag(
my_ts,
max_lag=52,
use_confint=True,
alpha=0.05,
min_lag=2 # skip the short‐term correlation at lag 1
)
print("Seasonal lag by confidence intervals:", season)
This should give you a much more transparent, correct, and configurable “seasonal lag” finder! 🚀