Skip to content

Commit

Permalink
Kernel tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sigvaldm committed May 15, 2019
1 parent 5282ed2 commit feefd48
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
4 changes: 2 additions & 2 deletions localreg/localreg.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ def biweight(t):
def triweight(t):
res = np.zeros_like(t)
ind = np.where(np.abs(t)<=1)
res[ind] = (35/32)*(1-t[ind]**2)**2
res[ind] = (35/32)*(1-t[ind]**2)**3
return res

def tricube(t):
res = np.zeros_like(t)
ind = np.where(np.abs(t)<=1)
res[ind] = (1-np.abs(t[ind])**3)**3
res[ind] = (70/81)*(1-np.abs(t[ind])**3)**3
return res

def gaussian(t):
Expand Down
87 changes: 85 additions & 2 deletions test/test_localreg.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,90 @@
"""

from localreg import *
import numpy as np
import pytest

def test_dummy():
return True
#
# PARAMETRIC KERNEL TESTS
#

all_kernels = [rectangular
,triangular
,epanechnikov
,biweight
,triweight
,tricube
,gaussian
,cosine
,logistic
,sigmoid
,silverman]

@pytest.mark.parametrize("kernel", [k for k in all_kernels if k!=silverman])
def test_nonnegative(kernel):
t = np.linspace(-5, 5, 100)
assert np.all(kernel(t)>=0)

@pytest.mark.parametrize("kernel", all_kernels)
def test_symmetry(kernel):
t = np.linspace(-5, 5, 100)
assert np.allclose(kernel(t), kernel(-t))

@pytest.mark.parametrize("kernel", all_kernels)
def test_normalized(kernel):
t = np.linspace(-20, 20, 5000)
dt = t[1]-t[0]
int = np.sum(kernel(t))*dt
assert int == pytest.approx(1, 1e-3)

#
# SIMPLE KERNEL TESTS
#

@pytest.fixture
def t():
return np.array([-1.5, -1, -0.5, 0, 0.5, 1, 1.5])

def test_rectangular(t):
assert np.allclose(rectangular(t),
np.array([0, 0.5, 0.5, 0.5, 0.5, 0.5, 0]))

def test_triangular(t):
assert np.allclose(triangular(t),
np.array([0, 0, 0.5, 1, 0.5, 0, 0]))

def test_epanechnikov(t):
assert np.allclose(epanechnikov(t),
0.75*np.array([0, 0, 0.75, 1, 0.75, 0, 0]))

def test_biweight(t):
assert np.allclose(biweight(t),
(15/16)*np.array([0, 0, 0.75**2, 1, 0.75**2, 0, 0]))

def test_triweight(t):
assert np.allclose(triweight(t),
(35/32)*np.array([0, 0, 0.75**3, 1, 0.75**3, 0, 0]))

def test_tricube(t):
assert np.allclose(tricube(t),
(70/81)*np.array([0, 0, 0.875**3, 1, 0.875**3, 0, 0]))

def test_gaussian(t):
assert np.allclose(gaussian(t),
(1/np.sqrt(2*np.pi))*np.array([np.exp(-1.125), np.exp(-0.5), np.exp(-0.125), 1, np.exp(-0.125), np.exp(-0.5), np.exp(-1.125)]))

def test_cosine(t):
assert np.allclose(cosine(t),
(np.pi/4)*np.array([0, 0, np.sqrt(2)/2, 1, np.sqrt(2)/2, 0, 0]))

def test_logistic(t):
assert np.allclose(logistic(t),
np.array([0.149146, 0.196612, 0.235004, 0.25, 0.235004, 0.196612, 0.149146]))

def test_sigmoid(t):
assert np.allclose(sigmoid(t),
(2/np.pi)*np.array([0.212548, 0.324027, 0.443409, 0.5, 0.443409, 0.324027, 0.212548]))

def test_silverman(t):
assert np.allclose(silverman(t),
0.5*np.array([0.333193, 0.491558, 0.637724, np.sqrt(2)/2, 0.637724, 0.491558, 0.333193]))

0 comments on commit feefd48

Please sign in to comment.