Exemplo n.º 1
0
def solve_with_moments(m1, M2, M3, K):
    """
    Whiten and unwhiten appropriately
    """

    assert symmetric_skew(M2) < 1e-2
    assert symmetric_skew(M3) < 1e-2

    W, Wt = get_whitener( M2, K )
    M3_ = sc.einsum( 'ijk,ia,jb,kc->abc', M3, W, W, W )

    #print "M3", M3
    pi_, M_, _, _ = candecomp(M3_, K)
    #print "mu", M_
    mu = Wt.dot(M_.dot(diag(pi_)))
    return mu
Exemplo n.º 2
0
def solve_mixture_model(model, data):
    """
    Whiten and unwhiten appropriately
    """

    d = model["d"]

    # Get moments
    moments = model.empirical_moments(data, model.observed_monomials(3))
    M2 = zeros((d, d))
    M3 = zeros((d, d, d))

    for i in xrange(d):
        for j in xrange(d):
            xij = sp.sympify('x%d * x%d' %(i+1, j+1))
            M2[i,j] = moments[xij]

            for k in xrange(d):
                xijk = sp.sympify('x%d * x%d * x%d' % (i+1, j+1, k+1))
                M3[i,j,k] = moments[xijk]

    k = model["k"]
    # Symmetrize
    M2, M3 = symmetrize(M2), symmetrize(M3)

    assert symmetric_skew(M2) < 1e-2
    assert symmetric_skew(M3) < 1e-2

    # Whiten
    W, Wt = get_whitener(M2, k)
    M3_ = einsum('ijk,ia,jb,kc->abc', M3, W, W, W)

    pi_, M_, _, _ = candecomp(M3_, k)

    # Unwhiten M
    M_ = Wt.dot(M_.dot(diag(pi_)))
    pi_ = 1./pi_**2
    # "Project" onto simplex
    pi_ = make_distribution(abs(pi_))
    M_ = array([make_distribution(col) for col in M_.T]).T

    return pi_, M_
Exemplo n.º 3
0
def solve_mixture_model(model, data):
    """
    Whiten and unwhiten appropriately
    """

    d = model["d"]

    # Get moments
    moments = model.empirical_moments(data, model.observed_monomials(3))
    M2 = zeros((d, d))
    M3 = zeros((d, d, d))

    for i in xrange(d):
        for j in xrange(d):
            xij = sp.sympify('x%d * x%d' % (i + 1, j + 1))
            M2[i, j] = moments[xij]

            for k in xrange(d):
                xijk = sp.sympify('x%d * x%d * x%d' % (i + 1, j + 1, k + 1))
                M3[i, j, k] = moments[xijk]

    k = model["k"]
    # Symmetrize
    M2, M3 = symmetrize(M2), symmetrize(M3)

    assert symmetric_skew(M2) < 1e-2
    assert symmetric_skew(M3) < 1e-2

    # Whiten
    W, Wt = get_whitener(M2, k)
    M3_ = einsum('ijk,ia,jb,kc->abc', M3, W, W, W)

    pi_, M_, _, _ = candecomp(M3_, k)

    # Unwhiten M
    M_ = Wt.dot(M_.dot(diag(pi_)))
    pi_ = 1. / pi_**2
    # "Project" onto simplex
    pi_ = make_distribution(abs(pi_))
    M_ = array([make_distribution(col) for col in M_.T]).T

    return pi_, M_
Exemplo n.º 4
0
def prerequisites( T, K ):
    """
    Does the tensor satisfy the prerequisites?
    (a) tensor should be symmetric (orthogonal)
    """
    if len(T.shape) != 3:
        return False
    elif not is_square(T):
        return False
    elif T.shape[0] < K:
        return False
    elif symmetric_skew(T) > 1e-1:
        return False
    else:
        return True
Exemplo n.º 5
0
def prerequisites(T, K):
    """
    Does the tensor satisfy the prerequisites?
    (a) tensor should be symmetric (orthogonal)
    """
    if len(T.shape) != 3:
        return False
    elif not is_square(T):
        return False
    elif T.shape[0] < K:
        return False
    elif symmetric_skew(T) > 1e-1:
        return False
    else:
        return True