# Time Series Clustering of Online Gambling Activities for Addicted Users' Detection
## Appendix A Time Series Analysis
### DTW Barycenter Averaging  
DBA is a global averaging method that consists of iteratively refining an initially average sequence (potentially arbitrary), in order to minimize its distance to the set of time series, with the aim of minimizing inertia as the sum of squared DTW distances from the average sequence to the set of time series. More deeply, for each iteration, DBA works in two steps:  
1. Computing DTW between each individual sequence and the temporary average sequence to be refined, in order to find associations between coordinates of the average sequence and coordinates of the set of sequences. 2. Updating each coordinate of the average sequence as the barycenter of coordinates associated with it during the first step. Let \(\mathbb{S}\) now be the set of sequences to be averaged, \(c=(c_{1},\ldots,c_{T})\) the the average sequence at iteration \(i\), and \(c^{\prime}=(c^{\prime}_{1},\ldots,c^{\prime}_{T})\) the update of \(c\) at iteration \(i+1\), for which we want to find the coordinates. The \(t^{th}\) coordinate of the average sequence \(c^{\prime}\) is defined as:  
\[c^{\prime}_{t}=\text{\rm barycenter}(\text{\rm assoc}(c_{t}))\]  
where the function \(\text{\rm assoc}(\cdot)\) links each coordinate of the average sequence to one or more coordinates of the sequences of \(\mathbb{S}\), and it is computed during DTW computation between \(c\) and each sequence of \(\mathbb{S}\). The \(\text{\rm barycenter}(\cdot)\) function is defined as:  
\[\text{\rm barycenter}(X_{1},\ldots,X_{\alpha})=\frac{X_{1}+\ldots+X_{\alpha} }{\alpha}\]  
Then, by computing again DTW between the average sequence and all sequences of \(\mathbb{S}\), the associations created by DTW may change. As it is impossible to anticipate how these associations will change, the algorithm makes \(c\) iteratively converge. Algorithm A1 details the complete DBA procedure. ```
Require: \(c=(c_{1},\ldots,c_{T^{\prime}})\) the initial average sequence;  Require: \(s_{1}=(s_{1},\ldots,s_{1T})\) the first sequence to average;...;  Require: \(s_{n}=(s_{1},\ldots,s_{nT})\) the n-th sequence to average;  Let \(T\) be the length of sequences;  Let \(\text{\rm assocTab}\) be a table of size \(T^{\prime}\) containing in each cell a set of coordinates  associated with each coordinate of \(c\);  Let \(m[T,T]\) be the temporary DTW (cost,path) matrix; \(\text{\rm assocTab}\leftarrow[0,\ldots,0]\); forseq in \(\mathbb{S}\)do \(m\gets\text{DTW}(c,seq)\); \(i\gets T^{\prime}\); \(j\gets T\); while\(i\geq 1 andj\geq 0\)do \(\text{\rm assocTab}[i]\leftarrow\text{\rm assocTab}[i]\cup seq_{j}\) ; \((i,j)\gets second(m[i,j])\)  end for  end for for\(i=1\) to \(T\)do \(c^{\prime}_{i}=\text{\rm barycenter}(\text{\rm assocTab}[i])\) ;  end for return\(c^{\prime}\) ;
```  
**Algorithm A1**DBA. In the algorithm initialization, there are two major factors to consider: the length of the starting average sequence and the values of its coordinates. The upper bound of the initial average is \(T^{N}\), but such a length cannot reasonably be used. However, the inherent redundancy of the data suggests that a much shorter sequence can be adequate. In practice, a length around \(T\) (the length of the sequences to average) performs well. As concerns the optimal values of the initial coordinates, they are theoretically impossible to determine. In methods that require an initialization, like K-means clustering, a large number of heuristics have been developed, like randomized choice or using an element of the set of sequences to average. DBA guarantees convergence: at each iteration, inertia can only decrease, since the new average sequence is closer (under DTW) to the elements it averages. If the update does not modify the alignment of the sequences, barycenters composing the average sequence will get closer to the coordinates of \(\mathbb{S}\). In the other case, if the alignment is modified, it means that DTW calculates a better alignment with a smaller inertia, which decreases in that case also.