def test_type_preservation_and_conversion(self): # The fractional_matrix_power matrix function should preserve # the type of a matrix whose eigenvalues # are positive with zero imaginary part. # Test this preservation for variously structured matrices. complex_dtype_chars = ('F', 'D', 'G') for matrix_as_list in ( [[1, 0], [0, 1]], [[1, 0], [1, 1]], [[2, 1], [1, 1]], [[2, 3], [1, 2]]): # check that the spectrum has the expected properties W = scipy.linalg.eigvals(matrix_as_list) assert_(not any(w.imag or w.real < 0 for w in W)) # Check various positive and negative powers # with absolute values bigger and smaller than 1. for p in (-2.4, -0.9, 0.2, 3.3): # check float type preservation A = np.array(matrix_as_list, dtype=float) A_power = fractional_matrix_power(A, p) assert_(A_power.dtype.char not in complex_dtype_chars) # check complex type preservation A = np.array(matrix_as_list, dtype=complex) A_power = fractional_matrix_power(A, p) assert_(A_power.dtype.char in complex_dtype_chars) # check float->complex for the matrix negation A = -np.array(matrix_as_list, dtype=float) A_power = fractional_matrix_power(A, p) assert_(A_power.dtype.char in complex_dtype_chars)
def fidelity(self, m, n): """Compute the fidelity between the density matrces m and n. :param numpy_array m: Density matrix :param numpy_array n: Density matrix :return: The fideltiy between m and n (:math:`\mathrm{Tr}(\sqrt{\sqrt{m}n\sqrt{m}})^2`). :rtype: complex """ sqrt_m = fractional_matrix_power(m, 0.5) F = np.trace(fractional_matrix_power(np.dot(sqrt_m,np.dot(n, sqrt_m)), 0.5))**2.0 return F
def get_mean_and_covariance(self, w2v, num_of_occurences): """ get mean and covariance of words vectors over the training set of word2vec model w2v -- word2vec model (in matrix form) num_of_occurences -- array that specifies weights for averaging over words """ weights = num_of_occurences/np.sum(num_of_occurences) try: w2v_temp = np.multiply(w2v, weights) except MemoryError: w2v_temp = np.copy(w2v) for wn in range(np.shape(w2v)[1]): w2v_temp[:, wn] *= weights[wn] self.Mean = np.sum(w2v_temp, 1) try: w2v_except0 = w2v - np.reshape(self.Mean, (len(self.Mean), 1)) except MemoryError: w2v_except0 = w2v_temp # just to set the right shape (to avoid memoryError) for wn in range(np.shape(w2v)[1]): w2v_except0[:, wn] = w2v[:, wn] - self.Mean try: w2v_normalized = np.multiply(w2v_except0, np.power(weights, 0.5)) except MemoryError: w2v_normalized = w2v_except0 for wn in range(np.shape(w2v_except0)[1]): w2v_normalized[:, wn] *= weights[wn]**0.5 self.Cov = np.dot(w2v_normalized, np.transpose(w2v_normalized)) self.Cov = self.Cov/np.shape(w2v)[1] self.SqrtCov = fractional_matrix_power(self.Cov, -0.5)
def test_larger_abs_fractional_matrix_powers(self): np.random.seed(1234) for n in (2, 3, 5): for i in range(10): M = np.random.randn(n, n) + 1j * np.random.randn(n, n) M_one_fifth = fractional_matrix_power(M, 0.2) # Test the round trip. M_round_trip = np.linalg.matrix_power(M_one_fifth, 5) assert_allclose(M, M_round_trip) # Test a large abs fractional power. X = fractional_matrix_power(M, -5.4) Y = np.linalg.matrix_power(M_one_fifth, -27) assert_allclose(X, Y) # Test another large abs fractional power. X = fractional_matrix_power(M, 3.8) Y = np.linalg.matrix_power(M_one_fifth, 19) assert_allclose(X, Y)
def test_singular(self): # Negative fractional powers do not work with singular matrices. for matrix_as_list in ( [[0, 0], [0, 0]], [[1, 1], [1, 1]], [[1, 2], [3, 6]], [[0, 0, 0], [0, 1, 1], [0, -1, 1]]): # Check fractional powers both for float and for complex types. for newtype in (float, complex): A = np.array(matrix_as_list, dtype=newtype) for p in (-0.7, -0.9, -2.4, -1.3): A_power = fractional_matrix_power(A, p) assert_(np.isnan(A_power).all()) for p in (0.2, 1.43): A_power = fractional_matrix_power(A, p) A_round_trip = fractional_matrix_power(A_power, 1/p) assert_allclose(A_round_trip, A)
def test_round_trip_random_complex(self): np.random.seed(1234) for p in range(1, 5): for n in range(1, 5): M_unscaled = np.random.randn(n, n) + 1j * np.random.randn(n, n) for scale in np.logspace(-4, 4, 9): M = M_unscaled * scale M_root = fractional_matrix_power(M, 1/p) M_round_trip = np.linalg.matrix_power(M_root, p) assert_allclose(M_round_trip, M)
def test_al_mohy_higham_2012_experiment_1(self): # Fractional powers of a tricky upper triangular matrix. A = _get_al_mohy_higham_2012_experiment_1() # Test remainder matrix power. A_funm_sqrt, info = funm(A, np.sqrt, disp=False) A_sqrtm, info = sqrtm(A, disp=False) A_rem_power = _matfuncs_inv_ssq._remainder_matrix_power(A, 0.5) A_power = fractional_matrix_power(A, 0.5) assert_array_equal(A_rem_power, A_power) assert_allclose(A_sqrtm, A_power) assert_allclose(A_sqrtm, A_funm_sqrt) # Test more fractional powers. for p in (1/2, 5/3): A_power = fractional_matrix_power(A, p) A_round_trip = fractional_matrix_power(A_power, 1/p) assert_allclose(A_round_trip, A, rtol=1e-2) assert_allclose(np.tril(A_round_trip, 1), np.tril(A, 1))
def test_type_conversion_mixed_sign_or_complex_spectrum(self): complex_dtype_chars = ("F", "D", "G") for matrix_as_list in ([[1, 0], [0, -1]], [[0, 1], [1, 0]], [[0, 1, 0], [0, 0, 1], [1, 0, 0]]): # check that the spectrum has the expected properties W = scipy.linalg.eigvals(matrix_as_list) assert_(any(w.imag or w.real < 0 for w in W)) # Check various positive and negative powers # with absolute values bigger and smaller than 1. for p in (-2.4, -0.9, 0.2, 3.3): # check complex->complex A = np.array(matrix_as_list, dtype=complex) A_power = fractional_matrix_power(A, p) assert_(A_power.dtype.char in complex_dtype_chars) # check float->complex A = np.array(matrix_as_list, dtype=float) A_power = fractional_matrix_power(A, p) assert_(A_power.dtype.char in complex_dtype_chars)
def fit(self,X,Y): self._compute_covariance(X,Y) S_11_ = fractional_matrix_power(self.S_11,-0.5) S_22_ = fractional_matrix_power(self.S_22,-0.5) T = np.dot(np.dot(S_11_,self.S_12),S_22_) U ,S ,V = np.linalg.svd(T) self.U = U[:,:self.k] self.S = S[:self.k] self.V = V[:self.k,:] self.A = np.dot(S_11_,U) self.B = np.dot(S_22_,V.T) self.A = self.A[:,:self.k] self.B = self.B[:,:self.k] self.coeff_ = np.dot(self.A,self.V) return self
def test_round_trip_random_float(self): # This test is more annoying because it can hit the branch cut; # this happens when the matrix has an eigenvalue # with no imaginary component and with a real negative component, # and it means that the principal branch does not exist. np.random.seed(1234) for p in range(1, 5): for n in range(1, 5): M_unscaled = np.random.randn(n, n) for scale in np.logspace(-4, 4, 9): M = M_unscaled * scale M_root = fractional_matrix_power(M, 1/p) M_round_trip = np.linalg.matrix_power(M_root, p) assert_allclose(M_round_trip, M)
def test_singular(self): # Negative fractional powers do not work with singular matrices. # Neither do non-integer fractional powers, # because the scaling and squaring cannot deal with it. for matrix_as_list in ( [[0, 0], [0, 0]], [[1, 1], [1, 1]], [[1, 2], [3, 6]], [[0, 0, 0], [0, 1, 1], [0, -1, 1]]): # check that the spectrum has the expected properties W = scipy.linalg.eigvals(matrix_as_list) assert_(np.count_nonzero(W) < len(W)) # check fractional powers both for float and for complex types for newtype in (float, complex): A = np.array(matrix_as_list, dtype=newtype) for p in (-0.7, -0.9, -2.4, -1.3): A_power = fractional_matrix_power(A, p) assert_(np.isnan(A_power).all()) for p in (0.2, 1.43): A_power = fractional_matrix_power(A, p) A_round_trip = fractional_matrix_power(A_power, 1/p) assert_allclose(A_round_trip, A)
def test_random_matrices_and_powers(self): # Each independent iteration of this fuzz test picks random parameters. # It tries to hit some edge cases. np.random.seed(1234) nsamples = 20 for i in range(nsamples): # Sample a matrix size and a random real power. n = random.randrange(1, 5) p = np.random.randn() # Sample a random real or complex matrix. matrix_scale = np.exp(random.randrange(-4, 5)) A = np.random.randn(n, n) if random.choice((True, False)): A = A + 1j * np.random.randn(n, n) A = A * matrix_scale # Check a couple of analytically equivalent ways # to compute the fractional matrix power. # These can be compared because they both use the principal branch. A_power = fractional_matrix_power(A, p) A_logm, info = logm(A, disp=False) A_power_expm_logm = expm(A_logm * p) assert_allclose(A_power, A_power_expm_logm)
def calculaW(d, a): d = np.matrix(d) a = np.matrix(a) dd = fractional_matrix_power(d, 0.5) w = dd * a * dd return w
def initnew0(z,ind,scales): # ind is the number of the initial estimator (1 to 6) #i=1 Hyperbolic tangent of standardized data #i=2 Spearman correlation matrix #i=3 Tukey normal scores #i=4 spherical wisnorization #i=5 BACON #i=6 Raw OGK estimate for scatter n=len(z) p=len(z[0]) #initial estimator 1: y=tanh(z) if ind==1: y1=np.tanh(z) k=pd.DataFrame(y1) R=k.corr() #initial estimator 2: spearman correlation matrix if ind==2 : tmp=np.copy(z) y2=tmp for j in range(p): y2[:,j]=rankdata(tmp[:,j]) k=pd.DataFrame(y2) R=k.corr() #initial estimator 3: Tukey normal scores if ind==3: tmp2=np.copy(z) y2=tmp2 for j in range(p): y2[:,j]=rankdata(tmp2[:,j]) y3=norm.ppf((y2-1/3)/(n+1/3)) k=pd.DataFrame(y3) R=k.corr() #initial estimator 4: spatial sign covariance matrix if ind==4: for i in range(n): if any(z[i,:]==0): z[i,:]=np.tile(0.0001,(1,p)) jerk=np.sqrt(np.sum(z**2,axis=1)) jerk=jerk.reshape(len(jerk),1) k=pd.DataFrame(z/(np.tile(jerk,(1,p)))) R=k.cov() #initial estimator 5: BACON if ind==5: d=np.sqrt(np.sum(z**2,axis=1)) ind5=np.argsort(d) ################################################################# Hinit=ind5[0:int(np.ceil(n/2))] k=pd.DataFrame(z[Hinit,:]) R=k.cov() #initial estimator 6: Raw OGK estimate for scatter if ind==6: P,lambda0=og.ogkscatter0(z,scales) #calculates initial estimator if ind!=6: L,P=np.linalg.eig(R) lambda0=np.diagflat(scaler(np.matmul(z,P),scales))**2 sqrtcov=np.matmul(np.matmul(P,fractional_matrix_power(lambda0,(1/2))),P.T) covar=np.matmul(np.matmul(P,lambda0),P.T) sqrtinvcov=np.matmul(np.matmul(P,(fractional_matrix_power(lambda0,(-1/2)))),P.T) mu=np.matmul(np.median(np.matmul(z,sqrtinvcov), axis=0),sqrtcov) dis=rs.resdis0(z,mu,covar) disind=np.argsort(dis) half=round(n/2) halfind=disind[:half] zhalf=z[halfind,:] pnd=pd.DataFrame(zhalf) covar=pnd.cov() muI=np.mean(zhalf, axis=0) scaleI=(np.linalg.det(covar))**(1/(2*p)) sigmaI=scaleI**(-2)*covar initest={"mu" : muI, "scale" : scaleI, "sigma" : sigmaI } return initest
def test_opposite_sign_complex_eigenvalues(self): M = [[2j, 4], [0, -2j]] R = [[1+1j, 2], [0, 1-1j]] assert_allclose(np.dot(R, R), M, atol=1e-14) assert_allclose(fractional_matrix_power(M, 0.5), R, atol=1e-14)
#adjMat[2,9] = 1 #adjMat[5,0] = 1 #adjMat[0,5] = 1 #adjMat[1,8] = 1 #adjMat[8,1] = 1 #print adjMat #print(np.sum(adjMat)) #show_graph(adjMat) # Create diagonal matrix diagMat = np.diag(adjMat.sum(axis=0)) #print diagMat # Create Laplacian matrix diagMatinv = la.fractional_matrix_power(diagMat,-0.5) lapMat = np.eye(2*N+N2) - np.dot(np.dot(diagMatinv,adjMat),diagMatinv) # Unnormalized #lapMat = diagMat - adjMat #print lapMat # Cal eigenvector of laplacian eigval,eigvec = np.linalg.eig(lapMat) # Sort from smallest to largest idx = np.argsort(eigval) eigval = eigval[idx] eigvec = eigvec[:,idx]
def sq_IG_distance(cov_1, cov_2): cov_1_pow = fractional_matrix_power(cov_1, -0.5) return norm(logm(np.linalg.multi_dot([cov_1_pow, cov_2, cov_1_pow])), ord='fro')**2
def avg_func(A, B, w): A_half = sqrtm(A) A_min_half = inv(A_half) midM = fractional_matrix_power(np.dot(np.dot(A_min_half, B), A_min_half), w) return np.dot(np.dot(A_half, midM), A_half)
x = np.asmatrix([[0, 0], [1, -1], [2, -2], [3, -3]]) # propagation rule: A * x <-- add all nghbr feats and self # degree matrix D = np.array(np.sum(A_hat, axis=0)) D = np.asmatrix(np.diag(D)) # weight matrix w = np.asmatrix([[1, -1], [-1, 1]]) # normalized prop rule # d_tf = tf.constant(D**-1, dtype=tf.float32) # a_tf = tf.constant(A_hat, dtype=tf.float32) # x_tf = tf.constant(x, dtype=tf.float32) # w_tf = tf.constant(w, dtype=tf.float32) # # res_tf = tf.matmul(d_tf, a_tf) # res_tf = tf.matmules_tf, x_tf) # res_tf = tf.matmul(res_tf, w_tf) # # print(res_tf) temp = np.matmul(fractional_matrix_power(D, -.5), A_hat) temp = np.matmul(temp, fractional_matrix_power(D, -.5)) temp = np.matmul(temp, x) temp = np.matmul(temp, w) print(temp) # at this point we can apply some activation function
def transformKMatrix(D_new,L_new): K_new = fractional_matrix_power(D_new,0.5).dot(L_new).dot(fractional_matrix_power(D_new,0.5)) return K_new
def generateLMatrix(K, D): D_ = fractional_matrix_power(D, -0.5) L = D_ @ K @ D_ # Instead of multiplication return L
# Shift operator print(f"A mul X: {A@X}") # Take the node feature into account graph.G = graph.G.copy() self_loops = [] for id, _ in graph.G.nodes.data(): self_loops.append((id, id)) print(self_loops) graph.addEdges(edges=self_loops) A = np.array(nx.attr_matrix(graph.G)[0]) print(f"\n==============\nNew Adj matrix:\n {A}") # Shift operator print(f"A mul X: {A@X}") # Normalize (D^{-1} A X ) degs = graph.G.degree() D = np.diag([deg for n,deg in degs]) normalized_AX = np.linalg.inv(D) @ A @ X print(f"D-1 mul A mul X: {normalized_AX}") #Symmetrically-normalization D_half_norm = fractional_matrix_power(D, -0.5) DADX = D_half_norm @ A @ D_half_norm @ X print(f"DADX: {DADX}") graph.showGraph()
def run(ro_0, H, dt, nt, l, config, fidelity_mode=False): # -------------------------------------------------------- a = get_a(H, H.capacity, H.n) _a = Matrix(H.size, H.size, dtype=np.complex128) _a.data = a.data # _a.to_csv('a') # print(a) # if __debug__: # _a.to_csv(a_csv) a_cross = a.getH() across_a = np.dot(a_cross, a) _a_cross_a = Matrix(H.size, H.size, dtype=np.complex128) _a_cross_a.data = across_a.data # print(a) # exit(1) # print("H:\n", color="yellow") # H.matrix.set_header(H.states) # H.matrix.print_pd() # _a.set_header(H.states) # print("a:\n", color="yellow") # _a.print_pd() # _a_cross_a.set_header(H.states) # print("acrossa:\n", color="yellow") # _a_cross_a.print_pd() # for i in range(a.shape[0]): # for j in range(a.shape[1]): # if a[i, j]: # print(H.states[i], H.states[j], a[i, j]) # exit(1) # print(np.matrix(a)) # if __debug__: # _a_cross_a.to_csv(a_cross_a_csv) # -------------------------------------------------------- U = Unitary(H, dt) # if __debug__: # U.to_csv(config.U_csv) U_conj = U.conj() # -------------------------------------------------------- ro_t = ro_0.data for k, v in H.states.items(): if v == [H.capacity, 0, 0]: index1 = k if v == [0, H.n, 0]: index2 = k if fidelity_mode: ro_0_sqrt = lg.fractional_matrix_power( ro_0.data[index1:index2 + 1, index1:index2 + 1], 0.5) fidelity = [] # print(np.round(np.diag(ro_t), 3)) # print() # print("ρ(0):\n", color="yellow") # ro_0.set_header(H.states) # ro_0.print_pd() H.print_states() # ---------------------------------------------------------------------------- with open(config.z_csv, "w") as csv_file: writer = csv.writer(csv_file, quoting=csv.QUOTE_NONE, lineterminator="\n") for t in range(0, nt + 1): # print(t, nt) # diag_abs = np.abs(np.diag(ro_t)[index1:index2 + 1]) # trace_abs = np.sum(diag_abs) diag = np.diag(ro_t) p = np.abs(diag[index1:index2 + 1]) # p = np.asarray(p).reshape(-1) # p = np.around(p, precision) norm = np.sum(np.abs(diag)) # print(t, "norm:", norm) Assert( abs(1 - norm) <= 0.1, str(t) + " " + str(norm) + ": ro is not normed", cf()) # Assert(abs(1 - trace_abs) <= 0.1, "ro is not normed", cf()) writer.writerow(["{:.3f}".format(x) for x in p]) # writer.writerow(["{:.5f}".format(x) for x in diag_abs]) # -------------------------------------------------------------------- if fidelity_mode: fidelity_t = Fidelity( ro_0_sqrt, ro_t[index1:index2 + 1, index1:index2 + 1]) # print(fidelity_t) fidelity.append(fidelity_t) # -------------------------------------------------------------------- # for i in np.diag(ro_t): # v = str(np.round(i, 3)) # # v = str(np.round(i, 3)) # print(v.rjust(5, ' '), end=' ') # v = str(np.round(np.sum(np.abs(diag[:-1])), 3)) # print(v) L = get_L(ro_t, a, a_cross, across_a) ro_t = U.data.dot(ro_t).dot(U_conj) + dt * (config.l * L) # ---------------------------------------------------------------------------- states = {} # print(np.round(np.diag(ro_t), 3)) # ro_0.data = ro_t # ro_0.set_header(H.states) # print("ρ(t):\n", color="yellow") # ro_0.print_pd() # ro_0.data = L # print("L(t):\n", color="yellow") # ro_0.to_csv("L") # ro_0.print_pd() # exit(1) cnt = 0 for k in range(index1, index2 + 1): states[cnt] = (H.states[k])[1:] cnt += 1 print(states) write_xbp(states, config.x_csv, ind=[[0, H.n], [H.n, 0]]) write_t(T_str_v(config.T), config.nt, config.y_csv) # write_t(config.T / config.mks / 1e-2, config.nt, config.y_csv) # ---------------------------------------------------------- if fidelity_mode: list_to_csv(config.fid_csv, fidelity, header=["fidelity"])
def sq_IG_distance(cov_1, cov_2) : cov_1_pow = fractional_matrix_power(cov_1, -0.5) return norm(logm(np.linalg.multi_dot([cov_1_pow, cov_2, cov_1_pow])), ord='fro') ** 2
def f(s): s = np.real_if_close(s) return np.trace( np.matmul(fractional_matrix_power(rho, s), fractional_matrix_power(sigma, 1 - s)))
def run(ro_0, H, dt, nt, config, fidelity_mode=False): # -------------------------------------------------------- U = Unitary(H, dt) if __debug__: U.write_to_file(config.U_csv) U_conj = U.conj() # -------------------------------------------------------- if fidelity_mode: ro_0_sqrt = lg.fractional_matrix_power(ro_0.data, 0.5) fidelity = [] ro_t = ro_0.data # ---------------------------------------------------------------------------- p_bin = dict.fromkeys(H.states_bin_keys) # -------------------------------------------------------- with open(config.z_csv, "w") as csv_file: writer = csv.writer(csv_file, quoting=csv.QUOTE_NONE, lineterminator="\n") with open(config.z_all_csv, "w") as csv_all_file: writer_all = csv.writer(csv_all_file, quoting=csv.QUOTE_NONE, lineterminator="\n") for t in range(0, nt): diag_abs = np.abs(np.diag(ro_t)) trace_abs = np.sum(diag_abs) Assert(abs(1 - trace_abs) <= 0.1, "ro is not normed", cf()) for k, v in p_bin.items(): p_bin[k] = 0 for k, v in H.states_bin.items(): for ind in v: p_bin[k] += diag_abs[ind] v_bin = [p_bin[k] for k in H.states_bin_keys] # -------------------------------------------------- writer.writerow(["{:.5f}".format(x) for x in v_bin]) # if __debug__: # writer_all.writerow(["{:.5f}".format(x) for x in diag_abs]) # -------------------------------------------------- if fidelity_mode: fidelity_t = Fidelity(ro_0_sqrt, ro_t) fidelity.append(fidelity_t) # -------------------------------------------------- ro_t = U.data.dot(ro_t).dot(U_conj) # -------------------------------------------------------------- states_bin = {} cnt = 0 for k in H.states_bin_keys: if k == "[" + str(0) + "," + str(int( config.n / 2)) + "]" or k == "[" + str(int( config.n / 2)) + "," + str(0) + "]": states_bin[cnt] = str(k) else: states_bin[cnt] = "" cnt += 1 # ---------------------------------------------------------- states = {} cnt = 0 for v in H.states_bin_keys: states[cnt] = v cnt += 1 # ---------------------------------------------------------- write_x(states, config.x_csv) write_t(config.T / config.mks, config.nt, config.y_csv) # ---------------------------------------------------------- if fidelity_mode: list_to_csv(fid_csv, fidelity, header=["fidelity"])
# tangent_matrices = np.zeros_like(training_data) # for k in range(len(training_data)) : # # tangent_matrices[k, :, :] = np.linalg.multi_dot([la.fractional_matrix_power(base_cov_matrix, 0.5), la.logm(np.linalg.multi_dot([la.fractional_matrix_power(base_cov_matrix, -0.5), training_data[k, :, :],la.fractional_matrix_power(base_cov_matrix, -0.5)])), la.fractional_matrix_power(base_cov_matrix, 0.5)]) # # # calculate the tangent space mean # tangent_space_base_cov_matrix = np.mean(tangent_matrices, axis=0) # # # project new tangent mean back to the manifold # base_cov_matrix = np.linalg.multi_dot([la.fractional_matrix_power(base_cov_matrix, 0.5), la.expm(np.linalg.multi_dot([la.fractional_matrix_power(base_cov_matrix, -0.5), tangent_space_base_cov_matrix ,la.fractional_matrix_power(base_cov_matrix, -0.5)])), la.fractional_matrix_power(base_cov_matrix, 0.5)]) # apply whitening transport and projection for training AND testing data transformed_training_data = np.zeros_like(training_data) transformed_testing_data = np.zeros_like(testing_data) base_cov_matrix_pow = la.fractional_matrix_power(base_cov_matrix, -0.5) for j in range(len(training_data)): transformed_training_data[j, :, :] = la.logm( np.linalg.multi_dot([ base_cov_matrix_pow, training_data[j, :, :], base_cov_matrix_pow ])) #print j for j in range(len(testing_data)): transformed_testing_data[j, :, :] = la.logm( np.linalg.multi_dot([ base_cov_matrix_pow, testing_data[j, :, :], base_cov_matrix_pow
def build_Hk_5(QE_xml_data_file,shift,shift_type,Hk_space,Hk_outfile,nbnds_norm=0,nbnds_in=0): """ returns Hk: build_Hk_2: includes all the bands that lay under the 'shift' energy. build_Hk_3: Optionally one can inclue a fixed number of bands with 'nbnds_in'; this capability is similar to WanT's 'atmproj_nbnd'. shift_type: 0 = regular shifting. 1 = new shifting build_Hk_4: -a bug for nonortho shifting is corrected: Sks was needed for that case. -changed the name of the output variable from Hks to Hk. build_Hk_5: reads nawf,nkpnts,nspin,shift,eigsmat, from QE_xml_data_file """ nproc = 1 if not os.path.isfile(QE_xml_data_file): sys.exit('File not found: {0:s}'.format(QE_xml_data_file)) data = np.load(QE_xml_data_file) nawf = int(data['nawf']) nkpnts = int(data['nkpnts']) nspin = int(data['nspin']) eigsmat = data['eigsmat'] Sks = data['Sk'] U = data['U'] if Hk_space.lower()=='ortho': del Sks elif Hk_space.lower()=='nonortho': if len(Sks.shape) != 3: sys.exit('Need Sks[nawf,nawf,nkpnts] for nonortho calculations') else: sys.exit('wrong Hk_space option. Only ortho and nonortho are accepted') tic = time.time() Hks = np.zeros((nawf,nawf,nkpnts,nspin),dtype=complex) for ispin in range(nspin): for ik in range(nkpnts): my_eigs=eigsmat[:,ik,ispin] E = np.diag(my_eigs) UU = U[:,:,ik,ispin] #transpose of U. Now the columns of UU are the eigenvector of length nawf if nbnds_norm > 0: norms = 1/np.sqrt(np.real(np.sum(np.conj(UU)*UU,axis=0))) UU[:,:nbnds_norm] = UU[:,:nbnds_norm]*norms[:nbnds_norm] kappa = shift if nbnds_in == 0: iselect = np.where(my_eigs <= shift)[0] elif nbnds_in > 0: iselect = range(nbnds_in) else: sys.exit('build_Hk_4: wrong nbnd variable') ac = UU[:,iselect] ee1 = E[np.ix_(iselect,iselect)] if shift_type ==0: Hks_aux = ac.dot(ee1).dot(np.conj(ac).T) + kappa*( -ac.dot(np.conj(ac).T)) elif shift_type==1: aux_p=la.inv(np.dot(np.conj(ac).T,ac)) Hks_aux = ac.dot(ee1).dot(np.conj(ac).T) + kappa*( -ac.dot(aux_p).dot(np.conj(ac).T)) else: sys.exit('shift_type not recognized') Hks_aux = np.triu(Hks_aux,1)+np.diag(np.diag(Hks_aux))+np.conj(np.triu(Hks_aux,1)).T if Hk_space.lower()=='ortho': Hks[:,:,ik,ispin] = Hks_aux + kappa*np.identity(nawf) elif Hk_space.lower()=='nonortho': Sk_half = sla.fractional_matrix_power(Sks[:,:,ik],0.5) if 'multi_dot' in dir (la): Hks[:,:,ik,ispin] =la.multi_dot([Sk_half,Hks_aux,Sk_half])+kappa*Sks[:,:,ik] else: Hks[:,:,ik,ispin] =np.dot(np.dot(Sk_half,Hks_aux),Sk_half)+kappa*Sks[:,:,ik] else: sys.exit('wrong Hk_space option. Only ortho and nonortho are accepted') np.savez(Hk_outfile,Hk=Hks,nbnds_norm=nbnds_norm,nbnds_in=nbnds_in,shift_type=shift_type,shift=shift) toc = time.time() hours, rem = divmod(toc-tic, 3600) minutes, seconds = divmod(rem, 60) print("Parallel calculation of H[k] with {0:d} processors".format(nproc)) print("Elapsed time {:0>2}:{:0>2}:{:05.2f}".format(int(hours),int(minutes),seconds)) return Hks
def run(HG, args, k=3): N = args.nodes H = getH(HG) W = getW(HG) Ht = np.transpose(H) Dv = getDv(HG) A = np.dot(np.dot(H, W), Ht) - Dv #adjacency matrix Dvp = linalg.fractional_matrix_power( Dv, -1 / 2) # -- Replaced by the following: (raises Dv to the -1/2) L = (.5) * (np.identity(Dvp.shape[1]) - np.dot(np.dot(Dvp, A), Dvp)) eigw, eigv = linalg.eigh(L, eigvals=(1, k)) #plt.scatter(eigv[:,0],eigv[:,1], c="r", alpha=0.3) #K-means algorithm data = whiten(eigv) centroids, _ = kmeans(data, k) idx, _ = vq(data, centroids) coalitionArray = [] completeCoals = [] for i in range(k): lcap = [0] lrel = [0] ldis = [0] coalitionArray.append(set()) for node in np.array(list(HG.nodes))[idx == i]: coalitionArray[i].add(node) lcap.append(lcap[-1] + node.EV.attributes[Attribute.capacity]) lrel.append( np.mean([ o.EV.attributes[Attribute.reliability] for o in coalitionArray[i] ])) ldis.append(ldis[-1] + node.EV.attributes[Attribute.discharge]) if (lcap[-1] >= args.capacity and ldis[-1] >= args.discharge): break if (lcap[-1] >= args.capacity and ldis[-1] >= args.discharge): completeCoals.append(coalitionArray[i]) if (not completeCoals): return None else: coalition = min(completeCoals, key=lambda x: len(x)) #if(lcap[-1] < args.capacity and ldis[-1] < args.discharge): #plt.plot(data[:,0], data[:,1], ".") if (args.plot): #fig = plt.figure() #ax = fig.add_subplot(111, projection='3d') #ax.scatter(data[idx==0,0], data[idx==0,1], data[idx==0,2], c='b') #ax.scatter(data[idx==1,0], data[idx==1,1], data[idx==1,2], c='r') #ax.scatter(data[idx==2,0], data[idx==2,1], data[idx==2,2], c='g') #plt.subplot(2,1,1) #plt.plot(data[idx==0,0],data[idx==0,1],'ob', # data[idx==1,0],data[idx==1,1],'or') #plt.plot(centroids[:,0],centroids[:,1],'sg', markersize=8) #points = np.random.randint(N, size=N/2) #for i in range(N): # plt.text(data[i,0], data[i,1], HG.nodes[i].EV.describe()) fig = plt.figure(4) fig.suptitle("Clustering") plt.subplot(3, 1, 1) plt.ylabel('Capacity') plt.plot(range(len(lcap)), lcap, "b") plt.axhline(y=args.capacity, color="r") plt.ylim(0, max(args.capacity * 1.1, lcap[-1] * 1.1)) plt.subplot(3, 1, 2) plt.ylabel('Reliability') plt.plot(range(len(lrel)), lrel, "r") plt.subplot(3, 1, 3) plt.ylabel('Discharge') plt.plot(range(len(ldis)), ldis, "g") plt.ylim(0, max(args.discharge * 1.1, ldis[-1] * 1.1)) plt.axhline(y=args.discharge, color="r") if (args.writepng): plt.savefig("Summary/clustering.png") else: plt.show() return coalition
temp = np.array(xrange(1829))+1 adDup = np.hstack((temp[:,np.newaxis],adMat)) temp = np.array(xrange(1830)) adDup = np.vstack((temp,adDup)) np.savetxt("aff.csv", adDup, delimiter=';') # social similarity matrix socialsim = sklearn.metrics.pairwise.pairwise_kernels(social,metric='rbf',gamma=0.8) adMat = adMat+socialsim # Make D matrix D = np.diag(adMat.sum(1)) # Make laplacian Dinv = la.fractional_matrix_power(D,-0.5) L = np.eye(numProj) - np.dot(np.dot(Dinv,adMat),Dinv) #L = D - adMat # Cal eigenvector of laplacian eigval,eigvec = np.linalg.eigh(L) # Sort from smallest to largest idx = np.argsort(eigval) eigval = eigval[idx] eigvec = eigvec[:,idx] Perform PCA uXPca,sXPca,wXPca = la.svd(eigvec, full_matrices=False) K=500
def test_opposite_sign_complex_eigenvalues(self): M = [[2j, 4], [0, -2j]] R = [[1 + 1j, 2], [0, 1 - 1j]] assert_allclose(np.dot(R, R), M, atol=1e-14) assert_allclose(fractional_matrix_power(M, 0.5), R, atol=1e-14)
def unwhiten_covariance(samples): return np.dot(fractional_matrix_power(np.cov(samples), -0.5), samples)
# Read in the graph and construct adjancency and degree matrix. adj_mat = np.identity(num_vertices) deg_mat = np.identity(num_vertices) with open(snap_file, "r") as fgraph: for line in fgraph.readlines(): if len(line.strip()) > 0: vsrc, vdst = tuple([int(num) for num in line.strip().split()]) assert (0 <= vsrc < num_vertices and 0 <= vdst < num_vertices) adj_mat[vdst, vsrc] = 1 deg_mat[vdst, vdst] += 1 # # Functions used in the epoch. # normed_deg_mat = fractional_matrix_power(deg_mat, -0.5) S_mat = np.dot(normed_deg_mat, np.dot(adj_mat, normed_deg_mat)) def activate(mat): return np.tanh(mat) def activate_derivate(mat): return 1 - np.multiply(np.tanh(mat), np.tanh(mat)) def softmax_row(mat): return softmax(mat, axis=1)
def whiten_covariance(samples): cov = np.cov(samples) eigenvalues, _ = LA.eigh(cov) assert_all_non_negative(eigenvalues) return np.dot(fractional_matrix_power(cov, -0.5), samples)
def display(t): move_data = lin.fractional_matrix_power(matrix, t * anim_speed / 1000) * data real.set_data(*move_data.real) imag.set_data(*move_data.real + move_data.imag) return [real, imag]
spikesavg = spikes.mean(axis=0)[np.newaxis, :] #%% stimcov = np.cov(stimulus.T) spkcov = np.cov(spikes.T) stspcov = np.zeros((stimcov.shape[0], spkcov.shape[0])) for i in range(stspcov.shape[0]): for j in range(stspcov.shape[1]): stspcov[i, j] = np.mean((stimulus[:, i] - np.mean(stimulus[:, i])) * (spikes[:, j] - np.mean(spikes[:, j]))) #%% ncomponents = min(stimulus.shape[1], spikes.shape[1]) stimcov_exp = fractional_matrix_power(stimcov, -0.5) spkcov_exp = fractional_matrix_power(spkcov, -0.5) whitened_cov = stimcov_exp @ stspcov @ spkcov_exp u, s, v = np.linalg.svd(whitened_cov, full_matrices=True) # rows of v correspond to components, to interpret it in the same way, # we transpose it v = v.T print('done') # SVD returns a much larger matrix than needed for the larger matrix, we take # only the needed part. respcomps = v[:, :ncomponents].reshape((ncells, filter_length[1], ncomponents)) # Reshape so that the order is: ncomponents, ncells, time respcomps = np.moveaxis(respcomps, [2, 0, 1], [0, 1, 2])
D = L + graph w, v = LA.eig(NL) #changing to lost w = w.tolist() e2 = sorted(w)[-2] pos = w.index(e2) #print(e2, pos) pos_vec = w.index(sorted(w)[3]) ev = v[pos_vec] #finding opt y #print(ev) D_half = fractional_matrix_power(D, 0.5) y = D_half.dot(ev) #print(y) print("upper bound:", 2 / e2) #finding optimal cut-off a = max(y) b = min(y) step = (a - b) / 20 print("max,min:", a, b) obj = 0 temp_obj = 0 val = 0 i = b + step
help='use laplacian likelihood instead of Gaussian') args = ap.parse_args() input_folder = args.exp_id folder = os.path.join("abhinav_model_dir", input_folder) gt, prd, covar, _, nme = get_our_data(folder) print("Loading done.") num_images = gt.shape[0] num_points = gt.shape[1] transformed_pts = np.zeros(prd.shape) for i in range(num_images): for j in range(num_points): # Ground - prediction as in paper transformed_pts[i, j] = fractional_matrix_power( covar[i, j], -0.5).dot(gt[i, j] - prd[i, j]) print("Transformation done.") # Add some points from the standard 2D bivariate normal/ standard 2D simplified # laplacian num_points_new = 10000 if args.laplacian: print("Getting standard Laplacian data from our function...") data = sample_from_simplified_laplacian(num_points_new, True) x1 = data[:, 0] y1 = data[:, 1] limit = (-10, 10) else: print("Getting standard Gaussian data from numpy inbuilt function...") x1, y1 = np.random.multivariate_normal([0, 0], [[1, 0], [0, 1]], num_points_new).T
def polyfit(maxdeg, filename): n_variables = np.loadtxt(filename, dtype='str').shape[1] - 1 variables = np.loadtxt(filename, usecols=(0, )) means = [np.mean(variables)] for j in range(1, n_variables): v = np.loadtxt(filename, usecols=(j, )) means = means + [np.mean(v)] variables = np.column_stack((variables, v)) f_dependent = np.loadtxt(filename, usecols=(n_variables, )) if n_variables > 1: C_1_2 = fractional_matrix_power(np.cov(variables.T), -1 / 2) x = [] z = [] for ii in range(len(variables[0])): variables[:, ii] = variables[:, ii] - np.mean(variables[:, ii]) x = x + ["x" + str(ii)] z = z + ["z" + str(ii)] if np.isnan(C_1_2).any() == False: variables = np.matmul(C_1_2, variables.T).T res = getBest(variables, f_dependent, maxdeg) parameters = res[0] params_error = res[1] deg = res[2] x = sympy.Matrix(x) M = sympy.Matrix(C_1_2) b = sympy.Matrix(means) M_x = M * (x - b) eq = mk_sympy_function(parameters, n_variables, deg) symb = sympy.Matrix(z) for i in range(len(symb)): eq = eq.subs(symb[i], M_x[i]) eq = simplify(eq) else: res = getBest(variables, f_dependent, maxdeg) parameters = res[0] params_error = res[1] deg = res[2] eq = mk_sympy_function(parameters, n_variables, deg) for i in range(len(x)): eq = eq.subs(z[i], x[i]) eq = simplify(eq) else: res = getBest(variables, f_dependent, maxdeg) parameters = res[0] params_error = res[1] deg = res[2] eq = mk_sympy_function(parameters, n_variables, deg) try: eq = eq.subs("z0", "x0") except: pass return (eq, params_error)