## Appendix C Internal and External Validation  
The unsupervised selection of an optimal number of clusters in our procedure is ruled by multiple methods for internal validation, which aims at the evaluation of separation and compactness of clusters in output. The aforementioned \(M(M-1)/2\) distances are partitioned according to cluster memberships established by means of cutting at a certain level the dendrogram yielded by UPGMA, such that \(k\) clusters are obtained. The total within sum of squares exploits the function:  
\[WSS(k)=\sum_{l=1}^{k}\frac{1}{2|C_{l}|}\sum_{(i,j)\in C_{l}}d\left(\mathbf{y}_{i ^{\prime}},\mathbf{y}_{j}\right)\]  
where \((i,j)\) represents the edge joining patients \(\mathbf{y}_{i}\) and \(\mathbf{y}_{j}\) belonging to the same cluster. The optimal \(k\) is given by the first integer such that \(\sum_{\gamma\geq k}|WSS(\gamma+1)-WSS(\gamma)|<\delta\), with \(\delta\) a sufficiently small preset parameter to obtain stability for a reasonably small value \(k\). Another method corresponds to silhouette width, measuring the dissimilarity of patients in different clusters. The similarity of the patient \(y_{i}\) in the cluster \(C_{l}\) with respect to remaining members is  
\[s(i)=\frac{1}{|C_{l}|-1}\sum_{j\in C_{l},\ j\
eq i}d\left(\mathbf{y}_{i^{\prime}}, \mathbf{y}_{j}\right)\]  
and analogously the dissimilarity with respect to patients in different clusters reads  
\[t(i)=\min_{l^{\prime}\
eq i}\frac{1}{|C_{l^{\prime}}|}\sum_{j\in C_{l^{\prime }}}d\left(\mathbf{y}_{i^{\prime}},\mathbf{y}_{j}\right)\]  
finally yielding the silhouette width  
\[w(i)=\frac{t(i)-s(i)}{\max\{t(i),s(i)\}}\]  
The range of this quantity is [1, 2, 3, 4, 5], with the lower bound corresponding to high dissimilarity with patients within the same cluster, while the upper one expresses high similarity with remaining members [22]. We will consider the mean value evaluated over all patients to define the average silhouette width, whose maximization targets the selection of an optimal number of clusters. The Dunn index measures the smallest separation between patients belonging to different clusters with respect to the largest cluster diameter [25]  
\[Dunn(k)=\frac{\min_{1\leq l,m\leq k;m\
eq l}\min_{y_{i}\in C_{l},\ y_{j}\in C _{m}}d(\mathbf{y}_{i},\mathbf{y}_{j})}{\max_{1\leq l\leq k}diam(C_{l})}\]  
where \(diam(C_{l})\ =\ \max_{x,y\in C_{l}}d(\mathbf{x},\mathbf{y})\). The optimal number of clusters is compared with the patients' partition defined by labels we wish to characterize in an unsupervised manner through the adjusted Rand index (ARI), representing an external validation method. The initial Rand index (RI) formulation of this quantity measures the overlap of clusters taking into account a set of objects \(O=\left\{\sigma_{1},\ldots,\ \sigma_{m}\right\}\) endowed with two partitions \(U=\left\{U_{1},\ldots,\ U_{p}\right\}\) and \(V=\left\{V_{1},\ldots,\ V_{q}\right\}\). Then define:  
* \(TP=\left\{\left(\sigma_{i},o_{j}\right)|o_{i},o_{j}\in U_{l^{\prime}};o_{i},o_ {j}\in V_{l^{\prime}}\right\}\)
* \(TN=\left\{\left(o_{i},o_{j}\right)\left|o_{i}\in U_{l_{1}};o_{j}\in U_{l_{2}};o _{i}\in V_{l^{\prime}_{1}},o_{j}\in V_{l^{\prime}_{2}}\right\}\)
* \(FP=\left\{\left(o_{i},o_{j}\right)\left|o_{i},o_{j}\in U_{l^{\prime}_{1}};o_{i }\in V_{l^{\prime}_{2}}\right\}\)
* \(FN=\left\{\left(o_{i},o_{j}\right)\left|\left|o_{i}\in U_{l_{1}};o_{j}\in U_{l ^{\prime}_{2}};o_{i},o_{j}\in V_{l^{\prime}_{p}}\right.\right\}\)  
with \(1\leq i,j\leq m\) and \(1\leq l,l_{1},l_{2}\leq p,\ 1\leq l^{\prime},l^{\prime}_{1},l^{\prime}_{2}\leq q\). Let's describe these sets in terms of binary classification by means of two class labels related to: objects \(o_{i},o_{j}\) belong to the same subset in \(U\) and \(V\) or objects \(o_{i},o_{j}\) belong to different subsets in \(U\) and \(V\). In this way the Rand index is equivalent with the definition of classifiers accuracy:  
\[RI=\frac{|TP|+|TN|}{|TP|+|TN|+|FP|+|FN|}=\frac{|TP|+|TN|}{\left( \begin{array} []{c}m\\ 2\end{array} \right)}\]  
where \(\left( \begin{array}{c}m\\ 2\end{array}