parent
51387dcb5d
commit
b5835a7549
@ -0,0 +1,240 @@
|
|||||||
|
'''
|
||||||
|
Created on 20. nov. 2010
|
||||||
|
|
||||||
|
@author: pab
|
||||||
|
'''
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
from numpy import array
|
||||||
|
import wafo.kdetools as wk
|
||||||
|
import pylab as plb
|
||||||
|
|
||||||
|
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.trapz(f, x)
|
||||||
|
0.94787730659349068
|
||||||
|
|
||||||
|
h1 = plb.plot(x, f) # 1D probability density plot
|
||||||
|
'''
|
||||||
|
def test1_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)
|
||||||
|
>>> f = kde(x)
|
||||||
|
>>> f
|
||||||
|
array([ 0.2039735 , 0.40252503, 0.54595078, 0.52219649, 0.3906213 ,
|
||||||
|
0.26381501, 0.16407362, 0.08270612, 0.02991145, 0.00720821])
|
||||||
|
|
||||||
|
>>> np.trapz(f, x)
|
||||||
|
0.92576174424281876
|
||||||
|
|
||||||
|
h1 = plb.plot(x, f) # 1D probability density plot
|
||||||
|
'''
|
||||||
|
def test2_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)
|
||||||
|
>>> f = kde(x)
|
||||||
|
>>> f
|
||||||
|
array([ 0.0541248 , 0.16555235, 0.33084399, 0.45293325, 0.48345808,
|
||||||
|
0.48345808, 0.45293325, 0.33084399, 0.16555235, 0.0541248 ])
|
||||||
|
|
||||||
|
>>> np.trapz(f, x)
|
||||||
|
0.97323338046725172
|
||||||
|
|
||||||
|
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)
|
||||||
|
>>> f
|
||||||
|
array([ 0.17252055, 0.41014271, 0.61349072, 0.57023834, 0.37198073,
|
||||||
|
0.21409279, 0.12738463, 0.07460326, 0.03956191, 0.01887164])
|
||||||
|
|
||||||
|
>>> np.trapz(f, x)
|
||||||
|
0.92938023659047952
|
||||||
|
|
||||||
|
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)
|
||||||
|
>>> f
|
||||||
|
array([ 0.0541248 , 0.16555235, 0.33084399, 0.45293325, 0.48345808,
|
||||||
|
0.48345808, 0.45293325, 0.33084399, 0.16555235, 0.0541248 ])
|
||||||
|
|
||||||
|
>>> np.trapz(f, x)
|
||||||
|
0.97323338046725172
|
||||||
|
|
||||||
|
h1 = plb.plot(x, f) # 1D probability density plot
|
||||||
|
'''
|
||||||
|
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)
|
||||||
|
>>> c
|
||||||
|
array([ 1., 6., 7., 2., 1., 2., 1., 0., 0., 0.])
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
>>> c
|
||||||
|
array([[ 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. ]])
|
||||||
|
|
||||||
|
|
||||||
|
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_4D():
|
||||||
|
'''
|
||||||
|
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)
|
||||||
|
>>> c
|
||||||
|
array([[[[ 1.77163904e-01, 3.41883175e-01, 3.13810608e-03],
|
||||||
|
[ 1.83770124e-01, 3.58861043e-01, 7.05946179e-03],
|
||||||
|
[ 0.00000000e+00, 2.91835067e-03, 6.38695629e-04]],
|
||||||
|
<BLANKLINE>
|
||||||
|
[[ 5.72573585e-01, 5.72071865e-01, 6.71606255e-03],
|
||||||
|
[ 4.35845892e-01, 8.80697705e-01, 1.09099593e-01],
|
||||||
|
[ 0.00000000e+00, 3.63686503e-02, 1.00358265e-02]],
|
||||||
|
<BLANKLINE>
|
||||||
|
[[ 3.48549923e-03, 3.46939323e-03, 0.00000000e+00],
|
||||||
|
[ 3.07662204e-03, 2.22868504e-01, 6.61257395e-02],
|
||||||
|
[ 0.00000000e+00, 1.88555613e-02, 5.67244468e-03]]],
|
||||||
|
<BLANKLINE>
|
||||||
|
<BLANKLINE>
|
||||||
|
[[[ 1.87720108e-01, 5.97977973e-01, 2.11731327e-02],
|
||||||
|
[ 2.56357594e-01, 6.28962785e-01, 5.44614852e-02],
|
||||||
|
[ 0.00000000e+00, 2.60268355e-02, 5.69610302e-03]],
|
||||||
|
<BLANKLINE>
|
||||||
|
[[ 6.09557834e-01, 8.58566538e-01, 4.53139824e-02],
|
||||||
|
[ 6.14958970e-01, 1.47373158e+00, 1.95935584e-01],
|
||||||
|
[ 0.00000000e+00, 1.07959459e-01, 2.44053065e-02]],
|
||||||
|
<BLANKLINE>
|
||||||
|
[[ 4.05931870e-02, 4.04056116e-02, 0.00000000e+00],
|
||||||
|
[ 3.58312786e-02, 1.18008528e-01, 2.47717418e-02],
|
||||||
|
[ 0.00000000e+00, 7.06358976e-03, 2.12498697e-03]]],
|
||||||
|
<BLANKLINE>
|
||||||
|
<BLANKLINE>
|
||||||
|
[[[ 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]],
|
||||||
|
<BLANKLINE>
|
||||||
|
[[ 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]],
|
||||||
|
<BLANKLINE>
|
||||||
|
[[ 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]]]])
|
||||||
|
|
||||||
|
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)
|
||||||
|
'''
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
Loading…
Reference in New Issue