yule_walker (from `statsmodels.regression`) raises exception when given an integer array #1869

Closed
WarrenWeckesser opened this Issue Aug 4, 2014 · 1 comment

Projects

None yet

2 participants

@WarrenWeckesser

For example:

In [53]: x = np.array([1, -1, 2, 2, 0, -2, 1, 0, -3, 0, 0])

In [54]: yule_walker(x, 3)
---------------------------------------------------------------------------
LinAlgError                               Traceback (most recent call last)
<ipython-input-54-469606f4fbdf> in <module>()
----> 1 yule_walker(x, 3)

/Users/warren/local_scipy/lib/python2.7/site-packages/statsmodels-0.6.0-py2.7-macosx-10.5-x86_64.egg/statsmodels/regression/linear_model.pyc in yule_walker(X, order, method, df, inv, demean)
    906     R = toeplitz(r[:-1])
    907 
--> 908     rho = np.linalg.solve(R, r[1:])
    909     sigmasq = r[0] - (r[1:]*rho).sum()
    910     if inv==True:

/Users/warren/local_scipy/lib/python2.7/site-packages/numpy/linalg/linalg.pyc in solve(a, b)
    379     signature = 'DD->D' if isComplexType(t) else 'dd->d'
    380     extobj = get_linalg_error_extobj(_raise_linalgerror_singular)
--> 381     r = gufunc(a, b, signature=signature, extobj=extobj)
    382 
    383     return wrap(r.astype(result_t))

/Users/warren/local_scipy/lib/python2.7/site-packages/numpy/linalg/linalg.pyc in _raise_linalgerror_singular(err, flag)
     88 
     89 def _raise_linalgerror_singular(err, flag):
---> 90     raise LinAlgError("Singular matrix")
     91 
     92 def _raise_linalgerror_nonposdef(err, flag):

LinAlgError: Singular matrix

Convert to floating point, and it works fine:

In [55]: yule_walker(1.0*x, 3)
Out[55]: (array([ 0.06383878, -0.3374662 ,  0.35104014]), 1.2893596257735698)

The code should probably ensure that the input is floating point at the same time it ensures it is an array. Otherwise, even the following code that is supposed to demean the array will not work:

    if demean:
        X -= X.mean()

Version info:

In [59]: statsmodels.__version__
Out[59]: '0.6.0.dev-8709f00'

In [60]: np.__version__
Out[60]: '1.9.0b1'

In [61]: scipy.__version__
Out[61]: '0.15.0.dev-8741fe1'
@josef-pkt josef-pkt added this to the 0.6 milestone Aug 4, 2014
@josef-pkt
Member

at least float32, or at least float64 ? or only convert ints ?
we never decided on dtype requirements, and don't enforce any yet.

I think float64 is fine here until someone complains and then we can change it.

@jseabold jseabold closed this in 869cff3 Sep 20, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment