Ejemplo n.º 1
0
    def test_2d_default_bandwidth(self):
        # N = 20
        # data = np.random.rayleigh(1, size=(2, N))
        data = DATA2D
        kde0 = wk.KDE(data, kernel=wk.Kernel('epan', 'hmns'), inc=512)

        assert_allclose(kde0.hs, [[0.8838122391117693, 0.08341940479019105],
                                  [0.08341940479019104, 0.7678179747855731]])
        self.assertRaises(ValueError, kde0.eval_points, [1, 2, 3])
        assert_allclose(kde0.eval_points([1, 2]), 0.11329600006973661)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    def test2a_KDE_1D_hs_5_alpha_5(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)
Ejemplo n.º 4
0
    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)

        f0 = kde(output='plot')
        self.assertIsInstance(f0, wo.PlotData)
        assert_allclose(np.trapz(f0.data, f0.args), 0.9871189376720593)

        f0 = kde.eval_grid_fast(output='plot')
        self.assertIsInstance(f0, wo.PlotData)
        assert_allclose(np.trapz(f0.data, f0.args), 0.9962507385131669)
Ejemplo n.º 5
0
    def test_KDE2D(self):
        # N = 20
        # data = np.random.rayleigh(1, size=(2, N))
        data = DATA2D
        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]])

        f0 = kde0.eval_grid_fast(x, x, output='plot')
        t = [[0.0443506097653615, 0.06433530873456418, 0.0041353838654317856],
             [0.07218297149063724, 0.1235819591878892, 0.009288890372002473],
             [
                 0.001613328022214066, 0.00794857884864038,
                 0.0005874786787715641
             ]]
        assert_allclose(f0.data, t)
Ejemplo n.º 6
0
    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)
        f0 = kde(output='plot')
        self.assertIsInstance(f0, wo.PlotData)
        assert_allclose(np.trapz(f0.data, f0.args), 0.9319800260106625)

        f0 = kde.eval_grid_fast(output='plot')
        self.assertIsInstance(f0, wo.PlotData)
        assert_allclose(np.trapz(f0.data, f0.args), 0.9319799696210691)
Ejemplo n.º 7
0
    def test0_KDE1D(self):
        data, x = self.data, self.x

        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_points(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
        ])
Ejemplo n.º 8
0
 def test_default_bandwidth_and_inc(self):
     kde0 = wk.KDE(self.data, hs=-1, alpha=0.0, inc=None)
     print(kde0.hs.tolist(), kde0.inc)
     assert_allclose(kde0.hs, 0.19682759537327105)
     assert_allclose(kde0.inc, 64)