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)
def test_finite_mixture_ml(): """ Test EM estimation of finite mixture distributions. """ me = ModelEngine(FiniteMixture(MixedBinomial(), 2)) assert_finite_mixture_ml_ok(me)
def test_finite_mixture_marginal(): """ Test finite-mixture marginal computation. """ from cargo.statistics.mixture import marginalize_mixture model = FiniteMixture(MixedBinomial(), 2) out = marginalize_mixture(model, [[(0.25, 0.25), (0.75, 0.75)]]) assert_equal(out.tolist(), [0.625])
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)
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], )
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]], )
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, )
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], )