From 0eb39deec5c3bca457199e0d01cb439c57050f4a Mon Sep 17 00:00:00 2001 From: frasu Date: Sun, 13 Apr 2025 14:45:55 +0000 Subject: [PATCH] src/delta_barth/analysis/forecast.py aktualisiert --- src/delta_barth/analysis/forecast.py | 33 ++++++++++++++-------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/delta_barth/analysis/forecast.py b/src/delta_barth/analysis/forecast.py index 0de4843..8827c8c 100644 --- a/src/delta_barth/analysis/forecast.py +++ b/src/delta_barth/analysis/forecast.py @@ -210,7 +210,18 @@ def _process_sales( df_cust["jahr"] = df_cust[DATE_FEAT].dt.year df_cust["monat"] = df_cust[DATE_FEAT].dt.month - monthly_sum = df_cust.groupby(["jahr", "monat"])[SALES_FEAT].sum().reset_index() + current_year = datetime.now().year + current_month = datetime.now().month + years = range(df_cust["jahr"].min(), current_year + 1) + + old_monthly_sum = df_cust.groupby(["jahr", "monat"])[SALES_FEAT].sum().reset_index() + + all_month_year_combinations = pd.DataFrame( + [(year, month) for year in years for month in range(1, 13) if (year < current_year or (year == current_year and month <= current_month))], columns=["jahr", "monat"] + ) + + monthly_sum = pd.merge(all_month_year_combinations, old_monthly_sum, on=["jahr", "monat"], how="left") + monthly_sum[SALES_FEAT] = monthly_sum[SALES_FEAT].fillna(0) monthly_sum[DATE_FEAT] = ( monthly_sum["monat"].astype(str) + "." + monthly_sum["jahr"].astype(str) ) @@ -220,7 +231,6 @@ def _process_sales( features = ["jahr", "monat"] target = SALES_FEAT - # ?? --- new: dates and forecast last_date = pd.to_datetime(datetime.datetime.now().strftime("%m.%Y"), format="%m.%Y") future_dates = pd.date_range( start=last_date + pd.DateOffset(months=1), periods=6, freq="MS" @@ -230,7 +240,7 @@ def _process_sales( # Randomized Search kfold = KFold(n_splits=5, shuffle=True) params: ParamSearchXGBRegressor = { - "n_estimators": scipy.stats.poisson(mu=1000), + "n_estimators": scipy.stats.poisson(mu=100), "learning_rate": [0.03, 0.04, 0.05], "max_depth": range(2, 9), "min_child_weight": range(1, 5), @@ -240,27 +250,19 @@ def _process_sales( "early_stopping_rounds": [20, 50], } - # ?? --- new: best_estimator (internal usage only) best_estimator = None best_params: BestParametersXGBRegressor | None = None best_score_mae: float | None = float("inf") best_score_r2: float | None = None best_start_year: int | None = None too_few_month_points: bool = True - # forecast: pd.DataFrame | None = None - # TODO: write routine to pad missing values in datetime row - # TODO problem: continuous timeline expected, but values can be empty for multiple months - # TODO: therefore, stepping with fixed value n does not result in timedelta of n episodes - # Option A: pad data frame with zero values --> could impede forecast algorithm - # Option B: calculate next index based on timedelta + stride = dopt_basics.datetime.timedelta_from_val(365, TimeUnitsTimedelta.DAYS) dates = cast(pd.DatetimeIndex, monthly_sum.index) min_date = dates.min() - # ?? --- new: use monthly basis for time windows # baseline: 3 years - 36 months starting_date = datetime.datetime.now() - relativedelta(months=36) - # starting_date = dates.max() - relativedelta(months=36) def get_index_date( dates: pd.DatetimeIndex, @@ -307,10 +309,9 @@ def _process_sales( X_train, X_test = train[features], test[features] y_train, y_test = train[target], test[target] - # ?? --- new: adapted condition to fit new for-loop # test set size fixed at 6 --> first iteration: baseline - 6 entries - # for each new year 10 new data points needed - if len(train) >= 30 + 10 * step: + # for each new year 10 new data points (i.e., sales strictly positive) needed + if len(train[train[SALES_FEAT] > 0]) >= 30 + 10 * step: too_few_month_points = False rand = RandomizedSearchCV( @@ -333,7 +334,7 @@ def _process_sales( best_params = cast(BestParametersXGBRegressor, rand.best_params_) best_score_mae = error best_score_r2 = cast(float, r2_score(y_test, y_pred)) - # --- new: use first_date for best_start_year + # --- new: use target_date for best_start_year best_start_year = target_date.year # --- new: store best_estimator best_estimator = copy.copy(rand.best_estimator_)