-
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.
Added several kernel functions and tests
- Loading branch information
Showing
11 changed files
with
614 additions
and
41 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
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 was deleted.
Oops, something went wrong.
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,56 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Tests. | ||
""" | ||
import numpy as np | ||
from KDEpy import KDE | ||
import pytest | ||
|
||
|
||
class TestNaiveKDE(): | ||
|
||
@pytest.mark.parametrize("kernel, bw, n, expected_result", | ||
[('box', 0.1, 5, np.array([2.101278e-19, | ||
3.469447e-18, | ||
1.924501e+00, | ||
0.000000e+00, | ||
9.622504e-01])), | ||
('box', 0.2, 5, np.array([3.854941e-18, | ||
2.929755e-17, | ||
9.622504e-01, | ||
0.000000e+00, | ||
4.811252e-01])), | ||
('box', 0.6, 3, np.array([0.1603751, | ||
0.4811252, | ||
0.4811252])), | ||
('tri', 0.6, 3, np.array([0.1298519, | ||
0.5098009, | ||
0.3865535])), | ||
('epa', 0.1, 6, np.array([0.000000e+00, | ||
7.285839e-17, | ||
2.251871e-01, | ||
1.119926e+00, | ||
0.000000e+00, | ||
1.118034e+00])), | ||
('biweight', 2, 5, np.array([0.1524078, | ||
0.1655184, | ||
0.1729870, | ||
0.1743973, | ||
0.1696706]))]) | ||
def test_against_R_density(self, kernel, bw, n, expected_result): | ||
""" | ||
Test against the following function call in R: | ||
d <- density(c(0, 0.1, 1), kernel="{kernel}", bw={bw}, | ||
n={n}, from=-1, to=1); | ||
d$y | ||
""" | ||
data = np.array([0, 0.1, 1]) | ||
x = np.linspace(-1, 1, num=n) | ||
y = KDE(kernel, bw=bw).fit(data).evaluate_naive(x) | ||
assert np.allclose(y, expected_result, atol=10**(-2.7)) | ||
|
||
|
||
if __name__ == "__main__": | ||
# --durations=10 <- May be used to show potentially slow tests | ||
pytest.main(args=['.', '--doctest-modules', '-v']) |
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,70 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Tests. | ||
""" | ||
import numpy as np | ||
from scipy.integrate import quad | ||
from KDEpy import KDE | ||
import pytest | ||
|
||
|
||
class TestKernelFunctions(): | ||
|
||
@pytest.mark.parametrize("fname, function", | ||
list(KDE._available_kernels.items())) | ||
def test_integral_unity(self, fname, function): | ||
""" | ||
Verify that all available kernel functions have an integral evaluating | ||
to unity. This is a requirement of the kernel functions. | ||
""" | ||
|
||
if function.finite_support: | ||
# When bw=1, the function is scaled by the standard deviation | ||
# so that std(f) = 1. Divide by standard deviation to get | ||
# the integration limits. | ||
a, b = tuple(s / np.sqrt(function.var) for s in function.support) | ||
else: | ||
a, b = -30, 30 | ||
integral, abserr = quad(function, a=a, b=b) | ||
assert np.isclose(integral, 1) | ||
|
||
@pytest.mark.parametrize("fname, function", | ||
list(KDE._available_kernels.items())) | ||
def test_monotonic_decreasing(self, fname, function): | ||
""" | ||
Verify that all available kernel functions decrease away from their | ||
mode. | ||
""" | ||
|
||
if function.finite_support: | ||
a, b = tuple(s / np.sqrt(function.var) for s in function.support) | ||
x = np.linspace(a, b) | ||
else: | ||
x = np.linspace(-50, 50) | ||
y = function(x) | ||
diffs_left = np.diff(y[x <= 0]) | ||
diffs_right = np.diff(y[x >= 0]) | ||
assert np.all(diffs_right <= 0) | ||
assert np.all(diffs_left >= 0) | ||
|
||
@pytest.mark.parametrize("fname, function", | ||
list(KDE._available_kernels.items())) | ||
def test_non_negative(self, fname, function): | ||
""" | ||
Verify that all available kernel functions are non-negative. | ||
""" | ||
|
||
if function.finite_support: | ||
a, b = tuple(s / np.sqrt(function.var) for s in function.support) | ||
x = np.linspace(a, b) | ||
else: | ||
x = np.linspace(-20, 20) | ||
y = function(x) | ||
assert np.all(y >= 0) | ||
|
||
|
||
if __name__ == "__main__": | ||
import pytest | ||
# --durations=10 <- May be used to show potentially slow tests | ||
pytest.main(args=['.', '--doctest-modules', '-v']) |
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,3 @@ | ||
.prompt { | ||
display: none; | ||
} |
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 |
---|---|---|
|
@@ -52,6 +52,9 @@ Doctest example: | |
:maxdepth: 2 | ||
:caption: Contents: | ||
|
||
intro_kde | ||
notebook.ipynb | ||
|
||
|
||
|
||
|
||
|
Oops, something went wrong.