Refactored kdetools.py into a subpackage. Simplified and removed obsolete code.

master
Per A Brodtkorb 8 years ago
parent 12414e46e2
commit 67e15d7c8c

File diff suppressed because it is too large Load Diff

@ -0,0 +1,3 @@
from .kdetools import * #@PydevCodeAnalysisIgnore
from .gridding import * #@PydevCodeAnalysisIgnore
from .kernels import * #@PydevCodeAnalysisIgnore

@ -0,0 +1,324 @@
'''
Created on 15. des. 2016
@author: pab
'''
from __future__ import division
from scipy import sparse
import numpy as np
from wafo.testing import test_docstrings
from itertools import product
__all__ = ['accum', 'gridcount']
def bitget(int_type, offset):
"""Returns the value of the bit at the offset position in int_type.
Example
-------
>>> bitget(5, np.r_[0:4])
array([1, 0, 1, 0])
"""
return np.bitwise_and(int_type, 1 << offset) >> offset
def accumsum(accmap, a, shape, dtype=None):
"""
Example
-------
>>> from numpy import array
>>> a = array([[1,2,3],[4,-1,6],[-1,8,9]])
>>> a
array([[ 1, 2, 3],
[ 4, -1, 6],
[-1, 8, 9]])
>>> # Sum the diagonals.
>>> accmap = array([[0,1,2],[2,0,1],[1,2,0]])
>>> s = accumsum(accmap, a, (3,)
>>> s
array([ 9, 7, 15])
"""
if dtype is None:
dtype = a.dtype
shape = np.atleast_1d(shape)
if len(shape) > 1:
binx = accmap[:, 0]
biny = accmap[:, 1]
out = sparse.coo_matrix(
(a.ravel(), (binx, biny)), shape=shape, dtype=dtype).tocsr()
else:
binx = accmap.ravel()
zero = np.zeros(len(binx))
out = sparse.coo_matrix(
(a.ravel(), (binx, zero)), shape=(shape, 1), dtype=dtype).tocsr()
return out
def accumsum2(accmap, a, shape):
"""
Example
-------
>>> from numpy import array
>>> a = array([[1,2,3],[4,-1,6],[-1,8,9]])
>>> a
array([[ 1, 2, 3],
[ 4, -1, 6],
[-1, 8, 9]])
>>> # Sum the diagonals.
>>> accmap = array([[0,1,2],[2,0,1],[1,2,0]])
>>> s = accumsum2(accmap, a, (3,)
>>> s
array([ 9, 7, 15])
"""
return np.bincount(accmap.ravel(), a.ravel(), np.array(shape).max())
def accum(accmap, a, func=None, size=None, fill_value=0, dtype=None):
"""An accumulation function similar to Matlab's `accumarray` function.
Parameters
----------
accmap : ndarray
This is the "accumulation map". It maps input (i.e. indices into
`a`) to their destination in the output array. The first `a.ndim`
dimensions of `accmap` must be the same as `a.shape`. That is,
`accmap.shape[:a.ndim]` must equal `a.shape`. For example, if `a`
has shape (15,4), then `accmap.shape[:2]` must equal (15,4). In this
case `accmap[i,j]` gives the index into the output array where
element (i,j) of `a` is to be accumulated. If the output is, say,
a 2D, then `accmap` must have shape (15,4,2). The value in the
last dimension give indices into the output array. If the output is
1D, then the shape of `accmap` can be either (15,4) or (15,4,1)
a : ndarray
The input data to be accumulated.
func : callable or None
The accumulation function. The function will be passed a list
of values from `a` to be accumulated.
If None, numpy.sum is assumed.
size : ndarray or None
The size of the output array. If None, the size will be determined
from `accmap`.
fill_value : scalar
The default value for elements of the output array.
dtype : numpy data type, or None
The data type of the output array. If None, the data type of
`a` is used.
Returns
-------
out : ndarray
The accumulated results.
The shape of `out` is `size` if `size` is given. Otherwise the
shape is determined by the (lexicographically) largest indices of
the output found in `accmap`.
Examples
--------
>>> from numpy import array, prod
>>> a = array([[1,2,3],[4,-1,6],[-1,8,9]])
>>> a
array([[ 1, 2, 3],
[ 4, -1, 6],
[-1, 8, 9]])
>>> # Sum the diagonals.
>>> accmap = array([[0,1,2],[2,0,1],[1,2,0]])
>>> s = accum(accmap, a)
>>> s
array([ 9, 7, 15])
>>> # A 2D output, from sub-arrays with shapes and positions like this:
>>> # [ (2,2) (2,1)]
>>> # [ (1,2) (1,1)]
>>> accmap = array([
... [[0,0],[0,0],[0,1]],
... [[0,0],[0,0],[0,1]],
... [[1,0],[1,0],[1,1]]])
>>> # Accumulate using a product.
>>> accum(accmap, a, func=prod, dtype=float)
array([[ -8., 18.],
[ -8., 9.]])
>>> # Same accmap, but create an array of lists of values.
>>> accum(accmap, a, func=lambda x: x, dtype='O')
array([[[1, 2, 4, -1], [3, 6]],
[[-1, 8], [9]]], dtype=object)
"""
def create_array_of_python_lists(accmap, a, size):
vals = np.empty(size, dtype='O')
for s in product(*[range(k) for k in size]):
vals[s] = []
for s in product(*[range(k) for k in a.shape]):
indx = tuple(accmap[s])
val = a[s]
vals[indx].append(val)
return vals
# Check for bad arguments and handle the defaults.
if accmap.shape[:a.ndim] != a.shape:
raise ValueError(
"The initial dimensions of accmap must be the same as a.shape")
if func is None:
func = np.sum
if dtype is None:
dtype = a.dtype
if accmap.shape == a.shape:
accmap = np.expand_dims(accmap, -1)
adims = tuple(range(a.ndim))
if size is None:
size = 1 + np.squeeze(np.apply_over_axes(np.max, accmap, axes=adims))
size = np.atleast_1d(size)
# Create an array of python lists of values.
vals = create_array_of_python_lists(accmap, a, size)
# Create the output array.
out = np.empty(size, dtype=dtype)
for s in np.product(*[range(k) for k in size]):
if vals[s] == []:
out[s] = fill_value
else:
out[s] = func(vals[s])
return out
def gridcount(data, X, y=1):
'''
Returns D-dimensional histogram using linear binning.
Parameters
----------
data = column vectors with D-dimensional data, shape D x Nd
X = row vectors defining discretization, shape D x N
Must include the range of the data.
Returns
-------
c = gridcount, shape N x N x ... x N
GRIDCOUNT obtains the grid counts using linear binning.
There are 2 strategies: simple- or linear- binning.
Suppose that an observation occurs at x and that the nearest point
below and above is y and z, respectively. Then simple binning strategy
assigns a unit weight to either y or z, whichever is closer. Linear
binning, on the other hand, assigns the grid point at y with the weight
of (z-x)/(z-y) and the gridpoint at z a weight of (y-x)/(z-y).
In terms of approximation error of using gridcounts as pdf-estimate,
linear binning is significantly more accurate than simple binning.
NOTE: The interval [min(X);max(X)] must include the range of the data.
The order of C is permuted in the same order as
meshgrid for D==2 or D==3.
Example
-------
>>> import numpy as np
>>> import wafo.kdetools as wk
>>> import pylab as plb
>>> N = 20
>>> data = np.random.rayleigh(1,N)
>>> data = np.array(
... [ 1.07855907, 1.51199717, 1.54382893, 1.54774808, 1.51913566,
... 1.11386486, 1.49146216, 1.51127214, 2.61287913, 0.94793051,
... 2.08532731, 1.35510641, 0.56759888, 1.55766981, 0.77883602,
... 0.9135759 , 0.81177855, 1.02111483, 1.76334202, 0.07571454])
>>> x = np.linspace(0,max(data)+1,50)
>>> dx = x[1]-x[0]
>>> c = wk.gridcount(data, x)
>>> np.allclose(c[:5], [ 0., 0.9731147, 0.0268853, 0., 0.])
True
>>> pdf = c/dx/N
>>> np.allclose(np.trapz(pdf, x), 1)
True
h = plb.plot(x,c,'.') # 1D histogram
h1 = plb.plot(x, pdf) # 1D probability density plot
See also
--------
bincount, accum, kdebin
Reference
----------
Wand,M.P. and Jones, M.C. (1995)
'Kernel smoothing'
Chapman and Hall, pp 182-192
'''
dat = np.atleast_2d(data)
x = np.atleast_2d(X)
y = np.atleast_1d(y).ravel()
d = dat.shape[0]
d1, inc = x.shape
if d != d1:
raise ValueError('Dimension 0 of data and X do not match.')
dx = np.diff(x[:, :2], axis=1)
xlo = x[:, 0]
xup = x[:, -1]
datlo = dat.min(axis=1)
datup = dat.max(axis=1)
if ((datlo < xlo) | (xup < datup)).any():
raise ValueError('X does not include whole range of the data!')
csiz = np.repeat(inc, d)
use_sparse = False
if use_sparse:
acfun = accumsum # faster than accum
else:
acfun = accumsum2 # accum
binx = np.asarray(np.floor((dat - xlo[:, np.newaxis]) / dx), dtype=int)
w = dx.prod()
if d == 1:
x.shape = (-1,)
c = np.asarray((acfun(binx, (x[binx + 1] - dat) * y, shape=(inc, )) +
acfun(binx + 1, (dat - x[binx]) * y, shape=(inc, ))) /
w).ravel()
else: # d>2
Nc = csiz.prod()
c = np.zeros((Nc,))
fact2 = np.asarray(np.reshape(inc * np.arange(d), (d, -1)), dtype=int)
fact1 = np.asarray(
np.reshape(csiz.cumprod() / inc, (d, -1)), dtype=int)
# fact1 = fact1(ones(n,1),:);
bt0 = [0, 0]
X1 = X.ravel()
for ir in range(2 ** (d - 1)):
bt0[0] = np.reshape(bitget(ir, np.arange(d)), (d, -1))
bt0[1] = 1 - bt0[0]
for ix in range(2):
one = np.mod(ix, 2)
two = np.mod(ix + 1, 2)
# Convert to linear index
# linear index to c
b1 = np.sum((binx + bt0[one]) * fact1, axis=0)
bt2 = bt0[two] + fact2
b2 = binx + bt2 # linear index to X
c += acfun(b1, np.abs(np.prod(X1[b2] - dat, axis=0)) * y,
shape=(Nc,))
c = np.reshape(c / w, csiz, order='F')
T = [i for i in range(d)]
T[1], T[0] = T[0], T[1]
# make sure c is stored in the same way as meshgrid
c = c.transpose(*T)
return c
if __name__ == '__main__':
test_docstrings(__file__)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -0,0 +1,18 @@
'''
Created on 15. des. 2016
@author: pab
'''
import inspect
def test_docstrings(name=''):
import doctest
if not name:
name = inspect.stack()[1][1]
print('Testing docstrings in {}'.format(name))
doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE |
doctest.ELLIPSIS)
if __name__ == '__main__':
pass

