# STGAT: Spatial-Temporal Graph Attention Neural Network for Stock Prediction
## 3 System Framework
### Spatial-Temporal Graph Attention Neural Network
#### 3.3.2 TCN Module Based on Residual Mechanism  
The TCN module proposed in this work mainly uses one-dimensional convolutional layers to process time series data. Based on time series data \(x_{1},x_{2},\ldots,x_{t}\), the TCN predicts \(y_{t}\) by sliding on the time dimension to capture the features and the time dependency relationship in the input sequence. Notably, the \(y_{t}\) denotes univariate outputs that depend solely on the casual constraints of \(x_{1},x_{2},\ldots,x_{t}\). Therefore, the value of time \(t\) only convolves with values before \(t\), which ensures every output in the time series only relates to previous input values and current time. This mechanism avoids data leakage, and the output value obtained from causal convolution at \(x_{t}\) is:  
\[(F*X)_{x_{t}}=\sum_{k=0}^{K}f_{k}x_{t-k}, \tag{13}\]  
where K represents the filter length, which determines the number of past time steps considered in calculating the current output. The weights of the filter \(F=(f_{1},f_{2},\ldots,f_{K})\) specify the influence of the input at each previous time step, \(x_{t-k}\), on the current output. As shown in Figure 6, each TCN module consists of three convolutional layers to transmit features in parallel, utilizing a residual mechanism to extract temporal information and weaken redundant information sufficiently.  
Figure 5: Multi-head attention mechanism.  
In STGAT, two TCN modules are applied for different functions. The first module processes the time series price data of all stocks from 20 trading days. In the initial layer, temporal features are extracted from the raw stock price data, resulting in a preliminary representation of these temporal patterns. The second and third layers work together as residual blocks, focusing on capturing finer details of the stock price fluctuations. The output from the second layer provides the early-stage residual features, while the feature map from the third layer is processed through a sigmoid function. This enables the calculation of attention weights, which are then used to generate a corresponding weight map that highlights the most influential features for further processing. Multiplying the residual features generated by the second layer with the feature map generated by the third layer results in a weighted feature map, which functions as residual information, significantly weakening the impact of redundant information. The final output feature map includes both the features of the temporal features and weighted feature map, enabling the model to capture deeper temporal features hidden in input data and exclude redundant information. The process of the first TCN module can be modeled as  
\[Y=TCN_{1}(X)+TCN_{2}(X)\odot\sigma(TCN_{3}(X)), \tag{14}\]  
where \(Y\) is the output feature, and \(\sigma\) (\(\cdot\)) is the sigmoid function.  
The second module fuses spatial and temporal features, respectively, generated by the GAT module and first TCN module. This operation of combining features enhances the model's robustness and predictive ability in dealing with highly uncertain market environments. The second temporal convolution module can be expressed as  
\[Y_{ST}=TCN_{4}(X_{fused})+TCN_{5}(X_{fused})\cdot\sigma(TCN_{6}(X_{fused})), \tag{15}\]  
where \(Y_{ST}\) is the spatial-temporal feature extracted with the GAT and TCN, and \(X_{fused}=concat(A,Y,dim=time)\) functions as a preliminary fused feature. After extracting features from the stock price data, STGAT uses linear layers to map the fused data to the final dimensions required for prediction, producing the predicted prices for all stocks in the pool, which can then be used for trading decisions.