Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LinAlgError: singular matrix with data having all mass at 0 for some steps #14

Closed
macks22 opened this issue Jan 12, 2018 · 2 comments
Closed

Comments

@macks22
Copy link

macks22 commented Jan 12, 2018

I get a LinAlgError: singular matrix when attempting to plot a joyplot on data that has groups with all zeros (i.e. all mass at exactly 0). It seems this is an issue from the direct pass-through to the kde estimator and ultimately comes from trying to take the inverse of a singular covariance matrix. While one could argue that is reasonable expected behavior, I feel a better default in this situation would be to plot a vertical line at x = 0, y = 1 (no tails). You can reproduce the described error via:

from joypy import joyplot
df = pd.DataFrame({
    'value': np.concatenate([np.random.uniform(0, 1, size=100), np.random.uniform(0.5, 1, size=100), np.zeros(100)]),
    'time': np.concatenate([np.zeros(100), np.ones(100), np.ones(100) * 2]).astype(int)
})
joyplot(df, by='time', columns=['value'])

Versions:

python==3.6.1
joypy==0.1.7
scipy==1.0.0
numpy==1.13.3
matplotlib=2.1.1

The exact error message is:

---------------------------------------------------------------------------
LinAlgError                               Traceback (most recent call last)
<ipython-input-14-35fa7743dda5> in <module>()
----> 1 joyplot(df, by='time', columns=['value'])

/Users/mack/anaconda2/envs/py3k/lib/python3.6/site-packages/joypy/joyplot.py in joyplot(data, column, by, grid, xlabelsize, xrot, ylabelsize, yrot, ax, figsize, hist, bins, fade, ylim, fill, linecolor, overlap, background, labels, xlabels, ylabels, range_style, x_range, title, colormap, **kwds)
    233                     title=title,
    234                     colormap=colormap,
--> 235                     **kwds)
    236
    237 ###########################################

/Users/mack/anaconda2/envs/py3k/lib/python3.6/site-packages/joypy/joyplot.py in _joyplot(data, grid, labels, sublabels, xlabels, xlabelsize, xrot, ylabelsize, yrot, ax, figsize, hist, bins, fade, xlim, ylim, fill, linecolor, overlap, background, range_style, x_range, tails, title, legend, loc, colormap, color, **kwargs)
    437                              fill=fill, linecolor=linecolor, label=sublabel,
    438                              zorder=element_zorder, color=element_color,
--> 439                              bins=bins, **kwargs)
    440
    441

/Users/mack/anaconda2/envs/py3k/lib/python3.6/site-packages/joypy/joyplot.py in plot_density(ax, x_range, v, kind, bw_method, bins, fill, linecolor, clip_on, **kwargs)
    248
    249     if kind == "kde":
--> 250         gkde = gaussian_kde(v, bw_method=bw_method)
    251         y = gkde.evaluate(x_range)
    252     elif kind == "counts":

/Users/mack/anaconda2/envs/py3k/lib/python3.6/site-packages/scipy/stats/kde.py in __init__(self, dataset, bw_method)
    170
    171         self.d, self.n = self.dataset.shape
--> 172         self.set_bandwidth(bw_method=bw_method)
    173
    174     def evaluate(self, points):

/Users/mack/anaconda2/envs/py3k/lib/python3.6/site-packages/scipy/stats/kde.py in set_bandwidth(self, bw_method)
    497             raise ValueError(msg)
    498
--> 499         self._compute_covariance()
    500
    501     def _compute_covariance(self):

/Users/mack/anaconda2/envs/py3k/lib/python3.6/site-packages/scipy/stats/kde.py in _compute_covariance(self)
    508             self._data_covariance = atleast_2d(np.cov(self.dataset, rowvar=1,
    509                                                bias=False))
--> 510             self._data_inv_cov = linalg.inv(self._data_covariance)
    511
    512         self.covariance = self._data_covariance * self.factor**2

/Users/mack/anaconda2/envs/py3k/lib/python3.6/site-packages/scipy/linalg/basic.py in inv(a, overwrite_a, check_finite)
    974         inv_a, info = getri(lu, piv, lwork=lwork, overwrite_lu=1)
    975     if info > 0:
--> 976         raise LinAlgError("singular matrix")
    977     if info < 0:
    978         raise ValueError('illegal value in %d-th argument of internal '

LinAlgError: singular matrix
@leotac
Copy link
Owner

leotac commented Jan 16, 2018

I feel a better default in this situation would be to plot a vertical line at x = 0, y = 1 (no tails).

Sounds reasonable. Feel free to open a PR if you want.

@albacrespi
Copy link

I have the same problem. I added some noise to my matrix but still gives the same error. What can I try to plot by groups?

@leotac leotac closed this as completed in d2a6322 Sep 7, 2019
leotac added a commit that referenced this issue Sep 7, 2019
Handle data groups with infinite prob density. Fix #14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants