Fixed a bug in kde.eval_grid_fast + updated tests

master
Per.Andreas.Brodtkorb 14 years ago
parent 767cae0ed8
commit c22d63b93f

@ -119,8 +119,8 @@ class TKDE(object):
0.20717946, 0.15907684, 0.1201074 , 0.08941027, 0.06574882]) 0.20717946, 0.15907684, 0.1201074 , 0.08941027, 0.06574882])
>>> kde.eval_grid_fast(x) >>> kde.eval_grid_fast(x)
array([ 0. , 1.16200356, 0.99256178, 0.81930973, 0.65479862, array([ 0. , 0.4614821 , 0.39554839, 0.32764086, 0.26275681,
0.51021576, 0.3896221 , 0.29266142, 0. , 0. ]) 0.20543731, 0.15741056, 0.11863464, 0. , 0. ])
import pylab as plb import pylab as plb
h1 = plb.plot(x, f) # 1D probability density plot h1 = plb.plot(x, f) # 1D probability density plot
@ -377,8 +377,8 @@ class KDE(object):
>>> f = kde0.eval_grid_fast() >>> f = kde0.eval_grid_fast()
>>> np.interp(x, kde0.args[0], f) >>> np.interp(x, kde0.args[0], f)
array([ 0.54344 , 1.04793706, 1.38047458, 1.28324876, 0.943131 , array([ 0.21165996, 0.41218257, 0.54961961, 0.51713209, 0.38292245,
0.63570524, 0.39404219, 0.19450807, 0.08505344, 0.08505344]) 0.25864661, 0.16113184, 0.08055992, 0.03576856, 0.03576856])
import pylab as plb import pylab as plb
h1 = plb.plot(x, f) # 1D probability density plot h1 = plb.plot(x, f) # 1D probability density plot
@ -473,6 +473,7 @@ class KDE(object):
self.args = args self.args = args
return self._eval_grid_fast(*args) return self._eval_grid_fast(*args)
def _eval_grid_fast(self, *args): def _eval_grid_fast(self, *args):
# TODO: This does not work correctly yet! Check it.
X = np.vstack(args) X = np.vstack(args)
d, inc = X.shape d, inc = X.shape
dx = X[:,1]-X[:,0] dx = X[:,1]-X[:,0]
@ -480,9 +481,9 @@ class KDE(object):
Xn = [] Xn = []
nfft0 = 2*inc nfft0 = 2*inc
nfft = (nfft0,)*d nfft = (nfft0,)*d
x0 = np.linspace(-inc, inc, nfft0) x0 = np.linspace(-inc, inc, nfft0+1)
for i in range(d): for i in range(d):
Xn.append(x0*dx[i]) Xn.append(x0[:-1]*dx[i])
Xnc = meshgrid(*Xn) if d>1 else Xn Xnc = meshgrid(*Xn) if d>1 else Xn
@ -493,7 +494,7 @@ class KDE(object):
Xn = np.dot(self.inv_hs, np.vstack(Xnc)) Xn = np.dot(self.inv_hs, np.vstack(Xnc))
# Obtain the kernel weights. # Obtain the kernel weights.
kw = self.kernel(Xn)/self._norm_factor kw = self.kernel(Xn)/(self._norm_factor * self.kernel.norm_factor(d, self.n))
kw.shape = shape0 kw.shape = shape0
kw = np.fft.ifftshift(kw) kw = np.fft.ifftshift(kw)
fftn = np.fft.fftn fftn = np.fft.fftn
@ -1302,11 +1303,12 @@ def bitget(int_type, offset):
Example Example
------- -------
>>> bitget(5, np.r_[0:4]) >>> bitget(5, np.r_[0:4])
array([1, 0, 1, 0]) array([1, 0, 1, 0])
''' '''
mask = (1 << offset)
return (int_type & mask) != 0 return np.bitwise_and(int_type, 1 << offset) >> offset
def gridcount(data, X): def gridcount(data, X):
''' '''

@ -9,6 +9,31 @@ from numpy import array
import wafo.kdetools as wk import wafo.kdetools as wk
import pylab as plb 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])
>>> f = kde0.eval_grid_fast(); f
array([ 0.07264948, 0.14135253, 0.24141397, 0.36045498, 0.46962192,
0.53604004, 0.5427015 , 0.49767387, 0.42419428, 0.34349993,
0.26650289, 0.19666903, 0.13569857, 0.0857818 , 0.04868357,
0.02432961])
>>> np.trapz(f,kde0.args)
array([ 0.97384215])
'''
def test1_TKDE1D(): def test1_TKDE1D():
''' '''
N = 20 N = 20

Loading…
Cancel
Save