Beispiel #1
0
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
Beispiel #2
0
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
Beispiel #3
0
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
Beispiel #4
0
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
Beispiel #5
0
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 )