def test_kde(self): zikm = eda.ZeroIGKdeMdl(self.x) kde = zikm.kde kde.logpdf([1, 2, 3]) kde2 = eda.ZeroIGKdeMdl(self.x_all_zero).kde assert kde2 is None kde3 = eda.ZeroIGKdeMdl(self.x_one_nonzero).kde assert kde3 is None
def test_kde_bw(self): zikm = eda.ZeroIGKdeMdl(self.x) n = len(zikm.x_nonzero) d = 1 # scott: n**(-1./(d+4)) np.testing.assert_allclose( zikm.bandwidth, zikm.x_nonzero.std(ddof=1) * n**(-1 / (d + 4))) # silverman: (n * (d + 2) / 4.)**(-1. / (d + 4)) zikm2 = eda.ZeroIGKdeMdl(self.x, "silverman") np.testing.assert_allclose( zikm2.bandwidth, zikm2.x_nonzero.std(ddof=1) * (n * (d + 2) / 4.)**(-1. / (d + 4))) zikm3 = eda.ZeroIGKdeMdl(self.x, 1) xnz_std = zikm.x_nonzero.std(ddof=1) np.testing.assert_allclose(1, zikm3.bandwidth / xnz_std) assert not np.allclose(zikm2.bandwidth, zikm3.bandwidth)
def test_all_zero(self): zikm = eda.ZeroIGKdeMdl(self.x_all_zero) assert zikm.bandwidth is None np.testing.assert_allclose(zikm.zi_mdl, np.log(3) + np.log(len(self.x_all_zero))) assert zikm.x_nonzero.size == 0 np.testing.assert_allclose(zikm.x, self.x_all_zero) np.testing.assert_allclose(zikm.kde_mdl, 0) np.testing.assert_allclose(zikm.mdl, zikm.zi_mdl + zikm.kde_mdl)
def test_encode(self): zimdl = eda.ZeroIGKdeMdl(self.x) qx = [0, 0, 0] + [1, 2, 10, 20] emdl = zimdl.encode(qx) ezi = zimdl._zi_encoder.encode(qx) ekde = zimdl._kde_encoder.encode(list(filter(lambda x: x != 0, qx))) qp = zimdl._k / zimdl._n np.testing.assert_allclose( ezi, -np.log(qp) * 4 - np.log(1 - qp) * 3 + np.log(3)) np.testing.assert_allclose(emdl, ezi + ekde)
def test_std_usage(self): zikm = eda.ZeroIGKdeMdl(self.x) np.testing.assert_allclose(zikm.x, self.x) assert zikm.x is not self.x np.testing.assert_allclose(zikm.x_nonzero, self.x[50:250]) np.testing.assert_allclose(zikm.mdl, zikm.zi_mdl + zikm.kde_mdl) np.testing.assert_allclose( zikm.zi_mdl, np.log(3) + eda.MultinomialMdl(self.x != 0).mdl) assert zikm._bw_method == "scott" assert zikm.bandwidth is not None # test > 0 value kde same zikm2 = eda.ZeroIGKdeMdl(self.x[50:250]) np.testing.assert_allclose(zikm2.kde_mdl, zikm.kde_mdl) np.testing.assert_allclose(zikm2.bandwidth, zikm.bandwidth)
def test_wrong_x_shape(self): with pytest.raises(ValueError) as excinfo: eda.ZeroIGKdeMdl(np.arange(10).reshape(5, 2))
def test_empty(self): zikm = eda.ZeroIGKdeMdl(np.array([])) assert zikm.mdl == np.log(3) assert zikm.zi_mdl == np.log(3) assert zikm.kde_mdl == 0
def test_one_nonzero(self): zikm = eda.ZeroIGKdeMdl(self.x_one_nonzero) assert zikm.bandwidth is None # 1 non-0, kde falls back on uniform np.testing.assert_allclose( zikm.kde_mdl, np.log(np.max(np.abs(self.x_one_nonzero)) * 2))
def test_all_nonzero(self): zikm = eda.ZeroIGKdeMdl(self.x_all_non_zero) np.testing.assert_allclose( zikm.zi_mdl, np.log(3) + np.log(len(self.x_all_non_zero)))