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)
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]], )
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_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_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_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)
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), )