示例#1
0
def test_tuple_map():
    """
    Test MAP parameter estimation under the tuple distribution.
    """

    from cargo.numpy import tolist_deeply

    model = Tuple([(Binomial(estimation_n=1), 2),
                   (Binomial(estimation_n=1), 1)])
    engine = ModelEngine(model)

    assert_almost_equal_deep(
        tolist_deeply(
            engine.map(
                ([(2, 3), (2, 3)], [(2, 3)]),
                [([0, 1], [0])] * 1 + [([1, 0], [1])] * 9,
                numpy.ones(10),
            ), ),
        ([(10.0 / 13.0, 1), (2.0 / 13.0, 1)], [(10.0 / 13.0, 1)]),
    )
    assert_almost_equal_deep(
        tolist_deeply(
            engine.map(
                ([(2, 3), (2, 3)], [(2, 3)]),
                [([0, 1], [0])] * 1 + [([1, 0], [1])] * 9,
                [1.00] * 1 + [3.00] * 9,
            ), ),
        ([(28.0 / 31.0, 1), (2.0 / 31.0, 1)], [(28.0 / 31.0, 1)]),
    )
示例#2
0
def test_tuple_map():
    """
    Test MAP parameter estimation under the tuple distribution.
    """

    from cargo.numpy import tolist_deeply

    model  = Tuple([(Binomial(estimation_n = 1), 2), (Binomial(estimation_n = 1), 1)])
    engine = ModelEngine(model)

    assert_almost_equal_deep(
        tolist_deeply(
            engine.map(
                ([(2, 3), (2, 3)], [(2, 3)]),
                [([0, 1], [0])] * 1 + [([1, 0], [1])] * 9,
                numpy.ones(10),
                ),
            ),
        ([(10.0 / 13.0, 1), (2.0 / 13.0, 1)], [(10.0 / 13.0, 1)]),
        )
    assert_almost_equal_deep(
        tolist_deeply(
            engine.map(
                ([(2, 3), (2, 3)], [(2, 3)]),
                [([0, 1], [0])] * 1 + [([1, 0], [1])] * 9,
                [1.00] * 1 + [3.00] * 9,
                ),
            ),
        ([(28.0 / 31.0, 1), (2.0 / 31.0, 1)], [(28.0 / 31.0, 1)]),
        )
示例#3
0
def test_tuple_ml():
    """
    Test ML parameter estimation under the tuple distribution.
    """

    from cargo.numpy import tolist_deeply

    model = Tuple([(Binomial(estimation_n=1), 2),
                   (Binomial(estimation_n=1), 1)])
    engine = ModelEngine(model)

    assert_almost_equal_deep(
        tolist_deeply(
            engine.ml(
                [([0, 1], [0])] * 2500 + [([1, 0], [1])] * 7500,
                numpy.ones(10000),
            ), ),
        ([(0.75, 1), (0.25, 1)], [(0.75, 1)]),
    )
    assert_almost_equal_deep(
        tolist_deeply(
            engine.ml(
                [([0, 1], [0])] * 1000 + [([1, 0], [1])] * 1000,
                [1.00] * 1000 + [3.00] * 1000,
            ), ),
        ([(0.75, 1), (0.25, 1)], [(0.75, 1)]),
    )
示例#4
0
def test_tuple_ml():
    """
    Test ML parameter estimation under the tuple distribution.
    """

    from cargo.numpy import tolist_deeply

    model  = Tuple([(Binomial(estimation_n = 1), 2), (Binomial(estimation_n = 1), 1)])
    engine = ModelEngine(model)

    assert_almost_equal_deep(
        tolist_deeply(
            engine.ml(
                [([0, 1], [0])] * 2500 + [([1, 0], [1])] * 7500,
                numpy.ones(10000),
                ),
            ),
        ([(0.75, 1), (0.25, 1)], [(0.75, 1)]),
        )
    assert_almost_equal_deep(
        tolist_deeply(
            engine.ml(
                [([0, 1], [0])] * 1000 + [([1, 0], [1])] * 1000,
                [1.00] * 1000 + [3.00] * 1000,
                ),
            ),
        ([(0.75, 1), (0.25, 1)], [(0.75, 1)]),
        )
示例#5
0
def test_mixed_binomial_ml():
    """
    Test max-likelihood estimation under the mixed binomial distribution.
    """

    me = ModelEngine(MixedBinomial())

    assert_almost_equal_deep(
        me.ml(
            [[(1, 2), (4, 5)], [(3, 4), (8, 8)]],
            numpy.ones((2, 2)),
        ),
        [5.0 / 7.0, 11.0 / 12.0],
    )
示例#6
0
def test_mixed_binomial_ml():
    """
    Test max-likelihood estimation under the mixed binomial distribution.
    """

    me = ModelEngine(MixedBinomial())

    assert_almost_equal_deep(
        me.ml(
            [[(1, 2), (4, 5)],
             [(3, 4), (8, 8)]],
            numpy.ones((2, 2)),
            ),
        [5.0 / 7.0, 11.0 / 12.0],
        )
示例#7
0
def test_binomial_ll():
    """
    Test log-probability computation in the binomial distribution.
    """

    me = ModelEngine(Binomial())

    assert_almost_equal(me.ll((0.25, 2), 1), -0.98082925)
    assert_almost_equal_deep(
        me.ll(
            [[(0.25, 2), (0.25, 5)], [(0.75, 4), (0.75, 8)]],
            [[1, 4], [3, 8]],
        ),
        [[-0.98082925, -4.22342160], [-0.86304622, -2.30145658]],
    )
