自回歸模型,過去的觀察值和現在的干擾值的聯系組合預測
滑動平均模型, 過去的感染治和現在的干擾值的線性組合預測
ARMA(Autoregressive–moving-average model)
Wiki
ARIMA(p,d,q) ,非平穩序列經過k階差分后變成平穩序列運用ARMA模型
繪制時序圖看看數據長什么樣,猜測是平穩還是非平穩
ADF(Augmented Dickey-Fuller unit root test)單位根平穩檢驗
隨機序列(白噪聲)檢驗
方法:Q統計量、LB統計量
繪制ACF(Autocorrelation)自相關圖,自相關系數
PACF(Partial Autocorrelation)偏自相關圖,
BIC信息量最小選擇p,q
p, q 階數一般不超過length/10
模型檢驗和參數估計
ARIMA模型預測
拖尾:始終有非零取值,不會在k大于某個常數后就恒等于零(或在0附近隨機波動)
截尾:在大于某個常數k后快速趨于0為k階截尾
statsmodels
Time Series analysis
ARIMA
import statsmodels.api as smimport pandas as pddf = pd.DataFrame(data)#dataframex = #LOAD YOUR DATAindex = pd.Index(sm.tsa.datetools.dates_from_range('1959Q1', '2009Q3'))#ordates = sm.tsa.datetools.dates_from_range('1980m1', length=nobs)df = pd.DataFrame(X,colomns=['x'],index=index)#plotdf.plot(df)#ACFsm.tsa.acf(df)sm.graphics.tsa.plot_acf(df)#PACFsm.tsa.pacf(df)sm.graphics.tsa.plot_pacf(df)#ADFsm.tsa.adfuller(df.x) #df.loc[:,'x'] | df.iloc[:,0]#diff差分pd.diff()#diagnostic白噪聲檢驗, 返回stats和psm.stats.diagnostic.acorr_ljungbox(df, lags=1)#model#from statsmodels.tsa.arima_model import ARIMA #ARMAmodel = sm.tsa.ARIMA(df, order=(p,d,q))model = sm.tsa.ARMA()arma_res = model.fit(trend='nc', disp=-1)#BICmodel.bic#模型報告model.summary2()model.summary()model.tail()#擬合結果model.PRedict()#預測圖fig, ax = plt.subplots(figsize=(10,8))fig = arma_res.plot_predict(start='1999m6', end='2001m5', ax=ax)legend = ax.legend(loc='upper left')#預測接下來5個數model.forecast(5)新聞熱點
疑難解答