Exemplo n.º 1
0
def test_emd2():
    """
    """
    d1 = Distribution(['a', 'b'], [0, 1], trim=False)
    d2 = Distribution(['a', 'b'], [1, 0], trim=False)
    emd = earth_movers_distance(d1, d2)
    assert emd == pytest.approx(1.0)
def test_plugable_fail():
    """ Tests for the pluggable form """
    d1 = Distribution("AB", [0.5, 0.5])
    d2 = Distribution("BC", [0.5, 0.5])
    f = jensen_divergence(renyi_entropy)
    with pytest.raises(ditException):
        f(d1, d2)
def get_dists_2():
    """
    Construct several example distributions.
    """
    d1 = Distribution(['0', '1'], [1/2, 1/2])
    d2 = Distribution(['0', '1'], [1/3, 2/3])
    d3 = Distribution(['0', '1'], [2/5, 3/5])
    return d1, d2, d3
Exemplo n.º 4
0
def test_relative_entropy2():
    """
    Test against known value.
    """
    d1 = Distribution(['0', '1'], [1 / 2, 1 / 2])
    d2 = Distribution(['1', '2'], [1 / 2, 1 / 2])
    ce = relative_entropy(d1, d2)
    assert ce == pytest.approx(0.0)
Exemplo n.º 5
0
def test_cross_entropy3():
    """
    Test against known value.
    """
    d1 = Distribution(['0', '1'], [1 / 2, 1 / 2])
    d2 = Distribution(['1', '2'], [1 / 2, 1 / 2])
    ce = cross_entropy(d1, d2, pmf_only=False)
    assert ce == pytest.approx(1.0)
Exemplo n.º 6
0
def test_cm_1():
    """
    Test that equivalent distributions have zero metric.
    """
    d1 = Distribution(['0', '1'], [1/3, 2/3])
    d2 = Distribution(['a', 'b'], [2/3, 1/3])
    cm = coupling_metric([d1, d2], p=1.0)
    assert cm == pytest.approx(0.0)
Exemplo n.º 7
0
def test_channel_capacity_joint2():
    """
    Test against a known value.
    """
    gm = Distribution(['00', '01', '10'], [1 / 3] * 3)
    m = Distribution(['0', '1'], [2 / 5, 3 / 5])
    _, marg = channel_capacity_joint(gm, [0], [1], marginal=True)
    assert marg.is_approx_equal(m, atol=1e-3)
Exemplo n.º 8
0
def test_vd2():
    """
    Test against known value
    """
    d1 = Distribution(['0', '1'], [1 / 2, 1 / 2])
    d2 = Distribution(['1', '2'], [1 / 4, 3 / 4])
    v = variational_distance(d1, d2)
    assert v == pytest.approx(0.75)
def test_info_trim2():
    """

    """
    d1 = Distribution(['000', '001', '110', '111', '222', '333'], [1/8]*4+[1/4]*2)
    d2 = Distribution(['000', '111', '222', '332'], [1/4]*4)
    d3 = info_trim(d1)
    assert d3.is_approx_equal(d2)
def get_dists_3():
    """
    Construct several example distributions.
    """
    d1 = Distribution(['0', '1', '2'], [1/5, 2/5, 2/5])
    d2 = Distribution(['0', '1', '2'], [1/4, 1/2, 1/4])
    d3 = Distribution(['0', '1', '2'], [1/3, 1/3, 1/3])
    d4 = Distribution(['0', '1', '2'], [1/6, 2/6, 3/6])
    return d1, d2, d3, d4
Exemplo n.º 11
0
def get_dists():
    """
    Construct several example distributions.
    """
    d1 = Distribution(['0', '1'], [1 / 2, 1 / 2])
    d2 = Distribution(['0', '2'], [1 / 2, 1 / 2])
    d3 = Distribution(['0', '1', '2'], [1 / 3, 1 / 3, 1 / 3])
    d4 = Distribution(['00', '11'], [2 / 5, 3 / 5])
    d5 = Distribution(['00', '11'], [1 / 2, 1 / 2])
    return d1, d2, d3, d4, d5
Exemplo n.º 12
0
def test_emd3():
    """
    """
    d1 = Distribution(['a', 'b'], [2 / 3, 1 / 3])
    d2 = Distribution(['c', 'd'], [0, 1], trim=False)
    emd1 = earth_movers_distance(d1, d2)
    assert emd1 == pytest.approx(1.0)
    distances = np.asarray([[0, 1], [1, 0]])
    emd2 = earth_movers_distance(d1, d2, distances=distances)
    assert emd2 == pytest.approx(2 / 3)
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_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_plugable():
    """ Tests for the pluggable form """
    d1 = Distribution("AB", [0.5, 0.5])
    d2 = Distribution("BC", [0.5, 0.5])
    f = jensen_divergence(renyi_entropy)
    val1 = f([d1, d2], order=1)
    val2 = f([d1, d2], [0.5, 0.5], 1)
    val3 = JSD([d1, d2])
    assert val1 == pytest.approx(val2)
    assert val1 == pytest.approx(val3)
    assert val2 == pytest.approx(val3)
Exemplo n.º 16
0
def test_dfd():
    """
    Test distribution_from_data.
    """
    data = [0,0,0,1,1,1]
    d1 = Distribution([(0,), (1,)], [1/2, 1/2])
    d2 = Distribution([(0,0), (0,1), (1,1)], [2/5, 1/5, 2/5])
    d1_ = distribution_from_data(data, 1, base='linear')
    d2_ = distribution_from_data(data, 2, base='linear')
    assert d1.is_approx_equal(d1_)
    assert d2.is_approx_equal(d2_)
Exemplo n.º 17
0
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)
Exemplo n.º 18
0
def test_to_string3():
    # Test printing
    outcomes = ['00', '01', '10', '11']
    pmf = [1/4]*4
    d = Distribution(outcomes, pmf)
    s_ = """Class:          Distribution
Alphabet:       ('0', '1') for all rvs
Base:           linear
Outcome Class:  str
Outcome Length: 2
RV Names:       None

x    p(x)
00   0.25
01   0.25
10   0.25
11   0.25"""

    # context manager?
    import sys
    from six import StringIO
    sio = StringIO()
    try:
        old = sys.stdout
        sys.stdout = sio
        print(d, end='')
    finally:
        sys.stdout = old
    sio.seek(0)
    s = sio.read()
    assert s == s_
Exemplo n.º 19
0
def test_simple_rd_4():
    """
    Test against know result, using scipy.
    """
    dist = Distribution(['0', '1'], [1 / 2, 1 / 2])
    rd = RDCurve(dist, beta_num=10, alpha=0.0, distortion=residual_entropy)
    assert rd.distortions[0] == pytest.approx(1.0)
Exemplo n.º 20
0
def BEC_joint(epsilon):
    """
    The joint distribution for the binary erase channel at channel capacity.

    Parameters
    ----------
    epsilon : float
        The noise level at which the input is erased.

    """
    pX = Distribution(['0', '1'], [1 / 2, 1 / 2])
    pYgX0 = Distribution(['0', '1', 'e'], [1 - epsilon, 0, epsilon])
    pYgX1 = Distribution(['0', '1', 'e'], [0, 1 - epsilon, epsilon])
    pYgX = [pYgX0, pYgX1]
    pXY = joint_from_factors(pX, pYgX, strict=False)
    return pXY
Exemplo n.º 21
0
def test_channel_capacity_joint1():
    """
    Test against a known value.
    """
    gm = Distribution(['00', '01', '10'], [1 / 3] * 3)
    cc = channel_capacity_joint(gm, [0], [1])
    assert cc == pytest.approx(0.3219280796196524)
Exemplo n.º 22
0
def test_init11():
    outcomes = ['0', '1']
    pmf = [1 / 2, 1 / 2]
    d = Distribution(outcomes, pmf)
    sd = ScalarDistribution.from_distribution(d)
    # Different sample space representations
    assert not d.is_approx_equal(sd)
Exemplo n.º 23
0
def test_pr_1():
    """
    Test
    """
    d1 = Distribution(list(product([0, 1], repeat=4)), [1 / 16] * 16)
    d2 = pr_box(0.0)
    assert d1.is_approx_equal(d2)
Exemplo n.º 24
0
def test_to_dict():
    outcomes = ['00', '01', '10', '11']
    pmf = [1/4]*4
    d = Distribution(outcomes, pmf)
    dd = d.to_dict()
    for o, p in dd.items():
        assert d[o] == pytest.approx(p)
Exemplo n.º 25
0
def test_zipped1():
    outcomes = ['00', '01', '10', '11']
    pmf = [1/4]*4
    d = Distribution(outcomes, pmf)
    zipped = d.zipped(mode='pants')
    with pytest.raises(ditException):
        list(zipped)
def test_fci3():
    """
    Test against known values
    """
    outcomes = [
        '000',
        'a00',
        '00c',
        'a0c',
        '011',
        'a11',
        '101',
        'b01',
        '01d',
        'a1d',
        '10d',
        'b0d',
        '110',
        'b10',
        '11c',
        'b1c',
    ]
    pmf = [1 / 16] * 16
    d = Distribution(outcomes, pmf)
    assert F(d) == pytest.approx(2.0)
Exemplo n.º 27
0
def test_two_way_skar2():
    """
    Test an unknown example (reduced or).
    """
    d = Distribution(['000', '011', '101'], [1 / 2, 1 / 4, 1 / 4])
    skar = two_way_skar(d, [[0], [2]], [1])
    assert np.isnan(skar)
Exemplo n.º 28
0
def test_DtoSD2():
    outcomes = [(0, ), (2, ), (4, )]
    pmf = [1 / 3] * 3
    d = Distribution(outcomes, pmf)
    sd = DtoSD(d, True)
    assert type(sd) is ScalarDistribution
    assert sd.outcomes == (0, 2, 4)
Exemplo n.º 29
0
def test_hypercontractivity_coefficient2():
    """
    Test against a known value.
    """
    d = Distribution(['00', '01', '10', '11'], [1 / 4] * 4)
    hc = hypercontractivity_coefficient(d, [[0], [1]])
    assert hc == pytest.approx(0.0)
Exemplo n.º 30
0
def pr_box(eta=1, name=False):
    """
    The Popescu-Rohrlich box, or PR box, is the canonical non-signalling,
    non-local probability distribution used in the study of superquantum
    correlations. It has two space-like seperated inputs, X and Y, and two
    associated outputs, A and B.

    `eta` is the noise level of this correlation. For 0 <= eta <= 1/2 the box
    can be realized classically. For 1/2 < eta <= 1/sqrt(2) the box can be
    realized quantum-mechanically.

    Parameters
    ----------
    eta : float, 0 <= eta <= 1
        The noise level of the box. Defaults to 1.

    name : bool
        Whether to set rv names or not. Defaults to False.

    Returns
    -------
    pr : Distribution
        The PR box distribution.
    """
    outcomes = list(product([0, 1], repeat=4))
    pmf = [((1 + eta) / 16 if (x * y == a ^ b) else (1 - eta) / 16)
           for x, y, a, b in outcomes]
    pr = Distribution(outcomes, pmf)

    if name:
        pr.set_rv_names("XYAB")

    return pr