示例#8
0
def test_mixed_binomial_ll():
    """
    Test log-probability computation in the mixed binomial distribution.
    """

    me = ModelEngine(MixedBinomial())

    assert_almost_equal(me.ll(0.25, (1, 2)), -0.98082925)
    assert_almost_equal_deep(
        me.ll(
            [[0.25], [0.75]],
            [[(1, 2), (4, 5)], [(3, 4), (8, 8)]],
        ),
        [[-0.98082925, -4.22342160], [-0.86304622, -2.30145658]],
    )
示例#9
0
def test_binomial_ml():
    """
    Test max-likelihood estimation under the binomial distribution.
    """

    me = ModelEngine(Binomial(estimation_n=2))

    assert_almost_equal_deep(
        me.ml(
            [[1, 2],
             [2, 0]],
            numpy.ones((2, 2)),
            ) \
            .tolist(),
        [(0.75, 2), (0.50, 2)],
        )
示例#10
0
def test_binomial_ml():
    """
    Test max-likelihood estimation under the binomial distribution.
    """

    me = ModelEngine(Binomial(estimation_n = 2))

    assert_almost_equal_deep(
        me.ml(
            [[1, 2],
             [2, 0]],
            numpy.ones((2, 2)),
            ) \
            .tolist(),
        [(0.75, 2), (0.50, 2)],
        )
示例#11
0
def test_binomial_ll():
    """
    Test log-probability computation in the binomial distribution.
    """

    me = ModelEngine(Binomial())

    assert_almost_equal(me.ll((0.25, 2), 1), -0.98082925)
    assert_almost_equal_deep(
        me.ll(
            [[(0.25, 2), (0.25, 5)],
             [(0.75, 4), (0.75, 8)]],
            [[1, 4],
             [3, 8]],
            ),
        [[-0.98082925, -4.22342160],
         [-0.86304622, -2.30145658]],
        )
示例#12
0
def test_mixed_binomial_ll():
    """
    Test log-probability computation in the mixed binomial distribution.
    """

    me = ModelEngine(MixedBinomial())

    assert_almost_equal(me.ll(0.25, (1, 2)), -0.98082925)
    assert_almost_equal_deep(
        me.ll(
            [[0.25],
             [0.75]],
            [[(1, 2), (4, 5)],
             [(3, 4), (8, 8)]],
            ),
        [[-0.98082925, -4.22342160],
         [-0.86304622, -2.30145658]],
        )
示例#13
0
def assert_finite_mixture_ml_ok(me):
    """
    Verify EM estimation of finite mixture distributions.
    """

    from cargo.testing import assert_almost_equal_deep

    (e,) = \
        me.ml(
            [[(7, 8)] * 100 + [(1, 8)] * 200],
            ones((1, 300)),
            )

    assert_almost_equal_deep(
        e[numpy.argsort(e["p"])].tolist(),
        [(1.0 / 3.0, 7.0 / 8.0),
         (2.0 / 3.0, 1.0 / 8.0)],
        places = 4,
        )
示例#14
0
def test_finite_mixture_map():
    """
    Test EM estimation of MAP finite mixture parameters.
    """

    engine = ModelEngine(FiniteMixture(MixedBinomial(), 2))

    (e,) = \
        engine.map(
            [[(1, 1)] * 2],
            [[(7, 8)] * 100 + [(1, 8)] * 200],
            ones((1, 300)),
            )

    assert_almost_equal_deep(
        e[numpy.argsort(e["p"])].tolist(),
        [(1.0 / 3.0, 7.0 / 8.0),
         (2.0 / 3.0, 1.0 / 8.0)],
        places = 4,
        )
示例#15
0
def test_mixed_binomial_map():
    """
    Test MAP estimation under the mixed binomial distribution.
    """

    me = ModelEngine(MixedBinomial())

    assert_almost_equal_deep(
        me.map(
            [(1   , 1   ),
             (0.25, 0.75),
             (2   , 3   )],
            [[(1, 2), (2, 2)],
             [(1, 2), (2, 2)],
             [(1, 2), (2, 2)]],
            numpy.ones((3, 2)),
            ) \
            .tolist(),
        [0.75, 0.75, 4.0 / 7.0],
        )
示例#16
0
def test_mixed_binomial_map():
    """
    Test MAP estimation under the mixed binomial distribution.
    """

    me = ModelEngine(MixedBinomial())

    assert_almost_equal_deep(
        me.map(
            [(1   , 1   ),
             (0.25, 0.75),
             (2   , 3   )],
            [[(1, 2), (2, 2)],
             [(1, 2), (2, 2)],
             [(1, 2), (2, 2)]],
            numpy.ones((3, 2)),
            ) \
            .tolist(),
        [0.75, 0.75, 4.0 / 7.0],
        )
示例#17
0
def test_binomial_map():
    """
    Test MAP estimation under the binomial distribution.
    """

    me = ModelEngine(Binomial(estimation_n=2))

    assert_almost_equal_deep(
        me.map(
            [(1   , 1   ),
             (0.25, 0.75),
             (2   , 3   )],
            [[1, 2],
             [1, 2],
             [1, 2]],
            numpy.ones((3, 2)),
            ) \
            .tolist(),
        [(0.75     , 2),
         (0.75     , 2),
         (4.0 / 7.0, 2)],
        )
示例#18
0
def test_binomial_map():
    """
    Test MAP estimation under the binomial distribution.
    """

    me = ModelEngine(Binomial(estimation_n = 2))

    assert_almost_equal_deep(
        me.map(
            [(1   , 1   ),
             (0.25, 0.75),
             (2   , 3   )],
            [[1, 2],
             [1, 2],
             [1, 2]],
            numpy.ones((3, 2)),
            ) \
            .tolist(),
        [(0.75     , 2),
         (0.75     , 2),
         (4.0 / 7.0, 2)],
        )