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_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 do_command(args): np.random.seed(args.seed) #methods = [("EM", do_em), ("TPM", do_tpm), ("Lasserre", do_lasserre), ("Dreesen", do_dreesen)] #methods = [("EM", do_em), ("TPM", do_tpm),("Lasserre", do_lasserre)] #, ("Dreesen", do_dreesen)] methods = [("EM", do_em), ("TPM", do_tpm),("Lasserre2", do_lasserre), ("Lasserre3", do_lasserreconstr)] #, ("Dreesen", do_dreesen)] avg_paramserror = Counter(); avg_nll = Counter(); for i in xrange(args.trials): model = MixtureModel.generate(k = args.k, d = args.d) data = model.sample(int(args.N)) tbl = make_table(model, data, methods) for mname,_ in methods: for row in tbl: if row[0] == mname: avg_paramserror[mname] += row[1]/args.trials avg_nll[mname] += row[2]/args.trials print_table(tbl) print avg_paramserror print avg_nll print args
def test_mom(): """Test the whether spectral can recover from simple mixture of 3 identical""" model = MixtureModel.generate(2, 2, dirichlet_scale = 0.9) k, d, M, w = model["k"], model["d"], model["M"], model["w"] N = 1e5 print "M", M xs = model.sample(N) print model.exact_moments(model.observed_monomials(3)) print model.empirical_moments(xs, model.observed_monomials(3)) M_ = find_means(xs, k) print "M_", M_ M_ = closest_permuted_matrix( M.T, M_.T ).T print "M", M print "M_", M_ print norm( M - M_ )/norm(M) assert( norm( M - M_ )/norm(M) < 1e-1 )