diff --git a/libpysal/graph/_kernel.py b/libpysal/graph/_kernel.py index aa6dce0b5..47f0c8de2 100644 --- a/libpysal/graph/_kernel.py +++ b/libpysal/graph/_kernel.py @@ -47,6 +47,11 @@ def _cosine(distances, bandwidth): return (numpy.pi / 4) * numpy.cos(numpy.pi / 2 * u) +def _exponential(distances, bandwidth): + u = distances / bandwidth + return numpy.exp(-u) + + def _boxcar(distances, bandwidth): r = (distances < bandwidth).astype(int) return r @@ -64,6 +69,7 @@ def _identity(distances, _): "cosine": _cosine, "boxcar": _boxcar, "discrete": _boxcar, + "exponential": _exponential, "identity": _identity, None: _identity, } @@ -106,6 +112,7 @@ def _kernel( - gaussian: - bisquare: - cosine: + - exponential: - boxcar/discrete: all distances less than `bandwidth` are 1, and all other distances are 0 - identity/None : do nothing, weight similarity based on raw distance diff --git a/libpysal/graph/tests/test_kernel.py b/libpysal/graph/tests/test_kernel.py index 60b740066..1d97e2c18 100644 --- a/libpysal/graph/tests/test_kernel.py +++ b/libpysal/graph/tests/test_kernel.py @@ -224,6 +224,9 @@ def test_kernels(kernel, grocs): elif kernel in ["identity", None]: assert weight.mean() == pytest.approx(39758.007361814016) assert weight.max() == pytest.approx(127937.75271993055) + elif kernel in ["exponential", None]: + assert weight.mean() == pytest.approx(0.25104208195691335) + assert weight.max() == pytest.approx(0.9875261386315732) else: # function assert weight.mean() == pytest.approx(0.6880384553732511) assert weight.max() == pytest.approx(0.9855481738848647)