tommyod / KDEpy

Kernel Density Estimation in Python
https://kdepy.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
584 stars 90 forks source link

Can `bw_selection.py` return a value when root finding did not converge? #133

Closed Kaiyangshi-Ito closed 1 year ago

Kaiyangshi-Ito commented 1 year ago

When ISJ bandwidth's root finding does not converge, can it return bandwidth selection using another method (e.g. Silverman) automatically just with a warning message? Or can it use some other root finding method? I think currently it uses brent method, but if the feasible set it uses doesn't contain a root, this should be the source of the problem. I encountered this problem quite often ever since upgrading to 1.1.1, though on a different data. So I'm not sure whether it's due to my data or due to change of implementation.

  File "KDEpy/FFTKDE.py", line 104, in fit
    super().fit(data, weights)
  File "KDEpy/BaseKDE.py", line 131, in fit
    self.bw = self.bw_method(self.data, self.weights)
  File "KDEpy/bw_selection.py", line 188, in improved_sheather_jones
    t_star = _root(_fixed_point, N, args=(N, I_sq, a2))
  File "KDEpy/bw_selection.py", line 120, in _root
    raise ValueError("Root finding did not converge. Need more data.")
ValueError: Root finding did not converge. Need more data.
tommyod commented 1 year ago

I've chosen to be explicit about failures rather than implicit. If I remember correctly version 1.1.1 introduces no changes in ISJ code, so the upgrade should not cause it.

Try:

I won't be making any changes to make these errors more "silent", since I personally prefer to be explicit when things fail. The user can always wrap the code in try..except to silence it :)

Also, do you have relatively little data? If so, that might also be the cause of it.