I am using Python's statsmodels module to plot a violin/bean plot of some data. I get the error "LinAlgError: singular matrix" in the KDE calculation whenever a single violin plot is drawn from a list of repeated instances of the same number. Regular boxplots work just fine.
Is this a bug or is there a reason behind this?
/home/me/myscript.py in plot_data_in_violinplot()
3435 plot_opts={'violin_fc':(0.8, 0.8, 0.8), 'cutoff':True,
'bean_color':'#FF6F00', 'bean_mean_color':'#009D91',
'bean_median_color':'b', 'bean_median_marker':'+'}
-> 3436 sm.graphics.beanplot(data_to_boxplot, ax=ax, jitter=jitter, plot_opts=plot_opts)
/usr/local/lib/python2.7/dist-packages/statsmodels/graphics/boxplots.py in beanplot(data, ax, labels, positions, side, jitter, plot_opts)
333 for pos_data, pos in zip(data, positions):
334 # Draw violins.
--> 335 xvals, violin = _single_violin(ax, pos, pos_data, width, side, plot_opts)
336
337 if jitter:
/usr/local/lib/python2.7/dist-packages/statsmodels/graphics/boxplots.py in _single_violin(ax, pos, pos_data, width, side, plot_opts)
170 pos_data = np.asarray(pos_data)
171 # Kernel density estimate for data at this position.
--> 172 kde = gaussian_kde(pos_data)
173
174 # Create violin for pos, scaled to the available space.
/usr/local/lib/python2.7/dist-packages/scipy/stats/kde.pyc in __init__(self, dataset, bw_method)
186
187 self.d, self.n = self.dataset.shape
--> 188 self.set_bandwidth(bw_method=bw_method)
189
190 def evaluate(self, points):
/usr/local/lib/python2.7/dist-packages/scipy/stats/kde.pyc in set_bandwidth(self, bw_method)
496 raise ValueError(msg)
497
--> 498 self._compute_covariance()
499
500 def _compute_covariance(self):
/usr/local/lib/python2.7/dist-packages/scipy/stats/kde.pyc in _compute_covariance(self)
507 self._data_covariance = atleast_2d(np.cov(self.dataset, rowvar=1,
508 bias=False))
--> 509 self._data_inv_cov = linalg.inv(self._data_covariance)
510
511 self.covariance = self._data_covariance * self.factor**2
/usr/local/lib/python2.7/dist-packages/scipy/linalg/basic.pyc in inv(a, overwrite_a, check_finite)
381 inv_a, info = getri(lu, piv, lwork=lwork, overwrite_lu=1)
382 if info > 0:
--> 383 raise LinAlgError("singular matrix")
384 if info < 0:
385 raise ValueError('illegal value in %d-th argument of internal '
LinAlgError: singular matrix