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

发布于 2025-01-17 06:45:40 字数 2156 浏览 4 评论 0 原文

我目前正在使用 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:
enter image description here

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.

enter image description here

Please help T.T

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦亿 2025-01-24 06:45:40

请注意,为了设置预测指数和置信区间,我们从元素总数中减去 57。还需要上置信区间和下置信区间的数据,以供后续绘图使用(conf_ins =fitting.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()

输入图片此处描述

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()).

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()

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).

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()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文