def test_BR1():
    """ Test that B + R = H """
    d = D(['000', '001', '010', '100', '111'], [1 / 5] * 5)
    assert B(d) + R(d) == pytest.approx(H(d))
示例#2
0
def test_B1():
    """ Test B for two dependent variables """
    d = D(['00', '11'], [1/2, 1/2])
    assert B(d) == pytest.approx(1)
示例#3
0
def test_B3():
    """ Test B for four dependent variables """
    d = D(['0000', '1111'], [1/2, 1/2])
    assert B(d) == pytest.approx(1)
    assert B(d, [[0], [1, 2]], [3]) == pytest.approx(0)
示例#4
0
def test_BR1():
    """ Test that B + R = H """
    d = D(['000', '001', '010', '100', '111'], [1 / 5] * 5)
    assert_almost_equal(B(d) + R(d), H(d))
示例#5
0
def test_p4():
    """ Test some simple base cases using D with varying bases """
    for i in range(2, 10):
        d = D([str(_) for _ in range(i)], [1 / i] * i)
        d.set_base(i)
        yield assert_almost_equal, P(d), i
示例#6
0
def test_R1():
    """ Test R for dependent variables """
    d = D(['00', '11'], [1 / 2, 1 / 2])
    assert_almost_equal(R(d), 0)
示例#7
0
def test_R3():
    """ Test R for a generic distribution """
    d = D(['000', '011', '101', '110'], [1 / 4] * 4)
    assert_almost_equal(R(d), 0)
    assert_almost_equal(R(d, [[0, 1], [2]]), 1)
def conditional_uniform2():
    events = [(a - 2, b - 2) for a, b, in product(range(5), range(5))
              if a <= b]
    probs = [1 / (3 - a) / 5 for a, _ in events]
    d = D(events, probs)
    return d
示例#9
0
def test_H1():
    """ Test H of a fair coin """
    d = D(['H', 'T'], [1 / 2] * 2)
    assert H(d) == pytest.approx(1)
示例#10
0
def test_tse2(n):
    """ Test TSE for giant bit distributions """
    d = D(['0' * n, '1' * n], [1 / 2, 1 / 2])
    tse = TSE(d)
    assert tse == pytest.approx((n - 1) / 2)
def miwin():
    d3 = [1, 2, 5, 6, 7, 9]
    d4 = [1, 3, 4, 5, 8, 9]
    d5 = [2, 3, 4, 6, 7, 8]
    d = D(list(product(d3, d4, d5)), [1 / 6**3] * 6**3)
    return d
示例#12
0
def test_caekl_3():
    """
    Test a known value.
    """
    d = D(['000', '011', '101', '110'], [1/4]*4)
    assert J(d) == pytest.approx(0.5)
示例#13
0
def test_mode2():
    """ Test mode on a generic distribution """
    d = D([(0, 0), (1, 0), (2, 1), (3, 1)], [1 / 8, 1 / 8, 3 / 8, 3 / 8])
    modes = [np.array([2, 3]), np.array([1])]
    for m1, m2 in zip(mode(d), modes):
        assert np.allclose(m1, m2)
示例#14
0
def test_median2():
    """ Test median on a generic distribution """
    d = D([(0, 0), (1, 0), (2, 1), (3, 1)], [1 / 8, 1 / 8, 3 / 8, 3 / 8])
    assert np.allclose(median(d), [2, 1])
示例#15
0
def test_B3():
    """ Test B for four dependent variables """
    d = D(['0000', '1111'], [1 / 2, 1 / 2])
    assert_almost_equal(B(d), 1)
    assert_almost_equal(B(d, [[0], [1, 2]], [3]), 0)
"""
Tests for dit.multivariate.exact_common_information
"""

from __future__ import division

import pytest

from dit import Distribution as D
from dit.multivariate import exact_common_information as G
from dit.multivariate.common_informations.exact_common_information import ExactCommonInformation
from dit.shannon import entropy

outcomes = ['0000', '0001', '0110', '0111', '1010', '1011', '1100', '1101']
pmf = [1 / 8] * 8
xor = D(outcomes, pmf)

