Delete future_forecast.py
Browse files- future_forecast.py +0 -100
future_forecast.py
DELETED
|
@@ -1,100 +0,0 @@
|
|
| 1 |
-
# prompt: Analyze the Hugging Face app's wine production forecast code, identify why some predictions have lower bounds of 0,
|
| 2 |
-
# and provide a complete fixed version of future_forecast.py that implements region-specific intervals with realistic lower bounds
|
| 3 |
-
# (30% of historical minimum production) while maintaining the original temporal validation methodology, rolling-origin validation,
|
| 4 |
-
# and comparison with persistence baselines.
|
| 5 |
-
|
| 6 |
-
"""Experimental one-year-ahead wine-production forecasts for 2026/27.
|
| 7 |
-
|
| 8 |
-
The forecasting features are built exclusively from observations preceding the
|
| 9 |
-
target campaign. Model selection uses rolling-origin validation over 2018-2022;
|
| 10 |
-
2023-2025 remains a final one-step-ahead test period.
|
| 11 |
-
"""
|
| 12 |
-
|
| 13 |
-
from __future__ import annotations
|
| 14 |
-
|
| 15 |
-
from dataclasses import dataclass, field
|
| 16 |
-
from pathlib import Path
|
| 17 |
-
from typing import Callable
|
| 18 |
-
|
| 19 |
-
import numpy as np
|
| 20 |
-
import pandas as pd
|
| 21 |
-
from sklearn.compose import ColumnTransformer
|
| 22 |
-
from sklearn.ensemble import RandomForestRegressor
|
| 23 |
-
from sklearn.impute import SimpleImputer
|
| 24 |
-
from sklearn.linear_model import LinearRegression, Ridge
|
| 25 |
-
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
|
| 26 |
-
from sklearn.pipeline import Pipeline
|
| 27 |
-
from sklearn.preprocessing import OneHotEncoder, StandardScaler
|
| 28 |
-
|
| 29 |
-
ROOT = Path(__file__).resolve().parent
|
| 30 |
-
DATA_DIR = ROOT / "data" / "processed"
|
| 31 |
-
TABLES_DIR = ROOT / "outputs" / "tables"
|
| 32 |
-
|
| 33 |
-
TARGET = "total_production_hl"
|
| 34 |
-
FEATURES = [
|
| 35 |
-
"region",
|
| 36 |
-
"year_start",
|
| 37 |
-
"vineyard_area_ha",
|
| 38 |
-
"production_lag_1",
|
| 39 |
-
"production_lag_2",
|
| 40 |
-
"production_mean_3",
|
| 41 |
-
"production_trend_1",
|
| 42 |
-
]
|
| 43 |
-
VALIDATION_YEARS = [2018, 2019, 2020, 2021, 2022]
|
| 44 |
-
TEST_YEARS = [2023, 2024, 2025]
|
| 45 |
-
FUTURE_YEAR = 2026
|
| 46 |
-
RANDOM_STATE = 42
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
@dataclass
|
| 50 |
-
class EvaluationResult:
|
| 51 |
-
metrics: pd.DataFrame
|
| 52 |
-
predictions: pd.DataFrame
|
| 53 |
-
selected_model: str
|
| 54 |
-
interval_half_width: float
|
| 55 |
-
# NEW: specific intervals per region
|
| 56 |
-
region_intervals: dict[str, float] = field(default_factory=dict)
|
| 57 |
-
# NEW: historical regional production minimums
|
| 58 |
-
region_min_production: dict[str, float] = field(default_factory=dict)
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
def load_history() -> pd.DataFrame:
|
| 62 |
-
production = pd.read_csv(DATA_DIR / "wine_production_by_region_clean.csv")
|
| 63 |
-
area = pd.read_csv(DATA_DIR / "vineyard_area_by_region_clean.csv")
|
| 64 |
-
history = production.merge(area, on=["region", "year_start"], how="left")
|
| 65 |
-
return history.sort_values(["region", "year_start"]).reset_index(drop=True)
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
def build_supervised_data(history: pd.DataFrame) -> pd.DataFrame:
|
| 69 |
-
data = history.sort_values(["region", "year_start"]).copy()
|
| 70 |
-
grouped_target = data.groupby("region", observed=True)[TARGET]
|
| 71 |
-
|
| 72 |
-
data["production_lag_1"] = grouped_target.shift(1)
|
| 73 |
-
data["production_lag_2"] = grouped_target.shift(2)
|
| 74 |
-
data["production_mean_3"] = grouped_target.transform(
|
| 75 |
-
lambda values: values.shift(1).rolling(3, min_periods=2).mean()
|
| 76 |
-
)
|
| 77 |
-
data["production_trend_1"] = data["production_lag_1"] - data["production_lag_2"]
|
| 78 |
-
|
| 79 |
-
return data.dropna(subset=["production_lag_1", "production_lag_2"]).reset_index(drop=True)
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
def make_preprocessor() -> ColumnTransformer:
|
| 83 |
-
numeric_features = [feature for feature in FEATURES if feature != "region"]
|
| 84 |
-
|
| 85 |
-
numeric = Pipeline(
|
| 86 |
-
[
|
| 87 |
-
("imputer", SimpleImputer(strategy="median", add_indicator=True)),
|
| 88 |
-
("scaler", StandardScaler()),
|
| 89 |
-
]
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
return ColumnTransformer(
|
| 93 |
-
[
|
| 94 |
-
("numeric", numeric, numeric_features),
|
| 95 |
-
("region", OneHotEncoder(handle_unknown="ignore"), ["region"]),
|
| 96 |
-
]
|
| 97 |
-
)
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
def make_pipeline(regressor: object) -> Pipeline:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|