In [1]:
m9 = np.loadtxt(open("../matrices/matriz-9.csv","rb"),delimiter=",")
m10 = np.loadtxt(open("../matrices/matriz-10.csv","rb"),delimiter=",")

In [5]:
m1 = np.loadtxt(open("../matrices/cor.matrix_C_NL.csv","rb"),delimiter=";")
m2 = np.loadtxt(open("../matrices/cor.matrix_M_NL.csv","rb"),delimiter=";")
m3 = np.loadtxt(open("../matrices/exemplo_35_NT.csv","rb"),delimiter=",")
m4 = np.loadtxt(open("../matrices/exemplo_2.csv","rb"),delimiter=";")

m5 = rc.rand_corr(35, 10**-4)
m6 = rc.rand_corr(35, 10**-4)
m7 =  np.loadtxt(open("../matrices/1-gorilla.csv","rb"),delimiter=",")
m8 = np.loadtxt(open("../matrices/4-hyllostomidae.csv","rb"),delimiter=",")
m9 = np.loadtxt(open("../matrices/matriz-9.csv","rb"),delimiter=",")
m10 = np.loadtxt(open("../matrices/matriz-10.csv","rb"),delimiter=",")

In [15]:
ms =[m1, m2, m3, m4, m5, m6, m7, m9, m10]
bp = []

for i in xrange(len(ms)):
    b_n = rc.triang_decomp(ms[i])
    p_n = rc.calc_params(b_n)
    bp.append((b_n, p_n))

In [24]:
r2, flex,isoc = rc.calc_path(ms, bp, -2, -1)

In [25]:
len(r2)


Out[25]:
100

R2


In [8]:
plot(r2)


Out[8]:
[<matplotlib.lines.Line2D at 0x384b050>]

Flexibilidade


In [9]:
plot(flex)


Out[9]:
[<matplotlib.lines.Line2D at 0x388f5d0>]

In [13]:
plot(rs)


Out[13]:
[<matplotlib.lines.Line2D at 0x41a9b90>]

Correlação com isométrico


In [12]:
plot(isoc)


Out[12]:
[<matplotlib.lines.Line2D at 0x3fab190>]

R2 x Flexibilidade


In [11]:
scatter(r2, flex, c=range(0,100))


Out[11]:
<matplotlib.collections.PathCollection at 0x3d98f50>

In [67]:
rand_m = rc.rand_corr(35, 10**-4)
ms2 =[ms[2], rand_m]
bp2 = []

for i in xrange(2):
    b_n = rc.triang_decomp(ms2[i])
    p_n = rc.calc_params(b_n)
    bp2.append((b_n, p_n))

r2, flex,isoc = rc.calc_path(ms2, bp2, 0, 1)

In [68]:
plot(r2)


Out[68]:
[<matplotlib.lines.Line2D at 0x78d8250>]

In [69]:
plot(flex)


Out[69]:
[<matplotlib.lines.Line2D at 0x78d7a90>]

In [70]:
plot(isoc)


Out[70]:
[<matplotlib.lines.Line2D at 0x7486750>]

In [71]:
scatter(r2, flex, c=range(0,100))


Out[71]:
<matplotlib.collections.PathCollection at 0x8473c50>

In [54]:
plot(r2)


Out[54]:
[<matplotlib.lines.Line2D at 0x3726890>]

In [55]:
plot(flex)


Out[55]:
[<matplotlib.lines.Line2D at 0x5eecf50>]

In [56]:
plot(isoc)


Out[56]:
[<matplotlib.lines.Line2D at 0x6208b50>]

In [57]:
scatter(r2, flex, c=range(0,100))


Out[57]:
<matplotlib.collections.PathCollection at 0x6d26f50>

In [100]:
r2, flex,isoc = rc.calc_path(ms, bp, 2, 3)

In [101]:
plot(r2)


Out[101]:
[<matplotlib.lines.Line2D at 0xac1e190>]

In [102]:
plot(flex)


Out[102]:
[<matplotlib.lines.Line2D at 0xa9efe50>]

In [103]:
scatter(r2, flex, c=range(0,100))


Out[103]:
<matplotlib.collections.PathCollection at 0xb2e3350>

In [86]:
pcolor(m4)


Out[86]:
<matplotlib.collections.PolyCollection at 0x949ed10>

