ARIMA 预测模型未显示在图表上

ARIMA forecast model don't show on graph

我目前正在使用 ARIMA 模型预测股票价格 SARIMAX(0,1,0)。我想以 95% 的置信区间预测测试数据集上的股票价格。我正在学习本教程 posted July 2021,但有些内容发生了变化,我不知道是什么。

原代码如下:

# Forecast
fc, se, conf = fitted.forecast(321, alpha=0.05)  # 95% conf
# Make as pandas series
fc_series = pd.Series(fc, index=test_data.index)
lower_series = pd.Series(conf[:, 0], index=test_data.index)
upper_series = pd.Series(conf[:, 1], index=test_data.index)
# Plot
plt.figure(figsize=(10,5), dpi=100)
plt.plot(train_data, label='training data')
plt.plot(test_data, color = 'blue', label='Actual Stock Price')
plt.plot(fc_series, color = 'orange',label='Predicted Stock Price')
plt.fill_between(lower_series.index, lower_series, upper_series, 
                 color='k', alpha=.10)
plt.title('ARCH CAPITAL GROUP Stock Price Prediction')
plt.xlabel('Time')
plt.ylabel('ARCH CAPITAL GROUP Stock Price')
plt.legend(loc='upper left', fontsize=8)
plt.show()

我的代码如下:

model = sm.tsa.statespace.SARIMAX(df_log, trend='c', order=(0,1,0))
fitted = model.fit(disp=False)
print(fitted.summary())

result = fitted.forecast(57, alpha =0.05)

# Make as pandas series
fc_series = pd.Series(result[564:620],test.index)
lower_series = pd.Series(result[564], test.index)
upper_series = pd.Series(result[620], test.index)

# Plot
plt.figure(figsize=(10,5), dpi=100)
plt.plot(df_log, label='training data')
plt.plot(test, color = 'blue', label='Actual Stock Price')
plt.plot(fc_series, color = 'orange',label='Predicted Stock Price')
plt.fill_between(lower_series.index, lower_series, upper_series, 
                 color='gray', alpha=.10)
plt.title('TSLA Stock Price Prediction')
plt.xlabel('Date')
plt.ylabel('TSLA Stock Price')
plt.legend(loc='best', fontsize=8)
plt.show()

我希望图表看起来类似如下:

但是,预测的股票价格没有显示在我的身上。 当我尝试在其自己的图表上绘制预测的股票价格时,它根本不显示。 好像没有采用日期组成的test.index

请帮忙T.T

请注意,要设置预测指数和置信区间,我们从元素总数中减去 57。还要求提供上限和下限置信区间的数据,以供后续绘制(conf_ins = fitted.get_forecast(57).summary_frame()).

import statsmodels.api as sm
import pandas_datareader.data as web
import matplotlib.pyplot as plt

df = web.DataReader('^GSPC', 'yahoo', start='2020-05-15', end='2021-10-01')
total = len(df)
aaa = 57
hist = total - aaa

model = sm.tsa.statespace.SARIMAX(df['Close'].values[:hist], trend='c', order=(0,1,0))
fitted = model.fit(disp=False)

result = fitted.forecast(aaa, alpha =0.05)
conf_ins = fitted.get_forecast(aaa).summary_frame()
ind = np.arange(total)

fig, ax = plt.subplots()
ax.plot(ind, df['Close'].values, label='Actual Stock Price')
ax.plot(ind[hist:], result,label='Predicted Stock Price')
ax.plot(ind[hist:], conf_ins['mean_ci_lower'])
ax.plot(ind[hist:], conf_ins['mean_ci_upper'])
ax.legend()
fig.autofmt_xdate()
plt.show()

设置轴的时间。但预测开始逐步制定。 我无法弄清楚这与什么有关。我保留这两个选项。 如果合适,请投票)。

import statsmodels.api as sm
import pandas_datareader.data as web
import matplotlib.pyplot as plt

df = web.DataReader('^GSPC', 'yahoo', start='2020-05-15', end='2021-10-01')
total = len(df)
aaa = 57
hist = total - aaa

model = sm.tsa.statespace.SARIMAX(df['Close'].values[:hist], trend='c', order=(0,1,0))
fitted = model.fit(disp=False)

result = fitted.forecast(aaa, alpha = 0.05)
conf_ins = fitted.get_forecast(aaa).summary_frame()
ind = np.arange(total)

fig, ax = plt.subplots()
ax.plot(df.index, df['Close'].values, label='Actual Stock Price')
ax.plot(df.index[hist:], result, label='Predicted Stock Price')
ax.plot(df.index[hist:], conf_ins['mean_ci_lower'])
ax.plot(df.index[hist:], conf_ins['mean_ci_upper'])
ax.legend()
fig.autofmt_xdate()
plt.show()