Effacer les filtres
Effacer les filtres

How to show component in each cluster created by linkage function

2 vues (au cours des 30 derniers jours)
Leonardo Coccia
Leonardo Coccia le 2 Juil 2021
Modifié(e) : KALASH le 3 Mai 2024
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);

Réponses (1)

KALASH
KALASH le 3 Mai 2024
Modifié(e) : KALASH le 3 Mai 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

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by