Skip to content

arma singular matrix #15

Closed
Closed

Description

@wesm

Original Launchpad bug 800012: https://bugs.launchpad.net/statsmodels/+bug/800012
Reported by: josef-pktd (joep).

import numpy as np
import scikits.statsmodels.api as sm

singular matrix with zero autocorrelation timeseries in ARMA ?

exog = sm.add_constant(np.random.randn(100, 2), prepend=True)
endog = exog.sum(1) + 0.2 * np.random.randn(100)

modarma = sm.tsa.ARMA(endog)
resarma = modarma.fit(order=(1,1))

Optimization terminated successfully.
Current function value: 6.341930
Iterations 12
Traceback (most recent call last):
File "", line 1, in
File "sm_overview.py", line 36, in
resarma = modarma.fit(order=(1,1))
File "E:\Josef\eclipsegworkspace\statsmodels-git\statsmodels-josef\scikits\statsmodels\tsa\arima_model.py", line 375, in fit
bounds=bounds, iprint=disp)
File "C:\Python26\lib\site-packages\scipy\optimize\lbfgsb.py", line 196, in fmin_l_bfgs_b
f, g = func_and_grad(x)
File "C:\Python26\lib\site-packages\scipy\optimize\lbfgsb.py", line 142, in func_and_grad
f = func(x, args)
File "E:\Josef\eclipsegworkspace\statsmodels-git\statsmodels-josef\scikits\statsmodels\tsa\arima_model.py", line 343, in
loglike = lambda params: -self.loglike_kalman(params)
File "E:\Josef\eclipsegworkspace\statsmodels-git\statsmodels-josef\scikits\statsmodels\tsa\arima_model.py", line 203, in loglike_kalman
return KalmanFilter.loglike(params, self)
File "E:\Josef\eclipsegworkspace\statsmodels-git\statsmodels-josef\scikits\statsmodels\tsa\kalmanf\kalmanfilter.py", line 632, in loglike
Q_0 = dot(inv(identity(m
*2)-kron(T_mat,T_mat)),
File "C:\Python26\lib\site-packages\numpy\linalg\linalg.py", line 445, in inv
return wrap(solve(a, identity(a.shape[0], dtype=a.dtype)))
File "C:\Python26\lib\site-packages\numpy\linalg\linalg.py", line 328, in solve
raise LinAlgError, 'Singular matrix'
numpy.linalg.linalg.LinAlgError: Singular matrix

Activity

  1. josef-pkt commented on Oct 6, 2011

    @josef-pkt
    Member

    I still get the exception, the example is random, I didn't get it with order (1,1), but with (2,2)

    (2011-10-06 after pandas-integration merge)

  2. jseabold commented on Apr 3, 2012

    @jseabold
    Member

    I can reproduce with this data on my windows machine, python 2.7, 0.4.0 rc1, numpy 1.6.1, scipy 0.10.0b1.

    http://www.pastie.org/3718289

    import pandas
    import statsmodels.api as sm
    import dateutil
    
    y = pandas.read_clipboard(names=['dates','y'], delimiter=",")
    dates = map(dateutil.parser.parse, y.dates)
    y.index = dates
    del y['dates']
    
    mod = sm.tsa.ARMA(y, freq='M').fit(order=(2,2), trend='nc')

    There are nans in start_params.

    [Edit: This is reproducible across platforms and numpy/scipy versions]

  3. jseabold commented on Apr 3, 2012

    @jseabold
    Member

    There is a problem with the Hannan-Rissanen starting parameters. They are not necessarily stationary and invertible, yet calling inverse ar/ma assumes they were and can lead to NaNs. (Also I'm not sure the parameters I gave are stationary...but this doesn't seem to be the root of the problem). If you give [0,0,0,0] it converges but far away from "true" parameters. If you give "true" parameters, it's a bit closer.

    I'm not sure what to do here yet. Replacing NaNs with zeros works (but converges to a non-optimal solution). Come back to this.

    start_params = mod._fit_start_params_hr((2,2,0))
    from statsmodels.tsa.tsatools import _ar_invtransparams, _ma_invtransparams
    k,k_ar, k_ma = 0,2,2
    arcoefs = newparams[k:k+k_ar]
    macoefs = newparams[k+k_ar:]
    _ar_invtransparams(arcoefs)
    _ma_invtransparams(macoefs)
  4. jseabold commented on Apr 3, 2012

    @jseabold
    Member

    For the record, gretl and stata also choke on this data, though there's nothing really to indicate the solution is fishy, except the MA roots in gretl. So I'm not as worried about this.

    If you ask gretl to be noisy (show iteration details), you can see that all the MA parameters it tries are "out of bounds" until it gets to an MA solution with a root on the unit circle and it stops.

    I didn't explore too much in Stata but it arrives at the same "solution" we do if you set start_params = [0,0,0,0].

    So the question is, fail early and loudly, or not at all? I'm inclined to raise a warning that there were NaNs in the starting values and that we are reseting them all to zero. But the user should try this with different start values and reach their own conclusions. Maybe even put a note about this in the docs.

    Thoughts?

  5. josef-pkt commented on Apr 3, 2012

    @josef-pkt
    Member

    I was also trying out different starting values. I'm using a different example, and I also have problems if the mean is misspecified. But I haven't tried yet with a case where the model is correctly specified.

    I would switch to different starting values automatically in this case, but I'm not sure yet if or how we want to issue a warning. My impression is that a specification search, e.g. minimize BIC, should reject these cases.

    Also besides the roots we might want to have other checks on the estimated parameters, but haven't thought much about it yet. regression diagnostics for ARMA ?

  6. jseabold commented on Aug 22, 2012

    @jseabold
    Member

    Here's another one that fails, simpler test case

    inv = sm.datasets.macrodata.load().data['realinv']
    arima_mod = sm.tsa.ARIMA(np.log(inv), (1,1,2)).fit()
  7. added a commit that references this issue on May 1, 2013
    8d7514c
  8. added a commit that references this issue on Sep 2, 2014
    223b0d5
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