我正在尝试使用 sktime SquareingResiduals 来预测样本外值。 这是适用于样本内预测的代码。
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)
现在我尝试预测样本外。
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`.
所以我将:
sqr = sqr.fit(df, fh=in_sample_date)
更改为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`.
然后我将:
sqr = sqr.fit(df, fh=in_sample_date)
更改为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.
然后我检查了其他模型的预测函数,并且 Predict() 函数对于非混合模型的样本内和样本外预测都运行良好:
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'])
为什么样本外/样本内预测函数对于 SquareingResiduals 不能一起工作?我们如何预测样本外/样本内值?
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
非常感谢您的关注。
您是否尝试过像这样修改 SquareingResiduals 调用中的初始窗口大小参数:
your_out_of_date_df_index = np.arange(12)
sqr = SquaringResiduals(forecaster=model,
residual_forecaster=var_fc,
initial_window=int(len(df))+len(your_out_of_date_df_index))
并在拟合函数中使用您的样本日期,就像您之前尝试过的那样:
sqr = sqr.fit(df, fh=out_sample_date)
y_pred2 = sqr.predict(out_sample_date) #out sample prediction
本来可以将此作为评论发布,但声誉太低。