Ejemplo n.º 1
0
def test_ay_polani():
    w, x, y, z = make_variables("W X Y Z", 2)
    wdist = UniformDist(w)

    # Ay & Polani, Example 3
    eq1 = Equation('BR', [w], [x, y], equations.branch_same_)
    eq2 = Equation('XOR', [x, y], [z], equations.xor_)

    # Build the graph
    eg3 = CausalGraph([eq1, eq2])
    m_eg3 = MeasureCause(eg3, wdist)

    # See the table on p29
    assert m_eg3.mutual_info(x, y) == 1
    assert m_eg3.mutual_info(x, y, w) == 0
    assert m_eg3.mutual_info(w, z, y) == 0

    assert m_eg3.causal_flow(x, y) == 0
    assert m_eg3.causal_flow(x, y, w) == 0
    assert m_eg3.causal_flow(w, z, y) == 1

    # Ay & Polani, Example 5.1
    def copy_first_(i1, i2, o1):
        o1[i1] = 1.0

    eq2 = Equation('COPYX', [x, y], [z], copy_first_)
    eg51 = CausalGraph([eq1, eq2])
    m_eg51 = MeasureCause(eg51, wdist)

    # See paragraph at top of page 30
    assert m_eg51.mutual_info(x, z, y) == 0
    assert m_eg51.causal_flow(x, z, y) == 1
    assert m_eg51.causal_flow(x, z) == 1

    # Ay & Polani, Example 5.2
    def random_sometimes_(i1, i2, o1):
        if i1 != i2:
            o1[:] = .5
        else:
            equations.xor_(i1, i2, o1)

    eq2 = Equation('RAND', [x, y], [z], random_sometimes_)
    eg52 = CausalGraph([eq1, eq2])
    m_eg52 = MeasureCause(eg52, wdist)

    # See pg 30
    expected = 3.0 / 4.0 * log2(4.0 / 3.0)
    assert_allclose(m_eg52.causal_flow(x, z, y), expected)
Ejemplo n.º 2
0
def test_ay_polani():
    w, x, y, z = make_variables("W X Y Z", 2)
    wdist = UniformDist(w)

    # Ay & Polani, Example 3
    eq1 = Equation('BR', [w], [x, y], equations.branch_same_)
    eq2 = Equation('XOR', [x, y], [z], equations.xor_)

    # Build the graph
    eg3 = CausalGraph([eq1, eq2])
    m_eg3 = MeasureCause(eg3, wdist)

    # See the table on p29
    assert m_eg3.mutual_info(x, y) == 1
    assert m_eg3.mutual_info(x, y, w) == 0
    assert m_eg3.mutual_info(w, z, y) == 0

    assert m_eg3.causal_flow(x, y) == 0
    assert m_eg3.causal_flow(x, y, w) == 0
    assert m_eg3.causal_flow(w, z, y) == 1

    # Ay & Polani, Example 5.1
    def copy_first_(i1, i2, o1):
        o1[i1] = 1.0

    eq2 = Equation('COPYX', [x, y], [z], copy_first_)
    eg51 = CausalGraph([eq1, eq2])
    m_eg51 = MeasureCause(eg51, wdist)

    # See paragraph at top of page 30
    assert m_eg51.mutual_info(x, z, y) == 0
    assert m_eg51.causal_flow(x, z, y) == 1
    assert m_eg51.causal_flow(x, z) == 1

    # Ay & Polani, Example 5.2
    def random_sometimes_(i1, i2, o1):
        if i1 != i2:
            o1[:] = .5
        else:
            equations.xor_(i1, i2, o1)

    eq2 = Equation('RAND', [x, y], [z], random_sometimes_)
    eg52 = CausalGraph([eq1, eq2])
    m_eg52 = MeasureCause(eg52, wdist)

    # See pg 30
    expected = 3.0 / 4.0 * log2(4.0 / 3.0)
    assert_allclose(m_eg52.causal_flow(x, z, y), expected)
Ejemplo n.º 3
0
def test_controlled_diamond():
    """This examples can move us from a correlation case to a diamond
    """
    c1, c2, s1, s2, s3, s4, a1 = make_variables('c1 c2 s1 s2 s3 s4 a1', 2)

    eq1 = Equation('SAME', [c1], [s1], equations.same_)
    eq2 = Equation('SAMEB', [c2], [s2, s3], equations.branch_same_)
    eq3 = Equation('AND', [s1, s2], [s4], equations.and_)
    eq4 = Equation('OR', [s3, s4], [a1], equations.or_)
    net = CausalGraph([eq1, eq2, eq3, eq4])

    # Let's just use Uniform
    m = MeasureCause(net, UniformDist(c1, c2))

    # Mutual info is pretty useless, as it is the same across these...
    assert m.mutual_info(s2, a1) == m.mutual_info(s3, a1)

    # Look how much better average sad is!
    assert m.average_sad(s2, a1) < m.average_sad(s3, a1)
