my ensemble model from statsforecast import StatsForecast from statsforecast.models…
- User
my ensemble model from statsforecast import StatsForecast from statsforecast.models import AutoARIMA, AutoETS, AutoCES, DynamicOptimizedTheta from statsforecast.utils import ConformalIntervals import numpy as np import polars as pl
Polars option to display all rows
pl.Config.set_tbl_rows(None)
Initialize the models
models = [ AutoARIMA(season_length=52), AutoETS(season_length=52), AutoCES(season_length=52), DynamicOptimizedTheta(season_length=52) ]
Initialize the StatsForecast model
sf = StatsForecast(models=models, freq='1w', n_jobs=-1)
Perform cross-validation with a step size of 1 to mimic an expanding window
crossvalidation_df = sf.cross_validation(df=y_cl4, h=16, step_size=1, n_windows=18, sort_df=True)
Calculate the ensemble mean
ensemble = crossvalidation_df[['AutoARIMA', 'AutoETS', 'CES', 'DynamicOptimizedTheta']].mean(axis=1)
Create a Series for the ensemble mean
ensemble_series = pl.Series('Ensemble', ensemble)
Add the ensemble mean as a new column to the DataFrame
crossvalidation_df = crossvalidation_df.with_columns(ensemble_series)
def wmape(y_true, y_pred): return np.abs(y_true - y_pred).sum() / np.abs(y_true).sum()
Calculate the WMAPE for the ensemble model
wmape_value = wmape(crossvalidation_df['y'], crossvalidation_df['Ensemble']) print('Average WMAPE for Ensemble: ', round(wmape_value, 4))
Calculate the errors for the ensemble model
errors = crossvalidation_df['y'] - crossvalidation_df['Ensemble']
For an individual forecast
individual_accuracy = 1 - (abs(crossvalidation_df['y'] - crossvalidation_df['Ensemble']) / crossvalidation_df['y']) individual_bias = (crossvalidation_df['Ensemble'] / crossvalidation_df['y']) - 1
Add these calculations as new columns to DataFrame
crossvalidation_df = crossvalidation_df.with_columns([ individual_accuracy.alias("individual_accuracy"), individual_bias.alias("individual_bias") ])
Print the individual accuracy and bias for each week
for row in crossvalidation_df.to_dicts(): id = row['unique_id'] date = row['ds'] accuracy = row['individual_accuracy'] bias = row['individual_bias'] print(f"{id}, {date}, Individual Accuracy: {accuracy:.4f}, Individual Bias: {bias:.4f}")
For groups of forecasts
group_accuracy = 1 - (errors.abs().sum() / crossvalidation_df['y'].sum()) group_bias = (crossvalidation_df['Ensemble'].sum() / crossvalidation_df['y'].sum()) - 1
Print the average group accuracy and group bias over all folds for the ensemble model
print('Average Group Accuracy: ', round(group_accuracy, 4)) print('Average Group Bias: ', round(group_bias, 4))
- Assistant
Your filtering command is almost correct, but you only selected the 'y' column as the result. If you want to keep all three columns (
unique_id,ds, andy), you should not subset the DataFrame only by the 'y' column. Instead, you should filter by theunique_idand keep the entire DataFrame as shown below:y_cl4_1series = y_cl4.filter(pl.col('unique_id') == '6067269')By doing this, you'll get a DataFrame that contains only the rows pertaining to the
unique_id'6067269', and it will include all the columns present in your original DataFramey_cl4.To check the top of the DataFrame with all columns included, you can use the
head()method like this:print(y_cl4_1series.head())This should return a DataFrame with the first few rows, including
unique_id,ds, andycolumns, filtered for theunique_id'6067269'.