In [1]:
import randomCorr
c = randomCorr.mkRandCorr(40, 10**-3)
O método utilizado é o descrito no artigo:
http://www.plosone.org/article/info:doi/10.1371/journal.pone.0048902
In [2]:
pcolor(c)
Out[2]:
In [4]:
plot(np.linalg.eigvals(c))
Out[4]:
In [3]:
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
n_traits = 40
rand_eigen = np.array(np.random.random(n_traits))
p = rand_ortho_matrix(rand_eigen)
pcolor(numpy.array(p))
Out[3]:
In [2]:
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
In [3]:
def flexibility(matrix1, num_vectors=1000):
traits = matrix1.shape[0]
rand_vec = np.random.multivariate_normal(np.zeros(traits),
np.identity(traits, float),
num_vectors).T
rand_vec = rand_vec/np.sqrt((rand_vec*rand_vec).sum(0))
delta_z1 = np.dot(matrix1, rand_vec)
ndelta_z1 = delta_z1/np.sqrt((delta_z1*delta_z1).sum(0))
return np.mean(np.diag(np.dot(ndelta_z1.T, rand_vec)))
In [76]:
s = 1000
r2 = np.zeros(s)
flex = np.zeros(s)
isoc = np.zeros(s)
for i in xrange(s):
c = randomCorr.mkRandCorr(40, 10**-3)
r2[i] = CalcR2(c)
flex[i] = flexibility(c)
isoc[i] = np.abs(np.dot(eig(c)[1][:,0], iso))
In [77]:
hist(isoc)
Out[77]:
In [78]:
hist(r2)
Out[78]:
In [79]:
hist(flex)
Out[79]:
In [81]:
scatter(r2,flex)
Out[81]:
In [43]:
corrcoef(r2, flex)
Out[43]:
In [65]:
p = np.dot(c, eig(c)[1][:,0])
In [66]:
np.sqrt(np.dot(p, p))
Out[66]:
In [64]:
eigvals(c)
Out[64]:
In [22]:
ms =[m1,m2,m3,m4]
In [71]:
params = []
for m in ms:
mb = mkB(m)
mp = calc_params(mb)
params.append((mb, mp))
In [79]:
plot(params[0][1][:,0])
Out[79]:
In [80]:
plot(params[1][1][:,0])
Out[80]:
In [81]:
plot(params[2][1][:,0])
Out[81]:
In [82]:
plot(params[3][1][:,0])
Out[82]:
In [102]:
diff = (m4-m3)/100
diff
Out[102]:
In [103]:
m_min = m3
for i in xrange(101):
pcolor(m_min)
savefig('{}.png'.format(i))
m_min += diff
In [56]:
import randomCorr as rc
In [68]:
m1 = np.loadtxt(open("../matrices/1-gorilla.csv","rb"),delimiter=",")
m2 = np.loadtxt(open("../matrices/2-peramelidae.csv","rb"),delimiter=",")
m3 = np.loadtxt(open("../matrices/3-molossidae.csv","rb"),delimiter=",")
m4 = np.loadtxt(open("../matrices/4-hyllostomidae.csv","rb"),delimiter=",")
#m5 = np.loadtxt(open("../matrices/5.csv","rb"),delimiter=",")
#m6 = np.loadtxt(open("../matrices/6.csv","rb"),delimiter=",")
ms =[m1,m2,m3,m4]
In [70]:
bp = []
for i in xrange(4):
b_n = rc.triang_decomp(ms[i])
p_n = rc.calc_params(b_n)
bp.append((b_n, p_n))
In [59]:
for i in xrange(6):
print np.max(ms[i] - np.dot(bp[i][0], bp[i][0].T))
In [45]:
for i in xrange(4):
bp_i = rc.triang_from_params(bp[i][1])
print np.max(ms[i] - np.dot(bp_i, bp_i.T))
In [58]:
diff = (bp[1][1] - bp[0][1])/100
p = bp[0][1]
m0 = ms[0]
for i in xrange(101):
new_b = rc.triang_from_params(p)
m0 = np.dot(new_b, new_b.T)
pcolor(m0)
savefig('{}.png'.format(i))
p += diff
In [72]:
diff = (ms[1] - ms[0])/100
m0 = ms[0]
for i in xrange(101):
pcolor(m0)
savefig('m-{:03d}.png'.format(i))
m0 += diff
In [39]:
pcolor(m1)
Out[39]:
In [23]:
pcolor(ms[0])
Out[23]:
In [14]:
tm = rc.triang_from_params(bp[0][1] + 100*diff)
pcolor(np.dot(tm, tm.T))
Out[14]:
In [135]:
m0 = ms[0]
s = 100
r2 = np.zeros(s)
flex = np.zeros(s)
isoc1 = np.zeros(s)
isoc2 = np.zeros(s)
iso = np.ones(m0.shape[0])/np.sqrt(m0.shape[0])
diff = (bp[3][1] - bp[0][1])/100
p = bp[0][1]
for i in xrange(100):
r2[i] = CalcR2(m0)
flex[i] = flexibility(m0)
isoc1[i] = np.abs(np.dot(eig(m0)[1][:,0], iso))
isoc2[i] = np.abs(np.dot(eig(m0)[1][:,1], iso))
p += diff
new_b = rc.triang_from_params(p)
m0 = np.dot(new_b, new_b.T)
In [156]:
scatter(r2[0], flex[0])
Out[156]:
In [37]:
CalcR2(ms[0])
Out[37]:
In [14]:
eig(ms[2])[1][:,0]
Out[14]:
In [15]:
eig(ms[3])[1][:,0]
Out[15]:
In [44]:
def calc_path(i, j, s=100, pcs=[0]):
m0 = ms[i]
r2 = np.zeros(s)
flex = np.zeros(s)
isoc = []
iso = np.ones(m0.shape[0])/np.sqrt(m0.shape[0])
diff = (bp[j][1] - bp[i][1])/100
p = bp[i][1]
for i in xrange(100):
r2[i] = CalcR2(m0)
flex[i] = flexibility(m0)
for j in pcs:
isoc.append(np.abs(np.dot(eig(m0)[1][:,j], iso)))
p += diff
new_b = rc.triang_from_params(p)
m0 = np.dot(new_b, new_b.T)
return r2, flex, isoc
In [71]:
rn, fn, ino = calc_path(2,3)
In [73]:
scatter(rn, fn, c=range(0,100))
annotate('3', xy=(rn[0], fn[0]), xycoords='data',
xytext=(0.05, 0.06), textcoords='axes fraction',
arrowprops=dict(facecolor='blue', shrink=0.05),
horizontalalignment='right', verticalalignment='top',
)
annotate('4', xy=(rn[-1], fn[-1]), xycoords='data',
xytext=(0.05, 0.2), textcoords='axes fraction',
arrowprops=dict(facecolor='red', shrink=0.05),
horizontalalignment='right', verticalalignment='top',
)
savefig('3-4-r2_vs_flex.png')
In [118]:
y = np.sqrt(np.diagonal(ms[]))
y = (y[:, np.newaxis] * y)
corrP = ms[5] / y
In [119]:
ms[4] = corrP
In [125]:
plt.ax
Out[125]:
In [109]:
t4 = rc.triang_decomp(ms[4])
In [111]:
np.max(np.dot(t4, t4.T) - ms[4])
Out[111]:
In [154]:
np.savetxt('np5.csv', ms[4], delimiter=',')
In [47]:
#m7 = np.loadtxt(open("../matrices/7.csv","rb"),delimiter=";")
#m8 = np.loadtxt(open("../matrices/8.csv","rb"),delimiter=";")
m7 = rc.rand_corr(35, 10**-3)
m8 = rc.rand_corr(35, 10**-3)
ms =[m7, m8]
In [31]:
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 [38]:
import randomCorr as rc
reload(rc)
Out[38]:
In [43]:
r,f,i = rc.calc_path(ms, bp, 0,1, pcs=[0])
In [44]:
plot(r)
Out[44]:
In [41]:
plot(f)
Out[41]:
In [46]:
plot(i)
Out[46]:
In [14]:
scatter(r, f, c=range(0,100))
annotate('7', xy=(r[0], f[0]), xycoords='data',
xytext=(0.05, 0.06), textcoords='axes fraction',
arrowprops=dict(facecolor='blue', shrink=0.05),
horizontalalignment='right', verticalalignment='top',
)
annotate('8', xy=(r[-1], f[-1]), xycoords='data',
xytext=(0.05, 0.2), textcoords='axes fraction',
arrowprops=dict(facecolor='red', shrink=0.05),
horizontalalignment='right', verticalalignment='top',
)
savefig('7-8_r2_vs_flex.png')
In [32]:
def calc_path(mi, mj, s=100, pcs=[0]):
m0 = mi
r2 = np.zeros(s)
flex = np.zeros(s)
isoc = []
iso = np.ones(m0.shape[0])/np.sqrt(m0.shape[0])
diff = (mj - mi)/100
for i in xrange(100):
r2[i] = CalcR2(m0)
flex[i] = flexibility(m0)
for j in pcs:
isoc.append(np.abs(np.dot(eig(m0)[1][:,j], iso)))
m0 += diff
return r2, flex, isoc
In [48]:
r,f,i=calc_path(ms[0], ms[1])
In [49]:
plot(r)
Out[49]:
In [50]:
plot(f)
Out[50]:
In [51]:
plot(i)
Out[51]:
In [53]:
scatter(r,f)
Out[53]:
In [54]:
m = rc.rand_corr(35, 10**-3)
In [31]:
import randomCorr as rc
r2 = []
flex = []
ms = []
for x in xrange(1000):
m = rc.rand_corr(35, 10**-3)
r2.append(rc.calc_r2(m))
flex.append(rc.flexibility(m))
ms.append(m)
In [61]:
lm = map(lambda m: m[3][7], ms)
hist(lm)
Out[61]:
In [27]:
r_f = zip(r2,flex)
rf = filter(lambda r: r[0] > 0.2 and r[0] < 0.23, r_f)
rs = map(lambda i: i[0], rf)
fs = map(lambda i: i[1], rf)
scatter(rs,fs, c=range(0, len(rs)))
print corrcoef(rs,fs)
In [29]:
scatter(r2, flex, c=range(1000))
print corrcoef(r2, flex)
In [ ]:
np.mean(
In [ ]: