An algorithm for measuring similarity between two temporal sequences, which may vary in speed
In time series analysis, dynamic time warping (DTW) is an algorithm for measuring similarity between two temporal sequences, which may vary in speed. For instance, similarities in walking could be detected using DTW, even if one person was walking faster than the other, or if there were accelerations and decelerations during the course of an observation. DTW has been applied to temporal sequences of video, audio, and graphics data — indeed, any data that can be turned into a one-dimensional sequence can be analyzed with DTW. A well-known application has been automatic speech recognition, to cope with different speaking speeds. Other applications include speaker recognition and online signature recognition. It can also be used in partial shape matching applications.
In general, DTW is a method that calculates an optimal match between two given sequences (e.g. time series) with certain restriction and rules:
We can plot each match between the sequences formula_7 and formula_8 as a path in a formula_9 matrix from formula_10 to formula_11, such that each step is one of formula_12. In this formulation, we see that the number of possible matches is the Delannoy number.
The optimal match is denoted by the match that satisfies all the restrictions and the rules and that has the minimal cost, where the cost is computed as the sum of absolute differences, for each matched pair of indices, between their values.
The sequences are "warped" non-linearly in the time dimension to determine a measure of their similarity independent of certain non-linear variations in the time dimension. This sequence alignment method is often used in time series classification. Although DTW measures a distance-like quantity between two given sequences, it doesn't guarantee the triangle inequality to hold.
In addition to a similarity measure between the two sequences (a so called "warping path" is produced), by warping according to this path the two signals may be aligned in time. The signal with an original set of points "X"(original), "Y"(original) is transformed to "X"(warped), "Y"(warped). This finds applications in genetic sequence and audio synchronisation. In a related technique sequences of varying speed may be averaged using this technique see the average sequence section.
This is conceptually very similar to the Needleman–Wunsch algorithm.
Implementation.
This example illustrates the implementation of the dynamic time warping algorithm when the two sequences s and t are strings of discrete symbols. For two symbols x and y, codice_1 is a distance between the symbols, e.g. codice_1 = formula_13.
 int DTWDistance(s: array [1..n], t: array [1..m]) {
 DTW := array [0..n, 0..m]
 for i := 0 to n
 for j := 0 to m
 DTW[i, j] := infinity
 DTW[0, 0] := 0
 for i := 1 to n
 for j := 1 to m
 cost := d(s[i], t[j])
 DTW[i, j] := cost + minimum(DTW[i-1, j ], // insertion
 DTW[i , j-1], // deletion
 DTW[i-1, j-1]) // match
 return DTW[n, m]
where codice_3 is the distance between codice_4 and codice_5 with the best alignment.
We sometimes want to add a locality constraint. That is, we require that if codice_6 is matched with codice_7, then formula_14 is no larger than w, a window parameter.
We can easily modify the above algorithm to add a locality constraint (differences ;mark ;marked ;/mark ;).
However, the above given modification works only if formula_15 is no larger than w, i.e. the end point is within the window length from diagonal. In order to make the algorithm work, the window parameter w must be adapted so that formula_16 (see the line marked with (*) in the code).
 int DTWDistance(s: array [1..n], t: array [1..m] ;mark ;, w: int ;/mark ;) {
 DTW := array [0..n, 0..m]
 ;mark ;w := max(w, abs(n-m)) ;/mark ; // adapt window size (*)
 for i := 0 to n
 for j:= 0 to m
 DTW[i, j] := infinity
 DTW[0, 0] := 0
 ;mark ;for i := 1 to n ;/mark ;
 ;mark ;for j := max(1, i-w) to min(m, i+w) ;/mark ;
 ;mark ;DTW[i, j] := 0 ;/mark ;
 for i := 1 to n
 for j := ;mark ;max(1, i-w) to min(m, i+w) ;/mark ;
 cost := d(s[i], t[j])
 DTW[i, j] := cost + minimum(DTW[i-1, j ], // insertion
 DTW[i , j-1], // deletion
 DTW[i-1, j-1]) // match
 return DTW[n, m]
Warping properties.
The DTW algorithm produces a discrete matching between existing elements of one series to another. In other words, it does not allow time-scaling of segments within the sequence. Other methods allow continuous warping. For example, Correlation Optimized Warping (COW) divides the sequence into uniform segments that are scaled in time using linear interpolation, to produce the best matching warping. The segment scaling causes potential creation of new elements, by time-scaling segments either down or up, and thus produces a more sensitive warping than DTW's discrete matching of raw elements.
Complexity.
The time complexity of the DTW algorithm is formula_17, where formula_18 and formula_19 are the lengths of the two input sequences. The 50 years old quadratic time bound was broken in 2016: an algorithm due to Gold and Sharir enables computing DTW in formula_20 time and space for two input sequences of length formula_18. 
This algorithm can also be adapted to sequences of different lengths. Despite this improvement, it was shown that a strongly subquadratic running time of the form formula_22 for some formula_23 cannot exist unless the Strong exponential time hypothesis fails.
While the dynamic programming algorithm for DTW requires formula_17 space in a naive implementation, the space consumption can be reduced to formula_25 using Hirschberg's algorithm.
Fast computation.
Fast techniques for computing DTW include PrunedDTW, SparseDTW, FastDTW, and the MultiscaleDTW.
A common task, retrieval of similar time series, can be accelerated by using lower bounds such as LB_Keogh, LB_Improved, or LB_Petitjean. However, the Early Abandon and Pruned DTW algorithm reduces the degree of acceleration that lower bounding provides and sometimes renders it ineffective.
In a survey, Wang et al. reported slightly better results with the LB_Improved lower bound than the LB_Keogh bound, and found that other techniques were inefficient. Subsequent to this survey, the LB_Enhanced bound was developed that is always tighter than LB_Keogh while also being more efficient to compute. LB_Petitjean is the tightest known lower bound that can be computed in linear time.
Average sequence.
Averaging for dynamic time warping is the problem of finding an average sequence for a set of sequences. 
NLAAF is an exact method to average two sequences using DTW.
For more than two sequences, the problem is related to the one of the multiple alignment and requires heuristics.
DBA is currently a reference method to average a set of sequences consistently with DTW.
COMASA efficiently randomizes the search for the average sequence, using DBA as a local optimization process.
Supervised learning.
A nearest-neighbour classifier can achieve state-of-the-art performance when using dynamic time warping as a distance measure.
Amerced Dynamic Time Warping.
Amerced Dynamic Time Warping (ADTW) is a variant of DTW designed to better control DTW's permissiveness in the alignments that it allows. The windows that classical DTW uses to constrain alignments introduce a step function. Any warping of the path is allowed within the window and none beyond it. In contrast, ADTW employs an additive penalty that is incurred each time that the path is warped. Any amount of warping is allowed, but each warping action incurs a direct penalty. ADTW significantly outperforms DTW with windowing when applied as a nearest neighbor classifier on a set of benchmark time series classification tasks.
Alternative approaches.
In functional data analysis, time series are regarded as discretizations of smooth (differentiable) functions of time. By viewing the observed samples at smooth functions, one can utilize continuous mathematics for analyzing data. Smoothness and monotonicity of time warp functions may be obtained for instance by integrating a time-varying radial basis function, thus being a one-dimensional diffeomorphism. Optimal nonlinear time warping functions are computed by minimizing a measure of distance of the set of functions to their warped average. Roughness penalty terms for the warping functions may be added, e.g., by constraining the size of their curvature. The resultant warping functions are smooth, which facilitates further processing. This approach has been successfully applied to analyze patterns and variability of speech movements.
Another related approach are hidden Markov models (HMM) and it has been shown that the Viterbi algorithm used to search for the most likely path through the HMM is equivalent to stochastic DTW.
DTW and related warping methods are typically used as pre- or post-processing steps in data analyses. If the observed sequences contain both random variation in both their values, shape of observed sequences and random temporal misalignment, the warping may overfit to noise leading to biased results. A simultaneous model formulation with random variation in both values (vertical) and time-parametrization (horizontal) is an example of a nonlinear mixed-effects model. In human movement analysis, simultaneous nonlinear mixed-effects modeling has been shown to produce superior results compared to DTW.
Applications.
Spoken-word recognition.
Due to different speaking rates, a non-linear fluctuation occurs in speech pattern versus time axis, which needs to be eliminated. DP matching is a pattern-matching algorithm based on dynamic programming (DP), which uses a time-normalization effect, where the fluctuations in the time axis are modeled using a non-linear time-warping function. Considering any two speech patterns, we can get rid of their timing differences by warping the time axis of one so that the maximal coincidence is attained with the other. Moreover, if the warping function is allowed to take any possible value, distinction can be made between words belonging to different categories. So, to enhance the distinction between words belonging to different categories, restrictions were imposed on the warping function slope.
Correlation power analysis.
Unstable clocks are used to defeat naive power analysis. Several techniques are used to counter this defense, one of which is dynamic time warping.
Finance and econometrics.
Dynamic time warping is used in finance and econometrics to assess the quality of the prediction versus real-world data.
References.
 ;templatestyles src="Reflist/styles.css" / ;