Skip to content
Open
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
74abd07
Allow for normalize=True for gaussian kernel as pdf, False K(0)=1
sjsrey Jun 3, 2025
a46413a
bool not binary for type
sjsrey Jun 3, 2025
ca5b0c8
Add tests for normalize kw in Kernel
sjsrey Jun 3, 2025
335a76f
numpy testing
sjsrey Jun 3, 2025
2c0cdc5
Correct reference for kernel defs
sjsrey Jun 26, 2025
c18939e
prototyping new _kernels.py
sjsrey Jun 26, 2025
c53ee7c
triangular (#29)
knaaptime Jun 26, 2025
645edbc
move primative kernels out to libpysal._kernels.py
sjsrey Jul 8, 2025
37fe0f6
renaming kernels.py
sjsrey Jul 8, 2025
29d859c
populate self-weight with K(0)
sjsrey Jul 8, 2025
7824797
modify imports
sjsrey Jul 8, 2025
cd15d2a
Merge branch 'main' into exp/kernel
sjsrey Jul 8, 2025
c5f937e
roll back graph kernels
sjsrey Jul 8, 2025
6d38dd0
Add tests and illustrative nb
sjsrey Jul 9, 2025
46a7e86
Correct Gaussian kernel function
sjsrey Jul 24, 2025
a1b3353
Update tests for gaussian correction
sjsrey Jul 24, 2025
162e761
ruff format rules and remove test nbs
sjsrey Jul 24, 2025
f7a1329
ruff tests
sjsrey Jul 24, 2025
d7cdf5b
Relocate kernels from graph to libpysal for reuse and centralization
sjsrey Jul 24, 2025
0b9662c
move taper/decay logic out of graph module (#30)
ljwolf Aug 28, 2025
c9f3b44
replay levi's change to not remove self-weight (#32)
knaaptime Aug 28, 2025
afc686c
gaussian constant term
sjsrey Aug 28, 2025
16bee5b
update tests for trim
sjsrey Aug 28, 2025
54e613c
import proper kernel.py in graph
sjsrey Aug 28, 2025
26d0ea3
Add exclude_loops=True for kernels on graph
sjsrey Sep 9, 2025
9ad0af2
Fix gaussian kernel.
sjsrey Sep 11, 2025
92b53ec
The coplanar tests assume we don't taper. In main this could be a bug?
sjsrey Sep 11, 2025
b5231a6
clean up doc strings
sjsrey Sep 19, 2025
37bdfd2
update tests for correct Gaussian kernel
sjsrey Sep 19, 2025
9e437ca
update tests for gaussian kernel
sjsrey Sep 20, 2025
709098b
Do not decay boxcar kernel
sjsrey Sep 25, 2025
83dd562
Update reference for kernels
sjsrey Jun 26, 2025
a7c6223
cast boxcar kernel to float
sjsrey Oct 1, 2025
b909e66
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 9, 2025
f689835
formatting and import sorting
sjsrey Oct 9, 2025
6471872
skip tests if pyproj not available
sjsrey Oct 9, 2025
4306257
Spelling
sjsrey Oct 9, 2025
02ba6a8
np.in1d is deprecated, using np.isin instead
sjsrey Oct 9, 2025
a5fdb75
Don't assign to unused variable in pytest
sjsrey Oct 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 23 additions & 25 deletions libpysal/kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ def _gaussian(distances, bandwidth):
c = 1 / numpy.sqrt(2 * numpy.pi)
k = c * numpy.exp(exponent_term)
return k
#return numpy.where(z <= 1, k, 0)


def _bisquare(distances, bandwidth):
Expand Down Expand Up @@ -197,6 +196,7 @@ def _boxcar(distances, bandwidth):
ndarray
Binary weights: 1 if distance < bandwidth, else 0.
"""
distances = numpy.asarray(distances)
return (distances < bandwidth).astype(int)


Expand Down Expand Up @@ -233,9 +233,9 @@ def _identity(distances, _):
}


def kernel(distances, bandwidth, kernel="gaussian", taper=True,decay=False):
"""
Evaluate a kernel function over a distance array.
def kernel(distances, bandwidth, kernel="gaussian", taper=True,
decay=False):
"""Evaluate a kernel function over a distance array.

Parameters
----------
Expand All @@ -249,40 +249,38 @@ def kernel(distances, bandwidth, kernel="gaussian", taper=True,decay=False):
'cosine', 'boxcar', 'discrete', 'exponential', 'identity'.
If callable, it should have the signature `(distances, bandwidth)`.
If None, the 'identity' kernel is used.
decay: bool (default: False)
whether to calculate the kernel using the decay formulation. In the
decay form, a kernel measures the distance decay in similarity between
observations. It varies from from maximal similarity (1) at a distance
of zero to minimal similarity (0 or negative) at some very large
(possibly infinite) distance. Otherwise, kernel functions are treated as
proper volume-preserving probability distributions.
taper : bool (default: True)
Set kernel = 0 for all distances exceeding the bandwith. To
evaluate kernel beyond bandwith set taper=False.
decay : bool (default: False)
whether to calculate the kernel using the decay formulation.
In the decay form, a kernel measures the distance decay in
similarity between observations. It varies from from maximal
similarity (1) at a distance of zero to minimal similarity (0
or negative) at some very large (possibly infinite) distance.
Otherwise, kernel functions are treated as proper
volume-preserving probability distributions.

Returns
-------
ndarray
Kernel function evaluated at distance values.

Notes:
------
All kernels are tapered by default.
Some kernels ("gaussian","exponential") can be supported over all real values. In
contrast, many other kernels (e.g. "triangular", "parabolic"), are tapered
when distances are greater than the bandwidth.
"""

box = False
if callable(kernel):
func = kernel
elif kernel is None:
func = _kernel_functions[None]
else:
func = _kernel_functions[kernel.lower()]

func = _kernel_functions[kernel]
if kernel.lower() == 'boxcar':
box = True
k = func(distances, bandwidth)

if taper:
k[distances > bandwidth] = 0
k[distances > bandwidth] = 0.0

if decay and not box:
k /= func(0.0, bandwidth)

if decay:
k /= func(0, bandwidth)

return k
Loading