Example #1
0
def freq_single(QN, A,B,C,DJ,DJK,DK,subdj,subdk):
	Jupper = QN[0]
	ind_upper = Jupper - QN[2] + QN[1]
	Jlower = QN[3]
	ind_lower = Jlower - QN[5] + QN[4]

	H_upper = ham(Jupper, A,B,C,DJ,DJK,DK,subdj,subdk)
	E_upper = la.eigs(H_upper)
	E_upper = sort(E_upper)

	H_lower = ham(Jlower, A,B,C,DJ,DJK,DK,subdj,subdk)
	E_lower = la.eigs(H_lower)
	E_lower = sort(E_lower)

	return float(E_upper[ind_upper]-E_lower[ind_lower])
def sig_lmc(C, A):
    '''
    This a function that using Lumped Markov chain to calculate
    the significance of clusters in a give communinity structure.
    refer to "Piccardi 2011 in PloS one".
    Here we normalize the original definition of persistence by
    the size of the corresponding cluster to get a better
    INPUT:
        "A" is a N-by-N weighted adjacency matrix
        "C" is a N-by-1 partition(cluster) vector
    OUTPUT:
        normalized persistence probability of all clusters
    '''
    '''
    Transition Matrix
    '''
    C = np.asarray(C)
    A = np.double(A)
    P = np.linalg.solve(np.diag(np.sum(A,axis = 1)),A)
    [eval, evec] = linalg.eigs(P.T, 1)
    if min(evec)<0:
        evec = -evec
    pi = np.double(evec.T)
    num_node = np.double(np.shape(A)[0])
    cl_label = np.double(np.unique(C))
    num_cl = len(cl_label)
    H = np.zeros((num_node, num_cl),dtype = np.double)
    for i in range(num_cl):
        H[:, i] = np.double((C==cl_label[i]))

    # Transition matrix of the lumped Markov chain

    Q = np.dot(np.dot(np.dot(np.linalg.solve(np.diag(np.dot(pi,H).flatten()),H.T),np.diag(pi.flatten())),P),H)
    persistence = np.multiply(np.divide(np.diag(Q), np.sum(H,axis = 0)),np.sum(H))
    return persistence
def sd_convergence(A, b, x0, x):
    '''
    Study convergence of steepest descent method based on x0 and condition
    number of A.
    '''
   
    assert A.shape == (2, 2), 'A is not a 2x2 matrix'
    assert b.shape == (2, ), 'b is not a vector of len 2'
    assert x0.shape == (2, ), 'x0 is not a vector of len 2'
    assert x.shape == (2, ), 'x0 is not a vector of len 2'

    assert abs(A[0, 1] - A[1, 0]) < 1E-15, 'A is not positive definite'

    lambdas, vecs = la.eigs(A)
    assert np.all(lmabdas > 0), 'A is not positive definite'

    lambda0, lambda1 = lambdas
    u0, u1 = vecs[:, 0], vecs[:, 1]

    # Swap if not sorted
    if lambda0 > lambda1:
        lambda1, lambd0 = lambda0, lambda1
        u1, u0 = u0, u1

    # error
    e0 = x0 - x

    k = lambda1/lambda0
    mu = u0.dot(e0)/u1.dot(e0)

    print 'Condition number is', k
    print 'Initial guess yields mu', mu

    X, n_iters = solve(A, b, x0)

    print 'error in solution', la.norm(X - x)
    print n_iters