# Time Series Clustering of Online Gambling Activities for Addicted Users' Detection
## Appendix A Time Series Analysis
### Dynamic Time Warping  
One of the leading techniques in finding the best alignment is Dynamic Time Warping (DTW) [15; 16; 17].  
DTW finds the optimal alignment between two time series and captures flexible similarities by aligning the elements inside both sequences. It finds the optimal alignment (or coupling) between two sequences of numerical values and captures flexible similaritiesby aligning the coordinates inside both sequences. It represents a generalization of the Euclidean distance, which allows a non-linear mapping of one time series to another one by minimizing the distance between both. DTW does not require that the two time series data have the same length, and it can handle local time-shifting by duplicating (or re-sampling) the previous element of the time sequence.  
Let \(X=\{x_{i}\}_{i=1}^{N}\) be a set of time series \(x_{i}=(x_{i1},\ldots,x_{it})\) assumed of length \(T\) (this assumption is only for the ease of explanation, since DTW can be applied equally on time series of equal or different lengths). An alignment \(\pi\) of length \(m\) between \(X_{i}\) and \(x_{j}\) is defined as the set of \(m\) couples of aligned elements of \(x_{i}\) to elements of \(x_{j}\), with \(T\leq m\leq 2T-1\), which means:  
\[\pi=((\pi_{1}(1),\pi_{2}(1)),\ldots,(\pi_{1}(m),\pi_{2}(m))) \tag{1}\]  
The alignment \(\pi\) defines a warping function, which creates a mapping from the time axis of \(x_{i}\) into the time axis of \(x_{j}\) and the applications \(\pi_{1}\) and \(\pi_{2}\), defined from \(\{1,\ldots,m\}\) to \(\{1,\ldots,T\}\). The following conditions are satisfied:  
Monotonicity  
\[1=\pi_{1}(1)\leq\pi_{1}(2)\leq\ldots\leq\pi_{1}(m)=T,\ 1=\pi_{2}(1)\leq\pi_{2}(2 )\leq\ldots\leq\pi_{2}(m)=T\]  
Intuitively, monotonicity defines that \(\pi_{1}\) and \(\pi_{2}\) can never \"go back\" in time.  
Boundary  
\[\pi_{1}(l+1)\leq\pi_{1}(l)+1\text{ and }\pi_{2}(l+1)\leq\pi_{2}(l)+1\] \[(\pi_{1}(l+1)-\pi_{1}(l))+(\pi_{2}(l+1)-\pi_{2}(l))\geq 1\ \forall l\in\{1,\ldots,m\}\]  
The first part of the boundary condition intuitively states that moving forward by one in time corresponds to a step of at most one in the \(\pi_{1}\) and \(\pi_{2}\) functions. The second condition states that moving forward by one in time forces at least one between \(\pi_{1}\) and \(\pi_{2}\) to also \"move forward\".  
Thus, an alignment \(\pi\) defines a way to associate all elements of \(x_{i}\) and \(x_{j}\) in which discrete steps in time correspond to either moving forward in \(x_{i}\), \(x_{j}\), or both.  
The DTW between two time series \(x_{i}\) and \(x_{j}\) is defined as:  
\[\text{DTW}(x_{i},x_{j})=\min_{\pi\in\mathcal{A}}\frac{1}{\pi}\sum_{(r_{i}^{ \prime},l)\in\pi}\phi(x_{it},x_{jt}) \tag{2}\]  
where \(\phi:\mathbb{R}\times\mathbb{R}\rightarrow\mathbb{R}^{+}\) is a positive, real-valued, divergence function, usually the Euclidean norm, and \(\mathcal{A}\) is the set of all possible alignments between \(x_{i}\) and \(x_{j}\).  
DTW is a dissimilarity measure on time series that makes it possible to capture temporal distortions, since its alignments deal with delays or time-shifting, whereas Euclidean alignments align elements observed at the same time. Boundary, monotonicity, and continuity are three important properties that are applied to the construction of the path. The boundary condition imposes that the first elements of the two time series are aligned to each other, as well as the last sequence's elements. Monotonicity preserves the time-ordering of elements, meaning that the alignment path does not go back in the \"time\" index. Continuity limits the warping path from long jumps, and it guarantees that alignment does not omit important features.  
It is also worth mentioning that DTW does not follow the triangle inequality; thus, it is a non-metric distance:  
\[\text{DTW}(x_{i},x_{j})+\text{DTW}(x_{j},x_{k})\
geq\text{DTW}(x_{i},x_{k}) \tag{3}\]As an example of the effect of DTW, Figure A1 shows the optimal alignment path with and without considering the time warping.