我目前正在使用 ARIMA 模型来预测股票价格 SARIMAX(0,1,0)。我想以 95% 的置信区间预测测试数据集上的股票价格。我正在关注本教程 2021 年 7 月发布,但有些事情发生了变化,我不知道是什么。
原始代码如下:
# 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 。

请帮助 TT
I'm currently using the ARIMA model to predict a stock price, SARIMAX(0,1,0). I wanted to forecast stock prices on the test dataset with 95% confidence interval. I'm following this tutorial posted July 2021, but some things have changed and I can't figure out what.
The original code are as follows:
# 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()
My code are as the following:
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()
I wanted the graph to look similarly as follows:

However, the predicted stock price doesn't show on mine.
When I try to plot the predicted stock price on its own graph, it doesn't show at all.
It seems that it failed to adopt the test.index consisting of dates.

Please help T.T
发布评论
评论(1)
请注意,为了设置预测指数和置信区间,我们从元素总数中减去 57。还需要上置信区间和下置信区间的数据,以供后续绘图使用(conf_ins =fitting.get_forecast(57).summary_frame())。
设置轴时间。但预测开始逐步进行。
我还无法弄清楚这与什么有关。我保留这两个选项。
如果合适,请投票)。
Note that to set the forecast indices and confidence intervals, we subtract 57 from the total number of elements. Data is also requested for the upper and lower confidence interval, for their subsequent drawing(conf_ins = fitted.get_forecast(57).summary_frame()).
Set time for axis. But forecasts began to be drawn stepwise.
I can't figure out what this is related to yet. I leave both options.
If it fits, then please vote).