Add improvements

This commit is contained in:
Urtzi Alfaro
2026-01-12 14:24:14 +01:00
parent 6037faaf8c
commit 230bbe6a19
61 changed files with 1668 additions and 894 deletions

View File

@@ -142,10 +142,25 @@ class ModelSelector:
# Zero ratio
zero_ratio = (y == 0).sum() / len(y)
# Seasonality strength (simple proxy using rolling std)
# Seasonality strength using autocorrelation at key lags (7 days, 30 days)
# This better captures periodic patterns without using future data
if len(df) >= 14:
rolling_mean = pd.Series(y).rolling(window=7, center=True).mean()
seasonality_strength = rolling_mean.std() / (np.std(y) + 1e-6) if np.std(y) > 0 else 0
# Calculate autocorrelation at weekly lag (7 days)
# Higher autocorrelation indicates stronger weekly patterns
try:
weekly_autocorr = pd.Series(y).autocorr(lag=7) if len(y) > 7 else 0
# Calculate autocorrelation at monthly lag if enough data
monthly_autocorr = pd.Series(y).autocorr(lag=30) if len(y) > 30 else 0
# Combine autocorrelations (weekly weighted more for bakery data)
seasonality_strength = abs(weekly_autocorr) * 0.7 + abs(monthly_autocorr) * 0.3
# Ensure in valid range [0, 1]
seasonality_strength = max(0.0, min(1.0, seasonality_strength))
except Exception:
# Fallback to simpler calculation if autocorrelation fails
seasonality_strength = 0.5
else:
seasonality_strength = 0.5 # Default