import pandas as pd
import numpy as np
import statsmodels.tsa.api as smt
import pandas_datareader.data as web
start = '2007-01-01'
end = '2015-01-01'
get_px = lambda x: web.DataReader(x, 'yahoo', start=start, end=end)['Adj Close']
symbols = ['SPY','TLT','MSFT']
# raw adjusted close prices
data = pd.DataFrame({sym:get_px(sym) for sym in symbols})
# log returns
lrets = np.log(data/data.shift(1)).dropna()
# Select best lag order for MSFT returns
max_lag = 30
mdl = smt.AR(lrets.MSFT).fit(maxlag=max_lag, ic='aic', trend='nc')
est_order = smt.AR(lrets.MSFT).select_order(maxlag=max_lag, ic='aic', trend='nc')
print('best estimated lag order = {}'.format(est_order))
print mdl.params
輸出會是這樣。爲什麼statsmodels select_order函數與AR模型中的params不同?
best estimated lag order = 23
L1.MSFT -0.075405
L2.MSFT -0.067423
L3.MSFT 0.031371
L4.MSFT -0.063610
L5.MSFT -0.045080
L6.MSFT -0.001510
L7.MSFT -0.051875
L8.MSFT -0.015192
L9.MSFT -0.018665
L10.MSFT 0.044720
L11.MSFT 0.041655
L12.MSFT 0.034231
L13.MSFT -0.042473
L14.MSFT -0.008583
L15.MSFT 0.022171
L16.MSFT 0.009983
L17.MSFT 0.038606
L18.MSFT -0.073060
dtype: float64
這裏的問題是,即使statsmodels決定,AR模型的理想滯後23,只有18個參數,當您檢查的參數。它不應該一樣嗎?如果來自「select_order」的次序數小於參數數組的長度,則可能是有意義的。我知道在這種情況下,「select_order」使用AIC標準來決定理想的滯後訂單。
有人可以解釋爲什麼嗎?假設23的確是理想的滯後階數,那麼我怎樣才能獲得額外的5個參數,因爲我在這裏只能得到18個參數?
根據最好的數到代碼中,默認的'方法'不同,「cmle」適合,select_order中選擇「mle」。嘗試使用相同的方法參數。 – user333700