Repository: statsmodels/statsmodels

Issue:
yule_walker (from `statsmodels.regression`) raises exception when given an integer array
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'
```



Discussion:
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.


Resolving patch:
diff --git a/statsmodels/regression/linear_model.py b/statsmodels/regression/linear_model.py
--- a/statsmodels/regression/linear_model.py
+++ b/statsmodels/regression/linear_model.py
@@ -891,7 +891,7 @@ def yule_walker(X, order=1, method="unbiased", df=None, inv=False, demean=True):
     method = str(method).lower()
     if method not in ["unbiased", "mle"]:
         raise ValueError("ACF estimation method must be 'unbiased' or 'MLE'")
-    X = np.array(X)
+    X = np.array(X, dtype=np.float64)
     if demean:
         X -= X.mean()                  # automatically demean's X
     n = df or X.shape[0]


Test patch:
diff --git a/statsmodels/tsa/tests/test_tsa_tools.py b/statsmodels/tsa/tests/test_tsa_tools.py
--- a/statsmodels/tsa/tests/test_tsa_tools.py
+++ b/statsmodels/tsa/tests/test_tsa_tools.py
@@ -50,6 +50,14 @@ def test_ywcoef():
     assert_array_almost_equal(mlywar.arcoef1000[1:],
                     -sm.regression.yule_walker(x1000, 20, method='mle')[0], 8)
 
+
+def test_yule_walker_inter():
+    # see 1869
+    x = np.array([1, -1, 2, 2, 0, -2, 1, 0, -3, 0, 0])
+    # it works
+    result = sm.regresion.yule_walker(x, 3)
+
+
 def test_duplication_matrix():
     for k in range(2, 10):
         m = tools.unvech(np.random.randn(k * (k + 1) / 2))