# GAN-Based Generation of Synthetic Data for Vehicle Driving Events
### Theoretical Background
#### 1.1.4 Real-World Time Series GAN (RTSGAN)  
RTSGAN is a framework proposed by [PERSON] [14] that allows for the generation of multidimensional time series data. The RTSGAN architecture has two main components: (1) an encoder-decoder module, and (2) a generator module. Their schematics can be seen in Figure 4. The encoder-decoder (autoencoder) module learns to encode each time series instance into a fixed-dimensional latent vector, from which the time series is reconstructed. The generator module uses a _Wasserstein GAN_ (WGAN) to produce vectors in the same latent space as the autoencoder. The time series are generated by feeding latent synthetic vectors produced in the generator into the decoder.  
The RTSGAN encoder encodes each data series into a compact invariant representation of the sequence length. First, the global \(y\) features are concatenated with the dynamic features \(x\) in each timestep as \(\epsilon_{i}=\left[x_{i},y\right]\). These are fed into an \(N\)-layer GRU network with \(d_{AE}\) hidden dimensions to obtain the states \(h_{i}^{N}\) at each timestep \(i\) at each GRU layer \(n\), as follows:  
\[h_{t}^{n}=\text{GRU}(\epsilon_{i}),\ i\in[1,l],\ n\in[1,N]. \tag{4}\]  
To better capture the temporal dynamics and global properties, pooling operations are applied to the hidden states of the last layer of the GRU network:  
\[\text{s}=\text{FC}(\left[\text{AvgPool}(h_{i}^{N}),\ \text{MaxPool}(h_{i}^{N}),\ h_{l}^{N} \right]) \tag{5}\]  
where FC stands for fully connected layer. At the end, the global information s is concatenated with the last hidden state to obtain a latent representation \(\text{r}\in\mathbb{R}^{(n+1)d_{AE}}\) for the time series, as follows:  
\[\text{r}=\left[\text{s},\{h_{l}^{n}\}_{n=1}^{N}\right]. \tag{6}\]  
The RTSGAN decoder attempts to reconstruct the time series from the latent representation \(\text{r}\) in two steps: (1) reconstruction of the global features \(\hat{\text{y}}\), and (2) reconstruction of the dynamic features \(\text{h}_{l}^{n}\). The global features \(\hat{\text{y}}\) are reconstructed with  
\[\hat{\text{y}}=\text{Act}(\text{W}_{y}\text{s}+\text{b}_{y}), \tag{7}\]  
where Act represents a _softmax_ function for categorical features and a _sigmoid_ function for continuous features.  
The dynamic feature decoder is another \(N\)-layer GRU network with hidden dimension \(d_{AE}\); it takes \(\text{h}_{l}^{n}\) as the initial state \(\hat{\text{h}}_{o}^{n}\). The reconstruction process follows the scheme  
\[\hat{\text{e}}_{i}=\left[\text{x}_{i-1},\ \text{s}\right], \tag{8}\]  
\[\hat{\text{h}}_{i}^{n}=\text{GRU}(\hat{\text{e}}_{i},\ \hat{\text{h}}_{i-1}^{n}),\ n \in\left[1,\ N\right], \tag{9}\]  
\[\hat{\text{x}}_{i}=\text{Act}(\text{W}_{x}\hat{\text{h}}_{i}^{N}+\text{b}_{x }). \tag{10}\]  
Figure 4: RTSGAN framework.  
The RTSGAN generator uses a version of WGAN. With the help of a 1-Lipschitz discriminator, the generator tries to minimize the 1-Wasserstein distance \(W(Pr,Pg)\) between the real data distribution and the synthetic data distribution:  
\[\min_{G}\max_{D}E_{\tau\sim encoder(X,y)}\big{[}D(r)\big{]}-E_{z\sim p(z)}\big{[} D(G(z))\big{]} \tag{11}\]  
where \(G\) and \(D\) represent the generator and the 1-Lipschitz discriminator, respectively. After training the WGAN, time series are generated as follows:  
\[\hat{X},\hat{y}=\text{decoder}(G(z)),\ z\sim p(z). \tag{12}\]  
The code used in this research for data generation with RTSGAN is available at [[https://github.com/acphile/RTSGAN](https://github.com/acphile/RTSGAN)]([https://github.com/acphile/RTSGAN](https://github.com/acphile/RTSGAN)) (accessed on 3 October 2024).