Dynamic Time Warping and Shape Similarity
A way to measure how similar two time series are in shape even when one is stretched, compressed, or shifted in time relative to the other — useful for finding historical analogues that a simple day-by-day comparison would miss.
Prerequisites: Reframing Forecasting as Supervised Learning
Two crashes both had the shape "slow decline, then a sharp final leg down, then a V-shaped recovery" — but one played out over three weeks and the other over three days. Line the two series up day-for-day and compute a simple point-by-point distance, and they'll look completely different, because day 5 of the fast crash lands in the middle of the slow crash's gentle decline. But to a trader who cares about shape, these are close to the same event, just at different speeds. Dynamic time warping (DTW) is a distance measure built to recognize that.
The idea: let the alignment bend
Ordinary point-by-point comparison forces day 1 to match day 1, day 2 to match day 2, and so on — a rigid, one-to-one alignment. DTW instead finds the best alignment between the two series, allowing points to stretch (one point in series A matched to several consecutive points in series B) or compress (several points in A matched to one point in B), as long as the alignment moves forward in time and never skips backward. It searches over all such valid alignments for the one that minimizes total point-to-point distance, and reports that minimum as the DTW distance.
In plain English: DTW asks "if I'm allowed to speed up or slow down parts of one series' timeline, how closely can I make it match the other series' shape?" — and the answer, the DTW distance, is small when the two series have a genuinely similar shape, even if they unfolded at different speeds or with a time offset.
Worked example: comparing a fast and slow decline
Suppose series A (a fast crash) has values over five days, and series B (a slow crash of the same overall shape) has values over nine days. A naive point-by-point comparison of the first five values of each ( vs , vs , vs , vs , vs ) produces a large distance and misses that the bottom of A (value 3, day 3) and the bottom of B (value 3, days 5–6) are structurally the same event. DTW instead searches for the best matching: it can match A's single day-3 low of 3 to both of B's day-5 and day-6 lows of 3, and similarly stretch the recovery leg of A across several days of B, so the shapes — decline, trough, recovery — align even though B takes almost twice as long to trace the same pattern. The resulting DTW distance is small, correctly flagging the two series as shape-similar despite the different lengths and speeds.
Where this gets used
DTW distances feed directly into -nearest-neighbor classifiers and clustering algorithms for time series — grouping historical crash patterns, volatility regime shapes, or intraday price paths by shape similarity rather than by rigid calendar alignment. It's also used to find historical "analogue days" for a current pattern: searching a database of past sessions for the ones whose DTW distance to today's partial price path is smallest, as a crude but interpretable way to generate scenario analysis.
Generate a few sample paths above and imagine comparing two of them that trace a similar shape but at different speeds — a rigid day-by-day comparison would score them as dissimilar, while DTW would recognize the shared shape after allowing the timelines to bend.
What this means in practice
DTW is a similarity measure, not a forecasting model — it tells you which historical patterns look alike, which is useful for pattern-matching, clustering strategies, and building features (like "distance to nearest historical crash pattern") that a downstream forecasting model can use. Its main practical cost is computation: naive DTW is quadratic in series length, though banded variants that restrict how far the alignment is allowed to bend keep it tractable for large historical libraries.
Dynamic time warping measures similarity between two time series by finding the best non-linear alignment of their timelines — allowing stretching and compression — rather than forcing a rigid day-for-day comparison, so it correctly identifies shape-similar patterns that unfold at different speeds.
The classic mistake is letting DTW's alignment bend without any constraint on how far it can stretch, which can let it match almost any two series by warping aggressively enough to force a resemblance. In practice a bounded warping window (limiting how many steps out of sync the alignment can drift) is used to keep matches meaningful rather than degenerate.
Related concepts
Practice in interviews
Further reading
- Sakoe & Chiba, Dynamic Programming Algorithm Optimization for Spoken Word Recognition
- Berndt & Clifford, Using Dynamic Time Warping to Find Patterns in Time Series