# Time Series Clustering of Online Gambling Activities for Addicted Users' Detection
## Appendix A Time Series Analysis
### K-Means for Time Series under DTW  
The goal of clustering is to partition data, in our case time series, into groups based on similarity or distance measures, so that data in the same cluster are similar, while different data points belong to different groups. Formally, the clustering structure is represented as a set of clusters \(C=\{C_{1},\ldots,C_{k}\}\) of the data \(X\), s.t. :  
\[\bigcup_{i=1}^{k}C_{i}=X\wedge C_{i}\cap C_{j}=\emptyset,\ \forall i\
eq j\]  
The clustering methods can be classified according to the type of input data to the algorithm, the clustering criteria defining the similarity (dissimilarity) or distance between data points, and the theory and fundamental concepts. Here, we focus on k-means clustering, which is a partitioning method that uses an iterative way to create the clusters by moving data points from one cluster to another, based on a distance measure, starting from an initial partitioning. This algorithm is one of the most popular clustering algorithms, as it provides a good trade-off between the quality of the solution obtained and its computational complexity. K-means requires that the number of clusters \(k\) will be pre-set by the user. A common approach to determine \(k\) is the elbow method, which follows a heuristic approach. The method consists of plotting the explained variation as a function of the number of clusters and picking the elbow of the curve as the number of clusters to use. This method follows the intuition that increasing the number of clusters will naturally improve the fit and explain more variation since there are more parameters (more clusters) to use, but that at some point, this is over-fitting, and the elbow reflects this. Generally, k-means is a clustering method that aims to find \(k\) centroids, one for each cluster, that minimize the sum of the distance of each data point from its respective cluster centroid. It solves, for \(x_{i}\in X\):  
\[\text{argmin}_{\{C_{1},\ldots,C_{k}\}}\sum_{j=1}^{k}\sum_{x_{i}\in C_{j}}d(x_ {i},c_{j}) \tag{10}\]  
where \(C_{1},\ldots,C_{k}\) are \(k\) non-overlapping clusters, \(c_{j}\) is the representative of cluster \(C_{j}\), and \(d(\cdot)\) is a distance function. In Algorithm 1 is presented the \"classic\" k-means algorithm. The algorithm starts with an initial set of cluster centers, or centroids, \(c_{1},c_{2},\ldots,c_{k}\), chosen at random or according to some heuristic criteria. The clustering algorithm uses an iterative refinement technique, in which, in each iteration, each data point is assigned to its nearest cluster centroid. Then, the cluster centroids are re-calculated. The refinement steps are repeated until the centroids no longer move. The complexity of each iteration, performed on \(N\) data points, is linear: \(\Theta(k\times N)\). This is one of the reasons for its popularity, but other reasons are the simplicity of implementation and speed of convergence. The classical version of the algorithm uses the Euclidean distance as the base metric, which we have already proven to be unsuitable for time series data. In our project, we use a modified version of the algorithm, called time series K-means, which uses DTW as the core metric and DBA to compute the cluster centers (centroids), which leads to better clusters and centroids. ```
Require: \(X=(x_{1},\ldots,x_{N})\) the input data;  Require: \(k\) the number of clusters; \(p\gets 0\);  Randomly choose \(k\) objects as initial centroids \((\{c_{1}^{(0)},c_{2}^{(0)},\ldots,c_{k}^{(0)}\})\) ; repeat  Assign each data point to the cluster with the nearest centroid; \(p\gets p+1\); \(j\gets T\); for\(j=1\) to \(k\)do  Update the centroid \(c_{j}^{(p)}\) of each cluster;  end for until\(c_{j}^{(p)}\approx c_{j}^{(p-1)}\forall j=1,2,\ldots,k\); return\(c_{1},c_{2},\ldots,c_{k}\) ;
```  
**Algorithm A2**k-means clustering. In the algorithm, in the first step, the clusters gather time series of similar shapes, which is due to the ability of DTW to deal with time shifts.