Time Warp Edit Distance

In the data analysis of time series, Time Warp Edit Distance (TWED) is a measure of similarity (or dissimilarity) between pairs of discrete time series, controlling the relative distortion of the time units of the two series using the physical notion of elasticity. In comparison to other distance measures, (e.g. DTW (dynamic time warping) or LCS (longest common subsequence problem)), TWED is a metric. Its computational time complexity is $$O(n^2)$$, but can be drastically reduced in some specific situations by using a corridor to reduce the search space. Its memory space complexity can be reduced to $$O(n)$$. It was first proposed in 2009 by P.-F. Marteau.

Definition
$$ \delta_{\lambda,\nu}(A^p_1,B^q_1) = Min \begin{cases} \delta_{\lambda,\nu}(A^{p-1}_1,B^q_1) + \Gamma(a^{'}_p \to \Lambda) & \rm{delete \ in \ A} \\ \delta_{\lambda,\nu}(A^{p-1}_1,B^{q-1}_1) + \Gamma(a^{'}_p \to b^{'}_q) & \rm{match \ or \ substitution}\\ \delta_{\lambda,\nu}(A^{p}_1,B^{q-1}_1) + \Gamma(\Lambda \to b^{'}_q) & \rm{delete \ in \ B} \end{cases} $$ whereas

$$ \Gamma(\alpha^{'}_p \to \Lambda) = d_{LP}(a^{'}_{p}, a^{'}_{p-1}) + \nu \cdot (t_{a_p} - t_{a_{p-1}}) + \lambda $$

$$ \Gamma(\alpha^{'}_p \to b^{'}_q) = d_{LP}(a^{'}_p, b^{'}_q) + d_{LP}(a^{'}_{p-1}, b^{'}_{q-1}) + \nu \cdot (|t_{a_p} - t_{b_q}| + |t_{a_{p-1}} - t_{b_{q-1}}|) $$

$$ \Gamma(\Lambda \to b^{'}_q) = d_{LP}(b^{'}_{p}, b^{'}_{p-1}) + \nu \cdot (t_{b_q} - t_{b_{q-1}}) + \lambda $$

Whereas the recursion $$ \delta_{\lambda,\nu} $$ is initialized as:

$$ \delta_{\lambda,\nu}(A^0_1,B^0_1) = 0, $$

$$ \delta_{\lambda,\nu}(A^0_1,B^j_1) = \infty\ \rm{for\ }j \ge 1 $$

$$ \delta_{\lambda,\nu}(A^i_1,B^0_1) = \infty\ \rm{for\ }i \ge 1 $$

with $$ a'_0 = b'_0 = 0 $$

Implementations
An implementation of the TWED algorithm in C with a Python wrapper is available at

TWED is also implemented into the Time Series Subsequence Search Python package (TSSEARCH for short) available at.

An R implementation of TWED has been integrated into the TraMineR, a R package for mining, describing and visualizing sequences of states or events, and more generally discrete sequence data.

Additionally, cuTWED is a CUDA- accelerated implementation of TWED which uses an improved algorithm due to G. Wright (2020). This method is linear in memory and massively parallelized. cuTWED is written in CUDA C/C++, comes with Python bindings, and also includes Python bindings for Marteau's reference C implementation.

Python
Backtracking, to find the most cost-efficient path:

MATLAB
Backtracking, to find the most cost-efficient path: