Пример #1
0
def test_entropy_subadditivity(dist):
    """
    H(X1, ...) <= sum(H(X_i))
    """
    h = H(dist)
    h_sum = sum(H(dist.marginal(rv)) for rv in dist.rvs)
    assert h <= h_sum + epsilon
Пример #2
0
def test_conditional_entropy(dist):
    """
    H(X|Y) <= H(X)
    """
    ch = H(dist, [0], [1])
    h = H(dist, [0])
    assert ch <= h + epsilon
Пример #3
0
def test_entropy_upper_bound(dist):
    """
    H(X) <= log(|X|)
    """
    h = H(dist)
    logX = np.log2(len(dist.outcomes))
    assert h <= logX + epsilon
Пример #4
0
def test_wolf():
    """
    Test against value from publication.
    """
    d = Wolfs_dice()
    entropy = H(d, 'W') / np.log2(np.e)
    assert entropy == pytest.approx(1.784990, abs=1e-6)
Пример #5
0
def test_fanos_inequality(dist):
    """
    H(X|Y) <= hb(P_e) + P_e log(|X| - 1)
    """
    dist1 = SD.from_distribution(dist.marginal([0]))
    dist2 = SD.from_distribution(dist.marginal([1]))

    ce = H(dist, [0], [1])

    X = len(set().union(dist1.outcomes, dist2.outcomes))

    eq_dist = dist1 == dist2
    P_e = eq_dist[False] if False in eq_dist else 0

    hb = H(SD([P_e, 1 - P_e]))

    assert ce <= hb + P_e * np.log2(X - 1) + epsilon
Пример #6
0
def test_minent_1():
    """
    Test minent
    """
    d = uniform(['000', '001', '010', '011', '100', '101', '110', '111'])
    meo = MinEntOptimizer(d, [[0], [1], [2]])
    meo.optimize()
    dp = meo.construct_dist()
    assert H(dp) == pytest.approx(1)
Пример #7
0
def test_H2():
    """ Test the various entropies of two independent events """
    d = D(['00', '01', '10', '11'], [1 / 4] * 4)
    assert_almost_equal(H(d), 2)
    assert_almost_equal(H(d, [0]), 1)
    assert_almost_equal(H(d, [1]), 1)
    assert_almost_equal(H(d, [0], [1]), 1)
    assert_almost_equal(H(d, [1], [0]), 1)
    assert_almost_equal(H(d, [0], [0]), 0)
    assert_almost_equal(H(d, [0], [0, 1]), 0)
Пример #8
0
def test_H2():
    """ Test the various entropies of two independent events """
    d = D(['00', '01', '10', '11'], [1 / 4] * 4)
    assert H(d) == pytest.approx(2)
    assert H(d, [0]) == pytest.approx(1)
    assert H(d, [1]) == pytest.approx(1)
    assert H(d, [0], [1]) == pytest.approx(1)
    assert H(d, [1], [0]) == pytest.approx(1)
    assert H(d, [0], [0]) == pytest.approx(0)
    assert H(d, [0], [0, 1]) == pytest.approx(0)
Пример #9
0
def test_maxent_3():
    """
    Test the RdnUnqXor distribution.
    """
    X00, X01, X02, Y01, Y02 = 'rR', 'aA', [0, 1], 'bB', [0, 1]
    inputs = product(X00, X01, X02, Y01, Y02)
    events = [(x00 + x01 + str(x02), x00 + y01 + str(y02),
               x00 + x01 + y01 + str(x02 ^ y02))
              for x00, x01, x02, y01, y02 in inputs]
    RdnUnqXor = uniform(events)
    d = maxent_dist(RdnUnqXor, [[0, 1], [0, 2], [1, 2]])
    assert H(d) == pytest.approx(6)
