- Introduced by
- Hideki Sakoe and Seibi Chiba
- Year
- 1978
- Type
- Elastic sequence alignment metric
- Family
- Time-series distance
What does it do?
Dynamic Time Warping (DTW) is a dissimilarity measure for comparing time series or other sequences that may differ in length or in the speed at which similar patterns unfold.
Hideki Sakoe and Seibi Chiba introduced it in 1978 for spoken word recognition, where it computes the minimum cumulative cost of aligning two sequences using dynamic programming. Because the alignment can stretch or compress the time axis, DTW suits sequences with similar shape but different timing; it does not correct differences in amplitude scale.
The intuition behind the method
Consider two signals that trace the same shape, one faster than the other. Point-by-point measures such as Euclidean distance compare values at the same time index, so a timing offset makes the signals look dissimilar even when their shapes match. DTW instead searches for a monotonic alignment that pairs each element of one sequence with one or more elements of the other. The distances along the cheapest such alignment are summed, and that total is the DTW value. The local distance between paired elements is chosen by the user; absolute or squared difference are common, and some treatments use the number of nonmatching pairs as the local cost.
History
Sakoe and Chiba's 1978 paper gave a time-normalisation algorithm for spoken word recognition, based on dynamic programming and a time-warping function. It defined symmetric and asymmetric forms of the time-normalised distance and found the symmetric form preferable, and it introduced the slope constraint to improve discrimination between word categories. Later work, including the FastDTW method of Salvador and Chan, sought faster approximations of DTW for longer sequences.
When to use it
Use DTW when two sequences describe the same kind of pattern but may differ in duration or speed, as in speech utterances, gestures or sensor recordings, and when overall shape matters more than exact timing. It is also suitable when the sequences have different lengths. Do not use it when the sequences must be compared at fixed time indices, because lock-step measures such as Euclidean distance are then the natural choice. For long sequences or large collections, its quadratic cost in sequence length makes it expensive, and constrained or approximate variants such as FastDTW, or lower-bounding methods, should be considered.
Limitations and considerations
- The dynamic program fills an n-by-m table, so time and memory both scale with the product of the two sequence lengths.
- The DTW value does not satisfy the triangle inequality, so it is not a metric in the strict mathematical sense, and metric-based indexing methods do not apply directly.
- Because each point may be matched to several points of the other sequence, outliers and noisy samples can pull the alignment and inflate the distance.
- Results depend on the choice of local distance, any window or slope constraint, and whether the sequences were normalised, so these settings must be reported and kept fixed when comparing results.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Accepts sequences of different lengths without resampling, because the alignment itself absorbs the length difference.
- Matches sequences whose shapes agree but whose timing differs, which lock-step distances treat as dissimilar.
- Yields an alignment path as well as a distance, so the correspondence between elements can be inspected.
- Has a long record of use in time-series classification and clustering, so it is well documented in the literature.
Applications
- Speech recognition, where utterances of the same word differ in duration and speaking rate.
- Motion and gesture analysis, comparing movement sequences performed at different speeds.
- Sensor data clustering and classification, where recordings of the same activity may vary in timing.
How it works
How it works
Build a matrix with one row per element of the first sequence and one column per element of the second. Cell (i, j) is set to the local distance between element i of the first sequence and element j of the second, and it will later hold the cost of the best alignment that ends at that pair.
Fill the matrix in order, so that each cell's accumulated cost is its local distance plus the smallest accumulated cost among its permitted predecessor cells. Which predecessors are permitted is set by the step pattern and by any global constraint, such as a window band around the diagonal or the slope constraint introduced by Sakoe and Chiba.
Start at the final cell and move back to the origin, at each step following the predecessor that produced the minimum. The recovered path is the optimal alignment, and the accumulated cost at the final cell is the unnormalised DTW distance. Because this total grows with path length, values for sequences of different lengths should be compared with care. Users should state any normalisation they apply, such as dividing by path length or by sequence length, and keep it fixed across comparisons. No universal cut-off exists for DTW values, so decisions should rest on relative ranking or on validation with labelled data.
Common pitfalls
- Assuming unconstrained warping only matches similar sequences: with no window or slope limit, the alignment can map many elements of one sequence to a single element of the other, which can make dissimilar sequences look close.
- Comparing raw sequences with different amplitude scales: DTW warps the time axis, not the values, so offsets or scale differences must be removed beforehand.
- Treating the DTW value as a metric when using distance-based search or clustering methods that rely on the triangle inequality, which DTW does not guarantee.
- Applying unconstrained DTW to long sequences or large collections without estimating the cost first, since the quadratic table size can make the computation impractical.
Frequently asked questions
Why is DTW better than Euclidean distance for time series?
Euclidean distance compares values at the same time index, so a timing offset makes similar shapes look different. DTW allows a flexible alignment that pairs elements with matching features even when they occur at different times. It is therefore preferable when shape similarity matters more than synchronised timing. It is not preferable when the time index itself carries meaning.
Is DTW symmetric?
With the symmetric form of the time-normalised distance described by Sakoe and Chiba, the value does not depend on the order of the two sequences. The asymmetric form is defined differently and may not share this property, so the form used should be stated. In either case, DTW does not satisfy the triangle inequality and is therefore not a metric in the strict sense.
How do I handle sequences of very different lengths?
DTW accepts sequences of different lengths directly. However, when lengths differ greatly, an unconstrained alignment can map many elements to one, which distorts the result. A global window constraint, such as a band around the diagonal, restricts how far the alignment may deviate from the diagonal and limits this distortion.
What is FastDTW and when should I use it?
FastDTW, described by Salvador and Chan, is an approximation to DTW that its authors aim to keep accurate while running in time and space that scale linearly with sequence length. Because it is an approximation, its values may differ from exact DTW, so it should be validated against exact DTW on a sample. It is worth considering when many sequences or very long sequences make exact DTW impractical.
Related methods
Sources
Sakoe, H., & Chiba, S. (1978). Dynamic programming algorithm optimization for spoken word recognition. IEEE Transactions on Acoustics, Speech, and Signal Processing, 26(1), 43-49.
Open DOI referenceSalvador, S., & Chan, P. (2007). FastDTW: Toward accurate dynamic time warping in linear time and space. KDD Explorations, 5(1), 70-86.
Open publication reference2015_Book_DataMining, Sect. 15.3 (page not recorded in library)