In [87]:
pcolor(m3)


Out[87]:
<matplotlib.collections.PolyCollection at 0x93548d0>

In [88]:
pcolor(m1)


Out[88]:
<matplotlib.collections.PolyCollection at 0x9bf7490>

In [89]:
pcolor(m2)


Out[89]:
<matplotlib.collections.PolyCollection at 0x9ed5050>

In [91]:
r2, flex,isoc = rc.calc_path(ms, bp, 1, 3)

In [92]:
plot(r2)


Out[92]:
[<matplotlib.lines.Line2D at 0x8153ed0>]

In [93]:
plot(flex)


Out[93]:
[<matplotlib.lines.Line2D at 0x9ea6e50>]

In [94]:
plot(isoc)


Out[94]:
[<matplotlib.lines.Line2D at 0xa6ebed0>]

In [95]:
scatter(r2, flex, c=range(0,100))


Out[95]:
<matplotlib.collections.PathCollection at 0xa9e2a50>

In [7]:
pcolor(m1)


Out[7]:
<matplotlib.collections.PolyCollection at 0x2ea9d50>

In [337]:
ps = rc.calc_params(m1)

In [338]:
ps[34]


Out[338]:
array([ 1.73493675,  1.35145372,  1.52635134,  1.40652609,  1.39494978,
        1.48772449,  1.73499343,  1.51957667,  1.50803899,  1.30525758,
        1.4098457 ,  1.17759939,  1.22889134,  1.43084039,  1.8098259 ,
        1.60290208,  1.67096229,  1.5382742 ,  1.24462763,  1.43797265,
        1.9162765 ,  1.7712118 ,  1.60637231,  1.33135363,  2.70115478,
        1.7656056 ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ])

In [24]:
np.random.random()/10


Out[24]:
0.02248194743794899

In [364]:
rs = []
for i in xrange(100):
    rp = np.tril(np.random.random(size=35*35).reshape((35,35))*np.pi/(10**10), -1)
    npr = ps + rp
    nm1 = rc.triang_from_params(npr)
    rs.append(rc.random_skewers(m1, nm1))

In [365]:
np.median(rs)


Out[365]:
0.48412170671245491

In [344]:
npr = ps + rp

In [347]:
nm1 = rc.triang_from_params(npr)

In [366]:
pcolor(nm1.dot(nm1.T))


Out[366]:
<matplotlib.collections.PolyCollection at 0x4bb29cb10>

In [342]:
pcolor(m1)


Out[342]:
<matplotlib.collections.PolyCollection at 0x4b9de3690>

In [43]:
pcolor(bp[0][1])


Out[43]:
<matplotlib.collections.PolyCollection at 0x472e1d0>

In [23]:
m1_params = bp[0][1]

In [24]:
m1_eigs = eigvals(m1)
plot(m1_eigs)


Out[24]:
[<matplotlib.lines.Line2D at 0x31a2b90>]

In [100]:
m1_params += rt

In [27]:
m1_params.shape


Out[27]:
(35, 35)

In [90]:
tm = np.array([[1,2,3],[4,5,6],[7,8,9]])

In [92]:
tm[tril_indices(3)] += 1

In [335]:
np.pi


Out[335]:
3.141592653589793

In [237]:
rt = np.tril(np.random.random(size=35*35).reshape((35,35)))/10

In [242]:
m1_params = bp[0][1]
m1_params[:,1] += rt[:,1]

In [243]:
new_m1 = rc.triang_from_params(m1_params)

In [244]:
nm1 = np.dot(new_m1, new_m1.T)
pcolor(nm1)


Out[244]:
<matplotlib.collections.PolyCollection at 0x17adfe90>

In [245]:
pcolor(m1)


Out[245]:
<matplotlib.collections.PolyCollection at 0x17db3ed0>

In [246]:
plot(eigvals(nm1))
plot(eigvals(m1))


Out[246]:
[<matplotlib.lines.Line2D at 0x16ffe510>]

In [236]:
pcolor(nm1 - m1)


Out[236]:
<matplotlib.collections.PolyCollection at 0x17312c50>

In [173]:
np.max(nm1 - m1)


Out[173]:
0.13046193887638716

In [229]:
m1_params[:,0]


