預測

Python 中的 ARIMA 模型

James Fulton

Climate informatics researcher

預測下一個值

以 AR(1) 模型為例

$y_t = a_1 y_{t-1} + \epsilon_t$

預測下一個值

$y_t = 0.6\; \text{x} \; 10 + \epsilon_t$

$y_t = 6.0 + \epsilon_t$

預測的不確定性

$ 5.0 < y_t < 7.0$

Python 中的 ARIMA 模型

一步預測(One-step-ahead)

Python 中的 ARIMA 模型

製作一步預測

# Make predictions for last 25 values
results = model.fit()

# Make in-sample prediction forecast = results.get_prediction(start=-25)
Python 中的 ARIMA 模型

製作一步預測

# Make predictions for last 25 values
results = model.fit()

# Make in-sample prediction forecast = results.get_prediction(start=-25)
# forecast mean mean_forecast = forecast.predicted_mean

預測平均是 pandas Series

2013-10-28    1.519368
2013-10-29    1.351082
2013-10-30    1.218016
Python 中的 ARIMA 模型

信賴區間

# Get confidence intervals of forecasts
confidence_intervals = forecast.conf_int()

信賴區間方法會回傳 pandas DataFrame

                                 lower y                       upper y
2013-09-28                     -4.720471                     -0.815384
2013-09-29                     -5.069875                      0.112505
2013-09-30                     -5.232837                      0.766300
2013-10-01                     -5.305814                      1.282935
2013-10-02                     -5.326956                      1.703974
Python 中的 ARIMA 模型

繪製預測圖

plt.figure()

# Plot prediction
plt.plot(dates, 
         mean_forecast.values, 
         color='red', 
         label='forecast')

# Shade uncertainty area plt.fill_between(dates, lower_limits, upper_limits, color='pink') plt.show()
Python 中的 ARIMA 模型

繪製預測圖

Python 中的 ARIMA 模型

動態預測

Python 中的 ARIMA 模型

製作動態預測

results = model.fit()
forecast = results.get_prediction(start=-25, dynamic=True)
# forecast mean
mean_forecast = forecast.predicted_mean

# Get confidence intervals of forecasts
confidence_intervals = forecast.conf_int()
Python 中的 ARIMA 模型

樣本外預測

forecast = results.get_forecast(steps=20)
# forecast mean
mean_forecast = forecast.predicted_mean

# Get confidence intervals of forecasts
confidence_intervals = forecast.conf_int()
Python 中的 ARIMA 模型

樣本外預測

forecast = results.get_forecast(steps=20)

Python 中的 ARIMA 模型

一起來練習吧!

Python 中的 ARIMA 模型

Preparing Video For Download...