Ejemplo n.º 4
0
def test_controlled_diamond():
    """This examples can move us from a correlation case to a diamond
    """
    c1, c2, s1, s2, s3, s4, a1 = make_variables('c1 c2 s1 s2 s3 s4 a1', 2)

    eq1 = Equation('SAME', [c1], [s1], equations.same_)
    eq2 = Equation('SAMEB', [c2], [s2, s3], equations.branch_same_)
    eq3 = Equation('AND', [s1, s2], [s4], equations.and_)
    eq4 = Equation('OR', [s3, s4], [a1], equations.or_)
    net = CausalGraph([eq1, eq2, eq3, eq4])

    # Let's just use Uniform
    m = MeasureCause(net, UniformDist(c1, c2))

    # Mutual info is pretty useless, as it is the same across these...
    assert m.mutual_info(s2, a1) == m.mutual_info(s3, a1)

    # Look how much better average sad is!
    assert m.average_sad(s2, a1) < m.average_sad(s3, a1)
Ejemplo n.º 5
0
def test_random_spec():
    def add_noise_(i1, i2, o1):
        if i2:
            # Random Coin toss
            o1[:] = .5
        else:
            # Otherwise specific
            o1[i1] = 1.0

    c1, c2, s1, a = make_variables('C1 C2 S1 A', 2)
    eq1 = Equation('Send1', [c1, c2], [s1], add_noise_)
    eq2 = Equation('Send2', [s1], [a], equations.same_)
    network = CausalGraph([eq1, eq2])
    root_dist = JointDist({c1: [.2, .8], c2: [.5, .5]})
    m = MeasureCause(network, root_dist)

    # Spec is the same
    assert m.mutual_info(s1, a) == m.average_sad(s1, a)
Ejemplo n.º 6
0
def test_random_spec():

    def add_noise_(i1, i2, o1):
        if i2:
            # Random Coin toss
            o1[:] = .5
        else:
            # Otherwise specific
            o1[i1] = 1.0

    c1, c2, s1, a = make_variables('C1 C2 S1 A', 2)
    eq1 = Equation('Send1', [c1, c2], [s1], add_noise_)
    eq2 = Equation('Send2', [s1], [a], equations.same_)
    network = CausalGraph([eq1, eq2])
    root_dist = JointDist({c1: [.2, .8], c2: [.5, .5]})
    m = MeasureCause(network, root_dist)

    # Spec is the same
    assert m.mutual_info(s1, a) == m.average_sad(s1, a)
Ejemplo n.º 7
0
def test_correlation():
    c, s, a, k = make_variables('C S A K', 2)
    eq1 = Equation('Send', [c], [s, k], equations.branch_same_)
    eq2 = Equation('Recv', [s], [a], equations.same_)
    network = CausalGraph([eq1, eq2])
    root_dist = JointDist({c: [.7, .3]})
    m = MeasureCause(network, root_dist)

    assert_allclose(m.mutual_info(s, a), m.mutual_info(k, a))
    assert_allclose(m.causal_flow(s, a), m.average_sad(s, a))

    assert m.causal_flow(k, a) == 0
    assert m.average_sad(k, a) == 0
    assert_allclose(m.average_sad(s, a), m.mutual_info(s, a))

    assert m.mutual_info(k, a) == m.mutual_info(s, a)
    assert m.mutual_info(k, a, s) == 0
    assert m.mutual_info(s, a, k) == 0
Ejemplo n.º 8
0
def test_correlation():
    c, s, a, k = make_variables('C S A K', 2)
    eq1 = Equation('Send', [c], [s, k], equations.branch_same_)
    eq2 = Equation('Recv', [s], [a], equations.same_)
    network = CausalGraph([eq1, eq2])
    root_dist = JointDist({c: [.7, .3]})
    m = MeasureCause(network, root_dist)

    assert_allclose(m.mutual_info(s, a), m.mutual_info(k, a))
    assert_allclose(m.causal_flow(s, a), m.average_sad(s, a))

    assert m.causal_flow(k, a) == 0
    assert m.average_sad(k, a) == 0
    assert_allclose(m.average_sad(s, a), m.mutual_info(s, a))

    assert m.mutual_info(k, a) == m.mutual_info(s, a)
    assert m.mutual_info(k, a, s) == 0
    assert m.mutual_info(s, a, k) == 0