# CL-TAD: A Contrastive-Learning-Based Method for Time Series Anomaly Detection
## Appendix A Algorithms in the Proposed Method  
```
1 Input a batch of subsequences: \(\{B_{i}\}_{i=0}^{S-1}\); length of a subsequence: \(L\)
2 Output a batch of masked subsequences : \(\{M_{j}\}_{j=0}^{N-1}\)\(\triangleright\)\(N=S\times L\)
3 begin
4 for\(i\) in range \((0,S-1)\)do
5 for\(j\) in range \((iL,iL+L-1)\)do
6\(M_{j}=copy(B_{i})\)\(\triangleright\)\(m\): masking value
7
8 end for
9
10 end for
11
12 end for
```  
**Algorithm A1**Masked data batch generation with \(\gamma\)  
```
1 Input a batch of masked samples \(\{M_{j}\}_{j=0}^{N-1}\); Reconstruction module \(\Phi\)
2 Output a batch of reconstructed samples \(\{R_{j}\}_{j=0}^{N-1}\)
3 begin
4\(\#\) Forward pass \(\{R_{j}\}_{j=0}^{N-1}=\Phi(\{M_{j}\}_{j=0}^{N-1})\)
5\(\#\) Minimize loss \(L^{rc}=\frac{1}{N}\sum_{j=0}^{N-1}\frac{1}{L}\sum_{t=0}^{L-1}||B_{\lfloor j/L \rfloor}[t]-R_{j}[t]||\)
6 end for
```  
**Algorithm A2**Reconstruction loss computation for the reconstruction module \(\Phi\)```
1 Input a batch of subsequence samples: \(\{B_{i}\}_{i=0}^{S-1}\); masking module: \(\gamma\); reconstruction module: \(\Phi\); transformation module: \(\psi\); encoder module: \(\Omega\); similarity score hyperparameter: \(\tau\); length of a sample: \(L\) Output \(\{U_{i}\}_{i=0}^{S-1}\), \(\{E_{j}\}_{j=0}^{N-1}\)
2 begin
3#Forward pass
4#Generate masking data batch \(\{M_{j}\}_{j=0}^{N-1}\) with module \(\gamma\) \(\{M_{j}\}_{j=0}^{N-1}=\gamma\) ( \(\{B_{i}\}_{i=0}^{S-1}\))
5#Generate positive samples \(\{R_{j}\}_{j=0}^{N-1}\) for \(\{B_{i}\}_{i=0}^{S-1}\) with reconstruction module \(\Phi\) \(\{R_{j}\}_{j=0}^{N-1}=\Phi\) ( \(\{M_{j}\}_{j=0}^{N-1}\))
6#Generate learnable augmented samples \(\{T_{j}\}_{j=0}^{N-1}\) of \(\{R_{j}\}_{j=0}^{N-1}\) with module \(\psi\) \(\{T_{j}\}_{j=0}^{N-1}=\psi\) ( \(\{R_{j}\}_{j=0}^{N-1}\))
7#Captures information from \(\{T_{j}\}_{j=0}^{N-1}\)and map to a new space \(\{E_{j}\}_{j=0}^{N-1}\) for contrastive learning with module \(\Omega\)
8\(\{E_{j}\}_{j=0}^{N-1}=\Omega\) ( \(\{T_{j}\}_{j=0}^{N-1}\))
9#Captures the information from \(\{B_{j}\}_{j=0}^{N-1}\)and map to a new space \(\{U_{j}\}_{j=0}^{N-1}\) for contrastive learning with module \(\Omega\)
10\(\{U_{i}\}_{i=0}^{S-1}=\Omega\) ( \(\{B_{i}\}_{i=0}^{S-1}\))
11#Calculate the reconstruction loss \(L^{\text{rc}}\) based on \(\{B_{i}\}_{i=0}^{S-1}\) and \(\{R_{j}\}_{j=0}^{N-1}\)
12\(L^{\text{rc}}=\frac{1}{N}\sum_{j=0}^{N-1}\frac{1}{L}\sum_{t=0}^{L-1}||B_{|j/L }|[t]-R_{j}[t]||\)
13#Calculate the contrastive loss \(L^{\text{ct}}\) based on \(\{U_{i}\}_{i=0}^{S-1}\) and \(\{E_{j}\}_{j=0}^{N-1}\) (line \(\mathbf{18}\sim\mathbf{31}\))
14#Calculate pairwise similarity score between all positive (L,E) pairs based on Equation 4 fori \(\in\) (0,S-1)do
15 for\(k\in\) (iL, iL+L-1)do
16\(v(U_{i},E_{k})=\exp(\frac{U_{i}^{T}E_{k}}{\|U_{i}\|\|E_{k}\|}*\frac{\sigma(u( U_{i}))}{\tau})\)\(\triangleright\)\(\sigma\): sigmoid function
17
18 end for
19#Calculate pairwise similarity score between all positive (E,U) pairs based on Equation 4 forj \(\in\) (0,N-1)do
20\(v(E_{j},U_{|j/L|})=\exp(\frac{E_{j}^{T}U_{|j/L|}}{\|E_{j}\|\|U_{|j/L|}}*\frac{ \sigma(u(E_{j}))}{\tau})\)\(\triangleright\)\(\sigma\): sigmoid function
21
22 end for
23
24 define\(L^{\text{ct}}(U_{i},E_{k})=-log\frac{v(U_{i},E_{k})}{\sum_{j=0}^{N-1}1_{\{j \in[i,j,L+L-1]|^{2}(U_{i},E_{j})+\sum_{j=0}^{N-1}1_{[m=j]|^{2}(U_{i},U_{i},U_{ m})+v(U_{i},E_{k})}\}}}\)
25 define\(L^{\text{ct}}(E_{j},U_{|j/L|})=-log\frac{\sum_{k=0}^{N-1}1_{\{k\in[i,j,L+L-1]|^{2}( E_{j},E_{k})+\sum_{i=0}^{S-1}0(E_{j},U_{i})}}}{\sum_{k=0}^{N-1}1_{\{k\in[i,j,L+L-1]|^{2}( E_{j},E_{k})+\sum_{i=0}^{S-1}0(E_{j},U_{i})}}}\)
26#Calculate an average contrastive loss
27\(L^{\text{ct}}=\frac{1}{2}((\frac{1}{N}\sum_{i=0}^{S-1}\sum_{k=lL}^{\text{L+L-1 }}L^{\text{ct}}(U_{i},E_{k})]+[\frac{1}{N}\sum_{j=0}^{N-1}L^{\text{ct}}(E_{j},U _{|j/L|})])\)
28#Calculate the final loss \(L^{\text{final}}\)
29\(L^{\text{final}}=L^{\text{rc}}+L^{\text{ct}}\)
30 Update weights in modules \(\Phi,\psi,\Omega\) to minimize the loss \(L^{\text{final}}\)
31
```
1 Input A set of subsequences for a testing time series: \(\mathbf{W}\), masking module: \(\gamma\), transformation function: \(\psi\), encoder function: \(\Omega\), threshold: \(\delta\) begin
2 foreach sliding window \(W_{i}\in\mathbf{W}\)do
3#Forward pass
4#Generate \(L\) masking samples \(\{M_{j}\}_{j=0}^{L-1}\) with module \(\gamma\)
5\(\{M_{j}\}_{j=0}^{L-1}=\gamma\) ( \(W_{i}\))
6#Generate \(L\) positive samples \(\{R_{j}\}_{j=0}^{L-1}\) for \(W_{i}\) with reconstruction module \(\Phi\)
7\(\{R_{j}\}_{j=0}^{L-1}=\Phi\) ( \(\{M_{j}\}_{j=0}^{L-1}\))
8#Generate \(L\) learnable augmented samples \(\{T_{j}\}_{j=0}^{L-1}\) of \(\{R_{j}\}_{j=0}^{L-1}\) with module \(\psi\)
9\(\{T_{j}\}_{j=0}^{L-1}=\psi\) ( \(\{R_{j}\}_{j=0}^{L-1}\))
10#Captures information from \(\{T_{j}\}_{j=0}^{L-1}\)and map to a new space \(\{E_{j}\}_{j=0}^{L-1}\) with module \(\Omega\)
11\(\{E_{j}\}_{j=0}^{L-1}=\Omega\) ( \(\{T_{j}\}_{j=0}^{L-1}\))
12#Captures the information from \(W_{i}\) and map to a new space \(U_{i}\) with module \(\Omega\)
13\(U_{i}=\Omega\) ( \(W_{i}\))
14#Detect anomaly of \(W_{i}\) (line \(16\sim 28\))
15 if\(i=0\)then
16#Detect anomaly at every time point of \(W_{i}\)
17 for\(j\) in range \((0,L-1)\)do
18\(D(U_{i},E_{j})=||U_{i}/||U_{i}||-E_{j}/||E_{j}||||_{2}\)
19 if\(D(U_{i},E_{j})>\delta\)then
20\(W_{i}[j]\) is an anomaly point
21
22 end if
23
24 end if
25
26 else
27 if\(i>0\)then
28#Detect anomaly at the last time point of \(W_{i}\)
29#Calculate distance between \(U_{i}\) and \(E_{L-1}\)
30\(D(U_{i},E_{L-1})=||U_{i}/||U_{i}||-E_{L-1}/||E_{L-1}||||_{2}\)
31 if\(D(U_{i},E_{L-1})>\delta\)then
32\(W_{i}[-1]\) is an anomaly point
33
34 end if
35
36 end if
37
38 end if
39
40 end if
410
42 end for
43
44 end for
```
**Algorithm A4**Anomaly score computation and anomaly detection
```
1 Input A set of subsequences for a test time series: \(\mathbf{W}\), masking module: \(\gamma\), transformation module: \(\psi\), encoder module: \(\Omega\)
2 Output a list of threshold \(\Delta\) begin
3
4# Determine the maximum distance between a positive (U,E) pair (line 5 \(\sim\) 28) \(d_{max}=0\)\(\triangleright\)\(d_{max}:\) maximum distance between a positive (U,E) pair
5 foreachinput data batch \(\{B_{i}\}_{i=0}^{S-1}\in\mathbf{W}\)do
6#Forward pass (line 8 \(\sim\) 17)
7#Generate masking data batch \(\{M_{j}\}_{j=0}^{N-1}\) with module \(\gamma\) \(\{M_{j}\}_{j=0}^{N-1}=\gamma\) ( \(\{B_{i}\}_{i=0}^{S-1}\))
8#Generate positive samples \(\{R_{j}\}_{j=0}^{N-1}\) for \(\{B_{i}\}_{i=0}^{S-1}\) with reconstruction module \(\Phi\) \(\{R_{j}\}_{j=0}^{N-1}=\Phi\) ( \(\{M_{j}\}_{j=0}^{N-1}\))
9#Generate learnable augmented samples \(\{T_{j}\}_{j=0}^{N-1}\) of \(\{R_{j}\}_{j=0}^{N-1}\) with module \(\psi\) \(\{T_{j}\}_{j=0}^{N-1}=\psi\) ( \(\{R_{j}\}_{j=0}^{N-1}\))
10#Captures information from \(\{T_{j}\}_{j=0}^{N-1}\)and map to a new space \(\{E_{j}\}_{j=0}^{N-1}\) for contrastive learning with module \(\Omega\) \(\{E_{j}\}_{j=0}^{N-1}=\Omega\) ( \(\{T_{j}\}_{j=0}^{N-1}\))
11#Captures the information from \(\{B_{j}\}_{j=0}^{N-1}\)and map to a new space \(\{U_{j}\}_{j=0}^{N-1}\) for contrastive learning with module \(\Omega\) \(\{U_{i}\}_{i=0}^{S-1}=\Omega\) ( \(\{B_{i}\}_{i=0}^{S-1}\))
12#Determine the maximum distance \(d_{max}\) (line 19 \(\sim\) 28)
13 for\(i\) in range (0, S-1)do
14 distances =[]
15 for\(k\) in range (iL, iL+L-1)do
16#Calculate distance between \(U_{i}\) and \(E_{k}\) \(D(U_{i},E_{k})=\|U_{i}-E_{k}\|_{2}\) \(\text{distances.append(}\)\(D(U_{i},E_{k})\))
17
18 end for
19\(d_{max}=max(d_{max},max(distances))\)
20
21 end for
22
23 end for
24
25
26#Generate a list of thresholds for testing \(d_{max}\) (line 30 \(\sim\) 35) \(\Delta=[]\) \(interval=\frac{d_{max}}{1001}\) for\(c\) in range (0,1000)do
27\(\Delta.append((c+1)*interval)\)
28
29 end for
30
31 end for
```
**Algorithm A5** Determination of the list of candidate thresholds
```
1 Input A set of subsequences for a test time series: \(W\); masking module: \(\gamma\); reconstruction module: \(\psi\); encoder module: \(\Omega_{2}\) a list of candidate thresholds: \(\Delta\); ground true labels: \(Gt\) Output The best \(F_{1}\) score and its threshold begin
2\(F_{1-best}=0\); \(Precision_{f}=0\); \(Recall_{f}=0\); \(threshold_{f}=0\); foreach\(\delta\in\Delta\)do
3\(\mathrm{O}=[]\) #Initial empty output sequence \(\mathrm{O}s\) foreach a subsequence \(W_{i}\in W\)do
4#Forward pass
5#Generate L masking samples \(\{M_{j}\}_{j=0}^{L-1}\) with module \(\gamma\) \(\{M_{j}\}_{j=0}^{L-1}=\gamma\) (\(W_{i}\))
6#Generate L positive samples \(\{R_{j}\}_{j=0}^{L-1}\) for \(W_{i}\) with reconstruction module \(\Phi\) \(\{R_{j}\}_{j=0}^{L-1}=\Phi\) (\(\{M_{j}\}_{j=0}^{L-1}\))
7#Generate L learnable augmented samples \(\{T_{j}\}_{j=0}^{L-1}\) of \(\{R_{j}\}_{j=0}^{L-1}\) with module \(\psi\) \(\{T_{j}\}_{j=0}^{L-1}=\psi\) (\(\{R_{j}\}_{j=0}^{L-1}\))
8#Captures information from \(\{T_{j}\}_{j=0}^{L-1}\)and map to a new space \(\{E_{j}\}_{j=0}^{L-1}\) with module \(\Omega\) \(\{E_{j}\}_{j=0}^{L-1}=\Omega\) (\(\{T_{j}\}_{j=0}^{L-1}\))
9#Captures the information from \(W_{i}\) and map to a new space \(U_{i}\) with module \(\Omega\) \(U_{i}=\Omega\) (\(W_{i}\))
10#Decet anomaly of \(W_{i}\) if\(i=0\)then
11#Detect anomaly at every time point of \(W_{i}\) for\(j\) in range \((0,L-1)\)do
12\(D(U_{i},E_{j})=||U_{i}-E_{j}||_{2}\) if\(D(U_{i},E_{j})\leq\delta\)then
13\(\mathrm{O}s.append(0)\)
14 else
15\(\mathrm{O}s.append(1)\)
16 end if
17
18
19 else
20 if\(i>0\)then
21#Detect anomaly at the last time point of \(W_{i}\) #Calculate distance between \(U_{i}\) and \(E_{L-1}\) \(D(U_{i},E_{L-1})=||U_{i}/||U_{i}||-E_{L-1}/||E_{L-1}||\)\(\mathrm{I}_{2}\) if\(D(U_{i},E_{j})\leq\delta\)then
22\(\mathrm{O}s.append(0)\)
23 else
24\(\mathrm{O}s.append(1)\)
25







## References

* [PERSON] and [PERSON] (2021) [PERSON]; [PERSON] Time series anomaly detection for cyber-physical systems via neural system identification and Bayesian filtering.