Skip to content

Commit

Permalink
Fix finite support bug due to changes in scipy.special.factorial2 (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyod authored Jul 12, 2023
1 parent 1fac7f0 commit 2de3b75
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
24 changes: 21 additions & 3 deletions KDEpy/kernel_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,17 @@ def gauss_integral(n):
True
"""
factor = np.sqrt(np.pi * 2)
if n % 2 == 0:

# Special case:
# factorial2(-1) used to return 1
# but now it returns 0
# https://github.com/scipy/scipy/pull/15841
# https://github.com/tommyod/KDEpy/issues/145
if n == 0:
return factor * factorial2(n - 0) / 2

# Normal cases
elif n % 2 == 0:
return factor * factorial2(n - 1) / 2
elif n % 2 == 1:
return factor * norm.pdf(0) * factorial2(n - 1)
Expand Down Expand Up @@ -264,6 +274,14 @@ def practical_support(self, bw, atol=10e-5):
"""
Return the support for practical purposes. Used to find a support value
for computations for kernel functions without finite (bounded) support.
Examples
--------
>>> kernel = Kernel(gaussian, var=1, support=np.inf)
>>> kernel.practical_support(bw=1)
3.8994...
>>> kernel.practical_support(bw=2)
7.4331...
"""
# If the kernel has finite support, return the support accounting for
# the bw
Expand Down Expand Up @@ -353,8 +371,8 @@ def evaluate(self, x, bw=1, norm=2):
}


if __name__ == "__main__" and False:
if __name__ == "__main__":
import pytest

# --durations=10 <- May be used to show potentially slow tests
pytest.main(args=[".", "--doctest-modules", "-v"])
pytest.main(args=[__file__, "--doctest-modules", "-v"])
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "KDEpy"
version = "1.1.3"
version = "1.1.4"
dependencies = [
"numpy>=1.14.2",
"scipy>=1.0.1",
Expand Down

0 comments on commit 2de3b75

Please sign in to comment.