예제 #1
0
파일: test_npdist.py 프로젝트: chebee7i/dit
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)
예제 #2
0
파일: test_npdist.py 프로젝트: chebee7i/dit
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))
예제 #3
0
def test_init1():
    # Invalid initializations.
    with pytest.raises(InvalidDistribution):
        Distribution([])
    with pytest.raises(InvalidDistribution):
        Distribution([], [])
    Distribution([], [], sample_space=[(0, 1)], validate=False)
예제 #4
0
파일: test_npdist.py 프로젝트: chebee7i/dit
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)
예제 #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
예제 #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)
예제 #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)
예제 #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)
예제 #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)
예제 #10
0
파일: test_npdist.py 프로젝트: chebee7i/dit
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))
예제 #11
0
파일: test_npdist.py 프로젝트: chebee7i/dit
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))
예제 #12
0
파일: test_npdist.py 프로젝트: fiatflux/dit
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)
예제 #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)
예제 #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)
예제 #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)
예제 #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)
예제 #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)
예제 #18
0
파일: test_npdist.py 프로젝트: liangkai/dit
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)
예제 #19
0
파일: test_npdist.py 프로젝트: chebee7i/dit
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)
예제 #20
0
파일: test_npdist.py 프로젝트: chebee7i/dit
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))
예제 #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)
예제 #22
0
파일: test_npdist.py 프로젝트: liangkai/dit
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')
예제 #23
0
def test_init6():
    outcomes = {'0', '1', '2'}
    pmf = [1 / 3] * 3
    with pytest.raises(ditException):
        Distribution(outcomes, pmf)
예제 #24
0
def test_init4():
    dist = {'0': 1 / 2, '1': 1 / 2}
    pmf = [1 / 2] * 2
    with pytest.raises(InvalidDistribution):
        Distribution(dist, pmf)
예제 #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)
예제 #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')
예제 #27
0
def test_is_homogeneous2():
    outcomes = ['00', '01']
    pmf = [1 / 2] * 2
    d = Distribution(outcomes, pmf)
    assert not d.is_homogeneous()
예제 #28
0
파일: test_npdist.py 프로젝트: psychon7/dit
def test_is_homogeneous1():
    outcomes = ['00', '11']
    pmf = [1 / 2, 1 / 2]
    d = Distribution(outcomes, pmf)
    assert d.is_homogeneous()
예제 #29
0
파일: test_npdist.py 프로젝트: chebee7i/dit
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)
예제 #30
0
파일: test_npdist.py 프로젝트: chebee7i/dit
def test_has_outcome1():
    d = Distribution(['0', '1'], [1, 0])
    d.make_sparse()
    assert_false(d.has_outcome('1', null=False))
예제 #31
0
파일: test_npdist.py 프로젝트: chebee7i/dit
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))
예제 #32
0
파일: test_npdist.py 프로젝트: chebee7i/dit
def test_has_outcome2():
    d = Distribution(['0', '1'], [1, 0])
    assert_false(d.has_outcome('1', null=False))
예제 #33
0
파일: test_npdist.py 프로젝트: chebee7i/dit
def test_is_homogeneous2():
    outcomes = ['00', '01']
    pmf = [1/2, 1/2]
    d = Distribution(outcomes, pmf)
    assert_false(d.is_homogeneous())
예제 #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)
예제 #35
0
def test_has_outcome2():
    d = Distribution(['0', '1'], [1, 0])
    assert not d.has_outcome('1', null=False)
예제 #36
0
def test_setitem1():
    d = Distribution(['0', '1'], [1 / 2] * 2)
    with pytest.raises(InvalidOutcome):
        d.__setitem__('2', 0)
예제 #37
0
파일: test_npdist.py 프로젝트: liangkai/dit
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])
예제 #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)
예제 #39
0
파일: test_npdist.py 프로젝트: liangkai/dit
def test_init1():
    # Invalid initializations.
    assert_raises(InvalidDistribution, Distribution, [])
    assert_raises(InvalidDistribution, Distribution, [], [])
    Distribution([], [], sample_space=[(0, 1)], validate=False)
예제 #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)
예제 #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)
예제 #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
예제 #43
0
def test_init5():
    outcomes = ['0', '1', '2']
    pmf = [1 / 2] * 2
    with pytest.raises(InvalidDistribution):
        Distribution(outcomes, pmf)
예제 #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')
예제 #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)
예제 #46
0
def test_setitem1():
    d = Distribution(['0', '1'], [1/2, 1/2])
    with pytest.raises(InvalidOutcome):
        d.__setitem__('2', 0)
예제 #47
0
파일: test_npdist.py 프로젝트: liangkai/dit
def test_is_homogeneous2():
    outcomes = ['00', '01']
    pmf = [1 / 2, 1 / 2]
    d = Distribution(outcomes, pmf)
    assert_false(d.is_homogeneous())
예제 #48
0
def test_has_outcome1():
    d = Distribution(['0', '1'], [1, 0])
    d.make_sparse()
    assert not d.has_outcome('1', null=False)
예제 #49
0
파일: test_npdist.py 프로젝트: liangkai/dit
def test_setitem1():
    d = Distribution(['0', '1'], [1 / 2, 1 / 2])
    assert_raises(InvalidOutcome, d.__setitem__, '2', 0)
예제 #50
0
파일: test_npdist.py 프로젝트: chebee7i/dit
def test_is_homogeneous1():
    outcomes = ['00', '11']
    pmf = [1/2, 1/2]
    d = Distribution(outcomes, pmf)
    assert_true(d.is_homogeneous())