Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exponential kernel to Graph #761

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions libpysal/graph/_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -64,6 +69,7 @@ def _identity(distances, _):
"cosine": _cosine,
"boxcar": _boxcar,
"discrete": _boxcar,
"exponential": _exponential,
"identity": _identity,
None: _identity,
}
Expand Down Expand Up @@ -106,6 +112,7 @@ def _kernel(
- gaussian:
- bisquare:
- cosine:
- exponential:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should these all probably have formulas here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally

- 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
Expand Down
3 changes: 3 additions & 0 deletions libpysal/graph/tests/test_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down