Example #1
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)]),
    )
Example #2
0
def test_engine_ll_complex_model():
    """
    Test log-likelihood computation under a complex model.
    """

    from cargo.log import get_logger

    get_logger("cargo.llvm.constructs", level = "DEBUG")

    # test the model
    from cargo.statistics import (
        Tuple,
        ModelEngine,
        MixedBinomial,
        FiniteMixture,
        )

    model  = FiniteMixture(Tuple([(MixedBinomial(), 128)]), 8)
    engine = ModelEngine(model)

    # generate some fake parameters
    sample    = numpy.empty((), model.sample_dtype   )
    parameter = numpy.empty((), model.parameter_dtype)

    sample["d0"]["n"] = 4
    sample["d0"]["k"] = 1

    parameter["p"]       = 1.0 / 8.0
    parameter["c"]["d0"] = 0.5

    assert_almost_equal(engine.ll(parameter, sample), -177.445678223)
Example #3
0
def test_engine_ll_complex_model():
    """
    Test log-likelihood computation under a complex model.
    """

    from cargo.log import get_logger

    get_logger("cargo.llvm.constructs", level="DEBUG")

    # test the model
    from cargo.statistics import (
        Tuple,
        ModelEngine,
        MixedBinomial,
        FiniteMixture,
    )

    model = FiniteMixture(Tuple([(MixedBinomial(), 128)]), 8)
    engine = ModelEngine(model)

    # generate some fake parameters
    sample = numpy.empty((), model.sample_dtype)
    parameter = numpy.empty((), model.parameter_dtype)

    sample["d0"]["n"] = 4
    sample["d0"]["k"] = 1

    parameter["p"] = 1.0 / 8.0
    parameter["c"]["d0"] = 0.5

    assert_almost_equal(engine.ll(parameter, sample), -177.445678223)
Example #4
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)]),
        )
Example #5
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)]),
        )
Example #6
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)]),
    )
Example #7
0
def test_delta_given():
    """
    Test posterior computation under the trivial delta distribution.
    """

    engine = ModelEngine(Delta(float))

    assert_equal(engine.given(42.0, [43.0]), 42.0)
Example #8
0
def test_delta_given():
    """
    Test posterior computation under the trivial delta distribution.
    """

    engine = ModelEngine(Delta(float))

    assert_equal(engine.given(42.0, [43.0]), 42.0)
Example #9
0
def test_delta_ll():
    """
    Test likelihood computation under the trivial delta distribution.
    """

    engine = ModelEngine(Delta(float))

    assert_equal(engine.ll(42.0, 42.1), numpy.finfo(float).min)
    assert_equal(engine.ll(42.0, 42.0), 0.0)
Example #10
0
def test_delta_ll():
    """
    Test likelihood computation under the trivial delta distribution.
    """

    engine = ModelEngine(Delta(float))

    assert_equal(engine.ll(42.0, 42.1), numpy.finfo(float).min)
    assert_equal(engine.ll(42.0, 42.0), 0.0)
Example #11
0
def test_finite_mixture_ll():
    """
    Test finite-mixture log-likelihood computation.
    """

    engine = ModelEngine(FiniteMixture(Delta(float), 2))
    p = [[(0.25, 1.0), (0.75, 2.0)]]

    assert_almost_equal(engine.ll(p,   1.0 ), numpy.log(0.25))
    assert_almost_equal(engine.ll(p, [ 2.0]), numpy.log(0.75))
    assert_almost_equal(engine.ll(p, [42.0]), numpy.finfo(float).min)
Example #12
0
def test_finite_mixture_given():
    """
    Test finite-mixture posterior-parameter computation.
    """

    model  = FiniteMixture(Delta(float), 2)
    engine = ModelEngine(model)
    out    = engine.given([(0.25, 1.0), (0.75, 2.0)], [2.0])

    assert_equal(out["c"].tolist(), [1.0, 2.0])
    assert_almost_equal(out["p"][0], 0.0)
    assert_almost_equal(out["p"][1], 1.0)
Example #13
0
def test_finite_mixture_given_binomials():
    """
    Test finite-mixture posterior-parameter computation with binomials.
    """

    model  = FiniteMixture(MixedBinomial(), 2)
    engine = ModelEngine(model)
    out    = engine.given([(0.25, 0.25), (0.75, 0.75)], [(1, 1), (2, 3)])

    assert_equal(out["c"].tolist(), [0.25, 0.75])
    assert_almost_equal(out["p"][0], 0.03571429)
    assert_almost_equal(out["p"][1], 0.9642857)
Example #14
0
def test_finite_mixture_given_enveloped():
    """
    Test mixture posterior computation with enveloped arrays.
    """

    model  = FiniteMixture(Delta(float), 2)
    engine = ModelEngine(model)
    out    = engine.given([(0.25, 1.0), (0.75, 2.0)], [[2.0]] * 3)

    for i in xrange(3):
        assert_almost_equal(out["p"][i, 0], 0.0)
        assert_almost_equal(out["p"][i, 1], 1.0)
Example #15
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],
    )
Example #16
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]],
    )
Example #17
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]],
    )
Example #18
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],
        )
Example #19
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)],
        )
Example #20
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)],
        )
Example #21
0
def test_finite_mixture_ml():
    """
    Test EM estimation of finite mixture distributions.
    """

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

    assert_finite_mixture_ml_ok(me)
Example #22
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]],
        )
Example #23
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]],
        )
Example #24
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,
        )
Example #25
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],
        )
Example #26
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],
        )
Example #27
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)],
        )
Example #28
0
def test_tuple_ll():
    """
    Test log-likelihood computation under the tuple distribution.
    """

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

    assert_almost_equal(
        engine.ll(
            ([(0.25, 1), (0.75, 1)], [(0.5, 1)]),
            ([1, 1], [0]),
            ),
        numpy.log(0.25 * 0.75 * 0.5),
        )
    assert_almost_equal(
        engine.ll(
            ([(0.25, 1), (0.75, 1)], [(0.5, 1)]),
            ([0, 1], [0]),
            ),
        numpy.log(0.75 * 0.75 * 0.5),
        )
Example #29
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)],
        )
Example #30
0
def test_tuple_ll():
    """
    Test log-likelihood computation under the tuple distribution.
    """

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

    assert_almost_equal(
        engine.ll(
            ([(0.25, 1), (0.75, 1)], [(0.5, 1)]),
            ([1, 1], [0]),
        ),
        numpy.log(0.25 * 0.75 * 0.5),
    )
    assert_almost_equal(
        engine.ll(
            ([(0.25, 1), (0.75, 1)], [(0.5, 1)]),
            ([0, 1], [0]),
        ),
        numpy.log(0.75 * 0.75 * 0.5),
    )