1. 程式人生 > >時間序列--用置信區間更好的解釋結果

時間序列--用置信區間更好的解釋結果

from pandas import Series
from statsmodels.tsa.arima_model import ARIMA
series = Series.from_csv('daily-total-female-births.csv', header=0)
X = series.values
X = X.astype('float32')
size = len(X) - 1
train, test = X[0:size], X[size:]
model = ARIMA(train, order=(5,1,1))
model_fit = model.fit(disp=False)
forecast, stderr, conf = model_fit.forecast()
print('Expected: %.3f' % test[0])
print('Forecast: %.3f' % forecast)
print('Standard Error: %.3f' % stderr)
print('95%% Confidence Interval: %.3f to %.3f' % (conf[0][0], conf[0][1]))

結果如下:

Expected: 50.000
Forecast: 45.878
Standard Error: 6.996
95% Confidence Interval: 32.167 to 59.590---------我們有95%的概率落在這個區間

from pandas import Series
from statsmodels.tsa.arima_model import ARIMA
series = Series.from_csv('daily-total-female-births.csv', header=0)
X = series.values
X = X.astype('float32')
size = len(X) - 1
train, test = X[0:size], X[size:]
model = ARIMA(train, order=(5,1,1))
model_fit = model.fit(disp=False)
intervals = [0.2, 0.1, 0.05, 0.01]
for a in intervals:
	forecast, stderr, conf = model_fit.forecast(alpha=a)
	print('%.1f%% Confidence Interval: %.3f between %.3f and %.3f' % ((1-a)*100, forecast, conf[0][0], conf[0][1]))

當然可以把區間設定大或小

80.0% Confidence Interval: 45.878 between 36.913 and 54.844
90.0% Confidence Interval: 45.878 between 34.371 and 57.386
95.0% Confidence Interval: 45.878 between 32.167 and 59.590
99.0% Confidence Interval: 45.878 between 27.858 and 63.898

plot_predict() 可以畫出來置信區間

emmm最後一點看的有點暈,等用到的時候查查這些函式的API把,這裡現有一個大致的思想

https://machinelearningmastery.com/time-series-forecast-uncertainty-using-confidence-intervals-python/