Jonnyishman February 2016
### scipy.stats.multivariate_normal raising `LinAlgError: singular matrix` even though my covariance matrix is invertible

I am having trouble trying to use `scipy.stats.multivariate_normal`

, hopefully one of you might be able to help.

I have a 2x2 matrix which is possible to find the inverse of using `numpy.linalg.inv()`

, however when I attempt to use it as the covariance matrix in `multivariate_normal`

I receive a `LinAlgError`

stating that it is a singular matrix:

```
In [89]: cov = np.array([[3.2e5**2, 3.2e5*0.103*-0.459],[3.2e5*0.103*-0.459, 0.103**2]])
In [90]: np.linalg.inv(cov)
Out[90]:
array([[ 1.23722158e-11, 1.76430200e-05],
[ 1.76430200e-05, 1.19418880e+02]])
In [91]: multivariate_normal([0,0], cov)
---------------------------------------------------------------------------
LinAlgError Traceback (most recent call last)
<ipython-input-91-44a6625beda5> in <module>()
----> 1 multivariate_normal([0,0], cov)
/mnt/ssd/Enthought_jli199/Canopy_64bit/User/lib/python2.7/site-packages/scipy/stats/_multivariate.pyc in __call__(self, mean, cov, allow_singular, seed)
421 return multivariate_normal_frozen(mean, cov,
422 allow_singular=allow_singular,
--> 423 seed=seed)
424
425 def _logpdf(self, x, mean, prec_U, log_det_cov, rank):
/mnt/ssd/Enthought_jli199/Canopy_64bit/User/lib/python2.7/site-packages/scipy/stats/_multivariate.pyc in __init__(self, mean, cov, allow_singular, seed)
591 """
592 self.dim, self.mean, self.cov = _process_parameters(None, mean, cov)
--> 593 self.cov_info = _PSD(self.cov, allow_singular=allow_singular)
594 self._dist = multivariate_normal_gen(seed)
595
/mnt/ssd/Enthought_jli199/Canopy_64bit/User/lib/python2.7/site-packages/scipy/stats/_multivariate.pyc in __init__(self, M, cond, rcond, lower, check_finite, allow_singular)
217 d = s[s > eps]
218 i
```

```
```

```
```

```
```### Answers

ali_m February 2016
By default `multivariate_normal`

checks whether any of the eigenvalues of the covariance matrix are less than some tolerance chosen based on its dtype and the magnitude of its largest eigenvalue (take a look at the source code for `scipy.stats._multivariate._PSD`

and `scipy.stats._multivariate._eigvalsh_to_eps`

for the full details).

As @kazemakase mentioned above, whilst your covariance matrix may be invertible according to the criteria used by `np.linalg.inv`

, it is still very ill-conditioned and fails the more stringent test used by `multivariate_normal`

.

You could pass `allow_singular=True`

to `multivariate_normal`

to skip this test, but in general it would be better to rescale your data to avoid passing such an ill-conditioned covariance matrix in the first place.

```
```#### Post Status

Asked in February 2016

Viewed 1,683 times

Voted 13

Answered 1 times
#### Search

## Leave an answer

```
```

```
```