Exemple #1
0
def test_summary_corner():
    data = sm.datasets.macrodata.load_pandas().data["cpi"].diff().dropna()
    dates = period_range(start='1959Q1', periods=len(data), freq='Q')
    data.index = dates
    res = AR(data).fit(maxlag=4)
    summ = res.summary().as_text()
    assert 'AR(4)' in summ
    assert 'L4.cpi' in summ
    assert '03-31-1959' in summ
    res = AR(data).fit(maxlag=0)
    summ = res.summary().as_text()
    assert 'const' in summ
    assert 'AR(0)' in summ
def test_summary_corner():
    data = sm.datasets.macrodata.load_pandas().data["cpi"].diff().dropna()
    dates = period_range(start="1959Q1", periods=len(data), freq="Q")
    data.index = dates
    with pytest.warns(FutureWarning):
        res = AR(data).fit(maxlag=4)
    summ = res.summary().as_text()
    assert "AR(4)" in summ
    assert "L4.cpi" in summ
    assert "03-31-1959" in summ
    with pytest.warns(FutureWarning):
        res = AR(data).fit(maxlag=0)
    summ = res.summary().as_text()
    assert "const" in summ
    assert "AR(0)" in summ
Exemple #3
0
d = best_diff(series)
print("最佳差分階數:%d階" % d)

diff_series=series.diff(1).dropna()
#利用自訂函式取得bic最佳的pq參數
p, q = best_p_q(d, series)
#印出最佳的ARIMA參數
print("最佳參數組合ARIMA(%d,%d,%d)" %(p,d,q))

#使用最佳參數組合fit 
model = ARIMA(series, order=(p,d,q))

#由於在fit的過程中有很多線性迴歸的debug訊息出現 因此設定disp =0 關閉
model_fit = model.fit(disp=0)
#顯示ARIMA模型的摘要
print(model_fit.summary())

#取得ARIMA fit過的值  (因fit的對象是差分過的  因此比較對象應為差分過的資料集)
arima_predict_diff=model_fit.fittedvalues
#作圖
plt.plot(diff_series, label='real-diff')
plt.plot(arima_predict_diff, color='red', linestyle='--', label='ARIMA(%d,%d,%d)'%(p,d,q))
plt.xlabel('Month')
plt.xticks(diff_series.index[::6], diff_series.index.strftime('%Y-%b')[::6] )
plt.ylabel('Sales-diff')
plt.title('ARIMA fitting result')
plt.legend(loc="best")
plt.show()

#取得ARIMA 符合原數據(差分前的scale)的預測結果
r=model_fit.predict(typ='levels')