Out[229]:
array([ 0.5716223 ,  1.6344928 ,  2.06237129,  1.73396276,  2.44708714,
        1.4959759 ,  1.9798569 ,  2.07231814,  1.68241282,  1.86193334,
        2.27778393,  1.69832203,  1.99214123,  2.30645345,  2.18388842,
        1.55968483,  2.51375188,  1.54126599,  3.09695283,  1.64499533,
        2.15484479,  2.1978003 ,  1.82412687,  1.5103795 ,  2.09328727,
        1.96714795,  2.04461154,  2.27097843,  2.45417228,  1.45427891,
        2.12258206,  2.47752266,  2.23661004,  1.8457056 ,  2.47147865])

In [8]:
import randomCorr as rc

rand_mat = rc.rand_corr(35, 10**-4)

In [9]:
pcolor(rand_mat)


Out[9]:
<matplotlib.collections.PolyCollection at 0x312aa90>

In [10]:
35*34/2


Out[10]:
595

In [63]:
[1,2,3,4,5][-3:]


Out[63]:
[3, 4, 5]

In [13]:
import randomCorr as rc

In [270]:
def krzCor (cov1, cov2):
  # Calculates the Krzanowski correlation between matrices
  #
  # Args:
  #     cov.matrix.(1,2): covariance being compared
  #     ret.dim: number of retained dimensions in the comparison,
  #              default for nxn matrix is n/2-1
  # Return:
  #     Kzranowski correlation

  
    ret_dim = cov1.shape[0]/2.0 - 1
    ev_c1 = np.linalg.eig(cov1)[1][0:ret_dim]
    ev_c2 = np.linalg.eig(cov2)[1][0:ret_dim]
    s = np.dot(ev_c2.T, ev_c1).dot(np.dot(ev_c1.T, ev_c2))
    print np.dot(ev_c1.T,ev_c2)
    sl = np.sum(np.linalg.eigvals(s)) / ret_dim
    return sl

In [271]:
krzCor(ms[0], ms[1])


[[-0.09001599 -0.05725788  0.3855146  ...,  0.01734618 -0.00299792
   0.00691611]
 [ 0.44811118  0.4252505   0.13952226 ...,  0.06932332  0.03130843
   0.09983611]
 [-0.21169017 -0.09795432 -0.09881213 ..., -0.03951768  0.02969759
   0.0093025 ]
 ..., 
 [-0.16300615  0.01337255  0.01247762 ..., -0.09686033  0.31492463
   0.03360655]
 [-0.0202943  -0.00297158  0.01027502 ..., -0.22216568  0.14393714
  -0.00080798]
 [-0.12848739  0.03848164  0.13350647 ...,  0.05976448 -0.00438261
  -0.03797534]]
Out[271]:
(0.96969696969696073+0j)

In [84]:
rcParams['figure.dpi'] = 100
rcParams['figure.figsize'] = (20,10)

In [93]:
%load_ext rmagic


/home/walrus/.virtualenvs/py/local/lib/python2.7/site-packages/pytz/__init__.py:31: UserWarning: Module multiprocessing was already imported from /usr/lib/python2.7/multiprocessing/__init__.pyc, but /home/walrus/.virtualenvs/py/lib/python2.7/site-packages is being added to sys.path
  from pkg_resources import resource_stream

In [97]:
m0 = ms[0]
m1 = ms[1]

In [99]:
%Rpush m0 m1

In [263]:
%%R

KrzCor <- function (cov.matrix.1, cov.matrix.2, ret.dim = NULL)
  # Calculates the Krzanowski correlation between matrices
  #
  # Args:
  #     cov.matrix.(1,2): covariance being compared
  #     ret.dim: number of retained dimensions in the comparison,
  #              default for nxn matrix is n/2-1
  # Return:
  #     Kzranowski correlation
{
  if (is.null(ret.dim))
    ret.dim = dim(cov.matrix.1)[1]/2 - 1
  EigenVectors <- function (x) return (eigen(x)$vectors[,1:ret.dim])
  A <- EigenVectors (cov.matrix.1)
  B <- EigenVectors (cov.matrix.2)
  S <- (t(A) %*% B )%*% (t(B) %*% A)
  print(t(A) %*% B)  
  SL <- sum (eigen(S)$values) / ret.dim
  return (SL)
}
KrzCor(m0, m1)


             [,1]        [,2]        [,3]         [,4]         [,5]
 [1,]  0.03539682  0.11517890  0.65182794  0.297743141  0.331535907
 [2,] -0.71750365  0.38753882 -0.24726414  0.243765635  0.173804322
 [3,]  0.23711574 -0.10976994  0.19247842  0.152086579  0.044747886
 [4,]  0.12809695  0.35408987  0.17986575 -0.076202699  0.032013957
 [5,]  0.06455249  0.41439850 -0.06999880 -0.009848932 -0.264892804
 [6,] -0.25156922 -0.02415680  0.25815672  0.074865785 -0.303155165
 [7,]  0.04394673 -0.23762170 -0.23978009  0.090803055  0.486554055
 [8,] -0.33529285 -0.37356266  0.19232388 -0.043072722 -0.061631805
 [9,] -0.17235141  0.01090196 -0.07494380 -0.564978234  0.006019927
