def test_rvfunctions2(): # Smoke test with int tuples d = dit.Distribution([(0,0), (0,1), (1,0), (1,1)], [1/4]*4) bf = dit.RVFunctions(d) d = dit.insert_rvf(d, bf.xor([0,1])) d = dit.insert_rvf(d, bf.xor([1,2])) assert d.outcomes == ((0,0,0,0), (0,1,1,0), (1,0,1,1), (1,1,0,1))
def test_rvfunctions1(): # Smoke test with strings d = dit.Distribution(['00', '01', '10', '11'], [1/4]*4) bf = dit.RVFunctions(d) d = dit.insert_rvf(d, bf.xor([0,1])) d = dit.insert_rvf(d, bf.xor([1,2])) assert_equal(d.outcomes, ('0000', '0110', '1011', '1101'))
def test_rvfunctions2(): # Smoke test with int tuples d = dit.Distribution([(0,0), (0,1), (1,0), (1,1)], [1/4]*4) bf = dit.RVFunctions(d) d = dit.insert_rvf(d, bf.xor([0,1])) d = dit.insert_rvf(d, bf.xor([1,2])) assert_equal(d.outcomes, ((0,0,0,0), (0,1,1,0), (1,0,1,1), (1,1,0,1)))
def test_rvfunctions1(): # Smoke test with strings d = dit.Distribution(['00', '01', '10', '11'], [1 / 4] * 4) bf = dit.RVFunctions(d) d = dit.insert_rvf(d, bf.xor([0, 1])) d = dit.insert_rvf(d, bf.xor([1, 2])) assert_equal(d.outcomes, ('0000', '0110', '1011', '1101'))
def test_RVFunctions_from_mapping2(): d = dit.Distribution([(0,0), (0,1), (1,0), (1,1)], [1/4]*4) bf = dit.RVFunctions(d) mapping = {(0,0): 0, (0,1): 1, (1,0): 1, (1,1): 0} d = dit.insert_rvf(d, bf.from_mapping(mapping, force=True)) outcomes = ((0, 0, 0), (0, 1, 1), (1, 0, 1), (1, 1, 0)) assert_equal(d.outcomes, outcomes)
def test_RVFunctions_from_mapping1(): d = dit.Distribution(['00', '01', '10', '11'], [1/4]*4) bf = dit.RVFunctions(d) mapping = {'00': '0', '01': '1', '10': '1', '11': '0'} d = dit.insert_rvf(d, bf.from_mapping(mapping)) outcomes = ('000', '011', '101', '110') assert_equal(d.outcomes, outcomes)
def test_RVFunctions_from_mapping1(): d = dit.Distribution(['00', '01', '10', '11'], [1 / 4] * 4) bf = dit.RVFunctions(d) mapping = {'00': '0', '01': '1', '10': '1', '11': '0'} d = dit.insert_rvf(d, bf.from_mapping(mapping)) outcomes = ('000', '011', '101', '110') assert_equal(d.outcomes, outcomes)
def test_RVFunctions_from_partition(): d = dit.Distribution(['00', '01', '10', '11'], [1/4]*4) bf = dit.RVFunctions(d) partition = (('00','11'), ('01', '10')) d = dit.insert_rvf(d, bf.from_partition(partition)) outcomes = ('000', '011', '101', '110') assert_equal(d.outcomes, outcomes)
def test_RVFunctions_from_partition(): d = dit.Distribution(['00', '01', '10', '11'], [1 / 4] * 4) bf = dit.RVFunctions(d) partition = (('00', '11'), ('01', '10')) d = dit.insert_rvf(d, bf.from_partition(partition)) outcomes = ('000', '011', '101', '110') assert_equal(d.outcomes, outcomes)
def test_RVFunctions_from_mapping2(): d = dit.Distribution([(0, 0), (0, 1), (1, 0), (1, 1)], [1 / 4] * 4) bf = dit.RVFunctions(d) mapping = {(0, 0): 0, (0, 1): 1, (1, 0): 1, (1, 1): 0} d = dit.insert_rvf(d, bf.from_mapping(mapping, force=True)) outcomes = ((0, 0, 0), (0, 1, 1), (1, 0, 1), (1, 1, 0)) assert_equal(d.outcomes, outcomes)
def Or(k=2): """ [0] or [1] = [2] """ d = dit.uniform_distribution(k, ['01']) d = dit.distconst.modify_outcomes(d, lambda x: ''.join(x)) d = dit.insert_rvf(d, lambda x: '1' if any(map(bool, map(int, x))) else '0') return d
def test_rvfunctions_ints(): d = dit.uniform_distribution(2, 2) rvf = dit.RVFunctions(d) partition = [(d.outcomes[i],) for i in range(len(d))] mapping = rvf.from_partition(partition) d2 = dit.insert_rvf(d, mapping) outcomes = ((0,0,0), (0,1,1), (1,0,2), (1,1,3)) assert_equal(d2.outcomes, outcomes)
def test_rvfunctions_ints(): d = dit.uniform_distribution(2, 2) rvf = dit.RVFunctions(d) partition = [(d.outcomes[i], ) for i in range(len(d))] mapping = rvf.from_partition(partition) d2 = dit.insert_rvf(d, mapping) outcomes = ((0, 0, 0), (0, 1, 1), (1, 0, 2), (1, 1, 3)) assert_equal(d2.outcomes, outcomes)
def test_rvfunctions3(): # Smoke test strings with from_hexes outcomes = ['000', '001', '010', '011', '100', '101', '110', '111'] pmf = [1 / 8] * 8 d = dit.Distribution(outcomes, pmf) bf = dit.RVFunctions(d) d = dit.insert_rvf(d, bf.from_hexes('27')) outcomes = ('0000', '0010', '0101', '0110', '1000', '1010', '1100', '1111') assert_equal(d.outcomes, outcomes)
def test_rvfunctions3(): # Smoke test strings with from_hexes outcomes = ['000', '001', '010', '011', '100', '101', '110', '111'] pmf = [1/8] * 8 d = dit.Distribution(outcomes, pmf) bf = dit.RVFunctions(d) d = dit.insert_rvf(d, bf.from_hexes('27')) outcomes = ('0000', '0010', '0101', '0110', '1000', '1010', '1100', '1111') assert_equal(d.outcomes, outcomes)
def test_rvfunctions4(): # Smoke test int tuples from_hexes outcomes = ['000', '001', '010', '011', '100', '101', '110', '111'] outcomes = [tuple(map(int, o)) for o in outcomes] pmf = [1 / 8] * 8 d = dit.Distribution(outcomes, pmf) bf = dit.RVFunctions(d) d = dit.insert_rvf(d, bf.from_hexes('27')) outcomes = ('0000', '0010', '0101', '0110', '1000', '1010', '1100', '1111') outcomes = tuple(tuple(map(int, o)) for o in outcomes) assert_equal(d.outcomes, outcomes)
def iid_sum(n, k=2): """ Returns a distribution relating n iid uniform draws to their sum. """ alphabet = range(k) outcomes = list(itertools.product(alphabet, repeat=n)) pmf = [1] * len(outcomes) d = dit.Distribution(outcomes, pmf, base='linear', validate=False) d.normalize() d = dit.insert_rvf(d, lambda x: (sum(x), )) return d
def test_rvfunctions4(): # Smoke test int tuples from_hexes outcomes = ['000', '001', '010', '011', '100', '101', '110', '111'] outcomes = [tuple(map(int, o)) for o in outcomes] pmf = [1/8] * 8 d = dit.Distribution(outcomes, pmf) bf = dit.RVFunctions(d) d = dit.insert_rvf(d, bf.from_hexes('27')) outcomes = ('0000', '0010', '0101', '0110', '1000', '1010', '1100', '1111') outcomes = tuple(tuple(map(int, o)) for o in outcomes) assert_equal(d.outcomes, outcomes)
def test_insert_rvf2(): # Test multiple insertion. d = dit.uniform_distribution(2, 2) d = dit.modify_outcomes(d, lambda x: ''.join(map(str, x))) def xor(outcome): o = str(int(outcome[0] != outcome[1])) # Here we are returning 2 random variables return o*2 # We are also inserting two times simultaneously. d2 = dit.insert_rvf(d, [xor, xor]) outcomes = ('000000', '011111', '101111', '110000') assert d2.outcomes == outcomes
def test_insert_rvf2(): # Test multiple insertion. d = dit.uniform_distribution(2, 2) d = dit.modify_outcomes(d, lambda x: ''.join(map(str, x))) def xor(outcome): o = str(int(outcome[0] != outcome[1])) # Here we are returning 2 random variables return o*2 # We are also inserting two times simultaneously. d2 = dit.insert_rvf(d, [xor, xor]) outcomes = ('000000', '011111', '101111', '110000') assert_equal(d2.outcomes, outcomes)
def iid_sum(n, k=2): """ Returns a distribution relating n iid uniform draws to their sum. """ alphabet = range(k) outcomes = list(itertools.product(alphabet, repeat=n)) pmf = [1] * len(outcomes) d = dit.Distribution(outcomes, pmf, base='linear', validate=False) d.normalize() d = dit.insert_rvf(d, lambda x: (sum(x),)) return d
def test_insert_rvf1(): # Test multiple insertion. d = dit.uniform_distribution(2, 2) def xor(outcome): o = int(outcome[0] != outcome[1]) # Here we are returning 2 random variables return (o, o) # We are also inserting two times simultaneously. d2 = dit.insert_rvf(d, [xor, xor]) outcomes = ((0, 0, 0, 0, 0, 0), (0, 1, 1, 1, 1, 1), (1, 0, 1, 1, 1, 1), (1, 1, 0, 0, 0, 0)) assert_equal(d2.outcomes, outcomes)
def test_insert_rvf1(): # Test multiple insertion. d = dit.uniform_distribution(2, 2) def xor(outcome): o = int(outcome[0] != outcome[1]) # Here we are returning 2 random variables return (o,o) # We are also inserting two times simultaneously. d2 = dit.insert_rvf(d, [xor, xor]) outcomes = ( (0, 0, 0, 0, 0, 0), (0, 1, 1, 1, 1, 1), (1, 0, 1, 1, 1, 1), (1, 1, 0, 0, 0, 0) ) assert_equal(d2.outcomes, outcomes)
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]])))
def summed_dice(a=1, b=1): """ Two die X and Y are summed to form Z in the following way: .. math:: Z = X + b * Y X and Y are distributed as: .. math:: P(X=i, Y=j) = a / 36 + (1 - a) * delta_{ij} / 6 Parameters ---------- a : float Specifies how independent X and Y are. a=0 means X and Y are perfectly correlated. a=1 means X and Y are independent. b : integer An integer specifying how to add X and Y. Returns ------- d : distribution The joint distribution for :math:`P(X, Y, Z)`. References ---------- Malte Harder, Christoph Salge, Daniel Polani A Bivariate Measure of Redundant Information """ outcomes = list(itertools.product(range(1, 7), repeat=2)) def pmf_func(i, j): return a / 36 + (1 - a) * int(i == j) / 6 pmf = [pmf_func(i, j) for i, j in outcomes] d = dit.Distribution(outcomes, pmf) b = int(b) d = dit.insert_rvf(d, lambda x: (x[0] + b * x[1], )) return d
def summed_dice(a=1, b=1): """ Two die X and Y are summed to form Z in the following way: Z = X + b * Y X and Y are distributed as: P(X=i, Y=j) = a / 36 + (1 - a) * delta_{ij} / 6 Parameters ---------- a : float Specifies how independent X and Y are. a=0 means X and Y are perfectly correlated. a=1 means X and Y are independent. b : integer An integer specifying how to add X and Y. Returns ------- d : distribution The joint distribution for P(X, Y, Z). References ---------- Malte Harder, Christoph Salge, Daniel Polani A Bivariate Measure of Redundant Information """ outcomes = list(itertools.product(range(1, 7), repeat=2)) def pmf_func(i, j): return a / 36 + (1 - a) * int(i == j) / 6 pmf = [pmf_func(i, j) for i, j in outcomes] d = dit.Distribution(outcomes, pmf) b = int(b) d = dit.insert_rvf(d, lambda x: (x[0] + b * x[1],)) return d
def test_4bit_xor(): u = dit.distconst.uniform_distribution(3, 2) xorfun = lambda outcome: (np.mod(np.sum(outcome), 2), ) dist = dit.insert_rvf(u, xorfun) pid = PID_SD(dist) assert_only_atom(pid, ((0, 1), (0, 2), (1, 2)), 1)
def nbit_xor(n): u = dit.distconst.uniform_distribution(n-1, 2) xorfun = lambda outcome: (np.mod(np.sum(outcome), 2),) return dit.insert_rvf(u, xorfun)