## 4 Self-Tuning Exploratory Data Analytics
### Probabilistic Approach
#### 4.2.3 ToPIC-Similarity
```
Data:X,K,K_train,K_train,Result:LS[] //variable initialization {topics = [ ],NTerms = [ ]; for\(K\) = K_train to K_traindo // build the LDA model;  LDAModel = [ ],LDModel = [ ],LDModel = [ ],Q \(\leftarrow\) (\(|V|\cdot\)TTR) / AgeFreq; // set the number of terms per topic; if\(Q\geq K\).AgeFreqthen \(n\leftarrow\) Q/K); else \(n\gets\) ArgFreq;  end if // collect together the terms of each topic; for\(t\gets 0\) to (K-1)do  NTTerms.append(LDModel.describeTopics(0[f].sort().take(\(n\)));  end if \(N\leftarrow\)NTerms.size();  topicDeser = zeros(K,N);  sumMatrix = zeros(K,K); for\(t\gets 0\) to (K-1)do for\(t\)word \(\leftarrow\) to Ndo // take the probability that the term has to be drawn // from the topic, given the LDModel  topicsDeser([f][word] \(\leftarrow\) LDAModel.describeTopics(0[f],NTerms[word]);  end if  end for  for\(t\gets 0\) to (K-1)do for\(s\gets 0\) to (K-1)do  simMatrix[f][d] \(\leftarrow\) cosine(topicsDeser(f], topicsDeser(s));  end if  end for  topicS.append(Frobenius-norm(simMatrix)?100/K); iftopicS[K] \(\geq\) topicS(K-1) AND secondDerivatives(topicS[K-1]) > 0 then  kS.append(topicS[K-1]); ifkS.label() > 3 then return kS.table();  end if  end for
```  
**Algorithm 2**ToPIC-similarity pseudo-code  
At the end of this step, a symmetric matrix of dimension \(K\) is obtained. The generic cell (_i,j_) contains the index of similarity between the topic of row i and the topic of column j. The topic similarity index for the considered model is obtained by calculating the Frobenius norm of the whole similarity matrix, and dividing the result by \(K\). Finally, since the topic similarity is a percentage, the index obtained is multiplied by 100. **K identification. ** Having calculated the topic similarity for each LDA model obtained with a different \(K\), this step illustrates the methodology adopted to identify the best configuration of \(K\). As the value of topic similarity decreases when the number of topics increases, two conditions have been set to find the best \(K\):  
* the chosen \(K\) must be a local minimum of the curve: _topic similarity_ (\(K_{i}\)) \(<\)_topic similarity_ (\(K_{i}\) + 1);
* the selected value must belong to a decreasing segment of the curve (the second derivative must be positive). ESCAPE considers the first three values that satisfy these requirements as the best \(K\) values to consider. The search ends when three values have been found, or when the considered K is larger than the \(K_{max}\) set at the beginning. For each experiment, three well-known statistical quality metrics are reported to characterize the found partitions. In ESCAPE, we have integrated three different measures to assess the quality of the probabilistic model: (i) _perplexity_, (ii) _entropy_, and (iii) _silhouette_. The perplexity [3] indicates how well the probabilistic model represents a sample. A lower perplexity value represents a better model for the analyzed collection. The entropy [51] is defined as the amount of information in a transmitted message. Hence, a message with high uncertainty indicates a large amount of entropy. Lastly, the silhouette [47] takes into account both the cohesion and the separation of a document. The cohesion represents how similar a document is with respect to its own clusters, while the separation represents how different a document is from documents belonging to other clusters. The silhouette index can assume values between \([-1,1]\), where a value close to \(1\) indicates that the document is correctly located in the proper cluster.