pythonmatplotlibforecastingconfidence-intervalvolatility

how to add the 95% interval in a time series forecast plot


I have the following code:

volatility = pd.DataFrame({
    'actual': df1['Annualised vol21'].values,
    'model': np.append(fitted, forecast),
})
y_train = volatility['actual'][:-forecast_horizon]
y_fit = volatility['model'][:-forecast_horizon]
y_test = volatility['actual'][-forecast_horizon - 1:]
y_for = volatility['model'][-forecast_horizon - 1:]

plt.plot(y_train, label='Train')
plt.plot(y_test, label='Test')
plt.plot(y_fit, label='Fitted')
plt.plot(y_for, label='Forecasted')
ci = 0.1 * np.std(y_for)/np.mean(y_for) ##### NOT SURE OF THIS LINE
plt.fill_between(y_for.index, y_for - ci , y_for + ci , color='b', alpha=.3)
plt.legend()
plt.ylim(0, 0.2)
plt.xlim(5000, 5500)
plt.show()

which gives: enter image description here

however, I am not sure about that confidence interval. I wanted the 95% so should I put 0.05 rather than 0.1 in the line above? also, are there assumption of normality in the definition I used? I would have liked a kind of plot which can generate automatically those interval.Thanks


Solution

  • You need to use a z-table for looking up the z values for particular confidence intervals. More information can be found here.

    However, here is a small table that might help:

    CI   z-value
    80%  1.282
    85%  1.440
    90%  1.645
    95%  1.960
    99%  2.576
    

    For your code, you need to modify it to:

    from matplotlib import pyplot as plt
    import numpy as np
    
    # Example data
    x = [x for x in range (1, 20, 1)]
    y_for = x
    
    plt.plot(x, y_for, label='Forecasted')
    # For a 95% CI
    ci = 1.960 * np.std(y)/np.mean(y)
    plt.fill_between(x, y_for-ci, y_for+ci, color='b', alpha=.3)
    
    ax.fill_between(x, (y-ci), (y+ci), color='b', alpha=.1)
    

    This gives:

    plot

    To modify to other confidence intervals, switch up the value 1.960 with the desired value from the table or use a z-table.