Esempio n. 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)]),
    )
Esempio n. 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)]),
        )
Esempio n. 3
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,
        )
Esempio n. 4
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],
        )
Esempio n. 5
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],
        )
Esempio n. 6
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)],
        )
Esempio n. 7
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)],
        )