# Financial Market Correlation Analysis and Stock Selection Application Based on TCN-Deep Clustering
## 2 Financial Time-Series Deep Clustering Network: TCN-Deep Clustering
### Training Process
#### 2.2.2 Time-Series Clustering Optimization  
After completing the initial feature extraction phase of the TCN autoencoder, the decoder network of the autoencoder is lifted and only the encoder part of the feature extraction is retained, and the clustering layer is customized on this basis. Take the temporal feature \(z_{i}\) extracted by the encoder as the input and obtain the stock clustering results through the similarity measurement (Euclidean distance). According to the optimal number of k clusters defined in the initial stage, the k cluster centers are obtained. The \(i\)-th cluster class center is \(\mu_{i}\).  
Traditional k-means uses the hard labeling approach to measure the similarity of the sample points to the cluster centers but it cannot measure the uncertainty of data to the cluster centers, especially if the outlier points are assigned with low accuracy, which may reduce the quality of clustering. Therefore, in this paper, the t-distribution of soft labels is used to measure the similarity between the sample points and clustering centers, as shown in Equation (8):  
\[q_{ij}=\frac{\left(1+\abs{z_{j}-\mu_{i}}^{2}/\alpha\right)^{-\frac{\alpha+1}{ 2}}}{\sum_{i}\left(1+\abs{z_{j}-\mu_{i}}^{2}/\alpha\right)^{-\frac{\alpha+1}{ 2}}} \tag{8}\]  
where \(\abs{z_{j}-\mu_{i}}^{2}\) is the distance from \(z_{j}\) to the cluster center \(\mu_{i}\). \(q_{ij}\) is the is the probability of classifying \(q_{ij}\) to the clustering center \(\mu_{i}\). \(\alpha\) is the degree of freedom of the t-distribution, which is set to 1. To improve the quality of clustering, the model defines a high-confidence target probability distribution based on the clustering center, as shown in Equation (9). \(p_{ij}\) is the probability that \(z_{j}\) is assigned to different clustering centers. \(f_{i}\) represents the probability that all temporal features are assigned to clustering center \(\mu_{i}\). \(f_{i^{\prime}}\) represents the probability that all temporal features are assigned to different clustering centers.  
\[p_{ij}=\frac{q_{ij}^{2}/f_{i}}{\sum_{i^{\prime}}q_{ij}^{2}/f_{i^{\prime}}}f_{ i}=\sum_{j}q_{ij} \tag{9}\]In order to make the probability distribution of the soft distribution of the clustering layer \(q_{ij}\) consistent with the auxiliary target distribution \(p_{ij}\), the KL divergence is defined as the temporal clustering layer loss, as shown in Equation (10):  
\[L_{c}=KL(P\|Q)=\sum_{i}\sum_{j}p_{ij}\log\frac{p_{ij}}{q_{ij}} \tag{10}\]  
TCN-Deep Clustering trains the temporal clustering layer using the KL divergence, thereby optimizing the clustering centers \(u_{j}\) and the encoder parameters of the autoencoder \(\theta(w,b)\) simultaneously. The gradients of the loss function \(L_{c}\) with respect to the temporal feature \(z_{i}\) and the clustering center \(u_{j}\) are calculated as (11) and (12).  
\[\frac{\partial L_{c}}{\partial z_{i}}=\frac{\alpha+1}{\alpha}\sum_{j}\!\left( 1+\frac{\left|z_{i}-\mu_{j}\right|^{2}}{\alpha}\right)^{-1}\left(p_{ij}-q_{ij} \right)\left(z_{i}-\mu_{j}\right) \tag{11}\]  
\[\frac{\partial L_{c}}{\partial\mu_{i}}=-\frac{\alpha+1}{\alpha}\sum_{i}\! \left(1+\frac{\left|z_{i}-\mu_{j}\right|^{2}}{\alpha}\right)^{-1}\left(p_{ij}- q_{ij}\right)\left(z_{i}-\mu_{j}\right) \tag{12}\]  
The gradient \(\frac{\partial L_{c}}{\partial z_{i}}\) is transferred to the encoder network parameters and used for backpropagation to calculate the network parameter gradient \(\frac{\partial L_{c}}{\partial\theta}\). In this study, the Adam optimization method was used to optimize the loss function.