Statsmodels AutoRegression 回测代码有效性

问题描述

我正在使用 stats 模型库学习 Python 中的自回归模型。

我正在做的是从这里获取一个显示金融股票回报的数据集:

Yahoo Finance Data

当我运行以下命令时,数据如下所示:

data['y'] = data['close'].shift(-1).astype(float)
data.dropna(inplace=True)
data.tail()

enter image description here

基本上,y 列是下一个时间步的收盘价。我的数据集中总共有 500 个时间步。

现在我想拟合一个简单的 AR 模型,使用当前收盘价预测下一期收盘价。为了测试模型的拟合度,我进行了回测。

from statsmodels.tsa.arima_model import AutoReg

def backtest(num_periods,data):
    predictions = []
    true_values = []
    x = data[['open','high','low','close']]
    y = data['y']
    for i in reversed(range(1,num_periods)):
        # split the data into training and test splits
        # the y_test variable should be a single value for the next period out of the sample
        x_train = x.iloc[:len(x)-i]
        y_train = y.iloc[:len(y)-i]
        x_test = x.iloc[len(x)-i]
        y_test = y.iloc[len(y)-i]
        # fit the model on the endogenous variables
        model = AutoReg(endog=x_train.close.astype(float),lags = 13).fit()
        # forecast for the period out of the 
        pred = model.predict(start=len(x_train),end=len(x_train)+1)
        # create the prediction and true value arrays
        predictions.append(pred)
        true_values.append(y_test)
    return true_values,predictions

true,pred = backtest(10,data)

但这给了我两个预测系列:

plt.plot(true,label='true',);
plt.plot(pred,label = 'pred',);
plt.legend();

enter image description here

这里发生了什么?我对 AR 模型进行回测的方法是否正确?我主要担心的是我似乎正在 y 上训练模型,但 y 来自下一个时期。因此,当我进行样本外预测时,它会从测试集中获取一个值。

如果能提供代码示例的任何指导,我们将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)