Skip to content

Missing trend in fitted AR model params? #5538

Closed

Description

@mloning

In some cases of the AR model, the trend seems to be dropped from the fitted params and the number of fitted params differs. I expected the number of fitted params to be the same for the same specifications of maxlag, trend='c'.

This seems to be at least inconsistent with the online documentation where I couldn't find any mention of this? Any idea why this is?

To reproduce the problem, run the code below:

Sorry for posting all of the data, but the problem seems to depend on the data. The data comes from a particular segment of the gunpoint training dataset.

from statsmodels.tsa.ar_model import AR
import numpy as np

# define data
a = np.array([-0.73272 , -0.72451 , -0.71322 , -0.70734 , -0.69868 , -0.69435 ,
       -0.66939 , -0.61584 , -0.50646 , -0.36608 , -0.22283 , -0.047168,
        0.1419  ,  0.29323 ,  0.42329 ,  0.55716 ,  0.68035 ,  0.78691 ,
        0.89422 ,  0.97334 ,  1.0395  ,  1.0935  ,  1.1428  ,  1.1796  ,
        1.2073  ,  1.213   ,  1.2158  ,  1.2299  ,  1.2299  ,  1.2338  ,
        1.2341  ,  1.2427  ,  1.2579  ,  1.2583  ,  1.2487  ,  1.2571  ,
        1.2516  ,  1.2525  ,  1.2576  ,  1.2408  ,  1.2475  ,  1.2464  ,
        1.245   ,  1.2468  ,  1.2526  ,  1.2384  ,  1.2384  ,  1.2441  ,
        1.2349  ,  1.2412  ,  1.235   ,  1.2393  ,  1.2369  ,  1.2252  ,
        1.2236  ,  1.2212  ,  1.2271  ,  1.223   ,  1.2367  ,  1.2328  ,
        1.2265  ,  1.2274  ,  1.2269  ,  1.2171  ,  1.2092  ,  1.2075  ,
        1.1895  ,  1.1762  ,  1.1432  ,  1.1067  ,  1.0547  ,  0.98044 ,
        0.89041 ,  0.77871 ,  0.65819 ,  0.50315 ,  0.34939 ,  0.18367 ,
        0.043842, -0.090519, -0.21244 , -0.33155 , -0.44732 , -0.54271 ,
       -0.6227  , -0.68627 , -0.72709 , -0.76162 , -0.77914 , -0.79041 ,
       -0.79569 , -0.79752 , -0.799   , -0.79718 , -0.80404 , -0.8125  ,
       -0.81943 , -0.82027 , -0.82311 , -0.82287 , -0.81811 , -0.81731 ,
       -0.82755 ])
b = np.array([-0.59477 , -0.60212 , -0.60924 , -0.6135  , -0.61131 , -0.61516 ,
       -0.61299 , -0.61363 , -0.61023 , -0.60687 , -0.59831 , -0.58668 ,
       -0.57709 , -0.56536 , -0.55586 , -0.54901 , -0.5392  , -0.53329 ,
       -0.5288  , -0.51145 , -0.49628 , -0.47031 , -0.43497 , -0.36743 ,
       -0.29702 , -0.23969 ,  0.06958 ,  0.25768 ,  0.43618 ,  0.57948 ,
        0.7313  ,  0.90601 ,  1.1171  ,  1.3602  ,  1.6123  ,  1.7955  ,
        1.8947  ,  1.9255  ,  1.9508  ,  1.9576  ,  1.9752  ,  1.9944  ,
        1.999   ,  2.0095  ,  2.0169  ,  2.0169  ,  2.0169  ,  2.02    ,
        2.0228  ,  2.0267  ,  2.0298  ,  2.0268  ,  2.0232  ,  2.0187  ,
        2.0114  ,  1.9897  ,  1.9277  ,  1.8376  ,  1.6944  ,  1.5212  ,
        1.3611  ,  1.2183  ,  1.0752  ,  0.95557 ,  0.83104 ,  0.68869 ,
        0.52236 ,  0.49685 ,  0.30739 ,  0.033138, -0.18204 , -0.33481 ,
       -0.47059 , -0.61725 , -0.73227 , -0.80944 , -0.85854 , -0.87032 ,
       -0.86165 , -0.83881 , -0.80939 , -0.79222 , -0.77832 , -0.75982 ,
       -0.74043 , -0.71732 , -0.69354 , -0.67625 , -0.66151 , -0.64793 ,
       -0.6419  , -0.63702 , -0.63379 , -0.63214 , -0.62507 , -0.62084 ,
       -0.61382 , -0.6043  , -0.59542 , -0.58973 , -0.58362 , -0.57583 ,
       -0.57109 ])

# helper function
def ar_coefs(x, maxlag=100, trend='nc'):
    nlags = np.minimum(len(x) - 1, maxlag)
    model = AR(endog=x)
    return model.fit(maxlag=nlags, trend=trend).params

# ensure data has the same shape
assert a.shape == b.shape

# the number of fitted params differs
print(ar_coefs(a, trend='c').shape == ar_coefs(b, trend='c').shape)
print(ar_coefs(a, trend='nc').shape == ar_coefs(b, trend='nc').shape)

Activity

  1. josef-pkt commented on Mar 21, 2019

    @josef-pkt
    Member

    Is nlags=100 in this case?
    Does it also happen with smaller nlags?

    Aside: nlags is much too large relative to nobs to give reliable estimates. So this case has most likely never been checked.

    Do you get different results with ARMA(nlags, 0)?

  2. mloning commented on Mar 21, 2019

    @mloning
    ContributorAuthor

    Yes, nlags=100 and len(a)=103. Strangely, this seems to be only reproducible for nlags=100. For smaller or larger lags, it seems to work as expected.

    The ARMA(nlags, 0) model throws an stationarity error.

  3. josef-pkt commented on Mar 21, 2019

    @josef-pkt
    Member

    This is then most likely a degenerate case.
    I don't know the details of AR, but ARMA has a check whether a constant or trend is already present in the model and if it is, then does not add an additional constant or trend to avoid collinearity.
    With only a few degrees of freedom, this check could indicate that there is already a constant.
    This is just a guess for the possible reason that constant is dropped.

    Given that this case does not produce reliable estimates, the bug (or feature) is not high priority.

    Why do you estimate a model with almost no degrees of freedom?
    AR coefficients for large lags will be mostly noise.

  4. josef-pkt commented on Mar 21, 2019

    @josef-pkt
    Member

    I think the tsa_tools.add_trend helper function should issue a warning in this case

            if np.any(col_const):
                if has_constant == 'raise':
                    raise ValueError("x already contains a constant")
                elif has_constant == 'skip':
                    columns = columns[1:]
                    trendarr = trendarr[:, 1:]
    

    AR._stackX creates the lagmat and then calls add_trend.

    IIUC: lagmat should have only a few rows and many columns in this case.
    cmle uses OLS with the the lagmat, so maybe this is just a pinv solution
    mle uses the full MLE including "pre-sample" observations, but this is still heavily overparameterized
    mle also uses cmle/ols as start_params.

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