如何在时间序列预测图中添加 95% 区间

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

我有以下代码:

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

给出:

但是,我不确定该置信区间。我想要 95%,所以我应该在上面的行中输入 0.05 而不是 0.1 吗?另外,我使用的定义中是否存在常态假设?我想要一种可以自动生成那些 interval.Thanks

的情节

您需要使用 z-table 来查找特定置信区间的 z 值。可以找到更多信息 here

但是,这里有一个小 table 可能会有所帮助:

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

对于您的代码,您需要将其修改为:

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)

这给出:

要修改为其他置信区间,请将值 1.960 切换为 table 中的所需值或使用 z-table.