Пример #1
0
def test_nested():
    outcomes = ['11', '00']
    ss = CartesianProduct.from_outcomes(outcomes)
    ss2 = CartesianProduct([[0], ss])
    assert list(ss2) == [(0, '11'), (0, '10'), (0, '01'), (0, '00')]
    ss2.sort()
    assert list(ss2) == [(0, '00'), (0, '01'), (0, '10'), (0, '11')]
Пример #2
0
def test_nested():
    outcomes = ['11', '00']
    ss = CartesianProduct.from_outcomes(outcomes)
    ss2 = CartesianProduct([[0], ss])
    assert_equal(list(ss2), [(0,'11'), (0,'10'), (0,'01'), (0,'00')])
    ss2.sort()
    assert_equal(list(ss2), [(0,'00'), (0,'01'), (0,'10'), (0,'11')])
Пример #3
0
    def test_sort(self):
        alphabets = [[0,1], [2,1]]
        ss = CartesianProduct(alphabets)
        indexes = [ss.index(i) for i in list(ss)]
        assert_equal(indexes, list(range(len(ss))))

        samplespace = list(ss)
        ss.sort()
        indexes = [ss.index(i) for i in samplespace]
        assert_equal(indexes, [1, 0, 3, 2])
Пример #4
0
class TestCartesianProduct(object):
    def setup_class(self):
        product = dit.helpers.get_product_func(str)
        alphabets = [['0', '1'], ['0', '1', '2']]
        self.ss = CartesianProduct(alphabets, product)

    def test_stralphabets(self):
        # Test that str product is inferred.
        x = CartesianProduct([['0', '1']] * 2)
        assert list(x) == ['00', '01', '10', '11']

    def test_samplespace(self):
        assert list(self.ss) == ['00', '01', '02', '10', '11', '12']
        assert len(self.ss) == 6
        assert self.ss.outcome_length() == 2
        assert '00' in self.ss
        assert not '22' in self.ss

    def test_marginalize(self):
        ss0 = self.ss.marginalize([1])
        assert list(ss0) == ['0', '1']

    def test_marginal(self):
        ss1 = self.ss.marginal([1])
        assert list(ss1) == ['0', '1', '2']

    def test_coalesce(self):
        ss2 = self.ss.coalesce([[1, 0], [1]])
        ss2_ = [
            ('00', '0'), ('00', '1'), ('00', '2'), ('01', '0'), ('01', '1'),
            ('01', '2'), ('10', '0'), ('10', '1'), ('10', '2'), ('11', '0'),
            ('11', '1'), ('11', '2'), ('20', '0'), ('20', '1'), ('20', '2'),
            ('21', '0'), ('21', '1'), ('21', '2')
        ]
        assert list(ss2) == ss2_

    def test_sort(self):
        alphabets = [[0, 1], [2, 1]]
        ss = CartesianProduct(alphabets)
        indexes = [ss.index(i) for i in list(ss)]
        assert indexes == list(range(len(ss)))

        samplespace = list(ss)
        ss.sort()
        indexes = [ss.index(i) for i in samplespace]
        assert indexes == [1, 0, 3, 2]

    def test_from_outcomes(self):
        outcomes = ['00', '11']
        ss = CartesianProduct.from_outcomes(outcomes)
        assert list(ss) == ['00', '01', '10', '11']
        with pytest.raises(ValueError):
            ss.index('22')
Пример #5
0
class TestCartesianProduct(object):
    def setup_class(self):
        product = dit.helpers.get_product_func(str)
        alphabets = [['0', '1'], ['0', '1', '2']]
        self.ss = CartesianProduct(alphabets, product)

    def test_stralphabets(self):
        # Test that str product is inferred.
        x = CartesianProduct([['0', '1']]*2)
        assert list(x) == ['00', '01', '10', '11']

    def test_samplespace(self):
        assert list(self.ss) == ['00', '01', '02', '10', '11', '12']
        assert len(self.ss) == 6
        assert self.ss.outcome_length() == 2
        assert '00' in self.ss
        assert not '22' in self.ss

    def test_marginalize(self):
        ss0 = self.ss.marginalize([1])
        assert list(ss0) == ['0', '1']

    def test_marginal(self):
        ss1 = self.ss.marginal([1])
        assert list(ss1) == ['0', '1', '2']

    def test_coalesce(self):
        ss2 = self.ss.coalesce([[1,0],[1]])
        ss2_ =  [('00', '0'), ('00', '1'), ('00', '2'),
                 ('01', '0'), ('01', '1'), ('01', '2'),
                 ('10', '0'), ('10', '1'), ('10', '2'),
                 ('11', '0'), ('11', '1'), ('11', '2'),
                 ('20', '0'), ('20', '1'), ('20', '2'),
                 ('21', '0'), ('21', '1'), ('21', '2')]
        assert list(ss2) == ss2_

    def test_sort(self):
        alphabets = [[0,1], [2,1]]
        ss = CartesianProduct(alphabets)
        indexes = [ss.index(i) for i in list(ss)]
        assert indexes == list(range(len(ss)))

        samplespace = list(ss)
        ss.sort()
        indexes = [ss.index(i) for i in samplespace]
        assert indexes == [1, 0, 3, 2]

    def test_from_outcomes(self):
        outcomes = ['00', '11']
        ss = CartesianProduct.from_outcomes(outcomes)
        assert list(ss) == ['00', '01', '10', '11']
        with pytest.raises(ValueError):
            ss.index('22')
Пример #6
0
class TestCartesianProduct(object):
    def setUp(self):
        product = dit.helpers.get_product_func(str)
        alphabets = [['0', '1'], ['0', '1', '2']]
        self.ss = CartesianProduct(alphabets, product)

    def test_stralphabets(self):
        # Test that str product is inferred.
        x = CartesianProduct([['0', '1']]*2)
        assert_equal(list(x), ['00', '01', '10', '11'])

    def test_samplespace(self):
        assert_equal(list(self.ss), ['00', '01', '02', '10', '11', '12'])
        assert_equal(len(self.ss), 6)
        assert_equal(self.ss.outcome_length(), 2)
        assert_true('00' in self.ss)
        assert_false('22' in self.ss)

    def test_marginalize(self):
        ss0 = self.ss.marginalize([1])
        assert_equal(list(ss0), ['0', '1'])

    def test_marginal(self):
        ss1 = self.ss.marginal([1])
        assert_equal(list(ss1), ['0', '1', '2'])

    def test_coalesce(self):
        ss2 = self.ss.coalesce([[1,0],[1]])
        ss2_ =  [('00', '0'), ('00', '1'), ('00', '2'),
                 ('01', '0'), ('01', '1'), ('01', '2'),
                 ('10', '0'), ('10', '1'), ('10', '2'),
                 ('11', '0'), ('11', '1'), ('11', '2'),
                 ('20', '0'), ('20', '1'), ('20', '2'),
                 ('21', '0'), ('21', '1'), ('21', '2')]
        assert_equal(list(ss2), ss2_)

    def test_sort(self):
        alphabets = [[0,1], [2,1]]
        ss = CartesianProduct(alphabets)
        indexes = [ss.index(i) for i in list(ss)]
        assert_equal(indexes, list(range(len(ss))))

        samplespace = list(ss)
        ss.sort()
        indexes = [ss.index(i) for i in samplespace]
        assert_equal(indexes, [1, 0, 3, 2])

    def test_from_outcomes(self):
        outcomes = ['00', '11']
        ss = CartesianProduct.from_outcomes(outcomes)
        assert_equal(list(ss), ['00', '01', '10', '11'])
        assert_raises(ValueError, ss.index, '22')
Пример #7
0
def expanded_samplespace(d, alphabets=None, union=True):
    """
    Returns a new distribution with an expanded sample space.

    Expand the sample space so that it is the Cartesian product of the
    alphabets for each random variable. Note, only the effective alphabet of
    each random variable is used. So if one index in an outcome only has the
    value 1, then its alphabet is [1], and not [0,1] for example.

    Parameters
    ----------
    d : distribution
        The distribution used to create the pruned distribution.

    alphabets : list
        A list of alphabets, with length equal to the outcome length in `d`.
        Each alphabet specifies the alphabet to be used for a single index
        random variable. The sample space of the new distribution will be the
        Cartesian product of these alphabets.

    union : bool
        If True, then the alphabet for each random variable is unioned.
        The unioned alphabet is then used for each random variable.

    Returns
    -------
    ed : distribution
        The distribution with an expanded sample space.

    Notes
    -----
    The default constructor for Distribution will create a Cartesian product
    sample space if not sample space is provided.

    """
    joint = d.is_joint()

    if alphabets is None:
        # Note, we sort the alphabets now, so we are possibly changing the
        # order of the original sample space.
        alphabets = list(map(sorted, d.alphabet))
    elif joint and len(alphabets) != d.outcome_length():
        L = len(alphabets)
        raise Exception("You need to provide {0} alphabets".format(L))

    if joint and union:
        alphabet = set.union(*map(set, alphabets))
        alphabet = list(sorted(alphabet))
        alphabets = [alphabet] * len(alphabets)

    if joint:
        sample_space = CartesianProduct(alphabets, d._product)
    else:
        sample_space = ScalarSampleSpace(alphabets)

    ed = d.__class__(d.outcomes,
                     d.pmf,
                     sample_space=sample_space,
                     base=d.get_base())
    return ed
Пример #8
0
def test_atoms():
    pmf = [0.125, 0.125, 0.125, 0.125, 0.25, 0, 0.25]
    outcomes = ['000', '011', '101', '110', '222', '321', '333']
    d = Distribution(outcomes, pmf)

    atoms = d._product(['0', '1', '2', '3'], repeat=3)
    assert list(d.atoms()) == list(atoms)

    patoms = ['000', '011', '101', '110', '222', '333']
    assert list(d.atoms(patoms=True)) == patoms

    ss = CartesianProduct.from_outcomes(outcomes + ['444'])
    d = Distribution(outcomes, pmf, sample_space=ss)
    atoms = d._product(['0', '1', '2', '3', '4'], repeat=3)
    assert list(d.atoms()) == list(atoms)
Пример #9
0
def test_atoms():
    pmf = [.125, .125, .125, .125, .25, 0, .25]
    outcomes = ['000', '011', '101', '110', '222', '321', '333']
    d = Distribution(outcomes, pmf)

    atoms = d._product(['0','1','2', '3'], repeat=3)
    assert_equal(list(d.atoms()), list(atoms))

    patoms = ['000', '011', '101', '110', '222', '333']
    assert_equal(list(d.atoms(patoms=True)), patoms)

    ss = CartesianProduct.from_outcomes(outcomes + ['444'])
    d = Distribution(outcomes, pmf, sample_space=ss)
    atoms = d._product(['0','1','2', '3', '4'], repeat=3)
    assert_equal(list(d.atoms()), list(atoms))
Пример #10
0
    def test_sort(self):
        alphabets = [[0, 1], [2, 1]]
        ss = CartesianProduct(alphabets)
        indexes = [ss.index(i) for i in list(ss)]
        assert indexes == list(range(len(ss)))

        samplespace = list(ss)
        ss.sort()
        indexes = [ss.index(i) for i in samplespace]
        assert indexes == [1, 0, 3, 2]
Пример #11
0
 def test_from_outcomes(self):
     outcomes = ['00', '11']
     ss = CartesianProduct.from_outcomes(outcomes)
     assert_equal(list(ss), ['00', '01', '10', '11'])
     assert_raises(ValueError, ss.index, '22')
Пример #12
0
 def setUp(self):
     product = dit.helpers.get_product_func(str)
     alphabets = [['0', '1'], ['0', '1', '2']]
     self.ss = CartesianProduct(alphabets, product)
Пример #13
0
 def test_stralphabets(self):
     # Test that str product is inferred.
     x = CartesianProduct([['0', '1']] * 2)
     assert list(x) == ['00', '01', '10', '11']
Пример #14
0
 def setup_class(self):
     product = dit.helpers.get_product_func(str)
     alphabets = [['0', '1'], ['0', '1', '2']]
     self.ss = CartesianProduct(alphabets, product)
Пример #15
0
 def test_from_outcomes(self):
     outcomes = ['00', '11']
     ss = CartesianProduct.from_outcomes(outcomes)
     assert list(ss) == ['00', '01', '10', '11']
     with pytest.raises(ValueError):
         ss.index('22')
Пример #16
0
 def test_from_outcomes(self):
     outcomes = ['00', '11']
     ss = CartesianProduct.from_outcomes(outcomes)
     assert list(ss) == ['00', '01', '10', '11']
     with pytest.raises(ValueError):
         ss.index('22')