def test_renyi_values(): """ Test specific values of the Renyi divergence. """ d1 = Distribution(['0', '1'], [0, 1]) d2 = Distribution(['0', '1'], [1/2, 1/2]) d3 = Distribution(['0', '1'], [1, 0]) assert_almost_equal(renyi_divergence(d1, d2, 1/2), np.log2(2)) assert_almost_equal(renyi_divergence(d2, d3, 1/2), np.log2(2)) assert_almost_equal(renyi_divergence(d1, d3, 1/2), np.inf)
def test_renyi_values(): """ Test specific values of the Renyi divergence. """ d1 = Distribution(['0', '1'], [0, 1]) d2 = Distribution(['0', '1'], [1/2, 1/2]) d3 = Distribution(['0', '1'], [1, 0]) assert renyi_divergence(d1, d2, 1/2) == pytest.approx(np.log2(2)) assert renyi_divergence(d2, d3, 1/2) == pytest.approx(np.log2(2)) assert renyi_divergence(d1, d3, 1/2) == pytest.approx(np.inf)
def test_alpha_symmetry(alpha, dists): """ Tests the alpha -> -alpha symmetry for the alpha divergence, and a similar symmetry for the Hellinger and Renyi divergences. """ for dist1, dist2 in product(dists, repeat=2): assert alpha_divergence(dist1, dist2, alpha) == pytest.approx( alpha_divergence(dist2, dist1, -alpha)) assert (1. - alpha) * hellinger_divergence( dist1, dist2, alpha) == pytest.approx( alpha * hellinger_divergence(dist2, dist1, 1. - alpha)) assert (1. - alpha) * renyi_divergence( dist1, dist2, alpha) == pytest.approx( alpha * renyi_divergence(dist2, dist1, 1. - alpha))
def test_alpha_symmetry(alpha, dists): """ Tests the alpha -> -alpha symmetry for the alpha divergence, and a similar symmetry for the Hellinger and Renyi divergences. """ for dist1, dist2 in product(dists, repeat=2): assert alpha_divergence(dist1, dist2, alpha) == pytest.approx(alpha_divergence(dist2, dist1, -alpha)) assert (1.-alpha)*hellinger_divergence(dist1, dist2, alpha) == pytest.approx(alpha*hellinger_divergence(dist2, dist1, 1.-alpha)) assert (1.-alpha)*renyi_divergence(dist1, dist2, alpha) == pytest.approx(alpha*renyi_divergence(dist2, dist1, 1.-alpha))
def test_renyi(alpha): """ Consistency test for Renyi entropy and Renyi divergence """ dist1 = Distribution(['0', '1', '2'], [1/4, 1/2, 1/4]) uniform = Distribution(['0', '1', '2'], [1/3, 1/3, 1/3]) h = renyi_entropy(dist1, alpha) h_u = renyi_entropy(uniform, alpha) div = renyi_divergence(dist1, uniform, alpha) assert h == pytest.approx(h_u - div)
def test_renyi(alpha): """ Consistency test for Renyi entropy and Renyi divergence """ dist1 = Distribution(['0', '1', '2'], [1/4, 1/2, 1/4]) uniform = Distribution(['0', '1', '2'], [1/3, 1/3, 1/3]) h = renyi_entropy(dist1, alpha) h_u = renyi_entropy(uniform, alpha) div = renyi_divergence(dist1, uniform, alpha) assert h == pytest.approx(h_u - div)
def test_alpha_symmetry(): """ Tests the alpha -> -alpha symmetry for the alpha divergence, and a similar symmetry for the Hellinger and Renyi divergences. """ alphas = [-1, 0, 0.5, 1, 2] test_dists = [get_dists_2(), get_dists_3()] for alpha in alphas: for dists in test_dists: for dist1 in dists: for dist2 in dists: assert_almost_equal(alpha_divergence(dist1, dist2, alpha), alpha_divergence(dist2, dist1, -alpha)) assert_almost_equal( (1. - alpha) * hellinger_divergence(dist1, dist2, alpha), alpha * hellinger_divergence(dist2, dist1, 1. - alpha)) assert_almost_equal( (1. - alpha) * renyi_divergence(dist1, dist2, alpha), alpha * renyi_divergence(dist2, dist1, 1. - alpha))
def test_renyi(): """ Consistency test for Renyi entropy and Renyi divergence """ dist1 = Distribution(['0', '1', '2'], [1/4, 1/2, 1/4]) uniform = Distribution(['0', '1', '2'], [1/3, 1/3, 1/3]) alphas = [0, 1, 2, 0.5] for alpha in alphas: h = renyi_entropy(dist1, alpha) h_u = renyi_entropy(uniform, alpha) div = renyi_divergence(dist1, uniform, alpha) assert_almost_equal(h, h_u - div)
def test_renyi(): """ Consistency test for Renyi entropy and Renyi divergence """ dist1 = Distribution(['0', '1', '2'], [1 / 4, 1 / 2, 1 / 4]) uniform = Distribution(['0', '1', '2'], [1 / 3, 1 / 3, 1 / 3]) alphas = [0, 1, 2, 0.5] for alpha in alphas: h = renyi_entropy(dist1, alpha) h_u = renyi_entropy(uniform, alpha) div = renyi_divergence(dist1, uniform, alpha) assert_almost_equal(h, h_u - div)
def test_alpha_symmetry(): """ Tests the alpha -> -alpha symmetry for the alpha divergence, and a similar symmetry for the Hellinger and Renyi divergences. """ alphas = [-1, 0, 0.5, 1, 2] test_dists = [get_dists_2(), get_dists_3()] for alpha in alphas: for dists in test_dists: for dist1, dist2 in product(dists, repeat=2): assert_almost_equal(alpha_divergence(dist1, dist2, alpha), alpha_divergence(dist2, dist1, -alpha)) assert_almost_equal((1.-alpha)*hellinger_divergence(dist1, dist2, alpha), alpha*hellinger_divergence(dist2, dist1, 1.-alpha)) assert_almost_equal((1.-alpha)*renyi_divergence(dist1, dist2, alpha), alpha*renyi_divergence(dist2, dist1, 1.-alpha))