I am trying to forecast out-of-sample value using sktime SquaringResiduals. Here is the code which working well for in-sample prediction.
from sktime.forecasting.arch import StatsForecastGARCH
from sktime.forecasting.squaring_residuals import SquaringResiduals
def hybridModel(p,q,model):
out_sample_date = FH(np.arange(12), is_relative=True)
in_sample_date = FH(df.index, is_relative=False)
var_fc = StatsForecastGARCH(p=p,q=q)
sqr = SquaringResiduals(forecaster=model, residual_forecaster=var_fc,initial_window=int(len(df)))
sqr = sqr.fit(df, fh=in_sample_date)
# y_pred2 = sqr.predict(out_sample_date) #out sample prediction
y_pred = sqr.predict(in_sample_date) #in sample prediction
fig,ax=plot_series(df, y_pred, labels=["passenger", "y_pred"])
return sqr,fig,y_pred,error_matrix(df,y_pred)
sqr,fig1,y_pred1,matrix1= hybridModel(1,1,forecaster)
Now I try to forecast out-sample.
y_pred2 = sqr.predict(out_sample_date) #out sample prediction
> ValueError: A different forecasting horizon `fh` has been provided
> from the one seen already in `fit`, in this instance of
> SquaringResiduals. If you want to change the forecasting horizon,
> please re-fit the forecaster. This is because the fitting of the
> forecaster SquaringResiduals depends on `fh`.
So I change:sqr = sqr.fit(df, fh=in_sample_date) to sqr = sqr.fit(df)
> ValueError: The forecasting horizon `fh` must be passed to `fit` of
> SquaringResiduals, but none was found. This is because fitting of the
> forecaster SquaringResiduals depends on `fh`.
Then I change: sqr = sqr.fit(df, fh=in_sample_date) to sqr = sqr.fit(df, fh=out_sample_date)
> ValueError: The `window_length` and the forecasting horizon are
> incompatible with the length of `y`. Found `window_length`=84,
> `max(fh)`=11, but len(y)=84. It is required that the window length
> plus maximum forecast horizon is smaller than the length of the time
> series `y` itself.
Then I checked predict function for other model, and predict() function working well for both in-sample and out-sample prediction for non-hybrid model:
from sktime.forecasting.tbats import TBATS
from sktime.forecasting.base import ForecastingHorizon as FH
import warnings
import numpy as np
import pandas as pd
import mlflow
from sktime.utils import mlflow_sktime as mf
from sktime.utils.plotting import plot_series
warnings.filterwarnings("ignore")
out_sample_date = FH(np.arange(12), is_relative=True)
in_sample_date = FH(df.index, is_relative=False)
forecaster = TBATS(
use_box_cox=True,
use_trend=True,
use_damped_trend=True,
sp=12,
use_arma_errors=True,
n_jobs=1)
forecaster.fit(df)
y_pred = forecaster.predict(in_sample_date)
y_pred2 = forecaster.predict(out_sample_date)
fig,ax = plot_series(df,y_pred,y_pred2,labels=['passenger','prediction','out_sample_pred'])
Why out-sample / in-sample prediction function does not work together for SquaringResiduals and how can we predict out-sample / in-sample value?
sqr = SquaringResiduals(forecaster=model, residual_forecaster=var_fc,initial_window=int(len(df)))
sqr = sqr.fit(df, fh=in_sample_date)
y_pred2 = sqr.predict(out_sample_date) #out sample prediction
Thank you so much for your attention.

The documentation explains that the forecaster is trained on
y(t_1),...y(t_i)wherei = initial_window, ... N-steps_ahead, and that this is used to calculate the residualr(t_i+steps_ahead) := y(t_i+steps_ahead) - ŷ(t_i+steps_ahead)for each value of i.The
initial_windowmust be less than or equal toN-steps_aheadto make any forecasts for a positive number ofsteps_ahead. I believe the reason for this is if we considerinitial_window = N-swhere s is greater than or equal to 0, andsteps_ahead=a, then in the first iteration of the loop over i, we get:Notice that
y(t_(N-s+a))is not known unlessN-s+a <= N, or equivalentlya < sbecause we don't know the true value of future timestamps.This means when you use SquaringResiduals, the maximum possible initial window you can supply is
max_initial_window = len(df)-max(out_sample_date). Notice that we are using max(out_sample_date) and not len(out_sample_date) becausenp.arange(12)only asks for forecasts ofsteps_ahead = 0, ... 11or a maximum forecast horizon of 11.Below is a fully reproducible example: