Exemplo n.º 1
0
def test_setitem2():
    d = Distribution(['00', '11'], [1, 0])
    d.make_sparse()
    d['11'] = 1/2
    d.normalize()
    assert_true('11' in d)
    assert_almost_equal(d['11'], 1/3)
Exemplo n.º 2
0
def test_marginalize():
    outcomes = ['000', '011', '101', '110']
    pmf = [1/4]*4
    d = Distribution(outcomes, pmf)
    d1 = d.marginal([0, 2])
    d2 = d.marginalize([1])
    assert_true(d1.is_approx_equal(d2))
Exemplo n.º 3
0
def test_init1():
    # Invalid initializations.
    with pytest.raises(InvalidDistribution):
        Distribution([])
    with pytest.raises(InvalidDistribution):
        Distribution([], [])
    Distribution([], [], sample_space=[(0, 1)], validate=False)
Exemplo n.º 4
0
def test_outcome_length():
    outcomes = ['000', '011', '101', '110']
    pmf = [1/4]*4
    d = Distribution(outcomes, pmf)
    d = d.marginal([0, 2])
    assert_equal(d.outcome_length(), 2)
    assert_equal(d.outcome_length(masked=True), 3)
Exemplo n.º 5
0
def test_outcome_length():
    outcomes = ['000', '011', '101', '110']
    pmf = [1 / 4] * 4
    d = Distribution(outcomes, pmf)
    d = d.marginal([0, 2])
    assert d.outcome_length() == 2
    assert d.outcome_length(masked=True) == 3
Exemplo n.º 6
0
def test_setitem2():
    d = Distribution(['00', '11'], [1, 0])
    d.make_sparse()
    d['11'] = 1/2
    d.normalize()
    assert '11' in d
    assert d['11'] == pytest.approx(1/3)
Exemplo n.º 7
0
def test_marginalize():
    outcomes = ['000', '011', '101', '110']
    pmf = [1 / 4] * 4
    d = Distribution(outcomes, pmf)
    d1 = d.marginal([0, 2])
    d2 = d.marginalize([1])
    assert d1.is_approx_equal(d2)
Exemplo n.º 8
0
def test_init8():
    outcomes = [(0,), (1,)]
    pmf = [1 / 2] * 2
    d1 = ScalarDistribution(pmf)
    d2 = Distribution.from_distribution(d1)
    d3 = Distribution(outcomes, pmf)
    assert d2.is_approx_equal(d3)
Exemplo n.º 9
0
def test_init9():
    outcomes = [(0,), (1,)]
    pmf = [1 / 2] * 2
    d1 = ScalarDistribution(pmf)
    d2 = Distribution.from_distribution(d1, base=10)
    d3 = Distribution(outcomes, pmf)
    d3.set_base(10)
    assert d2.is_approx_equal(d3)
Exemplo n.º 10
0
def test_copy():
    outcomes = ['0', '1']
    pmf = [1/2, 1/2]
    d1 = Distribution(outcomes, pmf)
    d2 = d1.copy(base=10)
    d3 = Distribution(outcomes, pmf)
    d3.set_base(10)
    assert_true(d2.is_approx_equal(d3))
Exemplo n.º 11
0
def test_init9():
    outcomes = [(0,), (1,)]
    pmf = [1/2, 1/2]
    d1 = ScalarDistribution(pmf)
    d2 = Distribution.from_distribution(d1, base=10)
    d3 = Distribution(outcomes, pmf)
    d3.set_base(10)
    assert_true(d2.is_approx_equal(d3))
Exemplo n.º 12
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 = outcomes
    assert_equal( list(d.atoms()), atoms)

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

    d = Distribution(outcomes, pmf, sample_space=outcomes + ['444'])
    atoms = outcomes + ['444']
    assert_equal( list(d.atoms()), atoms)
Exemplo n.º 13
0
def test_init8():
    outcomes = [(0,), (1,)]
    pmf = [1/2, 1/2]
    d1 = ScalarDistribution(pmf)
    d2 = Distribution.from_distribution(d1)
    d3 = Distribution(outcomes, pmf)
    assert d2.is_approx_equal(d3)
Exemplo n.º 14
0
def test_init2():
    # Cannot initialize with an iterator.
    # Must pass in a sequence for outcomes.
    outcomes = map(int, ['0', '1', '2', '3', '4'])
    pmf = [1 / 5] * 5
    with pytest.raises(TypeError):
        Distribution(outcomes, pmf)
Exemplo n.º 15
0
def test_copy():
    outcomes = ['0', '1']
    pmf = [1 / 2] * 2
    d1 = Distribution(outcomes, pmf)
    d2 = d1.copy(base=10)
    d3 = Distribution(outcomes, pmf)
    d3.set_base(10)
    assert d2.is_approx_equal(d3)
Exemplo n.º 16
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)
Exemplo n.º 17
0
def test_setitem2():
    d = Distribution(['00', '11'], [1, 0])
    d.make_sparse()
    d['11'] = 1 / 2
    d.normalize()
    assert '11' in d
    assert d['11'] == pytest.approx(1 / 3)
Exemplo n.º 18
0
def test_setitem2():
    d = Distribution(['00', '11'], [1, 0])
    d.make_sparse()
    d['11'] = 1 / 2
    d.normalize()
    assert_true('11' in d)
    assert_almost_equal(d['11'], 1 / 3)
Exemplo n.º 19
0
def test_zipped():
    pmf = [.125, .125, .125, .125, .25, 0, .25]
    outcomes = ['000', '011', '101', '110', '222', '321', '333']
    d = Distribution(outcomes, pmf)

    outcomes_, pmf_ = list(zip(*d.zipped()))
    d2 = Distribution(outcomes_, pmf_)
    assert_true(d.is_approx_equal(d2))

    outcomes_, pmf_ = list(zip(*d.zipped(mode='atoms')))
    d3 = Distribution(outcomes_, pmf_)
    assert_true(d.is_approx_equal(d3))

    outcomes_, pmf_ = list(zip(*d.zipped(mode='patoms')))
    d4 = Distribution(outcomes_, pmf_)
    d.make_sparse()
    np.testing.assert_allclose(d.pmf, d4.pmf)
Exemplo n.º 20
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))
Exemplo n.º 21
0
def test_init10():
    d1 = Distribution([(0, 0), (0, 1), (1, 0)], [0.5, 0.25, 0.25])
    pmf = [[0.5, 0.25], [0.25, 0]]
    d2 = Distribution.from_ndarray(pmf)
    assert d1.is_approx_equal(d2)
Exemplo n.º 22
0
def test_set_rv_names2():
    outcomes = ['00', '11']
    pmf = [1 / 2, 1 / 2]
    d = Distribution(outcomes, pmf)
    assert_raises(ditException, d.set_rv_names, 'XYZ')
Exemplo n.º 23
0
def test_init6():
    outcomes = {'0', '1', '2'}
    pmf = [1 / 3] * 3
    with pytest.raises(ditException):
        Distribution(outcomes, pmf)
Exemplo n.º 24
0
def test_init4():
    dist = {'0': 1 / 2, '1': 1 / 2}
    pmf = [1 / 2] * 2
    with pytest.raises(InvalidDistribution):
        Distribution(dist, pmf)
Exemplo n.º 25
0
def test_from_rv():
    rv = sps.rv_discrete(values=([1, 2], [3 / 10, 7 / 10]))
    d1 = Distribution.from_rv_discrete(rv)
    d2 = Distribution([(1,), (2,)], [3 / 10, 7 / 10])
    assert d1.is_approx_equal(d2)
Exemplo n.º 26
0
def test_set_rv_names2():
    outcomes = ['00', '11']
    pmf = [1 / 2] * 2
    d = Distribution(outcomes, pmf)
    with pytest.raises(ditException):
        d.set_rv_names('XYZ')
Exemplo n.º 27
0
def test_is_homogeneous2():
    outcomes = ['00', '01']
    pmf = [1 / 2] * 2
    d = Distribution(outcomes, pmf)
    assert not d.is_homogeneous()
Exemplo n.º 28
0
def test_is_homogeneous1():
    outcomes = ['00', '11']
    pmf = [1 / 2, 1 / 2]
    d = Distribution(outcomes, pmf)
    assert d.is_homogeneous()
Exemplo n.º 29
0
def test_coalesce():
    outcomes = ['000', '011', '101', '110']
    pmf = [1/4]*4
    d = Distribution(outcomes, pmf)
    d = d.coalesce([[0, 1], [2]])
    assert_equal(d.outcome_length(), 2)
Exemplo n.º 30
0
def test_has_outcome1():
    d = Distribution(['0', '1'], [1, 0])
    d.make_sparse()
    assert_false(d.has_outcome('1', null=False))
Exemplo n.º 31
0
def test_init7():
    outcomes = ['0', '1']
    pmf = [1/2, 1/2]
    d1 = Distribution(outcomes, pmf)
    d2 = Distribution.from_distribution(d1)
    assert_true(d1.is_approx_equal(d2))
Exemplo n.º 32
0
def test_has_outcome2():
    d = Distribution(['0', '1'], [1, 0])
    assert_false(d.has_outcome('1', null=False))
