fix: use exact 168-row lag for the seasonal-naive baseline

The baseline now reproduces mission/data.md exactly: MAE 5.40, WAPE 44.7%,
R2 0.462. The model's lag_week remains the previous observed same weekday/hour,
which handles sensor gaps and is documented in the model card.
This commit is contained in:
Noel Sabia committed 2026-11-14 05:04:41 +01:00
1 parent 939a8db482
commit dc83825de0
9 files changed
+41 -26

No files matched your search

+10 -2
View File
@@ -70,8 +70,16 @@ def _add_rollups(grid: pd.DataFrame) -> pd.DataFrame:
def _add_seasonal_lags(df: pd.DataFrame) -> pd.DataFrame:
"""Previous observed same weekday/hour values, strictly in the past."""
out = df.sort_values(["sensor", "weekday", "hour", "t"]).copy()
"""Previous observed same weekday/hour values, strictly in the past.
``lag_168`` is the exact 168-row (one observed week) lag used as the
seasonal-naive baseline. The model's ``lag_week`` deliberately uses the
previous *observed* same weekday/hour instead, so sensor gaps do not make
the feature unusable.
"""
chrono = df.sort_values(["sensor", "t"]).copy()
chrono["lag_168"] = chrono.groupby("sensor", observed=True)["y"].shift(168)
out = chrono.sort_values(["sensor", "weekday", "hour", "t"]).copy()
grouped = out.groupby(["sensor", "weekday", "hour"], observed=True)["y"]
out["lag_week"] = grouped.shift(1)
prior = pd.concat(
+3 -3
View File
@@ -29,7 +29,7 @@ from .weather import load_cached_weather
log = logging.getLogger(__name__)
MODEL_SPECS = [
("seasonal_naive", None, "same local hour last week"),
("seasonal_naive", None, "exact 168-row lag (same observed hour last week)"),
(
"day_no_weather",
config.FEATURE_DAY_NO_HOLIDAY,
@@ -107,7 +107,7 @@ def train_bundle(
test_start = data_end.floor("h") - pd.Timedelta(days=config.TEST_DAYS)
train = features[features["t"] < test_start].copy()
test = features[features["t"] >= test_start].copy()
eval_frame = test[test["lag_week"].notna() & test["y"].notna()].copy()
eval_frame = test[test["lag_168"].notna() & test["y"].notna()].copy()
if eval_frame.empty:
raise RuntimeError("holdout has no rows with a seasonal-naive baseline")
if len(train) < 100:
@@ -128,7 +128,7 @@ def train_bundle(
preds: dict[str, np.ndarray] = {}
for name, cols, description in MODEL_SPECS:
if name == "seasonal_naive":
pred = eval_frame["lag_week"].to_numpy(dtype=float)
pred = eval_frame["lag_168"].to_numpy(dtype=float)
else:
assert cols is not None
model = fit(train, cols)