def test_divergence(self): rv1d = rv_discrete([(0, ), (1, ), (2, )], pk=[0.3, 0.3, 0.4]) rv2d = rv_discrete([(0, ), (1, ), (4, )], pk=[0.2, 0.4, 0.4]) c1 = [(-2, 2), (1, 5)] c2 = [(-1, 4), (-3, 3)] # c_target = [(-1,2),(1,3)] rv1c = rv_continuous(KernelDensity().fit([[-1, 2]]), (2, ), c1) rv2c = rv_continuous(KernelDensity().fit([[1, 3]]), (2, ), c2) # the data makes no sense, this is just a pro forma test rv1 = rv_mixed( rv1c, rv1d, { (0, ): KernelDensity().fit([[-1, 2]]), (1, ): KernelDensity().fit([[-2, 3], [-1, 4]]) }) rv2 = rv_mixed( rv2c, rv2d, { (0, ): KernelDensity().fit([[-1, 2]]), (1, ): KernelDensity().fit([[-3, 4], [0, 0]]) }) divergence = rv1.divergence(rv2) self.assertEqual(type(divergence), np.float64) self.assertGreater(divergence, 0.0)
def test_common_coverage(self): c1 = [(-2, 2), (1, 5)] c2 = [(-1, 4), (-3, 3)] rv1 = rv_continuous(KernelDensity().fit([[0, 1]]), (2, ), c1) rv2 = rv_continuous(KernelDensity().fit([[0, 1]]), (2, ), c2) mc_points = rv1._common_coverage(rv2, 1000) self.assertEqual(mc_points.shape[0], 1000) self.assertEqual(mc_points.shape[1], rv1.shape[0])
def test_divergence(self): c1 = [(-2, 2), (1, 5)] c2 = [(-1, 4), (-3, 3)] # c_target = [(-1,2),(1,3)] rv1 = rv_continuous(KernelDensity().fit([[-1, 2]]), (2, ), c1) rv2 = rv_continuous(KernelDensity().fit([[1, 3]]), (2, ), c2) divergence = rv1.divergence(rv2) self.assertEqual(type(divergence), np.float64) self.assertGreater(divergence, 0.0)
def test_common_limits(self): c1 = [(-2, 2), (1, 5)] c2 = [(-1, 4), (-3, 3)] rv1 = rv_continuous(KernelDensity().fit([[0, 1]]), (2, ), c1) rv2 = rv_continuous(KernelDensity().fit([[0, 1]]), (2, ), c2) c_is = rv1._get_common_limits_of_continuous(rv2) c_reverse = rv2._get_common_limits_of_continuous(rv1) c_target = [(-1, 2), (1, 3)] self.assertListEqual(c_is, c_target) self.assertListEqual(c_is, c_reverse)
def test_coverage_from_multiple_item_list(self): rv = rv_continuous(self.kde, (2, ), coverage=[(-1, 1), (-2, 2)]) coverage_is = rv.coverage coverage_target = [(-1, 1), (-2, 2)] self.assertTrue( np.array_equal(coverage_is, coverage_target), f"coverage_is: {coverage_is} is not coverage_target: {coverage_target}." )
def test_coverage_from_tuple(self): rv = rv_continuous(self.kde, (2, ), coverage=(-1, 1)) coverage_is = rv.coverage coverage_target = [(-1, 1), (-1, 1)] self.assertTrue( np.array_equal(coverage_is, coverage_target), f"coverage_is: {coverage_is} is not coverage_target: {coverage_target}." )
def test_coverage_default(self): rv = rv_continuous(self.kde, (2, )) coverage_is = rv.coverage coverage_target = [(-np.inf, np.inf), (-np.inf, np.inf)] self.assertTrue( np.array_equal(coverage_is, coverage_target), f"coverage_is: {coverage_is} is not coverage_target: {coverage_target}." )
def test_score_samples(self): rv = rv_continuous(self.kde, (2, )) X = [[0, 0], [0, 0]] P_is = rv.score_samples(X) P_target = np.log(np.array([self.p_00, self.p_00])) is_close = np.allclose(P_is, P_target, atol=0.2) self.assertTrue( is_close, f"P_is: {P_is} is not close enough to P_target: {P_target}.")
def test_pdf_of_2d_array(self): rv = rv_continuous(self.kde, (2, )) X = [[0, 0], [0, 0]] P_is = rv.pdf(X) P_target = np.array([self.p_00, self.p_00]) is_close = np.allclose(P_is, P_target, atol=0.03) self.assertTrue( is_close, f"P_is: {P_is} is not close enough to P_target: {P_target}.")
def test_common_coverage(self): rv1d = rv_discrete([(0, ), (1, ), (2, )], pk=[0.3, 0.3, 0.4]) rv2d = rv_discrete([(0, ), (1, ), (4, )], pk=[0.2, 0.4, 0.4]) c1 = [(-2, 2), (1, 5)] c2 = [(-1, 4), (-3, 3)] rv1c = rv_continuous(KernelDensity().fit([[0, 1]]), (2, ), c1) rv2c = rv_continuous(KernelDensity().fit([[0, 1]]), (2, ), c2) rv1 = rv_mixed(rv1c, rv1d, {(0, ): KernelDensity().fit([[1, 1]])}) rv2 = rv_mixed(rv2c, rv2d, {(0, ): KernelDensity().fit([[1, 0]])}) y_target = np.repeat([0, 1], 1000) x, y = rv1._common_coverage(rv2, 1000) self.assertEqual(x.shape[0], 2000) self.assertEqual(x.shape[1], 2) self.assertEqual(y.shape[0], x.shape[0]) self.assertTrue(np.array_equal(y, y_target), f"y {y} is not equal y_target {y_target}.")
def test_get_cond_kdes_wrong_key(self): rv_x = rv_continuous(self.kde, (2, )) rv_y = rv_discrete(xk=[(0, ), (1, )], pk=[.5, .5]) cond_kdes = { (0, ): KernelDensity(bandwidth=1.0), (1, ): KernelDensity(bandwidth=99.0) } rv = rv_mixed(rv_x, rv_y, cond_kdes) with self.assertRaises(KeyError): kde_is = rv._get_cond_kde(3)
def test_pdf_for_2d_discrete(self): rv_x = rv_continuous(self.kde, (2, )) # 2d discrete (0,0) if for (x1,x2) : x1 < 0 and x2 < 0, (1,0) if x1 >= 0 and x2 < 0 and so on. y0 = np.apply_along_axis(lambda x: 1 if x[0] >= 0 else 0, 1, self.X) y1 = np.apply_along_axis(lambda x: 1 if x[1] >= 0 else 0, 1, self.X) y = np.vstack((y0, y1)).T # TODO: Implement sensible behavior for multidimensional discrete variables. # At the moment this fails, beause numpy does not support fast row selection via a multidimensional index, # e.g.: if y = [[0,1], [1,1]], then y == [0,1] results in [[True, True], [False, True]] # and not in [True, False] as required for this use case in order to use the result as a selection for X and train KDEs. pass
def test_get_cond_kdes_from_tuple(self): rv_x = rv_continuous(self.kde, (2, )) rv_y = rv_discrete(xk=[(0, 0), (1, 0)], pk=[.5, .5]) cond_kdes = { (0, 0): KernelDensity(bandwidth=1.0), (1, 0): KernelDensity(bandwidth=99.0) } rv = rv_mixed(rv_x, rv_y, cond_kdes) kde_is = rv._get_cond_kde((0, 0)) kde_target = cond_kdes[(0, 0)] self.assertEqual(kde_is, kde_target)
def test_pdf_for_1d_discrete(self): rv_x = rv_continuous(self.kde, (2, )) rv_y = rv_discrete(xk=[(0, ), (1, )], pk=[.5, .5]) cond_kdes = {(0, ): self.kde0, (1, ): self.kde1} rv = rv_mixed(rv_x, rv_y, cond_kdes) P_is = rv.pdf(x=[[0, 0], [0, 0]], y=[1, 0]) P_target = [self.p_00 * 0.5, self.p_00 * 0.5] is_close = np.isclose(P_is, P_target, rtol=0.2) self.assertTrue( is_close.all(), f"P_is: {P_is} is not close enough to P_target: {P_target}. ")
def test_pdf_of_1d_array_raises_Error(self): rv = rv_continuous(self.kde, (2, )) X = [0, 0] with self.assertRaises(ValueError): P_is = rv.pdf(X)
def test_coverage_from_invalid_parameter(self): with self.assertRaises(ValueError): rv = rv_continuous(self.kde, (2, ), coverage=[(-1, +1), (-2, +2), (-3, +3)])