Skip to content

Commit

Permalink
Merge pull request #6515 from alexlyttle/fix/issue6511
Browse files Browse the repository at this point in the history
BUG: fix #6511
  • Loading branch information
bashtage committed Feb 15, 2020
2 parents 033d56d + 80727f9 commit 577d1ec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion statsmodels/nonparametric/bandwidths.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def select_bandwidth(x, bw, kernel):
if bw not in bandwidth_funcs:
raise ValueError("Bandwidth %s not understood" % bw)
bandwidth = bandwidth_funcs[bw](x, kernel)
if bandwidth == 0:
if np.any(bandwidth == 0):
# eventually this can fall back on another selection criterion.
err = "Selected KDE bandwidth is 0. Cannot estimate density."
raise RuntimeError(err)
Expand Down
23 changes: 23 additions & 0 deletions statsmodels/nonparametric/tests/test_bandwidths.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@


from numpy.testing import assert_allclose
import pytest

# setup test data

Expand Down Expand Up @@ -70,3 +71,25 @@ class TestTriweight(CheckNormalReferenceConstant):

kern = kernels.Triweight()
constant = 3.15


class BandwidthZero(object):

def test_bandwidth_zero(self):

kern = kernels.Gaussian()
for bw in ['scott', 'silverman', 'normal_reference']:
with pytest.raises(RuntimeError,
match="Selected KDE bandwidth is 0"):
select_bandwidth(self.xx, bw, kern)


class TestAllBandwidthZero(BandwidthZero):

xx = np.ones((100, 3))


class TestAnyBandwidthZero(BandwidthZero):

xx = np.random.normal(size=(100, 3))
xx[:, 0] = 1.0

0 comments on commit 577d1ec

Please sign in to comment.