这是提前一步预测吗?我可以把它变成多步预测吗?
Is this one-step ahead prediction? Can I turn it into multistep prediction?
我“继承”了这段代码,对SARIMAX模型不熟悉。评论有助于理解发生了什么,但我不明白预测完成的最后一行。
该数据集共有 1171 行,分为 1000 行训练行和 171 行测试行。这转化为:
Model.predict(1000, 1170, exog=exo_test, typ='levels')
我查看了 predict() 的文档。第一个参数是 endog 参数,第二个应该是 exog 参数。但是当 exog=exog_test 时,1170 应该是什么意思?此外,文档中没有提到 'typ' 参数。
不明白的地方:
- 这是超前一步的预测吗?意味着它取真值,预测下一步然后丢弃预测,及时取下一个真值来预测下一步?
[1. a) 如果是一步预测,SARIMAX 模型是否需要拟合 again/retrained?难道不应该使用最后的真实值,因此在每一步预测之后都对真实值进行再训练吗?]
这是多步预测吗,意味着 t+2 中的预测是基于预测而不是 t+1 中的真实值?
由于我假设它是一步预测,是否可以“轻松”将其转换为多步预测?
完整代码:
#load dataset
df = pd.read_csv('data.csv', index_col = 'date', parse_dates = True)
#split the closing price into train and test data
train = df.iloc[:1000,4]
test = df.iloc[1000:,4]
#select exogenous variables
exo = df.iloc[:,6:61]
#split exogenuous variables into train and test data
exo_train = exo.iloc[:1000]
exo_test = exo.iloc[1000:]
#run auto_arima to find the best configuration (I selected m=7 and D=1 by running seasonal_decompose and acf and pacf plots)
auto_arima(df['close'], exogenous=exo, m=7, trace=True, D=1).summary()
#set the best configuration from auto_arima for the SARIMAX model
Model = SARIMAX(train, exog = exo_train, order=(1,0,2), seasonal_order = (0,1,1,7))
#train model
Model = Model.fit()
#get prediction
prediction = Model.predict(len(train), len(train)+len(test)-1, exog = exo_test, typ = 'levels')
一个简单的练习将表明这些是 动态 预测,multi-step 也是如此(也就是说,第一个是 1 步,然后第二个是 2 -步,依此类推)。
#generate dataset
import matplotlib.pyplot as plt
from statsmodels.tsa.api import ArmaProcess, SARIMAX
import numpy as np
np.random.seed(20220308)
ap = ArmaProcess.from_coeffs([1.8, -0.9])
sample = ap.generate_sample(1170)
#split the closing price into train and test data
train = sample[:1000]
test = sample[1000:]
#select exogenous variables
exo = np.random.standard_normal((1170, 2))
#split exogenuous variables into train and test data
exo_train = exo[:1000]
exo_test = exo[1000:]
#set the best configuration from auto_arima for the SARIMAX model
model = SARIMAX(train, exog = exo_train, order=(2,0,0), trend="c")
#train model
res = model.fit()
#get prediction
prediction = res.predict(len(train), len(train)+len(test)-1, exog = exo_test, typ = 'levels')
x = np.arange(len(prediction))
plt.plot(x,test, x, prediction)
plt.show()
产生
您可以看出它是 multi-step,因为该模型是固定的(AR(2))并且 long-run 预测恢复为无条件均值。
我“继承”了这段代码,对SARIMAX模型不熟悉。评论有助于理解发生了什么,但我不明白预测完成的最后一行。
该数据集共有 1171 行,分为 1000 行训练行和 171 行测试行。这转化为:
Model.predict(1000, 1170, exog=exo_test, typ='levels')
我查看了 predict() 的文档。第一个参数是 endog 参数,第二个应该是 exog 参数。但是当 exog=exog_test 时,1170 应该是什么意思?此外,文档中没有提到 'typ' 参数。
不明白的地方:
- 这是超前一步的预测吗?意味着它取真值,预测下一步然后丢弃预测,及时取下一个真值来预测下一步?
[1. a) 如果是一步预测,SARIMAX 模型是否需要拟合 again/retrained?难道不应该使用最后的真实值,因此在每一步预测之后都对真实值进行再训练吗?]
这是多步预测吗,意味着 t+2 中的预测是基于预测而不是 t+1 中的真实值?
由于我假设它是一步预测,是否可以“轻松”将其转换为多步预测?
完整代码:
#load dataset
df = pd.read_csv('data.csv', index_col = 'date', parse_dates = True)
#split the closing price into train and test data
train = df.iloc[:1000,4]
test = df.iloc[1000:,4]
#select exogenous variables
exo = df.iloc[:,6:61]
#split exogenuous variables into train and test data
exo_train = exo.iloc[:1000]
exo_test = exo.iloc[1000:]
#run auto_arima to find the best configuration (I selected m=7 and D=1 by running seasonal_decompose and acf and pacf plots)
auto_arima(df['close'], exogenous=exo, m=7, trace=True, D=1).summary()
#set the best configuration from auto_arima for the SARIMAX model
Model = SARIMAX(train, exog = exo_train, order=(1,0,2), seasonal_order = (0,1,1,7))
#train model
Model = Model.fit()
#get prediction
prediction = Model.predict(len(train), len(train)+len(test)-1, exog = exo_test, typ = 'levels')
一个简单的练习将表明这些是 动态 预测,multi-step 也是如此(也就是说,第一个是 1 步,然后第二个是 2 -步,依此类推)。
#generate dataset
import matplotlib.pyplot as plt
from statsmodels.tsa.api import ArmaProcess, SARIMAX
import numpy as np
np.random.seed(20220308)
ap = ArmaProcess.from_coeffs([1.8, -0.9])
sample = ap.generate_sample(1170)
#split the closing price into train and test data
train = sample[:1000]
test = sample[1000:]
#select exogenous variables
exo = np.random.standard_normal((1170, 2))
#split exogenuous variables into train and test data
exo_train = exo[:1000]
exo_test = exo[1000:]
#set the best configuration from auto_arima for the SARIMAX model
model = SARIMAX(train, exog = exo_train, order=(2,0,0), trend="c")
#train model
res = model.fit()
#get prediction
prediction = res.predict(len(train), len(train)+len(test)-1, exog = exo_test, typ = 'levels')
x = np.arange(len(prediction))
plt.plot(x,test, x, prediction)
plt.show()
产生
您可以看出它是 multi-step,因为该模型是固定的(AR(2))并且 long-run 预测恢复为无条件均值。