DBHT Dendrogram: private API
The dendrogram is the second half of the seam behind DBHTs, after Bubble Tree. HierarchyConstruct4s takes the clusters and the bubble membership and returns a Matlab-style linkage matrix, and turn_into_Hclust_merges is the one step that speaks the Clustering.Hclust convention. Each function is driven directly, from inputs small enough to work by hand, in test/test_13f_dbht_seam.jl.
PortfolioOptimisers.DendroConstruct — Function
DendroConstruct(Zi::MatNum, LabelVec1::VecNum,
LabelVec2::VecNum,
LinkageDist::Num_VecNum)Construct the linkage matrix by continually adding rows to the matrix.
This function appends a new row to the linkage matrix at each iteration, recording the merge of clusters as indicated by changes in the label vectors. It is used internally for building dendrograms in DBHT and related hierarchical clustering routines.
Algorithm
- Take
indx, the vertices whose label differs betweenLabelVec1andLabelVec2. Those are the vertices the merge moved. - Read the labels
LabelVec1gave them, drop the repeats and sort them. A merge joins two labels, so exactly two survive. - Append one row to
Zi: those two labels, followed byLinkageDist.
Arguments
Zi:i × 3linkage matrix at iterationiin the same format as the output from Matlab. Each row holds the two merged labels and the height of the merge.LabelVec1:N × 1label vector for the vertices in the bubble for the previous valid iteration.LabelVec2:N × 1label vector for the vertices in the bubble for the trial iteration.LinkageDist: Height of the current merge, written into the third column.
Returns
Z::MatNum:(i + 1)×3linkage matrix at iterationi + 1in the same format as the output from Matlab.turn_into_Hclust_mergesconverts it to theClustering.Hclustconvention.
Related
PortfolioOptimisers.LinkageFunction — Function
LinkageFunction(d::MatNum, labelvec::VecNum)Find the pair of clusters of smallest union diameter in a bubble.
This function scores every pair of clusters that labelvec names by the diameter of their union under the distance matrix d, and returns the pair of smallest score. The diameter is the largest non-zero distance inside the union, so the score is a complete linkage. Used internally for hierarchical linkage construction in DBHT dendrogram routines.
Mathematical definition
\[\begin{align} \delta(I,\, J) &= \underset{u,\,v \in \mathcal{C}_{I} \cup \mathcal{C}_{J},\; d_{u,\,v} \neq 0}{\max}\; d_{u,\,v}\,, \\ (I^{\star},\, J^{\star}) &= \underset{I < J}{\arg\min}\; \delta(I,\, J)\,. \end{align}\]
Where:
- $d_{u,\,v}$: Distance between vertices $u$ and $v$, the entry of the input matrix.
- $\mathcal{C}_{I}$: Vertices that carry label $I$.
- $\delta(I,\, J)$: Diameter of the union of the two clusters.
The union is scored, not the cut between the two clusters, so a distance between two members of the same cluster can set $\delta$. A pair whose union carries no non-zero distance scores $0$, which is the smallest score there is.
Algorithm
- Take
lvec, the sorted distinct labels oflabelvec. - For each pair
(r, c)of labels withr < c, select the vertices that carry either label. - Take the largest non-zero entry of the distance submatrix on those vertices, and record
(lvec[r], lvec[c], value)as a row ofLinks. Record0as the value when the submatrix carries no non-zero entry. - Take the row of smallest value, giving the pair
PairLinkand the scoredvu.
Arguments
d:Nv×Nvdistance matrix for the vertices assigned to a bubble. Row and columniare the same vertex, and entryioflabelvecnames its cluster.labelvec:Nv×1label vector for the vertices in the bubble.
Returns
PairLink::Vector{Int}:2 × 1vector of the two cluster labels of the selected pair.dvu::Number: Diameter of the union of that pair, the smallest such value over every pair.
Related
PortfolioOptimisers.build_link_and_dendro — Function
build_link_and_dendro(
rg::AbstractRange,
dpm::MatNum,
LabelVec::VecNum,
LabelVec1::VecNum,
LabelVec2::VecNum,
V::VecNum,
nc::Number,
Z::MatNum
)Iteratively construct the linkage matrix for a bubble or cluster.
This function iterates over the vertices in a bubble or cluster, merging the pair of clusters with the best linkage at each step (as determined by LinkageFunction), and appending the corresponding row to the linkage matrix using DendroConstruct. Used internally for building dendrograms in DBHT and related hierarchical clustering routines.
Algorithm
- Take the pair of smallest union diameter with
LinkageFunctionoverdpmandLabelVec. - Give both members of the pair the label
maximum(LabelVec1) + 1, so the merged cluster takes a label no vertex carries yet. - Write the merged labels back into
LabelVec2at the verticesV. - Append one row to
ZwithDendroConstruct, at the height1 / nc. - Subtract one from
nc, and copyLabelVec2intoLabelVec1. - Repeat from step 1 once for each element of
rg.
Arguments
rg: Range whose length sets the number of merges. Its values are not read.dpm:Nv×Nvdistance matrix for the vertices assigned to the bubble or cluster, in the order ofV.LabelVec:Nv×1label vector of those vertices. It is mutated by step 2.LabelVec1:N × 1label vector over every vertex, for the previous valid iteration.LabelVec2:N × 1label vector over every vertex, for the trial iteration. It is mutated by step 3.V:Nv×1vector of the indices of the vertices in the bubble or cluster, indexing the rows ofLabelVec1.nc::Number: Merge counter. Step 4 writes the height1 / ncand step 5 lowers it by one, so the heights of a run rise towards1.Z: Current linkage matrix, with three columns.
Returns
Z::MatNum: Linkage matrix after every merge of the range, one row longer per merge.nc::Number: Merge counter, lowered by the number of merges.LabelVec1::VecNum:N × 1label vector for the next iteration, carrying the merged labels.
Related
PortfolioOptimisers.HierarchyConstruct4s — Function
HierarchyConstruct4s(
Rpm::MatNum,
Dpm::MatNum,
Tc::VecNum,
Mv::MatNum
)Constructs the intra- and inter-cluster hierarchy by utilizing the Bubble Hierarchy structure of a Maximal Planar Graph, specifically a Planar Maximally Filtered Graph (PMFG).
This function builds a hierarchical clustering (dendrogram) by first constructing intra-cluster linkages within each cluster (using the bubble structure), and then merging clusters to form the global hierarchy. It is a core step in the DBHT (Direct Bubble Hierarchical Tree) clustering pipeline.
Algorithm
- Give every vertex its own label in
LabelVec1, and buildE, theN × maximum(Tc)indicator of the discrete clusteringTc. - For each cluster
k, restrictMvto the vertices of that cluster, givingMc, and assign each of them to exactly one bubble withBubbleMember, givingMvv. Set the merge counterncto the vertex count of the cluster less one. - For each bubble of the cluster that holds more than one vertex, merge its vertices with
build_link_and_dendrooverlength(V) - 1steps, on the distance submatrix of that bubble. - Merge the bubbles of the cluster with
build_link_and_dendrooverlength(Bub) - 1steps, on the distance submatrix of the whole cluster. Steps 3 and 4 share onenc, so the heights of a cluster rise across both. - Repeat steps 2 to 4 for each cluster, which leaves one label per cluster.
- Merge the clusters over
length(kvec) - 1steps: take the pair of smallest union diameter withLinkageFunctionover the whole ofDpm, and give both sides a fresh label. - Write the height of that merge from
dcland not from the score of step 6.dclstarts at1for every vertex and each merge sets both sides to the sum of the two, so the height counts the clusters the merge joins. This is what puts every inter-cluster merge above every intra-cluster one, whose heights never exceed1.
Arguments
Rpm:N × Nsparse weighted adjacency matrix of the PMFG. It is read byBubbleMemberalone.Dpm:N × Nshortest path lengths matrix of the PMFG. Every linkage score is read from it.Tc:N × 1cluster membership vector.Tc[n] = kindicates cluster membership of vertexnto thek-th discrete cluster.Mv:N × Nbbubble membership matrix.Mv[n, bi] = 1means vertexnis a vertex of bubblebi.
Returns
Z::MatNum:(N-1)×3linkage matrix in the same format as the output from Matlab. Each row holds the two merged labels and the height of the merge.turn_into_Hclust_mergesconverts it to theClustering.Hclustconvention.
Related
PortfolioOptimisers.turn_into_Hclust_merges — Function
turn_into_Hclust_merges(Z::MatNum)Convert a Matlab-style linkage matrix to a format compatible with Clustering.Hclust.
This function transforms a linkage matrix produced by DBHT or similar hierarchical clustering routines into the format required by Clustering.Hclust, including proper indexing and cluster size tracking.
This is the seam to Clustering.Hclust, so the convention below is the one every consumer downstream reads. DBHTs loads the first two columns into mleft and mright and the third into heights, and Clusters and everything that cuts a dendrogram reads them back on that convention.
Both conventions number one merge per row, and they differ in how a row names its two sides.
| The side is | Matlab writes | Clustering.Hclust writes |
|---|---|---|
a leaf, vertex a | a, which is at most N | -a |
the cluster built by row j | j + N | j |
A row therefore names only rows above it, and the size of the cluster it builds is the sum of the sizes of its two sides.
Algorithm
- Set
Ntosize(Z, 1) + 1, which is the leaf count, and append a fourth column of zeros to hold the cluster sizes. - For each row
iand for each of its first two entriesa: whenais at mostNit names a leaf, so write-ain its place and add1to the size of rowi. - Otherwise
anames the cluster that rowj = a - Nbuilt, so writejin its place and add the size of rowjto the size of rowi. Rowjis above rowi, so its size is already final.
Arguments
Z:(N-1)×3Matlab-style linkage matrix, where each row represents a merge step with cluster indices and linkage heights.
Returns
Z::MatNum:(N-1)×4linkage matrix inClustering.Hclustformat. Columns one and two hold the two sides on the convention of the table above, column three keeps the heights unchanged, and column four holds the leaf count of the cluster each row builds.
Related