2

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

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Luigi87
  • 265
  • 4
  • 13

1 Answers1

1

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.

Parth Shah
  • 1,237
  • 10
  • 24
  • thanks for your answer. Actually I knew about the 1.96 however when I put that value instead of 0.1 the shaded region becomes incredibly large, which made me doubt it was not correct..any suggestion why that? – Luigi87 Jul 31 '20 at 08:20
  • 1
    My best guess is that its not incorrect. This is because your y axis range is small and therefore even a 95% CI would be significantly large when visualized with that range. – Parth Shah Jul 31 '20 at 09:20