sbec = lambda p: D(['00', '0e', '1e', '11'], [(1 - p) / 2, p / 2, p / 2,
                                              (1 - p) / 2])
G_sbec = lambda p: min(1, entropy(p) + 1 - p)


@pytest.mark.slow
@pytest.mark.flaky(reruns=5)
@pytest.mark.parametrize(('rvs', 'crvs', 'val'), [
    (None, None, 2.0),
    ([[0], [1], [2]], None, 2.0),
    ([[0], [1]], [2, 3], 1.0),
    ([[0], [1]], [2], 1.0),
    ([[0], [1]], None, 0.0),
])
示例#17
0
def test_B5():
    """ Test B = I for two variables """
    d = D(['00', '01', '11'], [1 / 3] * 3)
    assert_almost_equal(B(d), I(d, [0], [1]))
示例#18
0
def test_I1():
    """ Test mutual information of independent variables """
    outcomes = ['00', '01', '10', '11']
    pmf = [1/4]*4
    d = D(outcomes, pmf)
    assert I(d, [0], [1]) == pytest.approx(0.0)
示例#19
0
def test_R2():
    """ Test R for independent variables """
    d = D(['00', '01', '10', '11'], [1 / 4] * 4)
    assert_almost_equal(R(d), 2)
    assert_almost_equal(R(d, [[0], [1]]), 2)
示例#20
0
def test_I2():
    """ Test mutual information of dependent variables """
    outcomes = ['00', '11']
    pmf = [1/2]*2
    d = D(outcomes, pmf)
    assert I(d, [0], [1]) == pytest.approx(1.0)
示例#21
0
def test_R4():
    """ Test that R = H - I in two variables """
    d = D(['00', '01', '11'], [1 / 3] * 3)
    assert_almost_equal(R(d), H(d) - I(d, [0], [1]))
示例#22
0
def test_I3():
    """ Test mutual information of overlapping variables """
    outcomes = ['000', '011', '101', '110']
    pmf = [1/4]*4
    d = D(outcomes, pmf)
    assert I(d, [0, 1], [1, 2]) == pytest.approx(2.0)
示例#23
0
def test_p3():
    """ Test some simple base cases using D """
    for i in range(2, 10):
        d = D([str(_) for _ in range(i)], [1 / i] * i)
        yield assert_almost_equal, P(d), i
示例#24
0
def test_ii1(i):
    """ Test II for giant bit distributions """
    outcomes = ['0' * i, '1' * i]
    pmf = [1 / 2, 1 / 2]
    d = D(outcomes, pmf)
    assert interaction_information(d) == pytest.approx((-1)**i)
示例#25
0
def test_p6():
    """ Test some joint and conditional perplexities """
    d = D(['00', '11'], [1 / 2] * 2)
    assert_almost_equal(P(d), 2)
    assert_almost_equal(P(d, [0], [1]), 1)
    assert_almost_equal(P(d, [1], [0]), 1)
示例#26
0
def test_B1():
    """ Test B for two dependent variables """
    d = D(['00', '11'], [1 / 2, 1 / 2])
    assert_almost_equal(B(d), 1)
示例#27
0
def test_B2():
    """ Test B for three dependent variables """
    d = D(['000', '111'], [1/2, 1/2])
    assert B(d) == pytest.approx(1)
    assert B(d, [[0], [1]], [2]) == pytest.approx(0)
示例#28
0
def test_B2():
    """ Test B for three dependent variables """
    d = D(['000', '111'], [1 / 2, 1 / 2])
    assert_almost_equal(B(d), 1)
    assert_almost_equal(B(d, [[0], [1]], [2]), 0)
示例#29
0
def test_B5():
    """ Test B = I for two variables """
    d = D(['00', '01', '11'], [1/3]*3)
    assert B(d) == pytest.approx(I(d, [0], [1]))
def test_R4():
    """ Test that R = H - I in two variables """
    d = D(['00', '01', '11'], [1 / 3] * 3)
    assert R(d) == pytest.approx(H(d) - I(d, [0], [1]))