You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Cf. Hyndman's paper on the forecast package in jstatsoft. Choosing d and D by minimizing the AIC when we use the full likelihood with diffuse prior of Durbin and Koopman leads to overdifferencing. So prefer unit root tests.
FWIW, I've found automdl to do systemically much better at order selection than the approach taken in auto.arima in R on a fairly large scale forecasting exercise. This is for monthly macroeconomic series. YMMV. Might be worth a monte carlo study.
parking some code that I found in a module, in case we ever want it
AFAIR, this was initially written by Skipper
this runs with current master with one FutureWarning
(original is scan.py using scikits.statsmodels)
import numpy as np
from statsmodels.tsa.api import lagmat,acf
from scipy import linalg
from scipy import stats
def scan(x, armax=5, mamax=5, demean=True, noraise=False):
"""
Smallest Canonical Matrix
Parameters
----------
x : array-like
Input array
armax : int
The maximum number of ar parameters to try
mamax : int
The maximum number of ma parameters to try
demean : bool, optional
Whether or not to demean x. This should only be False if you have
already demeaned x.
noraise : bool, optional
If True does not raise an error if non-trivial (ie., due to numerical
error) complex values are detected in the eigenvalues and eigenvectors.
This is for debugging.
Returns
-------
rho : array
armax + 1 by mamax + 1 array. Contains the squared canonical
correlation matrix estimates. The rows are for AR coefficients 0 through
`armax`, the columns are for MA coefficients 0 through `mamax`.
c : array
armax + 1 by mamax + 1 array of Chi-2 distributed test statistics
associated with rho.
p-values : array
The p-values associated with the values of c.
rec : tuple, optional
The tentative recommended order selection (p+d,q). If the process is
stationary this is (p,q). If not, the
Notes
-----
References
----------
Tsay and Tiao
Box, Jenkens, and Reinsel
"""
x = np.asarray(x)
if x.ndim > 1 and x.shape[1] > 1:
raise ValueError("Input should be univariate, got shape %s" \
% str(x.shape))
# if x.ndim == 1:
# x = x[:,None]
if demean:
z = x - x.mean()
nobs = float(len(z))
# truncate the series to max(m,j) so they are comparable
scan_matrix = np.zeros((armax+1,mamax+1))
c_matrix = np.zeros((armax+1,mamax+1))
N = np.zeros((armax+1,mamax+1)) # from Tsay and Tiao remove when done
for m in range(armax+1):
for j in range(mamax+1):
zlags = lagmat(z,maxlag=m+1+j,original='in',trim='both')
zm = zlags[:,:m+1]
zj = zlags[:,j+1:]
#print m, zm.shape, zj.shape
#NOTE: if m == 0, acf
#NOTE: if j == 0, pacf
## mat = np.dot(np.dot(np.linalg.inv(np.dot(zm.T,zm)),np.dot(zm.T,zj)),
## np.dot(np.linalg.inv(np.dot(zj.T,zj)),np.dot(zj.T,zm)))
beta_mj = np.linalg.lstsq(zm, zj)[0]
beta_jm = np.linalg.lstsq(zj, zm)[0]
mat = np.dot(beta_mj, beta_jm)
eig, phi = np.linalg.eig(mat) # phi are the eigenvectors
if np.any(np.iscomplex(eig)): # it *should* be a numerical error
eig = np.real_if_close(eig)
sm_eig = np.min(eig)
sm_phi = np.real_if_close(phi[:,eig.argmin()])
if np.any(np.iscomplex(sm_phi)) and not noraise:
class ComplexError(BaseException): pass
raise ComplexError("The eigenvector contains complex values. "+\
"You can rerun with noraise=True.\nPlease report as "+\
"a bug though")
N[m,j] = np.sum(np.abs(eig) < .05) # needs some rel. tolerance
#TODO: remove when debugged
if m == 0:
w = zlags[:,0]
else:
#w = zlags[:,0] - np.dot(zlags[:,1:m+2],sm_phi)
w = np.dot(zlags[:,:m+1], sm_phi/sm_phi[0])
#NOTE: how do we know that sm_phi is the "right" order
# or AR coefficients?
#racf2 = sm.tsa.acf(w, nlags=j, unbiased=True)[1:]**2
racf2 = acf(w, nlags=j, unbiased=True)[1:]**2
#NOTE: Box and Jenkins say square the above, sas doesn't,
# Tsay and Tiao says square it, so do it.
if j == 0:
dmj = 1.
else:
dmj = 1+2*np.sum(racf2)
cmj = -(nobs - m - j)*np.log(1.-sm_eig/dmj)
scan_matrix[m,j] = sm_eig
c_matrix[m,j] = cmj
p_vals = stats.chi2.sf(c_matrix,1)
return scan_matrix, c_matrix, p_vals, N
Activity
jseabold commentedon Feb 25, 2014
Partially addressed by #1334.
jseabold commentedon Apr 28, 2014
Cf. Hyndman's paper on the forecast package in jstatsoft. Choosing d and D by minimizing the AIC when we use the full likelihood with diffuse prior of Durbin and Koopman leads to overdifferencing. So prefer unit root tests.
jseabold commentedon Apr 29, 2014
FWIW, I've found automdl to do systemically much better at order selection than the approach taken in
auto.arimain R on a fairly large scale forecasting exercise. This is for monthly macroeconomic series. YMMV. Might be worth a monte carlo study.josef-pkt commentedon Feb 15, 2019
parking some code that I found in a module, in case we ever want it
AFAIR, this was initially written by Skipper
this runs with current master with one FutureWarning
(original is scan.py using scikits.statsmodels)
jseabold commentedon Mar 14, 2021
Inclined to close this as done. We expose AUTOMDL through x13 interface, have
arma_select_order_ic, and pmdarima implements full auto.arima.