Repository: statsmodels/statsmodels

Issue:
Kernelreg and fit
The KernelReg works OK. See the example below:

index=range(4,100)
index1=[i for i in range(4,100)]
print(index1)

def f(x):
    return x*x

values=list(map(f,index1))
print(values)

#now regress value against 

kr = KernelReg(values, index_a, var_type='c', bw=[12.5,1.])

kr_pts = kr.fit([index])

But the fit function after the reg throws the following IndexError message:
 
Traceback (most recent call last):

  File "<ipython-input-139-673f466f234a>", line 1, in <module>
    kr_pts = kr.fit(index_a)

  File "/anaconda3/lib/python3.6/site-packages/statsmodels/nonparametric/kernel_regression.py", line 370, in fit
    data_predict=data_predict[i, :])

  File "/anaconda3/lib/python3.6/site-packages/statsmodels/nonparametric/kernel_regression.py", line 154, in _est_loc_linear
    tosum=False) / float(nobs)

  File "/anaconda3/lib/python3.6/site-packages/statsmodels/nonparametric/_kernel_base.py", line 513, in gpke
    dens = Kval.prod(axis=1) / np.prod(bw[iscontinuous])

IndexError: boolean index did not match indexed array along dimension 0; dimension is 2 but corresponding boolean dimension is 1

The function was working OK previously. For some reason, it is no longer working.






Discussion:
This sounds like a numpy incompatibility.

Which version of numpy and of statsmodels are you using? 
Most likely it is fixed in 0.9.0 or in master.
 Thanks so much for the reply.

Below is the version of statsmodels and numpy

print(statsmodels.__version__)0.9.0
print(numpy.version.version)1.14.3


    On Friday, August 10, 2018, 1:03:33 PM EDT, Josef Perktold <notifications@github.com> wrote:  
 
 
This sounds like a numpy incompatibility.

Which version of numpy and of statsmodels are you using?
Most likely it is fixed in 0.9.0 or in master.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
   
temporarily prio-high

I don't have time to check this right now, but it needs to be checked before the next release.
KernelReg unit test coverage is not as good as it is supposed to be. So I wouldn't rely on seeing a test failure if this is not yet fixed

Resolving patch:
diff --git a/statsmodels/nonparametric/kernel_regression.py b/statsmodels/nonparametric/kernel_regression.py
--- a/statsmodels/nonparametric/kernel_regression.py
+++ b/statsmodels/nonparametric/kernel_regression.py
@@ -42,7 +42,6 @@
     LeaveOneOut, _get_type_pos, _adjust_shape, _compute_min_std_IQR
 
 
-
 __all__ = ['KernelReg', 'KernelCensoredReg']
 
 
@@ -57,9 +56,9 @@ class KernelReg(GenericKDE):
 
     Parameters
     ----------
-    endog: list with one element which is array_like
+    endog: array-like
         This is the dependent variable.
-    exog: list
+    exog: array-like
         The training data for the independent variable(s)
         Each element in the list is a separate variable
     var_type: str
@@ -74,9 +73,10 @@ class KernelReg(GenericKDE):
         'll' local Linear estimator.  Default is 'll'
     bw: str or array_like, optional
         Either a user-specified bandwidth or the method for bandwidth
-        selection.  If a string, valid values are 'cv_ls' (least-squares
+        selection. If a string, valid values are 'cv_ls' (least-squares
         cross-validation) and 'aic' (AIC Hurvich bandwidth estimation).
-        Default is 'cv_ls'.
+        Default is 'cv_ls'. User specified bandwidth must have as many
+        entries as the number of variables.
     defaults: EstimatorSettings instance, optional
         The default values for the efficient bandwidth estimation.
 
@@ -98,6 +98,11 @@ def __init__(self, endog, exog, var_type, reg_type='ll', bw='cv_ls',
         self.bw_func = dict(cv_ls=self.cv_loo, aic=self.aic_hurvich)
         self.est = dict(lc=self._est_loc_constant, ll=self._est_loc_linear)
         self._set_defaults(defaults)
+        if not isinstance(bw, string_types):
+            bw = np.asarray(bw)
+            if len(bw) != self.k_vars:
+                raise ValueError('bw must have the same dimension as the '
+                                 'number of variables.')
         if not self.efficient:
             self.bw = self._compute_reg_bw(bw)
         else:


Test patch:
diff --git a/statsmodels/nonparametric/tests/test_kernel_regression.py b/statsmodels/nonparametric/tests/test_kernel_regression.py
--- a/statsmodels/nonparametric/tests/test_kernel_regression.py
+++ b/statsmodels/nonparametric/tests/test_kernel_regression.py
@@ -330,6 +330,14 @@ def test_censored_efficient_user_specificed_bw(self):
         npt.assert_equal(model.bw, bw_user)
 
 
+def test_invalid_bw():
+    # GH4873
+    x = np.arange(400)
+    y = x ** 2
+    with pytest.raises(ValueError):
+        nparam.KernelReg(x, y, 'c', bw=[12.5, 1.])
+
+
 if __name__ == "__main__":
     import pytest
     pytest.main([__file__, '-vvs', '-x', '--pdb'])