[10,]  0.03712589 -0.22204417 -0.12121926 -0.054530160 -0.267013930
[11,] -0.08731441 -0.14797097  0.08888417 -0.454297752  0.362317170
[12,]  0.12272781  0.05110306 -0.17628177 -0.027943140  0.059526902
[13,] -0.15510036  0.02638310  0.26883912 -0.221713180 -0.308008112
[14,] -0.09612928  0.10596140  0.21493750 -0.167450700  0.154174192
[15,]  0.11847240  0.34235180 -0.07525277 -0.028095366  0.112753835
[16,] -0.11061691  0.10413382  0.18155223 -0.171571984  0.104907028
              [,6]        [,7]        [,8]          [,9]       [,10]
 [1,] -0.145444008  0.09664082  0.17996966 -0.1888276594 -0.08828576
 [2,] -0.072044211  0.14755559  0.04057027  0.1236898202  0.07021396
 [3,] -0.240006019 -0.06984127  0.17080029  0.4071896576  0.39051696
 [4,]  0.267831454  0.10566912 -0.36094275 -0.3391110329  0.32122794
 [5,] -0.177149442 -0.56894230  0.09358493 -0.0252557558  0.22373647
 [6,]  0.379029130 -0.32655585  0.11809627  0.1346255388 -0.18536586
 [7,]  0.193255267 -0.30403447  0.26011927 -0.1571921073  0.39088468
 [8,]  0.244075222 -0.07859894 -0.08269919 -0.2494186968  0.34153682
 [9,] -0.214574513 -0.03618627  0.31633096 -0.3016363855 -0.06293728
[10,]  0.191816640  0.29000256  0.25328535 -0.0869100316  0.10269543
[11,] -0.004827914 -0.14093716 -0.40445913  0.4027148638 -0.05965669
[12,] -0.118336172  0.31353421  0.18451097  0.0002735379  0.15575381
[13,] -0.265808977  0.07926065 -0.04453554  0.1551487721  0.42473350
[14,] -0.070304620 -0.05780775  0.24649606 -0.1302522334 -0.18474530
[15,]  0.465665616  0.13154960  0.03035814  0.2557936649  0.14796601
[16,]  0.150326574  0.04165095  0.03792570  0.0527891524  0.03437667
             [,11]       [,12]       [,13]       [,14]        [,15]
 [1,]  0.262207408 -0.04442018  0.18076005  0.09404626 -0.157713091
 [2,] -0.024626024 -0.05441700  0.08419855 -0.01277046  0.160079420
 [3,] -0.222890643  0.03804154 -0.02883259  0.16403081  0.422795648
 [4,]  0.104097607  0.09354987  0.26560514 -0.05576173  0.339863510
 [5,]  0.356846855 -0.02784929 -0.10003283  0.09875671 -0.199900950
 [6,] -0.050336058 -0.26001940  0.27217675 -0.10058512  0.007908418
 [7,] -0.008435322 -0.05208096  0.11170733  0.01845950 -0.071110602
 [8,]  0.097844453 -0.10514335 -0.32519410 -0.11472074  0.061892277
 [9,]  0.064506791  0.11502451  0.18731695  0.17301975  0.311229927
