Variance-covariance matrix and related methods¶
import toytree
tree = toytree.rtree.unittree(ntips=8, treeheight=10)
VCV matrix¶
The variance-covariance matrix (VCV) represents the sum lengths of shared edges between pairs of samples as covariances (off-diagonals) and sum root-to-tip edge lengths of each sample as variances (diagonals). This matrix thus represents the tree structure in a numeric way that is used internally in many comparative methods, such as modeling trait evolution on phylogenies. The default option is to return a VCV with expected variances and covariances under a Brownian motion model of evolution.
vcv = tree.pcm.get_vcv_matrix_from_tree(df=True)
vcv
r0 | r1 | r2 | r3 | r4 | r5 | r6 | r7 | |
---|---|---|---|---|---|---|---|---|
r0 | 10.0 | 7.5 | 5.0 | 5.0 | 2.5 | 0.0 | 0.0 | 0.0 |
r1 | 7.5 | 10.0 | 5.0 | 5.0 | 2.5 | 0.0 | 0.0 | 0.0 |
r2 | 5.0 | 5.0 | 10.0 | 7.5 | 2.5 | 0.0 | 0.0 | 0.0 |
r3 | 5.0 | 5.0 | 7.5 | 10.0 | 2.5 | 0.0 | 0.0 | 0.0 |
r4 | 2.5 | 2.5 | 2.5 | 2.5 | 10.0 | 0.0 | 0.0 | 0.0 |
r5 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 10.0 | 2.5 | 2.5 |
r6 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 2.5 | 10.0 | 5.0 |
r7 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 2.5 | 5.0 | 10.0 |
correlation matrix¶
The correlation matrix is computed from the VCV and is a standardized measure of similarity used internally by some phylogenetic comparative methods.
corr = tree.pcm.get_corr_matrix_from_tree(df=True)
corr
r0 | r1 | r2 | r3 | r4 | r5 | r6 | r7 | |
---|---|---|---|---|---|---|---|---|
r0 | 1.00 | 0.75 | 0.50 | 0.50 | 0.25 | 0.00 | 0.00 | 0.00 |
r1 | 0.75 | 1.00 | 0.50 | 0.50 | 0.25 | 0.00 | 0.00 | 0.00 |
r2 | 0.50 | 0.50 | 1.00 | 0.75 | 0.25 | 0.00 | 0.00 | 0.00 |
r3 | 0.50 | 0.50 | 0.75 | 1.00 | 0.25 | 0.00 | 0.00 | 0.00 |
r4 | 0.25 | 0.25 | 0.25 | 0.25 | 1.00 | 0.00 | 0.00 | 0.00 |
r5 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 1.00 | 0.25 | 0.25 |
r6 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.25 | 1.00 | 0.50 |
r7 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.25 | 0.50 | 1.00 |
VCV to distance matrix¶
Because the VCV contains all information about a trees edges, it can also be converted to a distance matrix of those edges using the Euclidean distance. Note this method is available only from the module-level API is it does not rely a ToyTree object.
dist = toytree.pcm.get_distance_matrix_from_vcv_matrix(vcv)
dist
r0 | r1 | r2 | r3 | r4 | r5 | r6 | r7 | |
---|---|---|---|---|---|---|---|---|
r0 | 0.0 | 5.0 | 10.0 | 10.0 | 15.0 | 20.0 | 20.0 | 20.0 |
r1 | 5.0 | 0.0 | 10.0 | 10.0 | 15.0 | 20.0 | 20.0 | 20.0 |
r2 | 10.0 | 10.0 | 0.0 | 5.0 | 15.0 | 20.0 | 20.0 | 20.0 |
r3 | 10.0 | 10.0 | 5.0 | 0.0 | 15.0 | 20.0 | 20.0 | 20.0 |
r4 | 15.0 | 15.0 | 15.0 | 15.0 | 0.0 | 20.0 | 20.0 | 20.0 |
r5 | 20.0 | 20.0 | 20.0 | 20.0 | 20.0 | 0.0 | 15.0 | 15.0 |
r6 | 20.0 | 20.0 | 20.0 | 20.0 | 20.0 | 15.0 | 0.0 | 10.0 |
r7 | 20.0 | 20.0 | 20.0 | 20.0 | 20.0 | 15.0 | 10.0 | 0.0 |
tree from vcv matrix¶
This converts the vcv to a distance matrix and returns an unrooted neighbor-joining distance tree. Below I infer the root and draw the resulting tree.
# returns tree from the vcv
tre = toytree.pcm.get_tree_from_vcv_matrix(vcv)
# root and draw the tree
toytree.mod.root_on_minimal_ancestor_deviation(tre).draw();