# Feature-Attended Federated LSTM for Anomaly Detection in the Financial Internet of Things +
## 4 Detail of Feature-Attended LSTM
### Feature Grouping  
In the financial traffic data set, data features have a certain correlation, that is, features are not completely orthogonal. If the features with low correlation are divided into a group as input, the feature information cannot be fully extracted, thus affecting the model performance. If a corresponding training channel is constructed for each feature, the computational overhead in the model training process will increase greatly.  
In a simple case, we can put each feature into one group. That is to say, we have \(k\) groups. Assume \(S_{max}\) denotes the anomaly detection accuracy in this simple case. Then, we can put two features \(f_{i}\) and \(f_{j}\) into one group, and we have \(k-1\) groups. Assume \(S_{i,j}\) denotes the anomaly detection accuracy in this case. Therefore, given a pre-defined threshold \(P\), \(\gamma_{f_{i},f_{j}}\) can be obtained from Formula (10):  
\[\gamma_{f_{i},f_{j}}=\begin{cases}\text{{true}},&\left(\frac{S_{max}-S_{i,j}} {S_{max}}-P\right)\geq 0\\ \text{{false}},&\left(\frac{S_{max}-S_{i,j}}{S_{max}}-P\right)<0\end{cases} \tag{10}\]  
Then, the eigen correlation matrix \(\Gamma\) could be expressed as Equation (11):  
\[\Gamma=\begin{bmatrix}\gamma_{f_{1},f_{1}}&\cdots&\gamma_{f_{1},f_{K}}\\ \cdots&\ddots&\vdots\\ \gamma_{f_{K},f_{1}}&\cdots&\gamma_{f_{K},f_{K}}\end{bmatrix} \tag{11}\]  
When executing the feature grouping algorithm, the matrix \(\Gamma\) is traversed. For each element \(\gamma_{f_{i},f_{j}}(i,j\leq K)\), if \(\left(\gamma_{f_{i},f_{j}}=\text{{true}}\right)\) and \(\left((i==K)\&(j==K)=false\right)\), we will then choose one of the following actions:  
* When \(f_{i}\) and \(f_{j}\) are already in one same group, switch to \(\gamma_{f_{i},f_{j+1}}\) (when \(j<K\)) or \(\gamma_{f_{i+1},f_{1}}\) (when \(j=K\)).
* When \(f_{i}\) is in a group \(g_{a}\), and \(f_{j}\) is in another group \(g_{b}\), \(g_{a}\) and \(g_{b}\) should be merged into one new group \(g^{\prime}\). Then, switch to \(\gamma_{f_{i},f_{j+1}}\) (when \(j<K\)) or \(\gamma_{f_{i+1},f_{1}}\) (when \(j=K\)).  
* When \(f_{i}\) (or \(f_{j}\)) is in a group, but \(f_{j}\) (or \(f_{i}\)) not, we should put \(f_{j}\) (or \(f_{i}\)) into the group that \(f_{i}\) (or \(f_{j}\)) is in. Then, switch to \(\gamma_{f_{i},f_{j+1}}\) (when \(j<K\)) or \(\gamma_{f_{i+1},f_{1}}\) (when \(j=K\)).
* When \(f_{i}\) is not in any group, and \(f_{j}\) neither, we should create a new group, and put both \(f_{i}\) and \(f_{j}\) into this group. Then, switch to \(\gamma_{f_{i},f_{j+1}}\) (when \(j<K\)) or \(\gamma_{f_{i+1},f_{1}}\) (when \(j=K\)).  
Algorithm 1 shows the detailed feature-based iterative grouping algorithm.  
```
1:correlation matrix \(\Gamma\), group set \(G\)
2:Initialize \(G=\O\)
3:for\(i=1;i\leq K\); \(i++\)do
4:for\(j=1;j\leq K\); \(j++\)do
5:if\(\gamma_{f_{i},f_{j}}==true\)then
6:if\(\exists f_{i},f_{j}\in g,g\in G\)then
7: continue to next loop;
8:elseif\(\exists f_{i}\in g_{a},f_{i}\in g_{b}\), and \(g_{a}\in G,g_{b}\in G(g_{a}\cap g_{b}=\O)\)then
9: create a new group \(g^{{}^{\prime}}=g_{a}\cup g_{b}\), then set \(g^{{}^{\prime}}\in G\);
10:elseif\(\exists f_{i}\in g_{a},g_{a}\in G\) and \(\forall\;f_{j}\
otin g_{b}(b\in N_{+},b\leq m)\)then
11: set \(f_{j}\in g_{a}\);
12:elseif\(\exists f_{j}\in g_{a},g_{a}\in G\) and \(\forall\;f_{i}\
otin g_{b}(b\in N_{+},b\leq m)\)then
13: set \(f_{i}\in g_{a}\);
14:else
15: create a new group \(g_{l}=\left\{f_{i},f_{j}\right\}\), then set \(g_{l}\in G\);
16:endif
17:endif
18:endfor
```  
**Algorithm 1** Feature-based iterative grouping algorithm