[10,]  0.066680620 -0.24838756  0.32741071  0.38366666 -0.101647516
[11,]  0.302316394 -0.15500501  0.17668811  0.11476668 -0.147458418
[12,]  0.188472648 -0.08652586  0.06401772 -0.28823030 -0.392998364
[13,] -0.241346249 -0.10807222  0.10762545 -0.09527502 -0.220180042
[14,] -0.178052988 -0.32604750 -0.44343037  0.15862116 -0.014838168
[15,] -0.079775161 -0.22267983 -0.16738819  0.25511047 -0.086483200
[16,] -0.399399823  0.43438312  0.04696080  0.15909912 -0.301306854
             [,16]
 [1,] -0.051469833
 [2,] -0.054197770
 [3,] -0.202863437
 [4,]  0.076688669
 [5,] -0.008981618
 [6,]  0.029739979
 [7,]  0.290154131
 [8,] -0.427004926
 [9,] -0.206154623
[10,]  0.123816851
[11,] -0.070317538
[12,] -0.431798807
[13,]  0.203117685
[14,]  0.062749440
[15,] -0.172031238
[16,] -0.139945777
[1] 0.7522517

In [272]:
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)))

In [276]:
reload(rc)


Out[276]:
<module 'randomCorr' from '/home/walrus/corr-param/randomCorr.py'>

In [320]:
r2, flex,isoc, rs = rc.calc_path(ms, bp, 0, 1, calc_rs=True)

In [321]:
scatter(rs, r2)


Out[321]:
<matplotlib.collections.PathCollection at 0x3f768fbd0>

In [322]:
plot(rs)
plot(r2)


Out[322]:
[<matplotlib.lines.Line2D at 0x3f71d2fd0>]

In [318]:
rr2 = []
for rm in xrange(1000):
    rr2.append(rc.calc_r2(rc.rand_corr(35, 10**-4)))

In [319]:
hist(rr2)


Out[319]:
(array([ 19, 129, 259, 288, 170,  95,  28,   7,   4,   1]),
 array([ 0.15205963,  0.17040514,  0.18875065,  0.20709616,  0.22544168,
        0.24378719,  0.2621327 ,  0.28047822,  0.29882373,  0.31716924,
        0.33551475]),
 <a list of 10 Patch objects>)

In [293]:
rc.calc_r2(ms[2])


Out[293]:
0.043627394957983269

In [294]:
random_skewers(ms[0], ms[2])


Out[294]:
0.53342272026997206

In [331]:
plot(rs)
plot(r2)


Out[331]:
[<matplotlib.lines.Line2D at 0x4b7abe0d0>]

In [334]:
scatter(r2, rs)


Out[334]:
<matplotlib.collections.PathCollection at 0x4b9281ad0>

In [6]:
m = m8

ms = [m]
b_n = rc.triang_decomp(m)
p_n = rc.calc_params(b_n)

bp = [(b_n, p_n)]

for i in xrange(1000):    
    rand_matrix = rc.rand_corr(35, 10**-4)
    b_n = rc.triang_decomp(rand_matrix)
    p_n = rc.calc_params(b_n)
    ms.append(rand_matrix)
    bp.append((b_n, p_n))


/home/walrus/corr-param/randomCorr.py:85: RuntimeWarning: invalid value encountered in arccos
  p[i, j] = np.arccos(b[i, j]/np.exp(np.sum(np.log(np.sin(p[i, 0:j])))))

In [19]:
r2_a = []
flex_a = []
isoc_a = []

for i in xrange(1,12):
    r2, flex,isoc = rc.calc_path(ms, bp, 0, i)
   # rand_idx = np.random.randint(0,100, size=3)
   # for j in rand_idx:
    r2_a.append(r2[1])
    flex_a.append(flex[1])
    isoc_a.append(isoc[1])

In [20]:
scatter(r2_a, flex_a, c=range(11))


Out[20]:
<matplotlib.collections.PathCollection at 0x6a68790>

In [1]:
import randomCorr as rc

In [12]:
rc.calc_r2(m)


Out[12]:
0.056338811311237767

In [13]:
rc.flexibility(m)


Out[13]:
0.60940634664460314

In [17]:
len(r2_a)


Out[17]:
11

In [15]:
reload(rc)


Out[15]:
<module 'randomCorr' from '/home/walrus/corr-param/randomCorr.py'>

In [21]:
r2, flex,isoc = rc.calc_path(ms, bp, 0, 17)

In [22]:
scatter(r2, flex, c=range(100))


Out[22]:
<matplotlib.collections.PathCollection at 0x69ea1d0>

In [ ]: