Dynamic Time Warping (DTW)¶
Given two sequences, \(S^{(1)}\) and \(S^{(2)}\), the Dynamic Time Warping (DTW) algorithm finds the best way to align two sequences. During this alignment process, we quantify the misalignment using a distance similar to the Levenshtein distance, where the distance between two series \(S^{(1)}_{1:i}\) (with \(i\) elements) and \(S^{(2)}_{1:j}\) (with \(j\) elements) is^{3}
where \(S^{(1)}_i\) is the \(i\)the element of the series \(S^{(1)}\), \(d(x,y)\) is a predetermined distance, e.g., Euclidean distance. This definition reveals the recursive nature of the DTW distance.
Notations in the Definition: \(S_{1:i}\) and \(S_{i}\)
The notation \(S_{1:i}\) stands for a series that contains the elements starting from the first to the \(i\)th in series \(S\). For example, we have a series
The notation \(S^1_{1:4}\) represents
The notation \(S_i\) indicates the \(i\)th element in \(S\). For example,
If we map these two notations to Python,
 \(S_{1:i}\) is equivalent to
S[0:i]
, and  \(S_i\) is equivalent to
S[i1]
.
Note that the indices in Python look strange. This is also the reason we choose to use subscripts not square brackets in our definition.
Levenshtein Distance
Given two words, e.g., \(w^{a} = \mathrm{cats}\) and \(w^{b} = \mathrm{katz}\). Suppose we can only use three operations: insertions, deletions and substitutions. The Levenshtein distance calculates the number of such operations needed to change from the first word \(w^a\) to the second one \(w^b\) by applying singlecharacter edits. In this example, we need two replacements, i.e., "c" > "k"
and "s" > "z"
.
The Levenshtein distance can be solved using recursive algorithms ^{1}.
DTW is very useful when comparing series with different lengths. For example, most error metrics require the actual time series and predicted series to have the same length. In the case of different lengths, we can perform DTW when calculating these metrics^{2}.
Examples¶
The forecasting package darts provides a demo of DTW.

trekhleb. javascriptalgorithms/src/algorithms/string/levenshteindistance at master · trekhleb/javascriptalgorithms. In: GitHub [Internet]. [cited 27 Jul 2022]. Available: https://github.com/trekhleb/javascriptalgorithms/tree/master/src/algorithms/string/levenshteindistance ↩

Unit8. Metrics — darts documentation. In: Darts [Internet]. [cited 7 Mar 2023]. Available: https://unit8co.github.io/darts/generated_api/darts.metrics.metrics.html?highlight=dtw#darts.metrics.metrics.dtw_metric ↩

Petitjean F, Ketterlin A, Gançarski P. A global averaging method for dynamic time warping, with applications to clustering. Pattern recognition 2011; 44: 678–693. ↩