In [2]:
import randomCorr as rc
m1 = np.loadtxt(open("../matrices/3-molossidae.csv","rb"),delimiter=",")
m2 = np.loadtxt(open("../matrices/t3.csv","rb"),delimiter=",")
ms =[m1,np.identity(35)]
bp = []
for i in xrange(2):
b_n = rc.triang_decomp(ms[i])
p_n = rc.calc_params(b_n)
bp.append((b_n, p_n))
In [7]:
pcolor(ms[1])
Out[7]:
In [18]:
def rand_ortho_matrix(eigen_values):
size = len(eigen_values)
A = np.mat(np.random.random((size,size)))
Q, R = np.linalg.qr(A)
return Q * np.diag(eigen_values) * Q.T
In [86]:
eig.dec = eigvals(m1)
rm1 = rand_ortho_matrix(eig.dec)
pcolor(array(rm1))
Out[86]:
In [87]:
pcolor(m1)
Out[87]:
In [120]:
import randomCorr as rc
pm1 = rc.calc_params(m1)
pm1[:,0] += np.random.rand(35)/100000
print pm1.shape
t1 = rc.triang_from_params(pm1)
pcolor(t1.dot(t1.T))
eigvals(t1.dot(t1.T))
Out[120]:
In [118]:
plot(i)
Out[118]:
In [159]:
def calc_corr_path(ms, bp, i0, j, s=100, pcs=[0]):
m0 = ms[i0]
corrs = []
iso = np.ones(m0.shape[0])/np.sqrt(m0.shape[0])
diff = (bp[j][1] - bp[i0][1])/100
p = bp[i0][1]
for i in xrange(100):
p += diff
new_b = rc.triang_from_params(p)
m0 = np.dot(new_b, new_b.T)
corrs.append(matrix_correlation(ms[i0], m0))
return corrs
In [160]:
c = calc_corr_path(ms, bp, 0, 1)
In [164]:
matrix_correlation(ms[0], ms[])
Out[164]:
In [44]:
scatter(r,f, c=range(0,100))
Out[44]:
In [21]:
import numpy as np
In [22]:
m1s =np.ones(35*35).reshape((35,35))
In [25]:
eig(m1s)[0]
Out[25]:
In [27]:
eig(np.identity(35))
Out[27]:
In [29]:
m1.shape
Out[29]:
In [19]:
plot(eigvals(m1))
Out[19]:
In [52]:
nt = 35
import numpy as np
h = np.array(np.zeros(nt*1000)).reshape((1000,nt))
for i in xrange(1000):
c = rc.rand_corr(nt, 10**-3)
h[i] = eigvals(c)
In [55]:
hist(h[:,0])
Out[55]:
In [80]:
m1 = np.loadtxt(open("../matrices/2-peramelidae.csv","rb"),delimiter=",")
eigvals(m1)[0]
Out[80]:
In [81]:
bp = []
b_n = rc.triang_decomp(m1)
p_n = rc.calc_params(b_n)
bp.append((b_n, p_n))
In [88]:
bp[0][0][:,0]
Out[88]:
In [91]:
m2 = np.loadtxt(open("../matrices/1-gorilla.csv","rb"),delimiter=",")
b_n = rc.triang_decomp(m2)
p_n = rc.calc_params(b_n)
bp.append((b_n, p_n))
In [92]:
bp[1][0][:,0]
Out[92]:
In [93]:
rm = rc.rand_corr(35, 10**-3)
In [94]:
b_n = rc.triang_decomp(m2)
p_n = rc.calc_params(b_n)
bp.append((b_n, p_n))
In [95]:
bp[-1][0][:,0]
Out[95]:
In [101]:
import numpy as np
In [113]:
x,y= np.invert(np.tri(10,10, dtype=bool)).nonzero()
In [111]:
import randomCorr as rc
mr = rc.rand_corr(10, 10**-3)
In [122]:
np.corrcoef(mr[x,y], mr[x,y])[1,0]
Out[122]:
In [119]:
t
Out[119]:
In [127]:
mr[np.triu_indices(10,1)]
Out[127]:
In [128]:
mr[x,y]
Out[128]:
In [129]:
%load ../../bayesian-cov-estimation/utils/matrix_functions.py
In [130]:
import numpy as np
import pandas as pd
def CalcR2(Matrix):
tr = Matrix.shape[1]
x, y = np.asarray(np.invert(np.tri(tr, tr, dtype=bool)),
dtype=float).nonzero()
R2Tot = np.mean(Matrix[x, y] * Matrix[x, y])
return R2Tot
def eigen_var_calc(Matrix):
eVal, eVec = np.linalg.eigh(Matrix)
eigenMax = np.zeros(40, float)
eigenMax[0] = sum(eVal)
R2Tot = np.var(eVal) / np.var(eigenMax)
return R2Tot
def icv_calc(Matrix):
eVal, eVec = np.linalg.eigh(Matrix)
icv = np.sqrt(np.var(eVal)) / np.mean(eVal)
return icv
def cos_angle(vector1, vector2):
angle = np.dot(vector1, vector2) / (np.linalg.norm(vector1) *
np.linalg.norm(vector2))
return angle
def random_skewers(matrix1, matrix2, num_vectors=1000):
traits = matrix1.shape[0]
rand_vec = np.random.multivariate_normal(np.zeros(traits),
np.identity(traits, float),
num_vectors).T
delta_z1 = np.dot(matrix1, rand_vec)
delta_z2 = np.dot(matrix2, rand_vec)
ndelta_z1 = delta_z1/np.sqrt((delta_z1*delta_z1).sum(0))
ndelta_z2 = delta_z2/np.sqrt((delta_z2*delta_z2).sum(0))
return np.mean(np.diag(np.dot(ndelta_z1.T, ndelta_z2)))
def read_matrices(matrices, labels, num_traits):
raw_matrices = pd.read_csv(matrices)
with open(labels) as f:
monkey_labels = f.read().splitlines()
def make_symetric(matrix):
matrix = np.tril(matrix) + np.tril(matrix, k=-1).transpose()
return matrix
node_matrices = {monkey_labels[0]: np.array(raw_matrices.ix[0:num_traits-1, ])}
for i in range(1, len(monkey_labels)):
new_matrix = np.array(raw_matrices.ix[i*num_traits:(((i+1)*num_traits)-1), :])
node_matrices[monkey_labels[i]] = make_symetric(new_matrix)
return node_matrices
def matrix_correlation(matrix1, matrix2):
tr = matrix1.shape[0]
correlation = np.corrcoef(matrix1[np.triu_indices(tr,1)], matrix2[np.triu_indices(tr,1)])[1, 0]
return correlation
In [132]:
matrix_correlation(mr,mr)
Out[132]:
In [137]:
np.corrcoef(mr[0],mr[3])
Out[137]:
In [ ]: