Statsmodels:使用 ARIMA 实施直接和递归的多步预测策略

Statsmodels: Implementing a direct and recursive multi-step forecasting strategy with ARIMA

我目前正在尝试使用 statsmodels ARIMA 库实现直接和递归多步预测策略,它提出了一些问题。

递归多步预测策略将训练一步模型,预测下一个值,将预测值附加到我输入预测方法的外生值的末尾并重复。这是我的递归实现:

def arima_forecast_recursive(history, horizon=1, config=None):
    # make list so can add / remove elements
    history = history.tolist()
    model = ARIMA(history, order=config)
    model_fit = model.fit(trend='nc', disp=0)

    for i, x in enumerate(history):
        yhat = model_fit.forecast(steps=1, exog=history[i:])
        yhat.append(history)
    return np.array(yhat)

def walk_forward_validation(dataframe, config=None):
    n_train = 52  # Give a minimum of 2 forecasting periods to capture any seasonality
    n_test = 26  # Test set should be the size of one forecasting horizon
    n_records = len(dataframe)
    tuple_list = []

    for index, i in enumerate(range(n_train, n_records)):
        # create the train-test split
        train, test = dataframe[0:i], dataframe[i:i + n_test]

        # Test set is less than forecasting horizon so stop here.
        if len(test) < n_test:
            break

        yhat = arima_forecast_recursive(train, n_test, config)
        results = smape3(test, yhat)
        tuple_list.append(results)

    return tuple_list

与执行直接策略类似,我只需将我的模型拟合到可用的训练数据上,然后使用它来一次预测总的多步预测。我不确定如何使用 statsmodels 库实现此目的。

我的尝试(产生结果)如下:

def walk_forward_validation(dataframe, config=None):
    # This currently implements a direct forecasting strategy
    n_train = 52  # Give a minimum of 2 forecasting periods to capture any seasonality
    n_test = 26  # Test set should be the size of one forecasting horizon
    n_records = len(dataframe)
    tuple_list = []

    for index, i in enumerate(range(n_train, n_records)):
        # create the train-test split
        train, test = dataframe[0:i], dataframe[i:i + n_test]

        # Test set is less than forecasting horizon so stop here.
        if len(test) < n_test:
            break

        yhat = arima_forecast_direct(train, n_test, config)
        results = smape3(test, yhat)
        tuple_list.append(results)

    return tuple_list

def arima_forecast_direct(history, horizon=1, config=None):
    model = ARIMA(history, order=config)
    model_fit = model.fit(trend='nc', disp=0)
    return model_fit.forecast(steps=horizon)[0]

让我特别困惑的是,模型是否应该只对所有预测拟合一次或多次拟合以在多步预测中进行单个预测?从 Souhaib Ben Taieb's doctoral thesis (page 35 paragraph 3) 中可以看出,直接模型将估计 H 个模型,其中 H 是预测范围的长度,因此在我的示例中,预测范围为 26,应该估计 26 个模型而不是一个模型。如上所示,我当前的实现仅适用于一种模型。

我不明白的是,如果我对相同的训练数据多次调用 ARIMA.fit() 方法,我将得到一个模型,我将得到一个与预期不同的拟合正常随机变化?

我的最后一个问题是关于优化的。使用前向验证之类的方法在统计上可以得到非常显着的结果,但对于许多时间序列来说,它的计算成本非常高。上面的两个实现都已使用 joblib 并行循环执行功能调用,这显着减少了我笔记本电脑上的运行时间。但是我想知道是否可以对上述实现做任何事情来使它们更有效率。当 运行 这些方法用于约 2000 个单独的时间序列(所有系列总共约 500,000 个数据点)时,运行时间为 10 小时。我分析了代码,大部分执行时间都花在了 statsmodels 库中,这很好,但是 walk_forward_validation() 方法和 ARIMA.fit() 的运行时间之间存在差异。这是预期的,因为 walk_forward_validation() 方法显然不只是调用 fit 方法,但如果可以更改其中的任何内容以加快执行时间,请告诉我。

这段代码的想法是为每个时间序列找到一个最优的 arima 顺序,因为单独研究 2000 个时间序列是不可行的,因此 walk_forward_validation() 方法每次被调用 27 次系列。所以总体上大约是 27,000 次。因此,任何可以在此方法中找到的性能节省都会产生影响,无论它有多小。

一般情况下,ARIMA只能进行递归预测,不能进行直接预测。可能对用于直接预测的 ARIMA 变体进行了一些研究,但它们不会在 Statsmodels 中实施。在 statsmodels 中(或在 R auto.arima() 中),当您为 h > 1 设置一个值时,它只是执行递归预测以到达那里。

据我所知,none 的标准预测库已经实现了直接预测,您将不得不自己编写代码。

Taken from Souhaib Ben Taieb's doctoral thesis (page 35 paragraph 3) it is presented that direct model will estimate H models, where H is the length of the forecast horizon, so in my example with a forecast horizon of 26, 26 models should be estimated instead of just one.

我没读过Ben Taieb的论文,但是从his paper "Machine Learning Strategies for Time Series Forecasting"来看,对于直接预测,一个H值只有一个模型。所以对于H=26,只有一个模型。如果你需要对 1 到 H 之间的每个值进行预测,就会有 H 个模型,但是对于一个 H,只有一个模型。