Error in Data frame definition while Multiple TS Stat Forecasting in Python

Question:

I was trying to replicate this code for stat forecasting in python,
I came across an odd error "name ‘forecasts’ is not defined" which is quite strange as I was able to replicate the code without any errors before.

The difference here from this to the reference code (which is given in the link below and the code which I was successfully able to implement), I am not using a training set and extracting the last 6 months for evaluation and instead I am using the entire training data for creating a stat forecast.

Eg: If my time series data was till Sept-22, I wanted to give the entire data till Sept-22 as the training set for my stat model and previous the training data had time series till March-22 and the rest 6 months was test. But now there are Errors which I am not able to understand why as the logic is same?

Attached is the simplified data frame used for the calculation:

{'Key': {0: 65162552161356, 1: 65162552635756, 2: 65162552843456, 3: 65162552842856, 4: 65162552736856}, '2021-04-01': {0: 31, 1: 0, 2: 281, 3: 207, 4: 55}, '2021-05-01': {0: 25, 1: 0, 2: 72, 3: 104, 4: 6}, '2021-06-01': {0: 16, 1: 0, 2: 108, 3: 32, 4: 14}, '2021-07-01': {0: 8, 1: 0, 2: 107, 3: 78, 4: 10}, '2021-08-01': {0: 21, 1: 0, 2: 80, 3: 40, 4: 9}, '2021-09-01': {0: 24, 1: 0, 2: 40, 3: 73, 4: 3}, '2021-10-01': {0: 13, 1: 0, 2: 36, 3: 79, 4: 11}, '2021-11-01': {0: 59, 1: 0, 2: 65, 3: 139, 4: 14}, '2021-12-01': {0: 51, 1: 0, 2: 41, 3: 87, 4: 10}, '2022-01-01': {0: 2, 1: 0, 2: 43, 3: 47, 4: 6}, '2022-02-01': {0: 0, 1: 0, 2: 0, 3: 63, 4: 3}, '2022-03-01': {0: 0, 1: 0, 2: 16, 3: 76, 4: 18}, '2022-04-01': {0: 0, 1: 0, 2: 37, 3: 32, 4: 8}, '2022-05-01': {0: 0, 1: 0, 2: 106, 3: 96, 4: 40}, '2022-06-01': {0: 0, 1: 0, 2: 101, 3: 75, 4: 16}, '2022-07-01': {0: 0, 1: 0, 2: 60, 3: 46, 4: 14}, '2022-08-01': {0: 0, 1: 0, 2: 73, 3: 91, 4: 13}, '2022-09-01': {0: 0, 1: 0, 2: 19, 3: 17, 4: 2}}

Here is the link for reference : https://towardsdatascience.com/time-series-forecasting-with-statistical-models-f08dcd1d24d1

import random
from itertools import product
from IPython.display import display, Markdown
from multiprocessing import cpu_count
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from statsforecast import StatsForecast
from nixtlats.data.datasets.m4 import M4, M4Info
from statsforecast.models import (
    adida, 
    croston_classic, 
    croston_sba, 
    croston_optimized,
    historic_average,
    imapa,
    naive,
    random_walk_with_drift, 
    seasonal_exponential_smoothing,
    seasonal_naive, 
    seasonal_window_average,
    ses, 
    tsb,
    window_average
)
df = pd.read_excel ('C:/X/X/X/2.1 Demand_Data_Used.xlsx')
df['Key'] = df['Key'].astype(str)
df = pd.melt(df,id_vars='Key',value_vars=list(df.columns[1:]),var_name ='ds')
df.columns = df.columns.str.replace('Key', 'unique_id')
df.columns = df.columns.str.replace('value', 'y')
df["ds"] = pd.to_datetime(df["ds"],format='%Y-%m-%d')
df=df[["ds","unique_id","y"]]

df['unique_id'] = df['unique_id'].astype('object')
df = df.set_index('unique_id')
df.reset_index()

seasonality = 30 #Monthly data

models = [
    adida,
    croston_classic,
    croston_sba,
    croston_optimized,
    historic_average,
    imapa,
    naive,
    random_walk_with_drift,
    (seasonal_exponential_smoothing, seasonality, 0.2),
    (seasonal_naive, seasonality),
    (seasonal_window_average, seasonality, 2 * seasonality),
    (ses, 0.1),
    (tsb, 0.3, 0.2),
    (window_average, 2 * seasonality)
    ]

fcst = StatsForecast(df=df, models=models, freq='MS', n_jobs=cpu_count())
%time forecasts = fcst.forecast(6)
forecasts.reset_index()

forecasts = forecasts.reset_index().merge(df_test, how='left', on=['unique_id', 'ds'])
models = forecasts.drop(columns=['unique_id', 'ds', 'y']).columns.to_list()

Attached is the error image:

enter image description here

Can anyone let me know what is it that I am doing wrong? It would be very much appreciated.

Asked By: user20068761

||

Answers:

The problem arises because of the Croston family. I have opened an issue to solve the problem. In the meantime, skipping those models works.

models = [
    adida,
    #croston_classic,
    #croston_sba,
    #croston_optimized,
    historic_average,
    imapa,
    naive,
    random_walk_with_drift,
    (seasonal_exponential_smoothing, seasonality, 0.2),
    (seasonal_naive, seasonality),
    (seasonal_window_average, seasonality, 2 * seasonality),
    (ses, 0.1),
    (tsb, 0.3, 0.2),
    (window_average, 2 * seasonality)
    ]
fcst = StatsForecast(df=df, models=models, freq='MS', n_jobs=cpu_count())
fcst.forecast(6)

Update:

The newest version of StatsForecast fixes the issue. You can use it using,

from statsforecast.models import CrostonClassic, CrostonSBA, CrostonOptimized

models = [
    CrostonClassic(),
    CrostonSBA(),
    CrostonOptimized()
]

fcst = StatsForecast(df=df, models=models, freq='MS', n_jobs=cpu_count())
fcst.forecast(6)
Answered By: fede garza