pythonarimaforecastsarimax

ARIMA forecast model don't show on graph


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


Solution

  • 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