How to show component in each cluster created by linkage function
6 views (last 30 days)
Show older comments
Goodmorning everyone, I'm applying Hierarchical Clustering using linkage function to a 94x94 Correlation distance matrix. So I have 94 assets. My question is how to show the component (returns indices) of each cluster founded by the linkage function. Obvoiusly, as long as the index in the output link is <=94 this identify the asset index. My question is how to show clusters made by other clusters (subclusters) in terms of asset indices for each cluster. For example, in link, cluster 6 is made by asset 60 and cluster 99 (99 identify the cluster 5 that is made by asset 12 and 87) and so for. Thanks a lot to everyone!
C = corr(returns);
distCorr = ((1-C)/2).^0.5;
link = linkage(distCorr, 'single', 'euclidean');
figure(1)
[H,T,outperm] = dendrogram(link,0);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/672048/image.png)
0 Comments
Answers (1)
KALASH
on 3 May 2024
Edited: KALASH
on 3 May 2024
Hi Leonardo,
To trace back the composition of any cluster, including subclusters, you can recursively explore the linkage matrix. Below is a MATLAB code snippet for your reference:
function indices = getClusterIndices(link, clusterIdx, numAssets)
% Initialize indices array
indices = [];
% If the clusterIdx is an original asset, return its index
if clusterIdx <= numAssets
indices = clusterIdx;
else
% Calculate index in the linkage matrix
linkIdx = clusterIdx - numAssets;
% Recursively get indices for the left and right branches
left = link(linkIdx, 1);
right = link(linkIdx, 2);
indices = [indices, getClusterIndices(link, left, numAssets)];
indices = [indices, getClusterIndices(link, right, numAssets)];
end
end
To use this function, pass it the linkage matrix “link”, the index of the cluster you're interested in (which can be greater than the number of assets for merged clusters), and the total number of assets (94 in your case).
For example, use the following function syntax to obtain the asset indices for cluster 99 -
clusterIndices = getClusterIndices(link, 99, 94);
disp(clusterIndices);
Also remember, that MATLAB uses 1-based indexing, so an index of 1 corresponds to the first asset in your original matrix.
Feel free to adjust the function according to your specific requirements.
Hope it helps!
Best,
Kalash
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!