Exemplo n.º 33
0
def test_is_homogeneous2():
    outcomes = ['00', '01']
    pmf = [1/2, 1/2]
    d = Distribution(outcomes, pmf)
    assert_false(d.is_homogeneous())
Exemplo n.º 34
0
def test_zipped():
    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)

    outcomes_, pmf_ = list(zip(*d.zipped()))
    d2 = Distribution(outcomes_, pmf_)
    assert d.is_approx_equal(d2)

    outcomes_, pmf_ = list(zip(*d.zipped(mode='atoms')))
    d3 = Distribution(outcomes_, pmf_)
    assert d.is_approx_equal(d3)

    outcomes_, pmf_ = list(zip(*d.zipped(mode='patoms')))
    d4 = Distribution(outcomes_, pmf_)
    d.make_sparse()
    assert np.allclose(d.pmf, d4.pmf)
Exemplo n.º 35
0
def test_has_outcome2():
    d = Distribution(['0', '1'], [1, 0])
    assert not d.has_outcome('1', null=False)
Exemplo n.º 36
0
def test_setitem1():
    d = Distribution(['0', '1'], [1 / 2] * 2)
    with pytest.raises(InvalidOutcome):
        d.__setitem__('2', 0)
Exemplo n.º 37
0
def test_init3():
    dist = {'0': 1 / 2, '1': 1 / 2}
    d = Distribution(dist)
    assert_equal(d.outcomes, ('0', '1'))
    assert_array_almost_equal(d.pmf, [1 / 2, 1 / 2])
Exemplo n.º 38
0
def test_init10():
    d1 = Distribution([(0, 0), (0, 1), (1, 0)], [0.5, 0.25, 0.25])
    pmf = [[0.5, 0.25], [0.25, 0]]
    d2 = Distribution.from_ndarray(pmf)
    assert d1.is_approx_equal(d2)
Exemplo n.º 39
0
def test_init1():
    # Invalid initializations.
    assert_raises(InvalidDistribution, Distribution, [])
    assert_raises(InvalidDistribution, Distribution, [], [])
    Distribution([], [], sample_space=[(0, 1)], validate=False)
Exemplo n.º 40
0
def test_from_rv():
    rv = sps.rv_discrete(values=([1, 2], [3/10, 7/10]))
    d1 = Distribution.from_rv_discrete(rv)
    d2 = Distribution([(1,), (2,)], [3/10, 7/10])
    assert d1.is_approx_equal(d2)
Exemplo n.º 41
0
def test_init3():
    dist = {'0': 1 / 2, '1': 1 / 2}
    d = Distribution(dist)
    assert d.outcomes == ('0', '1')
    assert np.allclose(d.pmf, [1 / 2] * 2)
Exemplo n.º 42
0
def test_coalesce():
    outcomes = ['000', '011', '101', '110']
    pmf = [1 / 4] * 4
    d = Distribution(outcomes, pmf)
    d = d.coalesce([[0, 1], [2]])
    assert d.outcome_length() == 2
Exemplo n.º 43
0
def test_init5():
    outcomes = ['0', '1', '2']
    pmf = [1 / 2] * 2
    with pytest.raises(InvalidDistribution):
        Distribution(outcomes, pmf)
Exemplo n.º 44
0
def test_set_rv_names2():
    outcomes = ['00', '11']
    pmf = [1/2, 1/2]
    d = Distribution(outcomes, pmf)
    with pytest.raises(ditException):
        d.set_rv_names('XYZ')
Exemplo n.º 45
0
def test_init7():
    outcomes = ['0', '1']
    pmf = [1 / 2] * 2
    d1 = Distribution(outcomes, pmf)
    d2 = Distribution.from_distribution(d1)
    assert d1.is_approx_equal(d2)
Exemplo n.º 46
0
def test_setitem1():
    d = Distribution(['0', '1'], [1/2, 1/2])
    with pytest.raises(InvalidOutcome):
        d.__setitem__('2', 0)
Exemplo n.º 47
0
def test_is_homogeneous2():
    outcomes = ['00', '01']
    pmf = [1 / 2, 1 / 2]
    d = Distribution(outcomes, pmf)
    assert_false(d.is_homogeneous())
Exemplo n.º 48
0
def test_has_outcome1():
    d = Distribution(['0', '1'], [1, 0])
    d.make_sparse()
    assert not d.has_outcome('1', null=False)
Exemplo n.º 49
0
def test_setitem1():
    d = Distribution(['0', '1'], [1 / 2, 1 / 2])
    assert_raises(InvalidOutcome, d.__setitem__, '2', 0)
Exemplo n.º 50
0
def test_is_homogeneous1():
    outcomes = ['00', '11']
    pmf = [1/2, 1/2]
    d = Distribution(outcomes, pmf)
    assert_true(d.is_homogeneous())