def test_mom(): """Test the whether spectral can recover from simple mixture of 3 identical""" model = MixtureModel.generate(2, 3, dirichlet_scale = 0.9) _, _, M, _ = model["k"], model["d"], model["M"], model["w"] N = 1e5 xs = model.sample(N) moments = model.exact_moments(model.observed_monomials(3)) moments_ = model.empirical_moments(xs, model.observed_monomials(3)) print "moment diff", dict_diff(moments, moments_) w_true, params = solve_mixture_model(model, moments) w_data, params_ = solve_mixture_model(model, moments_) w_true, params = fix_parameters(M, params, w_true) w_data, params_ = fix_parameters(M, params_, w_data) print "true", M print "with exact moments", params print "error with exact moments", norm(M - params)/norm(M) print "with empirical moments", params_ print "error with empirical moments", norm(M - params_)/norm(M) assert norm(M - params)/norm(M) < 1e-10 assert norm(M - params_)/norm(M) < 1e-1
def test_mixture_em(): model = MixtureModel(k = 2, d = 3, M = array([[0.7, 0.3],[0.2, 0.3],[0.1, 0.4]]), w = array([0.6,0.4])) k, d, M, w = model["k"], model["d"], model["M"], model["w"] N = 1e5 xs = model.sample(N) lhood, _, (params, w_true) = MixtureModelEM(k, d).run(xs, params=(M, w)) w_true, params = fix_parameters(M, params, w_true) lhood_, _, (params_, w_guess) = MixtureModelEM(k, d).run(xs) w_data, params_ = fix_parameters(M, params_, w_guess) print "true", M print "true lhood", model.llikelihood(xs) print "with em*", params print "em lhood*", lhood print "em lhood*", model.using(M = params, w = w_true).llikelihood(xs) print "error with em*", norm(M - params)/norm(M) print "with em", params_ print "em lhood", lhood_ print "em lhood", model.using(M = params_, w = w_guess).llikelihood(xs) print "error with em", norm(M - params_)/norm(M) assert norm(M - params)/norm(M) < 1e-1 assert norm(M - params_)/norm(M) < 1e-1
def test_mom(): """Test the whether spectral can recover from simple mixture of 3 identical""" model = MixtureModel.generate(2, 3, dirichlet_scale=0.9) _, _, M, _ = model["k"], model["d"], model["M"], model["w"] N = 1e5 xs = model.sample(N) moments = model.exact_moments(model.observed_monomials(3)) moments_ = model.empirical_moments(xs, model.observed_monomials(3)) print "moment diff", dict_diff(moments, moments_) w_true, params = solve_mixture_model(model, moments) w_data, params_ = solve_mixture_model(model, moments_) w_true, params = fix_parameters(M, params, w_true) w_data, params_ = fix_parameters(M, params_, w_data) print "true", M print "with exact moments", params print "error with exact moments", norm(M - params) / norm(M) print "with empirical moments", params_ print "error with empirical moments", norm(M - params_) / norm(M) assert norm(M - params) / norm(M) < 1e-10 assert norm(M - params_) / norm(M) < 1e-1
def make_table(model, data, methods): tbl = [] tbl.append(["Method","Parameter error", "Likelihood"]) tbl.append(["True", 0., model.llikelihood(data)]) print "true", model["M"] for name, method in methods: w, params = method(model, data) w, params = fix_parameters(model["M"], params, w) print name, params lhood = model.using(M=params, w=w).llikelihood(data) tbl.append([name, column_rerr(model["M"], params), lhood]) return tbl