<jupyter_start><jupyter_text>Information Flows in Causal NetworksThis notebook replicates some examples from [Ay & Polani (2008), "Information Flows in Causal Networks" in Advances in Complex Systems Volume 11, Issue 01](http://www.worldscientific.com/doi/abs/10.1142/S0219525908001465%0A)<jupyter_code>from causalinfo import *
from numpy import log2
from numpy.testing import assert_allclose
# You only need this if you want to draw pretty pictures of the Networksa
from nxpd import draw, nxpdParams
nxpdParams['show'] = 'ipynb'
w, x, y, z = make_variables("W X Y Z", 2)
wdist = UniformDist(w)<jupyter_output><empty_output><jupyter_text>Ay & Polani, Example 3<jupyter_code>eq1 = Equation('BR', [w], [x, y], equations.branch_same_)
eq2 = Equation('XOR', [x, y], [z], equations.xor_)
# Build the graph
eg3 = CausalGraph([eq1, eq2])
draw(eg3.full_network)
eg3
m_eg3 = MeasureCause(eg3, wdist)

# See the table on p29a
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<jupyter_output><empty_output><jupyter_text>Ay & Polani, Example 5.1<jupyter_code>def copy_first_(i1, i2, o1):
        o1[i1] = 1.0

eq2 = Equation('COPY_FIRST', [x, y], [z], copy_first_)
eg51 = CausalGraph([eq1, eq2])
draw(eg51.full_network)
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<jupyter_output><empty_output><jupyter_text>Ay & Polani, Example 5.2<jupyter_code>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])
draw(eg52.full_network)
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)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1412566975158305, 'fraction_numerical': 0.06039941548952752, 'mean_word_length': 5.37888198757764, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '44403', 'license': 'MIT', 'n_tokens_mistral': 935, 'n_tokens_neox': 839, 'n_words': 209, 'path': 'notebooks/ay_polani.ipynb', 'repo_name': 'brettc/causalinfo', 'size': '2053'}