@ -3,398 +3,473 @@ Created on 20. nov. 2010
@author: pab
'''
import numpy as np # @UnusedImport
from numpy import array # @UnusedImport
import wafo.kdetools as wk # @UnusedImport
# import pylab as plb
def test0_KDE1D():
'''
>>> data = array([0.75355792, 0.72779194, 0.94149169, 0.07841119,
... 2.32291887, 1.10419995, 0.77055114, 0.60288273,
... 1.36883635, 1.74754326, 1.09547561, 1.01671133,
... 0.73211143, 0.61891719, 0.75903487, 1.8919469,
... 0.72433808, 1.92973094, 0.44749838, 1.36508452])
>>> x = np.linspace(0, max(data.ravel()) + 1, 10)
>>> import wafo.kdetools as wk
>>> kde = wk.KDE(data, hs=0.5, alpha=0.5)
>>> kde0 = wk.KDE(data, hs=0.5, alpha=0.0, inc=16)
>>> kde0.eval_grid(x)
array([ 0.2039735 , 0.40252503, 0.54595078, 0.52219649, 0.3906213 ,
0.26381501, 0.16407362, 0.08270612, 0.02991145, 0.00720821])
>>> kde0.eval_grid_fast(x)
array([ 0.20729484, 0.39865044, 0.53716945, 0.5169322 , 0.39060223,
0.26441126, 0.16388801, 0.08388527, 0.03227164, 0.00883579])
>>> f = kde0.eval_grid_fast(); f
array([ 0.06807544, 0.12949095, 0.21985421, 0.33178031, 0.44334874,
0.52429234, 0.55140336, 0.52221323, 0.45500674, 0.3752208 ,
0.30046799, 0.235667 , 0.17854402, 0.12721305, 0.08301993,
0.04862324])
>>> np.allclose(np.trapz(f,kde0.args), array([ 0.96716261]))
True
'''
def test1_TKDE1D():
'''
N = 20
data = np.random.rayleigh(1, size=(N,))
>>> data = array([0.75355792, 0.72779194, 0.94149169, 0.07841119,
... 2.32291887, 1.10419995, 0.77055114, 0.60288273,
... 1.36883635, 1.74754326, 1.09547561, 1.01671133,
... 0.73211143, 0.61891719, 0.75903487, 1.8919469,
... 0.72433808, 1.92973094, 0.44749838, 1.36508452])
>>> x = np.linspace(0.01, max(data.ravel()) + 1, 10)
>>> kde = wk.TKDE(data, hs=0.5, L2=0.5)
>>> f = kde(x)
>>> f
array([ 1.03982714, 0.45839018, 0.39514782, 0.32860602, 0.26433318,
0.20717946, 0.15907684, 0.1201074 , 0.08941027, 0.06574882])
>>> np.allclose(np.trapz(f, x), 0.94787730659349068)
True
h1 = plb.plot(x, f) # 1D probability density plot
'''
def test1_KDE1D():
'''
from __future__ import division
import unittest
import numpy as np
from numpy.testing import assert_allclose
from numpy import array, inf
import wafo.kdetools as wk
class TestKdeTools(unittest.TestCase):
def setUp(self):
# N = 20
# data = np.random.rayleigh(1, size=(N,))
self.data = array([0.75355792, 0.72779194, 0.94149169, 0.07841119,
2.32291887, 1.10419995, 0.77055114, 0.60288273,
1.36883635, 1.74754326, 1.09547561, 1.01671133,
0.73211143, 0.61891719, 0.75903487, 1.8919469,
0.72433808, 1.92973094, 0.44749838, 1.36508452])
self.x = np.linspace(0, max(self.data) + 1, 10)
def test0_KDE1D(self):
data, x = self.data, self.x
# kde = wk.KDE(data, hs=0.5, alpha=0.5)
kde0 = wk.KDE(data, hs=0.5, alpha=0.0, inc=16)
fx = kde0.eval_grid(x)
assert_allclose(fx, [0.2039735, 0.40252503, 0.54595078,
0.52219649, 0.3906213, 0.26381501, 0.16407362,
0.08270612, 0.02991145, 0.00720821])
fx = kde0.eval_grid(x, r=1)
assert_allclose(-fx, [0.11911419724002906, 0.13440000694772541,
0.044400116190638696, -0.0677695267531197,
-0.09555596523854318, -0.07498819087690148,
-0.06167607128369182, -0.04678588231996062,
-0.024515979196411814, -0.008022010381009501])
fx = kde0.eval_grid(x, r=2)
assert_allclose(fx, [0.08728138131197069, 0.07558648034784508,
0.05093715852686607, 0.07908624791267539,
0.10495675573359599, 0.07916167222333347,
0.048168330179460386, 0.03438361415806721,
0.02197927811015286, 0.009222988165160621])
ffx = kde0.eval_grid_fast(x)
assert_allclose(ffx, [0.20729484, 0.39865044, 0.53716945, 0.5169322,
0.39060223, 0.26441126, 0.16388801, 0.08388527,
0.03227164, 0.00883579], 1e-6)
fx = kde0.eval_grid_fast(x, r=1)
assert_allclose(fx, [-0.11582450668441863, -0.12901768780183628,
-0.04402464127812092, 0.0636190549560749,
0.09345144501310157, 0.07573621607126926,
0.06149475587201987, 0.04550210608639078,
0.024427027615689087, 0.00885576504750473])
fx = kde0.eval_grid_fast(x, r=2)
assert_allclose(fx, [0.08499284131672676, 0.07572564161758065,
0.05329987919556978, 0.07849796347259348,
0.10232741197885842, 0.07869015379158453,
0.049431823916945394, 0.034527256372343613,
0.021517998409663567, 0.009527401063843402])
f = kde0.eval_grid_fast()
assert_allclose(np.trapz(f, kde0.args), 0.995001)
assert_allclose(f, [0.011494108953097538, 0.0348546729842836,
0.08799292403553607, 0.18568717590587996,
0.32473136104523725, 0.46543163412700084,
0.5453201564089711, 0.5300582814373698,
0.44447650672207173, 0.3411961246641896,
0.25103852230993573, 0.17549519961525845,
0.11072988772879173, 0.05992730870218242,
0.02687783924833738, 0.00974982785617795])
def skiptest0_KDEgauss_1D(self):
data, x = self.data, self.x
# kde = wk.KDE(data, hs=0.5, alpha=0.5)
kde0 = wk.KDEgauss(data, hs=0.5, alpha=0.0, inc=16)
fx = kde0.eval_grid(x)
assert_allclose(fx, [0.2039735, 0.40252503, 0.54595078,
0.52219649, 0.3906213, 0.26381501, 0.16407362,
0.08270612, 0.02991145, 0.00720821])
fx = kde0.eval_grid(x, r=1)
assert_allclose(-fx, [0.11911419724002906, 0.13440000694772541,
0.044400116190638696, -0.0677695267531197,
-0.09555596523854318, -0.07498819087690148,
-0.06167607128369182, -0.04678588231996062,
-0.024515979196411814, -0.008022010381009501])
fx = kde0.eval_grid(x, r=2)
assert_allclose(fx, [0.08728138131197069, 0.07558648034784508,
0.05093715852686607, 0.07908624791267539,
0.10495675573359599, 0.07916167222333347,
0.048168330179460386, 0.03438361415806721,
0.02197927811015286, 0.009222988165160621])
ffx = kde0.eval_grid_fast(x)
# print(ffx.tolist())
assert_allclose(ffx, [0.20729484, 0.39865044, 0.53716945, 0.5169322,
0.39060223, 0.26441126, 0.16388801, 0.08388527,
0.03227164, 0.00883579], 1e-6)
fx = kde0.eval_grid_fast(x, r=1)
assert_allclose(fx, [-0.11582450668441863, -0.12901768780183628,
-0.04402464127812092, 0.0636190549560749,
0.09345144501310157, 0.07573621607126926,
0.06149475587201987, 0.04550210608639078,
0.024427027615689087, 0.00885576504750473])
fx = kde0.eval_grid_fast(x, r=2)
assert_allclose(fx, [0.08499284131672676, 0.07572564161758065,
0.05329987919556978, 0.07849796347259348,
0.10232741197885842, 0.07869015379158453,
0.049431823916945394, 0.034527256372343613,
0.021517998409663567, 0.009527401063843402])
f = kde0.eval_grid_fast()
assert_allclose(f, [0.06807544, 0.12949095, 0.21985421, 0.33178031,
0.44334874, 0.52429234, 0.55140336, 0.52221323,
0.45500674, 0.3752208, 0.30046799, 0.235667,
0.17854402, 0.12721305, 0.08301993, 0.04862324])
assert_allclose(np.trapz(f, kde0.args), 0.96716261)
def test1_TKDE1D(self):
data = self.data
x = np.linspace(0.01, max(data) + 1, 10)
kde = wk.TKDE(data, hs=0.5, L2=0.5)
f = kde(x)
assert_allclose(f, [1.03982714, 0.45839018, 0.39514782, 0.32860602,
0.26433318, 0.20717946, 0.15907684, 0.1201074,
0.08941027, 0.06574882])
assert_allclose(np.trapz(f, x), 0.94787730659349068)
f = kde.eval_grid_fast(x)
assert_allclose(f, [1.0401892415290148, 0.45838973393693677,
0.39514689240671547, 0.32860531818532457,
0.2643330110605783, 0.20717975528556506,
0.15907696844388747, 0.12010770443337843,
0.08941129458260941, 0.06574899139165799])
f = kde.eval_grid_fast2(x)
assert_allclose(f, [1.0401892415290148, 0.45838973393693677,
0.39514689240671547, 0.32860531818532457,
0.2643330110605783, 0.20717975528556506,
0.15907696844388747, 0.12010770443337843,
0.08941129458260941, 0.06574899139165799])
assert_allclose(np.trapz(f, x), 0.9479438058416647)
def test1_KDE1D(self):
data, x = self.data, self.x
kde = wk.KDE(data, hs=0.5)
f = kde(x)
assert_allclose(f, [0.2039735, 0.40252503, 0.54595078, 0.52219649,
0.3906213, 0.26381501, 0.16407362, 0.08270612,
0.02991145, 0.00720821])
assert_allclose(np.trapz(f, x), 0.92576174424281876)
def test2_KDE1D(self):
# data, x = self.data, self.x
data = np.asarray([1, 2])
x = np.linspace(0, max(np.ravel(data)) + 1, 10)
kde = wk.KDE(data, hs=0.5)
f = kde(x)
assert_allclose(f, [0.0541248, 0.16555235, 0.33084399, 0.45293325,
0.48345808, 0.48345808, 0.45293325, 0.33084399,
0.16555235, 0.0541248])
assert_allclose(np.trapz(f, x), 0.97323338046725172)
def test1a_KDE1D(self):
data, x = self.data, self.x
kde = wk.KDE(data, hs=0.5, alpha=0.5)
f = kde(x)
assert_allclose(f, [0.17252055, 0.41014271, 0.61349072, 0.57023834,
0.37198073, 0.21409279, 0.12738463, 0.07460326,
0.03956191, 0.01887164])
assert_allclose(np.trapz(f, x), 0.92938023659047952)
def test2a_KDE1D(self):
# data, x = self.data, self.x
data = np.asarray([1, 2])
x = np.linspace(0, max(np.ravel(data)) + 1, 10)
kde = wk.KDE(data, hs=0.5, alpha=0.5)
f = kde(x)
assert_allclose(f, [0.0541248, 0.16555235, 0.33084399, 0.45293325,
0.48345808, 0.48345808, 0.45293325, 0.33084399,
0.16555235, 0.0541248])
assert_allclose(np.trapz(f, x), 0.97323338046725172)
def test_KDE2D(self):
# N = 20
# data = np.random.rayleigh(1, size=(2, N))
data = array([
[0.38103275, 0.35083136, 0.90024207, 1.88230239, 0.96815399,
0.57392873, 1.63367908, 1.20944125, 2.03887811, 0.81789145,
0.69302049, 1.40856592, 0.92156032, 2.14791432, 2.04373821,
0.69800708, 0.58428735, 1.59128776, 2.05771405, 0.87021964],
[1.44080694, 0.39973751, 1.331243, 2.48895822, 1.18894158,
1.40526085, 1.01967897, 0.81196474, 1.37978932, 2.03334689,
0.870329, 1.25106862, 0.5346619, 0.47541236, 1.51930093,
0.58861519, 1.19780448, 0.81548296, 1.56859488, 1.60653533]])
x = np.linspace(0, max(np.ravel(data)) + 1, 3)
kde0 = wk.KDE(data, hs=0.5, alpha=0.0, inc=512)
assert_allclose(kde0.eval_grid(x, x),
[[3.27260963e-02, 4.21654678e-02, 5.85338634e-04],
[6.78845466e-02, 1.42195839e-01, 1.41676003e-03],
[1.39466746e-04, 4.26983850e-03, 2.52736185e-05]])
t = [[0.0443506097653615, 0.06433530873456418, 0.0041353838654317856],
[0.07218297149063724, 0.1235819591878892, 0.009288890372002473],
[0.001613328022214066, 0.00794857884864038, 0.0005874786787715641]
]
assert_allclose(kde0.eval_grid_fast(x, x), t)
def test_gridcount_1D(self):
data, x = self.data, self.x
dx = x[1] - x[0]
c = wk.gridcount(data, x)
assert_allclose(c, [0.78762626, 1.77520717, 7.99190087, 4.04054449,
1.67156643, 2.38228499, 1.05933195, 0.29153785, 0.,
0.])
t = np.trapz(c / dx / len(data), x)
assert_allclose(t, 0.9803093435140049)
def test_gridcount_2D(self):
N = 20
data = np.random.rayleigh(1, size=(N,))
>>> data = array([0.75355792, 0.72779194, 0.94149169, 0.07841119,
... 2.32291887, 1.10419995, 0.77055114, 0.60288273,
... 1.36883635, 1.74754326, 1.09547561, 1.01671133,
... 0.73211143, 0.61891719, 0.75903487, 1.8919469,
... 0.72433808, 1.92973094, 0.44749838, 1.36508452])
>>> x = np.linspace(0, max(data.ravel()) + 1, 10)
>>> kde = wk.KDE(data, hs=0.5)
>>> f = kde(x)
>>> np.allclose(f, [ 0.2039735 , 0.40252503, 0.54595078, 0.52219649,
... 0.3906213, 0.26381501, 0.16407362, 0.08270612, 0.02991145,
... 0.00720821])
True
>>> np.allclose(np.trapz(f, x), 0.92576174424281876)
True
h1 = plb.plot(x, f) # 1D probability density plot
'''
def test2_KDE1D():
'''
# data = np.random.rayleigh(1, size=(2, N))
data = array([
[0.38103275, 0.35083136, 0.90024207, 1.88230239, 0.96815399,
0.57392873, 1.63367908, 1.20944125, 2.03887811, 0.81789145,
0.69302049, 1.40856592, 0.92156032, 2.14791432, 2.04373821,
0.69800708, 0.58428735, 1.59128776, 2.05771405, 0.87021964],
[1.44080694, 0.39973751, 1.331243, 2.48895822, 1.18894158,
1.40526085, 1.01967897, 0.81196474, 1.37978932, 2.03334689,
0.870329, 1.25106862, 0.5346619, 0.47541236, 1.51930093,
0.58861519, 1.19780448, 0.81548296, 1.56859488, 1.60653533]])
x = np.linspace(0, max(np.ravel(data)) + 1, 5)
dx = x[1] - x[0]
X = np.vstack((x, x))
c = wk.gridcount(data, X)
assert_allclose(c,
[[0.38922806, 0.8987982, 0.34676493, 0.21042807, 0.],
[1.15012203, 5.16513541, 3.19250588, 0.55420752, 0.],
[0.74293418, 3.42517219, 1.97923195, 0.76076621, 0.],
[0.02063536, 0.31054405, 0.71865964, 0.13486633, 0.],
[0., 0., 0., 0., 0.]], 1e-5)
t = np.trapz(np.trapz(c / (dx**2 * N), x), x)
assert_allclose(t, 0.9011618785736376)
def test_gridcount_3D(self):
N = 20
data = np.random.rayleigh(1, size=(N,))
>>> data = array([ 0.75355792, 0.72779194, 0.94149169, 0.07841119,
... 2.32291887, 1.10419995, 0.77055114, 0.60288273, 1.36883635,
... 1.74754326, 1.09547561, 1.01671133, 0.73211143, 0.61891719,
... 0.75903487, 1.8919469, 0.72433808, 1.92973094, 0.44749838,
... 1.36508452])
>>> data = np.asarray([1,2])
>>> x = np.linspace(0, max(data.ravel()) + 1, 10)
>>> kde = wk.KDE(data, hs=0.5)
>>> f = kde(x)
>>> np.allclose(f,
... [ 0.0541248 , 0.16555235, 0.33084399, 0.45293325, 0.48345808,
... 0.48345808, 0.45293325, 0.33084399, 0.16555235, 0.0541248 ])
True
>>> np.allclose(np.trapz(f, x), 0.97323338046725172)
True
h1 = plb.plot(x, f) # 1D probability density plot
'''
def test1a_KDE1D():
'''
N = 20
data = np.random.rayleigh(1, size=(N,))
>>> data = array([
... 0.75355792, 0.72779194, 0.94149169, 0.07841119, 2.32291887,
... 1.10419995, 0.77055114, 0.60288273, 1.36883635, 1.74754326,
... 1.09547561, 1.01671133, 0.73211143, 0.61891719, 0.75903487,
... 1.8919469 , 0.72433808, 1.92973094, 0.44749838, 1.36508452])
>>> x = np.linspace(0, max(data.ravel()) + 1, 10)
>>> kde = wk.KDE(data, hs=0.5, alpha=0.5)
>>> f = kde(x)
>>> np.allclose(f,
... [ 0.17252055, 0.41014271, 0.61349072, 0.57023834, 0.37198073,
... 0.21409279, 0.12738463, 0.07460326, 0.03956191, 0.01887164])
True
>>> np.allclose(np.trapz(f, x), 0.92938023659047952)
True
h1 = plb.plot(x, f) # 1D probability density plot
'''
def test2a_KDE1D():
'''
N = 20
data = np.random.rayleigh(1, size=(N,))
>>> data = array([
... 0.75355792, 0.72779194, 0.94149169, 0.07841119, 2.32291887,
... 1.10419995, 0.77055114, 0.60288273, 1.36883635, 1.74754326,
... 1.09547561, 1.01671133, 0.73211143, 0.61891719, 0.75903487,
... 1.8919469 , 0.72433808, 1.92973094, 0.44749838, 1.36508452])
>>> data = np.asarray([1,2])
>>> x = np.linspace(0, max(data.ravel()) + 1, 10)
>>> kde = wk.KDE(data, hs=0.5, alpha=0.5)
>>> f = kde(x)
>>> np.allclose(f,
... [ 0.0541248 , 0.16555235, 0.33084399, 0.45293325, 0.48345808,
... 0.48345808, 0.45293325, 0.33084399, 0.16555235, 0.0541248 ])
True
>>> np.allclose(np.trapz(f, x), 0.97323338046725172)
True
h1 = plb.plot(x, f) # 1D probability density plot
'''
def test_KDE2D():
'''
N = 20
data = np.random.rayleigh(1, size=(2, N))
>>> data = array([[
... 0.38103275, 0.35083136, 0.90024207, 1.88230239, 0.96815399,
... 0.57392873, 1.63367908, 1.20944125, 2.03887811, 0.81789145,
... 0.69302049, 1.40856592, 0.92156032, 2.14791432, 2.04373821,
... 0.69800708, 0.58428735, 1.59128776, 2.05771405, 0.87021964],
... [1.44080694, 0.39973751, 1.331243 , 2.48895822, 1.18894158,
... 1.40526085, 1.01967897, 0.81196474, 1.37978932, 2.03334689,
... 0.870329 , 1.25106862, 0.5346619 , 0.47541236, 1.51930093,
... 0.58861519, 1.19780448, 0.81548296, 1.56859488, 1.60653533]])
>>> x = np.linspace(0, max(data.ravel()) + 1, 3)
>>> kde = wk.KDE(data, hs=0.5, alpha=0.5)
>>> kde0 = wk.KDE(data, hs=0.5, alpha=0.0, inc=16)
>>> np.allclose(kde0.eval_grid(x, x),
... [[ 3.27260963e-02, 4.21654678e-02, 5.85338634e-04],
... [ 6.78845466e-02, 1.42195839e-01, 1.41676003e-03],
... [ 1.39466746e-04, 4.26983850e-03, 2.52736185e-05]])
True
>>> np.allclose(kde0.eval_grid_fast(x, x),
... [[ 0.04435061, 0.06433531, 0.00413538],
... [ 0.07218297, 0.12358196, 0.00928889],
... [ 0.00161333, 0.00794858, 0.00058748]])
True
'''
def test_smooth_params():
'''
>>> data = np.array([[
... 0.932896 , 0.89522635, 0.80636346, 1.32283371, 0.27125435,
... 1.91666304, 2.30736635, 1.13662384, 1.73071287, 1.06061127,
... 0.99598512, 2.16396591, 1.23458213, 1.12406686, 1.16930431,
... 0.73700592, 1.21135139, 0.46671506, 1.3530304 , 0.91419104],
... [ 0.62759088, 0.23988169, 2.04909823, 0.93766571, 1.19343762,
... 1.94954931, 0.84687514, 0.49284897, 1.05066204, 1.89088505,
... 0.840738 , 1.02901457, 1.0758625 , 1.76357967, 0.45792897,
... 1.54488066, 0.17644313, 1.6798871 , 0.72583514, 2.22087245],
... [ 1.69496432, 0.81791905, 0.82534709, 0.71642389, 0.89294732,
... 1.66888649, 0.69036947, 0.99961448, 0.30657267, 0.98798713,
... 0.83298728, 1.83334948, 1.90144186, 1.25781913, 0.07122458,
... 2.42340852, 2.41342037, 0.87233305, 1.17537114, 1.69505988]])
>>> gauss = wk.Kernel('gaussian')
>>> gauss.hns(data)
array([ 0.18154437, 0.36207987, 0.37396219])
>>> gauss.hos(data)
array([ 0.195209 , 0.3893332 , 0.40210988])
>>> gauss.hmns(data)
array([[ 3.25196193e-01, -2.68892467e-02, 3.18932448e-04],
[ -2.68892467e-02, 3.91283306e-01, 2.38654678e-02],
[ 3.18932448e-04, 2.38654678e-02, 4.05123874e-01]])
>>> gauss.hscv(data)
array([ 0.16858959, 0.32739383, 0.3046287 ])
>>> gauss.hstt(data)
array([ 0.18099075, 0.50409881, 0.11018912])
>>> gauss.hste(data)
array([ 0.16750009, 0.29059113, 0.17994255])
>>> gauss.hldpi(data)
array([ 0.1732289 , 0.33159097, 0.3107633 ])
>>> np.allclose(gauss.hisj(data),
... array([ 0.29542502, 0.74277133, 0.51899114]))
True
'''
def test_gridcount_1D():
'''
N = 20
data = np.random.rayleigh(1, size=(N,))
>>> data = array([
... 0.75355792, 0.72779194, 0.94149169, 0.07841119, 2.32291887,
... 1.10419995, 0.77055114, 0.60288273, 1.36883635, 1.74754326,
... 1.09547561, 1.01671133, 0.73211143, 0.61891719, 0.75903487,
... 1.8919469 , 0.72433808, 1.92973094, 0.44749838, 1.36508452])
>>> x = np.linspace(0, max(data.ravel()) + 1, 10)
>>> dx = x[1] - x[0]
>>> c = wk.gridcount(data, x)
>>> np.allclose(c,
... [ 0.78762626, 1.77520717, 7.99190087, 4.04054449, 1.67156643,
... 2.38228499, 1.05933195, 0.29153785, 0. , 0. ])
True
h = plb.plot(x, c, '.') # 1D histogram
h1 = plb.plot(x, c / dx / N) # 1D probability density plot
t = np.trapz(c / dx / N, x)
print(t)
'''
def test_gridcount_2D():
'''
N = 20
data = np.random.rayleigh(1, size=(2, N))
>>> data = array([[
... 0.38103275, 0.35083136, 0.90024207, 1.88230239, 0.96815399,
... 0.57392873, 1.63367908, 1.20944125, 2.03887811, 0.81789145,
... 0.69302049, 1.40856592, 0.92156032, 2.14791432, 2.04373821,
... 0.69800708, 0.58428735, 1.59128776, 2.05771405, 0.87021964],
... [ 1.44080694, 0.39973751, 1.331243 , 2.48895822, 1.18894158,
... 1.40526085, 1.01967897, 0.81196474, 1.37978932, 2.03334689,
... 0.870329 , 1.25106862, 0.5346619 , 0.47541236, 1.51930093,
... 0.58861519, 1.19780448, 0.81548296, 1.56859488, 1.60653533]])
>>> x = np.linspace(0, max(data.ravel()) + 1, 5)
>>> dx = x[1] - x[0]
>>> X = np.vstack((x, x))
>>> c = wk.gridcount(data, X)
>>> np.allclose(c,
... [[ 0.38922806, 0.8987982 , 0.34676493, 0.21042807, 0. ],
... [ 1.15012203, 5.16513541, 3.19250588, 0.55420752, 0. ],
... [ 0.74293418, 3.42517219, 1.97923195, 0.76076621, 0. ],
... [ 0.02063536, 0.31054405, 0.71865964, 0.13486633, 0. ],
... [ 0. , 0. , 0. , 0. , 0. ]])
True
h = plb.plot(x, c, '.') # 1D histogram
h1 = plb.plot(x, c / dx / N) # 1D probability density plot
t = np.trapz(c / dx / N, x)
print(t)
'''
def test_gridcount_3D():
'''
N = 20
data = np.random.rayleigh(1, size=(3, N))
>>> data = np.array([[
... 0.932896 , 0.89522635, 0.80636346, 1.32283371, 0.27125435,
... 1.91666304, 2.30736635, 1.13662384, 1.73071287, 1.06061127,
... 0.99598512, 2.16396591, 1.23458213, 1.12406686, 1.16930431,
... 0.73700592, 1.21135139, 0.46671506, 1.3530304 , 0.91419104],
... [ 0.62759088, 0.23988169, 2.04909823, 0.93766571, 1.19343762,
... 1.94954931, 0.84687514, 0.49284897, 1.05066204, 1.89088505,
... 0.840738 , 1.02901457, 1.0758625 , 1.76357967, 0.45792897,
... 1.54488066, 0.17644313, 1.6798871 , 0.72583514, 2.22087245],
... [ 1.69496432, 0.81791905, 0.82534709, 0.71642389, 0.89294732,
... 1.66888649, 0.69036947, 0.99961448, 0.30657267, 0.98798713,
... 0.83298728, 1.83334948, 1.90144186, 1.25781913, 0.07122458,
... 2.42340852, 2.41342037, 0.87233305, 1.17537114, 1.69505988]])
>>> x = np.linspace(0, max(data.ravel()) + 1, 3)
>>> dx = x[1] - x[0]
>>> X = np.vstack((x, x, x))
>>> c = wk.gridcount(data, X)
>>> np.allclose(c,
... [[[ 8.74229894e-01, 1.27910940e+00, 1.42033973e-01],
... [ 1.94778915e+00, 2.59536282e+00, 3.28213680e-01],
... [ 1.08429416e-01, 1.69571495e-01, 7.48896775e-03]],
... [[ 1.44969128e+00, 2.58396370e+00, 2.45459949e-01],
... [ 2.28951650e+00, 4.49653348e+00, 2.73167915e-01],
... [ 1.10905565e-01, 3.18733817e-01, 1.12880816e-02]],
... [[ 7.49265424e-02, 2.18142488e-01, 0.00000000e+00],
... [ 8.53886762e-02, 3.73415131e-01, 0.00000000e+00],
... [ 4.16196568e-04, 1.62218824e-02, 0.00000000e+00]]])
True
'''
def test_gridcount_4D():
'''
# data = np.random.rayleigh(1, size=(3, N))
data = np.array([
[0.932896, 0.89522635, 0.80636346, 1.32283371, 0.27125435,
1.91666304, 2.30736635, 1.13662384, 1.73071287, 1.06061127,
0.99598512, 2.16396591, 1.23458213, 1.12406686, 1.16930431,
0.73700592, 1.21135139, 0.46671506, 1.3530304, 0.91419104],
[0.62759088, 0.23988169, 2.04909823, 0.93766571, 1.19343762,
1.94954931, 0.84687514, 0.49284897, 1.05066204, 1.89088505,
0.840738, 1.02901457, 1.0758625, 1.76357967, 0.45792897,
1.54488066, 0.17644313, 1.6798871, 0.72583514, 2.22087245],
[1.69496432, 0.81791905, 0.82534709, 0.71642389, 0.89294732,
1.66888649, 0.69036947, 0.99961448, 0.30657267, 0.98798713,
0.83298728, 1.83334948, 1.90144186, 1.25781913, 0.07122458,
2.42340852, 2.41342037, 0.87233305, 1.17537114, 1.69505988]])
x = np.linspace(0, max(np.ravel(data)) + 1, 3)
dx = x[1] - x[0]
X = np.vstack((x, x, x))
c = wk.gridcount(data, X)
assert_allclose(c,
[[[8.74229894e-01, 1.27910940e+00, 1.42033973e-01],
[1.94778915e+00, 2.59536282e+00, 3.28213680e-01],
[1.08429416e-01, 1.69571495e-01, 7.48896775e-03]],
[[1.44969128e+00, 2.58396370e+00, 2.45459949e-01],
[2.28951650e+00, 4.49653348e+00, 2.73167915e-01],
[1.10905565e-01, 3.18733817e-01, 1.12880816e-02]],
[[7.49265424e-02, 2.18142488e-01, 0.0],
[8.53886762e-02, 3.73415131e-01, 0.0],
[4.16196568e-04, 1.62218824e-02, 0.0]]])
t = np.trapz(np.trapz(np.trapz(c / dx**3 / N, x), x), x)
assert_allclose(t, 0.5164999727560187)
def test_gridcount_4D(self):
N = 20
data = np.random.rayleigh(1, size=(2, N))
>>> data = array([[
... 0.38103275, 0.35083136, 0.90024207, 1.88230239, 0.96815399,
... 0.57392873, 1.63367908, 1.20944125, 2.03887811, 0.81789145],
... [ 0.69302049, 1.40856592, 0.92156032, 2.14791432, 2.04373821,
... 0.69800708, 0.58428735, 1.59128776, 2.05771405, 0.87021964],
... [ 1.44080694, 0.39973751, 1.331243 , 2.48895822, 1.18894158,
... 1.40526085, 1.01967897, 0.81196474, 1.37978932, 2.03334689],
... [ 0.870329 , 1.25106862, 0.5346619 , 0.47541236, 1.51930093,
... 0.58861519, 1.19780448, 0.81548296, 1.56859488, 1.60653533]])
>>> x = np.linspace(0, max(data.ravel()) + 1, 3)
>>> dx = x[1] - x[0]
>>> X = np.vstack((x, x, x, x))
>>> c = wk.gridcount(data, X)
>>> np.allclose(c,
... [[[[ 1.77163904e-01, 1.87720108e-01, 0.00000000e+00],
... [ 5.72573585e-01, 6.09557834e-01, 0.00000000e+00],
... [ 3.48549923e-03, 4.05931870e-02, 0.00000000e+00]],
... [[ 1.83770124e-01, 2.56357594e-01, 0.00000000e+00],
... [ 4.35845892e-01, 6.14958970e-01, 0.00000000e+00],
... [ 3.07662204e-03, 3.58312786e-02, 0.00000000e+00]],
... [[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
... [ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
... [ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00]]],
... [[[ 3.41883175e-01, 5.97977973e-01, 0.00000000e+00],
... [ 5.72071865e-01, 8.58566538e-01, 0.00000000e+00],
... [ 3.46939323e-03, 4.04056116e-02, 0.00000000e+00]],
... [[ 3.58861043e-01, 6.28962785e-01, 0.00000000e+00],
... [ 8.80697705e-01, 1.47373158e+00, 0.00000000e+00],
... [ 2.22868504e-01, 1.18008528e-01, 0.00000000e+00]],
... [[ 2.91835067e-03, 2.60268355e-02, 0.00000000e+00],
... [ 3.63686503e-02, 1.07959459e-01, 0.00000000e+00],
... [ 1.88555613e-02, 7.06358976e-03, 0.00000000e+00]]],
... [[[ 3.13810608e-03, 2.11731327e-02, 0.00000000e+00],
... [ 6.71606255e-03, 4.53139824e-02, 0.00000000e+00],
... [ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00]],
... [[ 7.05946179e-03, 5.44614852e-02, 0.00000000e+00],
... [ 1.09099593e-01, 1.95935584e-01, 0.00000000e+00],
... [ 6.61257395e-02, 2.47717418e-02, 0.00000000e+00]],
... [[ 6.38695629e-04, 5.69610302e-03, 0.00000000e+00],
... [ 1.00358265e-02, 2.44053065e-02, 0.00000000e+00],
... [ 5.67244468e-03, 2.12498697e-03, 0.00000000e+00]]]])
True
h = plb.plot(x, c, '.') # 1D histogram
h1 = plb.plot(x, c / dx / N) # 1D probability density plot
t = np.trapz(x, c / dx / N)
print(t)
'''
def test_docstrings():
import doctest
print('Testing docstrings in %s' % __file__)
doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE)
if __name__ == '__main__':
test_docstrings()
# data = np.random.rayleigh(1, size=(2, N))
data = array([
[0.38103275, 0.35083136, 0.90024207, 1.88230239, 0.96815399,
0.57392873, 1.63367908, 1.20944125, 2.03887811, 0.81789145],
[0.69302049, 1.40856592, 0.92156032, 2.14791432, 2.04373821,
0.69800708, 0.58428735, 1.59128776, 2.05771405, 0.87021964],
[1.44080694, 0.39973751, 1.331243, 2.48895822, 1.18894158,
1.40526085, 1.01967897, 0.81196474, 1.37978932, 2.03334689],
[0.870329, 1.25106862, 0.5346619, 0.47541236, 1.51930093,
0.58861519, 1.19780448, 0.81548296, 1.56859488, 1.60653533]])
x = np.linspace(0, max(np.ravel(data)) + 1, 3)
dx = x[1] - x[0]
X = np.vstack((x, x, x, x))
c = wk.gridcount(data, X)
assert_allclose(c,
[[[[1.77163904e-01, 1.87720108e-01, 0.0],
[5.72573585e-01, 6.09557834e-01, 0.0],
[3.48549923e-03, 4.05931870e-02, 0.0]],
[[1.83770124e-01, 2.56357594e-01, 0.0],
[4.35845892e-01, 6.14958970e-01, 0.0],
[3.07662204e-03, 3.58312786e-02, 0.0]],
[[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0]]],
[[[3.41883175e-01, 5.97977973e-01, 0.0],
[5.72071865e-01, 8.58566538e-01, 0.0],
[3.46939323e-03, 4.04056116e-02, 0.0]],
[[3.58861043e-01, 6.28962785e-01, 0.0],
[8.80697705e-01, 1.47373158e+00, 0.0],
[2.22868504e-01, 1.18008528e-01, 0.0]],
[[2.91835067e-03, 2.60268355e-02, 0.0],
[3.63686503e-02, 1.07959459e-01, 0.0],
[1.88555613e-02, 7.06358976e-03, 0.0]]],
[[[3.13810608e-03, 2.11731327e-02, 0.0],
[6.71606255e-03, 4.53139824e-02, 0.0],
[0.0, 0.0, 0.0]],
[[7.05946179e-03, 5.44614852e-02, 0.0],
[1.09099593e-01, 1.95935584e-01, 0.0],
[6.61257395e-02, 2.47717418e-02, 0.0]],
[[6.38695629e-04, 5.69610302e-03, 0.0],
[1.00358265e-02, 2.44053065e-02, 0.0],
[5.67244468e-03, 2.12498697e-03, 0.0]]]])
t = np.trapz(np.trapz(np.trapz(np.trapz(c / dx**4 / N, x), x), x), x)
assert_allclose(t, 0.21183518274521254)
class TestKernels(unittest.TestCase):
def setUp(self):
self.names = ['epanechnikov', 'biweight', 'triweight', 'logistic',
'p1epanechnikov', 'p1biweight', 'p1triweight',
'triangular', 'gaussian', 'rectangular', 'laplace']
def test_stats(self):
truth = {
'biweight': (0.14285714285714285, 0.7142857142857143, 22.5),
'logistic': (3.289868133696453, 1./6, 0.023809523809523808),
'p1biweight': (0.14285714285714285, 0.7142857142857143, 22.5),
'triangular': (0.16666666666666666, 0.6666666666666666, inf),
'gaussian': (1, 0.28209479177387814, 0.21157109383040862),
'epanechnikov': (0.2, 0.6, inf),
'triweight': (0.1111111111111111, 0.8158508158508159, inf),
'p1triweight': (0.1111111111111111, 0.8158508158508159, inf),
'p1epanechnikov': (0.2, 0.6, inf),
'rectangular': (0.3333333333333333, 0.5, inf),
'laplace': (2, 0.25, inf)}
for name in self.names:
kernel = wk.Kernel(name)
assert_allclose(kernel.stats(), truth[name])
# truth[name] = kernel.stats()
# print(truth)
def test_norm_factors_1d(self):
truth = {
'biweight': 1.0666666666666667, 'logistic': 1.0,
'p1biweight': 1.0666666666666667, 'triangular': 1.0,
'gaussian': 2.5066282746310002, 'epanechnikov': 1.3333333333333333,
'triweight': 0.91428571428571426, 'laplace': 2,
'p1triweight': 0.91428571428571426,
'p1epanechnikov': 1.3333333333333333, 'rectangular': 2.0}
for name in self.names:
kernel = wk.Kernel(name)
assert_allclose(kernel.norm_factor(d=1, n=20), truth[name])
# truth[name] = kernel.norm_factor(d=1, n=20)
def test_effective_support(self):
truth = {'biweight': (-1.0, 1.0), 'logistic': (-7.0, 7.0),
'p1biweight': (-1.0, 1.0), 'triangular': (-1.0, 1.0),
'gaussian': (-4.0, 4.0), 'epanechnikov': (-1.0, 1.0),
'triweight': (-1.0, 1.0), 'p1triweight': (-1.0, 1.0),
'p1epanechnikov': (-1.0, 1.0), 'rectangular': (-1.0, 1.0),
'laplace': (-7.0, 7.0)}
for name in self.names:
kernel = wk.Kernel(name)
assert_allclose(kernel.effective_support(), truth[name])
# truth[name] = kernel.effective_support()
# print(truth)
# self.assertTrue(False)
def test_that_kernel_is_a_pdf(self):
for name in self.names:
kernel = wk.Kernel(name)
xmin, xmax = kernel.effective_support()
x = np.linspace(xmin, xmax, 4*1024+1)
m0 = kernel.norm_factor(d=1, n=1)
pdf = kernel(x)/m0
# print(name)
# print(pdf[0], pdf[-1])
# print(np.trapz(pdf, x) - 1)
assert_allclose(np.trapz(pdf, x), 1, 1e-2)
# self.assertTrue(False)
class TestSmoothing(unittest.TestCase):
def setUp(self):
self.data = np.array([
[0.932896, 0.89522635, 0.80636346, 1.32283371, 0.27125435,
1.91666304, 2.30736635, 1.13662384, 1.73071287, 1.06061127,
0.99598512, 2.16396591, 1.23458213, 1.12406686, 1.16930431,
0.73700592, 1.21135139, 0.46671506, 1.3530304, 0.91419104],
[0.62759088, 0.23988169, 2.04909823, 0.93766571, 1.19343762,
1.94954931, 0.84687514, 0.49284897, 1.05066204, 1.89088505,
0.840738, 1.02901457, 1.0758625, 1.76357967, 0.45792897,
1.54488066, 0.17644313, 1.6798871, 0.72583514, 2.22087245],
[1.69496432, 0.81791905, 0.82534709, 0.71642389, 0.89294732,
1.66888649, 0.69036947, 0.99961448, 0.30657267, 0.98798713,
0.83298728, 1.83334948, 1.90144186, 1.25781913, 0.07122458,
2.42340852, 2.41342037, 0.87233305, 1.17537114, 1.69505988]])
self.gauss = wk.Kernel('gaussian')
def test_hns(self):
hs = self.gauss.hns(self.data)
assert_allclose(hs, [0.18154437, 0.36207987, 0.37396219])
def test_hos(self):
hs = self.gauss.hos(self.data)
assert_allclose(hs, [0.195209, 0.3893332, 0.40210988])
def test_hms(self):
hs = self.gauss.hmns(self.data)
assert_allclose(hs, [[3.25196193e-01, -2.68892467e-02, 3.18932448e-04],
[-2.68892467e-02, 3.91283306e-01, 2.38654678e-02],
[3.18932448e-04, 2.38654678e-02, 4.05123874e-01]])
def test_hscv(self):
hs = self.gauss.hscv(self.data)
assert_allclose(hs, [0.16858959, 0.32739383, 0.3046287])
def test_hstt(self):
hs = self.gauss.hstt(self.data)
assert_allclose(hs, [0.18099075, 0.50409881, 0.11018912])
def test_hste(self):
hs = self.gauss.hste(self.data)
assert_allclose(hs, [0.16750009, 0.29059113, 0.17994255])
def test_hldpi(self):
hs = self.gauss.hldpi(self.data)
assert_allclose(hs, [0.1732289, 0.33159097, 0.3107633])
def test_hisj(self):
hs = self.gauss.hisj(self.data)
assert_allclose(hs, [0.29542502, 0.74277133, 0.51899114])
if __name__ == "__main__":
# import sys;sys.argv = ['', 'Test.testName']
unittest.main()

Loading…
Cancel
Save