-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ISJ bandwidth computation now respects the weights (#71)
* removes redundant callable checks * adds weights to bandwidth API and implements them for ISJ * uses weights in bandwidth calculation * adds pycharm files to gitignore * remove unused code * add basic tests and fix for weights <= 0 * adds test for bandwidth weight being the same as resampling * force tests to be lowercase functions * add parameters to docstring Co-authored-by: Tommy <10076072+tommyod@users.noreply.github.com>
- Loading branch information
1 parent
698c8aa
commit 06408d3
Showing
8 changed files
with
79 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,3 +102,5 @@ ENV/ | |
# mypy | ||
.mypy_cache/ | ||
.pytest_cache/ | ||
|
||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Tests for the bandwidth selection. | ||
""" | ||
|
||
import pytest | ||
import numpy as np | ||
|
||
from KDEpy.bw_selection import _bw_methods, improved_sheather_jones | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def data() -> np.ndarray: | ||
return np.random.randn(100, 1) | ||
|
||
|
||
@pytest.mark.parametrize("method", _bw_methods.values()) | ||
def test_equal_weights_dont_changed_bw(data, method): | ||
weights = np.ones_like(data).squeeze() * 2 | ||
bw_no_weights = method(data, weights=None) | ||
bw_weighted = method(data, weights=weights) | ||
np.testing.assert_almost_equal(bw_no_weights, bw_weighted) | ||
|
||
|
||
def test_isj_bw_weights_single_zero_weighted_point(data): | ||
data_with_outlier = np.concatenate((data.copy(), np.array([[1000]]))) | ||
weights = np.ones_like(data_with_outlier).squeeze() | ||
weights[-1] = 0 | ||
|
||
np.testing.assert_array_almost_equal( | ||
improved_sheather_jones(data), | ||
improved_sheather_jones(data_with_outlier, weights), | ||
) | ||
|
||
|
||
# multiple runs to allow a good spread of catching errors | ||
@pytest.mark.parametrize("execution_number", range(5)) | ||
def test_isj_bw_weights_same_as_resampling(data, execution_number): | ||
sample_weights = np.random.randint(low=1, high=100, size=len(data)) | ||
data_resampled = np.repeat(data, repeats=sample_weights).reshape((-1, 1)) | ||
np.testing.assert_array_almost_equal( | ||
improved_sheather_jones(data_resampled), | ||
improved_sheather_jones(data, sample_weights), | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
# --durations=10 <- May be used to show potentially slow tests | ||
pytest.main(args=[__file__, "--doctest-modules", "-v", "--durations=15"]) |