def test_J11(): """ Test a property of J from result 5 of the paper """ pmf = np.array([1 / 2, 1 / 4, 1 / 8, 1 / 16, 1 / 16]) N = len(pmf) d = SD(pmf) q = SD(1 / (N - 1) * (1 - pmf)) j2 = (N - 1) * (H(q) - np.log2(N - 1)) assert_almost_equal(J(d), j2)
def test_J6(): """ Test a property of J from result 2 of the paper with base 10 """ for i in range(2, 10): d = SD([1 / i] * i) d.set_base(10) yield assert_almost_equal, J(d), (i - 1) * (np.log10(i) - np.log10(i - 1))
def test_J10(): """ Test a property of J from result 4 of the paper """ d = SD([1 / 2, 1 / 4, 1 / 8, 1 / 16, 1 / 16]) j = J(d) h = H(d) s = sum(J(p) for p in d.pmf) assert_almost_equal(h, s - j)
def test_J10(): """ Test a property of J from result 4 of the paper """ d = SD([1/2, 1/4, 1/8, 1/16, 1/16]) j = J(d) h = H(d) s = sum(J(p) for p in d.pmf) assert h == pytest.approx(s-j)
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
def test_R5(): """ Test that R fails on SDs """ d = SD([1 / 4] * 4) assert_raises(ditException, R, d)
def test_H1(): """ Test the entropy of a fair coin """ d = SD([1 / 2, 1 / 2]) assert H(d) == pytest.approx(1.0)
def test_p1(i): """ Test some simple base cases using SD """ assert P(SD([1 / i] * i)) == pytest.approx(i)
def test_p2(): """ Test some simple base cases using SD with varying bases """ for i in range(2, 10): d = SD([1 / i] * i) d.set_base(i) yield assert_almost_equal, P(d), i
def test_J4(): """ Test a property of J from result 2 of the paper """ for i in range(2, 10): d = SD([1 / i] * i) yield assert_almost_equal, J(d), (i - 1) * (np.log2(i) - np.log2(i - 1))
def test_J2(): """ Test a simple base case using ScalarDistribution """ d = SD([1/2]*2) assert J(d) == pytest.approx(1)
def test_H4(): """ Test entropy in base 10 """ d = SD([1 / 10] * 10) d.set_base(10) assert_almost_equal(H(d), 1.0)
def test_H1(): """ Test the entropy of a fair coin """ d = SD([1 / 2, 1 / 2]) assert_almost_equal(H(d), 1.0)
def test_R5(): """ Test that R fails on SDs """ d = SD([1 / 4] * 4) with pytest.raises(ditException): R(d)
def test_B6(): """ Test that B fails on SDs """ d = SD([1 / 4] * 4) with pytest.raises(ditException): B(d)
def test_tc8(): """ Test that T fails on SDs """ d = SD([1 / 3] * 3) assert_raises(ditException, T, d)
def test_coi8(): """ Test that I fails on ScalarDistributions """ d = SD([1 / 3] * 3) with pytest.raises(ditException): I(d)
def test_tc8(): """ Test that T fails on SDs """ d = SD([1/3]*3) with pytest.raises(ditException): T(d)
def test_J2(): """ Test a simple base case using ScalarDistribution """ d = SD([1 / 2] * 2) assert_almost_equal(J(d), 1)
def test_J7(): """ Test a property of J from result 1 of the paper """ for i in range(3, 10): d = SD([1 / i] * i) yield assert_true, J(d) < H(d)
def test_J4(i): """ Test a property of J from result 2 of the paper """ d = SD([1/i]*i) assert J(d) == pytest.approx((i-1)*(np.log2(i) - np.log2(i-1)))
def test_p1(): """ Test some simple base cases using SD """ for i in range(2, 10): yield assert_almost_equal, P(SD([1 / i] * i)), i
def test_J8(): """ Test a property of J from result 1 of the paper using log bases """ for i in range(3, 10): d = SD([1 / i] * i) d.set_base(i) yield assert_true, J(d) < H(d)
def test_J5(i): """ Test a property of J from result 2 of the paper with a log base """ d = SD([1/i]*i) d.set_base('e') assert J(d) == pytest.approx((i-1)*(np.log(i)-np.log(i-1)))
def test_J6(i): """ Test a property of J from result 2 of the paper with base 10 """ d = SD([1/i]*i) d.set_base(10) assert J(d) == pytest.approx((i-1)*(np.log10(i)-np.log10(i-1)))
def test_J7(i): """ Test a property of J from result 1 of the paper """ d = SD([1/i]*i) assert J(d) < H(d)
def test_p2(i): """ Test some simple base cases using SD with varying bases """ d = SD([1 / i] * i) d.set_base(i) assert P(d) == pytest.approx(i)
def test_J8(i): """ Test a property of J from result 1 of the paper using log bases """ d = SD([1/i]*i) d.set_base(i) assert J(d) < H(d)
def test_H4(): """ Test entropy in base 10 """ d = SD([1 / 10] * 10) d.set_base(10) assert H(d) == pytest.approx(1.0)
def test_B6(): """ Test that B fails on SDs """ d = SD([1 / 4] * 4) assert_raises(ditException, B, d)