Skip to content

ARIMA order selection #1224

Open
Open

Description

@jseabold

Could include some tools to suggest orders / make automatic order selection suggestions a little easier. E.g., in SAS

SAS and gretl also expose the AUTOMDL statement in X13-ARIMA. I have some code for this one already.

Activity

  1. jseabold commented on Feb 25, 2014

    @jseabold
    MemberAuthor

    Partially addressed by #1334.

  2. jseabold commented on Apr 28, 2014

    @jseabold
    MemberAuthor

    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.

  3. jseabold commented on Apr 29, 2014

    @jseabold
    MemberAuthor

    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.

  4. josef-pkt commented on Feb 15, 2019

    @josef-pkt
    Member

    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
    
    # taken from SAS/ Box Jenkins series A
    x = np.array([17.0, 16.6, 16.3, 16.1, 17.1, 16.9, 16.8, 17.4, 17.1,
        17.0, 16.7, 17.4, 17.2, 17.4, 17.4, 17.0, 17.3, 17.2, 17.4, 16.8,
        17.1, 17.4, 17.4, 17.5, 17.4, 17.6, 17.4, 17.3, 17.0, 17.8, 17.5,
        18.1, 17.5, 17.4, 17.4, 17.1, 17.6, 17.7, 17.4, 17.8, 17.6, 17.5,
        16.5, 17.8, 17.3, 17.3, 17.1, 17.4, 16.9, 17.3, 17.6, 16.9, 16.7,
        16.8, 16.8, 17.2, 16.8, 17.6, 17.2, 16.6, 17.1, 16.9, 16.6, 18.0,
        17.2, 17.3, 17.0, 16.9, 17.3, 16.8, 17.3, 17.4, 17.7, 16.8, 16.9,
        17.0, 16.9, 17.0, 16.6, 16.7, 16.8, 16.7, 16.4, 16.5, 16.4, 16.6,
        16.5, 16.7, 16.4, 16.4, 16.2, 16.4, 16.3, 16.4, 17.0, 16.9, 17.1,
        17.1, 16.7, 16.9, 16.5, 17.2, 16.4, 17.0, 17.0, 16.7, 16.2, 16.6,
        16.9, 16.5, 16.6, 16.6, 17.0, 17.1, 17.1, 16.7, 16.8, 16.3, 16.6,
        16.8, 16.9, 17.1, 16.8, 17.0, 17.2, 17.3, 17.2, 17.3, 17.2, 17.2,
        17.5, 16.9, 16.9, 16.9, 17.0, 16.5, 16.7, 16.8, 16.7, 16.7, 16.6,
        16.5, 17.0, 16.7, 16.7, 16.9, 17.4, 17.1, 17.0, 16.8, 17.2, 17.2,
        17.4, 17.2, 16.9, 16.8, 17.0, 17.4, 17.2, 17.2, 17.1, 17.1, 17.1,
        17.4, 17.2, 16.9, 16.9, 17.0, 16.7, 16.9, 17.3, 17.8, 17.8, 17.6,
        17.5, 17.0, 16.9, 17.1, 17.2, 17.4, 17.5, 17.9, 17.0, 17.0, 17.0,
        17.2, 17.3, 17.4, 17.4, 17.0, 18.0, 18.2, 17.6, 17.8, 17.7, 17.2,
        17.4])
    
    
    s,c,p,n = scan(x,5,5)
    
    sas_s = np.array([[0.3263, 0.2479, 0.1654, 0.1387, 0.1183, 0.1417],
        [0.0643, 0.0012, 0.0028, 0.0000, 0.0051, 0.0002],
        [0.0061, 0.0027, 0.0021, 0.0011, 0.0017, 0.0079],
        [0.0072, 0.0000, 0.0007, 0.0005, 0.0019, 0.0021],
        [0.0049, 0.0010, 0.0014, 0.0014, 0.0039, 0.0145],
        [0.0202, 0.0009, 0.0016, 0.0000, 0.0126, 0.0001]])
    
    sas_p = np.array([[.0000, .0000, .0000, 0.0007, 0.0037, 0.0024],
        [0.0003, 0.6649, 0.5194, 0.9235, 0.3993, 0.8528],
        [0.2754, 0.5106, 0.5860, 0.7346, 0.6782, 0.2766],
        [0.2349, 0.9812, 0.7667, 0.7861, 0.6810, 0.6546],
        [0.3297, 0.7154, 0.7113, 0.6995, 0.5807, 0.2205],
        [0.0477, 0.7254, 0.6652, 0.9576, 0.2660, 0.9168]])
    
    print(np.max(np.abs(sas_p - p)))
    print(np.max(np.abs(sas_s - s)))
    
    from statsmodels.tsa.arima_process import arma_generate_sample
    nsample = 2000 +1000
    ar = [1.0, -0.3, -0.]
    ma = [1.0,  0.5,  0.3]
    y2 = arma_generate_sample(ar,ma,nsample,0.1)[1000:]
    s,c,p,n = scan(y2, 5, 5)
    print((p<0.025).astype(int))
    print((p<0.05).astype(int))
    
  5. jseabold commented on Mar 14, 2021

    @jseabold
    MemberAuthor

    Inclined to close this as done. We expose AUTOMDL through x13 interface, have arma_select_order_ic, and pmdarima implements full auto.arima.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions