Exemplo n.º 1
0
def test_random_distribution():
    # Test with no alpha
    pmf = np.array([2.48224944e-01, 5.86112396e-01, 5.26167518e-05, 1.65610043e-01])
    outcomes = ((0, 0), (0, 1), (1, 0), (1, 1))
    for prng in [None, dit.math.prng]:
        dit.math.prng.seed(1)
        d = dit.random_distribution(2, 2, prng=prng)
        assert_equal(d.outcomes, outcomes)
        np.testing.assert_allclose(d.pmf, pmf)

    # Test with a single alphabet specified
    dit.math.prng.seed(1)
    d = dit.random_distribution(2, [[0, 1]])
    assert_equal(d.outcomes, outcomes)
    np.testing.assert_allclose(d.pmf, pmf)

    # Test with two alphabets specified
    dit.math.prng.seed(1)
    d = dit.random_distribution(2, [[0, 1], [0, 1]])
    assert_equal(d.outcomes, outcomes)
    np.testing.assert_allclose(d.pmf, pmf)

    # Test with invalid number of alphabets
    assert_raises(TypeError, dit.random_distribution, 3, [3, 2])
    assert_raises(TypeError, dit.random_distribution, 3, [3, 2, 3])

    # Test with concentration parameters
    pmf = np.array([0.15092872, 0.23236257, 0.05765063, 0.55905808])
    dit.math.prng.seed(1)
    d = dit.random_distribution(2, 2, alpha=[1, 2, 1, 3])
    assert_equal(d.outcomes, outcomes)
    np.testing.assert_allclose(d.pmf, pmf)
    assert_raises(ditException, dit.random_distribution, 2, 2, alpha=[1])
Exemplo n.º 2
0
def test_random_distribution():
    # Test with no alpha
    pmf = np.array(
        [2.48224944e-01, 5.86112396e-01, 5.26167518e-05, 1.65610043e-01])
    outcomes = ((0, 0), (0, 1), (1, 0), (1, 1))
    for prng in [None, dit.math.prng]:
        dit.math.prng.seed(1)
        d = dit.random_distribution(2, 2, prng=prng)
        assert_equal(d.outcomes, outcomes)
        np.testing.assert_allclose(d.pmf, pmf)

    # Test with a single alphabet specified
    dit.math.prng.seed(1)
    d = dit.random_distribution(2, [[0, 1]])
    assert_equal(d.outcomes, outcomes)
    np.testing.assert_allclose(d.pmf, pmf)

    # Test with two alphabets specified
    dit.math.prng.seed(1)
    d = dit.random_distribution(2, [[0, 1], [0, 1]])
    assert_equal(d.outcomes, outcomes)
    np.testing.assert_allclose(d.pmf, pmf)

    # Test with invalid number of alphabets
    assert_raises(TypeError, dit.random_distribution, 3, [3, 2])
    assert_raises(TypeError, dit.random_distribution, 3, [3, 2, 3])

    # Test with concentration parameters
    pmf = np.array([0.15092872, 0.23236257, 0.05765063, 0.55905808])
    dit.math.prng.seed(1)
    d = dit.random_distribution(2, 2, alpha=[1, 2, 1, 3])
    assert_equal(d.outcomes, outcomes)
    np.testing.assert_allclose(d.pmf, pmf)
    assert_raises(ditException, dit.random_distribution, 2, 2, alpha=[1])
Exemplo n.º 3
0
def test_combined_variable():
    """Test that using a coalesced input gives the same result as constraining
    on a bivariate marginal."""
    d = dit.random_distribution(4, 2)
    r1 = disclosure(d, cons=[[0, 1], [2]], output=[3])
    r2 = disclosure(d.coalesce([[0, 1], [2], [3]]))
    assert (np.isclose(r1, r2))

    # Same, but on self-disclosure
    d = dit.random_distribution(3, 2)
    r1 = self_disclosure(d, cons=[[0, 1], [2]])
    r2 = self_disclosure(d.coalesce([[0, 1], [2]]))
    assert (np.isclose(r1, r2))
Exemplo n.º 4
0
def test_simplex_grid3():
    # Test with Distribution
    d = dit.random_distribution(1, 2)
    dists = np.asarray([x.pmf for x in dit.simplex_grid(2, 2**2, using=d)])
    dists_ = np.asarray([(0.0, 1.0), (0.25, 0.75), (0.5, 0.5),
                         (0.75, 0.25), (1.0, 0.0)])
    np.testing.assert_allclose(dists, dists_)
Exemplo n.º 5
0
def test_all_constraints():
    """Test that fully constrained disclosure is zero."""
    nb_reps = 10
    for _ in range(nb_reps):
        d = dit.random_distribution(3, 2)
        syn = disclosure(d, cons=[[0, 1]])
        assert (np.isclose(syn, 0))
Exemplo n.º 6
0
def test_simplex_grid3():
    # Test with Distribution
    d = dit.random_distribution(1, 2)
    dists = np.asarray([x.pmf for x in dit.simplex_grid(2, 2**2, using=d)])
    dists_ = np.asarray([(0.0, 1.0), (0.25, 0.75), (0.5, 0.5), (0.75, 0.25),
                         (1.0, 0.0)])
    np.testing.assert_allclose(dists, dists_)
Exemplo n.º 7
0
def test_no_constraints_self_disclosure():
    """Test that unconstrained self-disclosure is joint entropy."""
    nb_reps = 10
    for _ in range(nb_reps):
        d = dit.random_distribution(3, 2)
        H = entropy(d)
        syn = self_disclosure(d, cons=[])
        assert (np.isclose(H, syn))
Exemplo n.º 8
0
def test_no_constraints():
    """Test that unconstrained disclosure is MI."""
    nb_reps = 10
    for _ in range(nb_reps):
        d = dit.random_distribution(3, 2)
        MI = coinformation(d, [[0, 1], [2]])
        syn = disclosure(d, cons=[])
        assert (np.isclose(MI, syn))
Exemplo n.º 9
0
def test_constraint_marginal():
    """Test that the marginals are actually preserved."""
    nb_reps = 10
    u = dit.distconst.uniform_distribution(2,2)
    for _ in range(nb_reps):
        Pjoint = dit.random_distribution(2, 2)
        marg = np.random.choice([[0], [1], [0,1]])
        P = build_constraint_matrix([marg], u)
        Pmarg = Pjoint.marginal(marg)
        Pmarg.make_dense()
        assert(np.allclose(Pmarg.pmf, P @ Pjoint.pmf))
Exemplo n.º 10
0
def test_channel():
    nb_samples = 5
    for _ in range(nb_samples):
        # Build random dist but with I(X1; X2) = 0
        r = dit.random_distribution(3,2)
        pX, pYgX = r.condition_on([0,1])
        u = dit.distconst.uniform_distribution(2,2)
        dist = dit.cdisthelpers.joint_from_factors(u, pYgX, strict=False)

        # Compute disclosure and extract optimal synergistic channel
        S, C = disclosure_channel(dist)

        # Optimal synergistic channel is XOR and disclosure is I(X1 xor X2; Y)
        xorfun = lambda outcome: (np.mod(outcome[0] + outcome[1], 2),)
        dist = dit.insert_rvf(dist, xorfun)
        assert(np.allclose(C['pVgX'], [[1,0,0,1],[0,1,1,0]]))
        assert(np.isclose(S, coinformation(dist, [[2],[3]])))
Exemplo n.º 11
0
def test_simplex_grid4():
    # Test with Distribution but with wrong length specified.
    d = dit.random_distribution(2, 2)
    g = dit.simplex_grid(5, 2**2, using=d)
    np.testing.assert_raises(Exception, next, g)
Exemplo n.º 12
0
def test_simplex_grid4():
    # Test with Distribution but with wrong length specified.
    d = dit.random_distribution(2, 2)
    g = dit.simplex_grid(5, 2**2, using=d)
    np.testing.assert_raises(Exception, next, g)
Exemplo n.º 13
0
"""
Tests for the various mutual informations.
"""

import pytest

from dit import random_distribution
from dit.multivariate import (coinformation as I,
                              total_correlation as T,
                              dual_total_correlation as B,
                              caekl_mutual_information as J,
                              interaction_information as II,
                             )


@pytest.mark.parametrize('d', [random_distribution(2, 4) for _ in range(10)])
def test_mis1(d):
    """
    Test that all the mutual informations match for bivariate distributions.
    """
    i = I(d)
    t = T(d)
    b = B(d)
    j = J(d)
    ii = II(d)
    assert i == pytest.approx(t)
    assert t == pytest.approx(b)
    assert b == pytest.approx(j)
    assert j == pytest.approx(ii)
Exemplo n.º 14
0
def test_simplex_grid4():
    # Test with Distribution but with wrong length specified.
    d = dit.random_distribution(2, 2)
    g = dit.simplex_grid(5, 2**2, using=d)
    with pytest.raises(Exception):
        next(g)
Exemplo n.º 15
0
def test_simplex_grid4():
    # Test with Distribution but with wrong length specified.
    d = dit.random_distribution(2, 2)
    g = dit.simplex_grid(5, 2**2, using=d)
    with pytest.raises(Exception):
        next(g)
Exemplo n.º 16
0
"""
Tests for the various mutual informations.
"""

import pytest

from dit import random_distribution
from dit.multivariate import (
    coinformation as I,
    total_correlation as T,
    dual_total_correlation as B,
    caekl_mutual_information as J,
    interaction_information as II,
)


@pytest.mark.parametrize('d', [random_distribution(2, 4) for _ in range(10)])
def test_mis1(d):
    """
    Test that all the mutual informations match for bivariate distributions.
    """
    i = I(d)
    t = T(d)
    b = B(d)
    j = J(d)
    ii = II(d)
    assert i == pytest.approx(t)
    assert t == pytest.approx(b)
    assert b == pytest.approx(j)
    assert j == pytest.approx(ii)