Пример #10
0
def test_H3():
    """ Test the various entropies of two independent events with names """
    d = D(['00', '01', '10', '11'], [1 / 4] * 4)
    d.set_rv_names('XY')
    assert_almost_equal(H(d), 2)
    assert_almost_equal(H(d, ['X']), 1)
    assert_almost_equal(H(d, ['Y']), 1)
    assert_almost_equal(H(d, ['X'], ['Y']), 1)
    assert_almost_equal(H(d, ['Y'], ['X']), 1)
    assert_almost_equal(H(d, ['X'], ['X']), 0)
    assert_almost_equal(H(d, ['X'], ['X', 'Y']), 0)
Пример #11
0
def test_H3():
    """ Test the various entropies of two independent events with names """
    d = D(['00', '01', '10', '11'], [1 / 4] * 4)
    d.set_rv_names('XY')
    assert H(d) == pytest.approx(2)
    assert H(d, ['X']) == pytest.approx(1)
    assert H(d, ['Y']) == pytest.approx(1)
    assert H(d, ['X'], ['Y']) == pytest.approx(1)
    assert H(d, ['Y'], ['X']) == pytest.approx(1)
    assert H(d, ['X'], ['X']) == pytest.approx(0)
    assert H(d, ['X'], ['X', 'Y']) == pytest.approx(0)
Пример #12
0
def test_coi7():
    """ Test that H = I for one variable """
    outcomes = "ABC"
    pmf = [1 / 3] * 3
    d = D(outcomes, pmf)
    assert H(d) == pytest.approx(I(d))
Пример #13
0
def test_H7():
    """ Test H for uniform distributions using SDs in various bases """
    for i in range(2, 10):
        d = uniform(i)
        d.set_base(i)
        yield assert_almost_equal, H(d), 1
Пример #14
0
def test_H1():
    """ Test H of a fair coin """
    d = D(['H', 'T'], [1 / 2, 1 / 2])
    assert_almost_equal(H(d), 1)
Пример #15
0
def test_H1():
    """ Test H of a fair coin """
    d = D(['H', 'T'], [1 / 2, 1 / 2])
    assert H(d) == pytest.approx(1)
Пример #16
0
def test_H4():
    """ Test H for uniform distributions """
    for i in range(2, 10):
        d = D.from_distribution(uniform(i))
        yield assert_almost_equal, H(d), np.log2(i)
Пример #17
0
def test_H5():
    """ Test H for uniform distributions in various bases """
    for i in range(2, 10):
        d = D.from_distribution(uniform(i))
        d.set_base(i)
        yield assert_almost_equal, H(d), 1
Пример #18
0
def test_H6():
    """ Test H for uniform distributions using ScalarDistributions """
    for i in range(2, 10):
        d = uniform(i)
        yield assert_almost_equal, H(d), np.log2(i)
Пример #19
0
def test_H4(i):
    """ Test H for uniform distributions """
    d = D.from_distribution(uniform(i))
    assert H(d) == pytest.approx(np.log2(i))
Пример #20
0
def test_H6(i):
    """ Test H for uniform distributions using ScalarDistributions """
    d = uniform(i)
    assert H(d) == pytest.approx(np.log2(i))
Пример #21
0
def test_H5(i):
    """ Test H for uniform distributions in various bases """
    d = D.from_distribution(uniform(i))
    d.set_base(i)
    assert H(d) == pytest.approx(1)
Пример #22
0
def test_giant_bit1(n, k):
    """
    tests for the giant bit entropy
    """
    d = giant_bit(n, k)
    assert H(d) == pytest.approx(np.log2(k))
Пример #23
0
def test_H7(i):
    """ Test H for uniform distributions using SDs in various bases """
    d = uniform(i)
    d.set_base(i)
    assert H(d) == pytest.approx(1)
Пример #24
0
def test_coi7():
    """ Test that H = I for one variable """
    outcomes = "ABC"
    pmf = [1/3]*3
    d = D(outcomes, pmf)
    assert_almost_equal(H(d), I(d))