DYNAMIC TIME WARPING · DTW DISTANCE · ALIGNMENT PATH · LAG PROFILE · PATTERN SEARCH · RUNS IN YOUR BROWSER · NOTHING UPLOADED

How alike are these two series once timing can stretch?

Paste a table with one series per column: two seasons, two sensors, this year's sales curve and last year's. Dynamic time warping (Sakoe & Chiba 1978) lets one series run faster or slower than the other and finds the best way to line them up.

You get the DTW distance and, for every point, a lag: how far the second series runs ahead or behind, read as timing, level or a hop across a peak, so a lag is only called a delay where it is one. Put a short pattern in one column to search a long series for it.

Nothing you enter leaves this browser tab. Statistical output from data you supply, not advice. Not legal, tax, or financial advice.

Your data

A text column, such as a date, names the rows. Blank cells at the ends of a column are dropped; blank cells inside it are filled by straight lines.

Example: spring now comes earlier to Central Park, and autumn stays later

The example is the normal daily high at New York's Central Park for each day of the year, averaged over five 30-year periods (NOAA GHCN-Daily, public domain). Aligning 1871-1900 with 1991-2020, smoothed over ± 15 days with the default band of 37 days, the warp removes 85% of the point-for-point difference. From March through June the recent curve runs ahead: the high that 1871-1900 reached in early April, 1991-2020 reaches 14 days earlier, and in March 23 days earlier. From August through November it runs behind: October's old normal now comes 7 to 8 days later. Midwinter is read as a level difference (no shift explains it) and part of July as a hop across the summer peak, so neither is called a delay. With no smoothing, or ± 7 days, April's lag stays between 14 and 16 days.

This is the station's record as kept, which includes urban growth around the park and changes of instrument and site since the 1870s, not a pure measure of climate change.

Questions

What is dynamic time warping?

It is a way to compare two series that may run at different speeds. Instead of matching the first point with the first, the second with the second and so on, it lets one series stretch or squeeze in time and finds the matching of points, always moving forward, that makes the two as close as possible. The total difference along that best matching is the DTW distance.

How do I read the lag?

For each point of the first series, the lag is how many rows later the second series reaches the matched value. A lag of -14 in April means the second series reaches April's value of the first series 14 rows earlier. Lags are only a delay where the tool reads the gap as timing; where it says level or turn, the lag is not a delay.

What do timing, level and turn mean?

Timing means warping closes at least half of the gap at that point, so the difference there is when, not how much. Level means warping cannot close the gap, so one series is simply higher. Turn means the point sits near a peak or trough, where the same value occurs on both sides, so the warp can hop across and the lag says nothing about delay.

Which window should I use?

A band around the diagonal (Sakoe and Chiba) is the usual choice: it stops the warp from matching points that are implausibly far apart in time, and it is faster. Set it to the largest shift you think is real. If the path runs along the edge of the band, the tool says so, and the band is probably too narrow. The Itakura window limits the slope instead, between one half and two.

Should I z-normalise?

Yes when you care about shape and not level, for example comparing the daily pattern of two shops of different size. No when the level is part of the question, for example when one period is warmer than another and you want to know how many days earlier a temperature now arrives.

How does the pattern search work?

Put the short pattern in one column and the long series in another and choose subsequence mode. The pattern may start and end anywhere in the long series, and may be stretched up to twice its length or squeezed to half. One pass finds the cheapest match ending at every point; the tool then lists the best matches that do not overlap.

Is my data uploaded anywhere?

No. The file is read and aligned by JavaScript in this browser tab. There are no accounts, cookies or analytics vendors.

How it works

The tool fills the table D(i, j) of the cheapest way to align the first i points of A with the first j of B (Sakoe & Chiba 1978). With the symmetric2 pattern, D(i, j) = min(D(i−1, j−1) + 2d, D(i−1, j) + d, D(i, j−1) + d) where d = |ai − bj|, and the distance per step is D(N, M) / (N + M). symmetric1 weights every move by 1 and has no per-step form; asymmetric moves one row of A at a time and divides by N. The band keeps |j − i(M−1)/(N−1)| ≤ w; the Itakura window keeps slopes between 1/2 and 2 (Itakura 1975). Patterns, windows and normalisations follow Giorgino (2009), so results match the dtw package for R and Python.

The lag at point i of A is the mean matched row of B minus the diagonal. It is read as timing when the warp leaves at most half of the gap |ai − bi|, as level when it leaves more, as turn when A barely changes over the nearby rows or changes the opposite way from B at the matched point, and as no real gap when the gap is under 5% of A's standard deviation. The path is pinned to both corners, so lags in the first and last few rows are pulled toward zero.

Pattern search uses open-begin, open-end alignment with the asymmetric pattern (Tormene et al. 2009; Müller 2007): the first row of the table is free, so one pass gives the cheapest match of the whole pattern ending at every point and where it started. Matches are taken in order of cost, skipping any that overlap one already taken.

Checked against dtw-python 1.9.0 on 120 alignments (three step patterns, four windows, two point differences, five shapes), distances, per-step distances and paths all identical, and on six pattern searches; plus brute force over every path on small series. Implemented from the papers; no third-party code was ported.