Fast Exact Dynamic Time Warping on Run-Length Encoded Time Series

Vincent Froese, Brijnesh J. Jain, Maciej Rymar, Mathias WellerAlgorithmica 2023

TL;DR

This paper introduces the first exact algorithm for computing Dynamic Time Warping (DTW) on run-length encoded time series. This new approach significantly speeds up DTW calculations for time series with long constant segments, with its runtime depending only on the compressed representation's length, not the original series' length.

What Problem Does This Paper Address?

Dynamic Time Warping (DTW) is a crucial tool for measuring similarity between time series, widely used across scientific domains. However, its standard dynamic programming solution has a quadratic time complexity ($O(n^2)$), making it prohibitively slow for the very long time series common in real-world applications (e.g., sensor data, financial records). While many heuristic and approximate methods exist, this paper focuses on the need for exact DTW computation that can still be efficient. The challenge is to accelerate DTW without sacrificing accuracy, especially for time series that naturally exhibit long periods of constant values (e.g., sensor readings that stay stable for extended durations).

Methods

The core innovation is an algorithm that leverages run-length encoding (RLE) of time series. Instead of processing each individual data point, the algorithm operates on blocks of consecutive identical values. The key idea is to analyze the structure of optimal warping paths within this RLE representation.

The algorithm works by:

  • Representing Time Series with Runs: Time series are compressed into sequences of (value, run-length) pairs. For example, a series [1, 1, 1, 2, 2, 3] becomes [(1, 3), (2, 2), (3, 1)].
  • Block-Based Dynamic Programming: The DTW matrix is conceptually divided into blocks, where each block corresponds to a pair of runs from the two RLE time series. The algorithm computes DTW costs by considering only specific "critical" points on the boundaries of these blocks and along "block diagonals."
  • Diagonal-Conformity: A crucial lemma proves that there always exists an optimal warping path that is "diagonal-conform," meaning it primarily moves along block diagonals and block boundaries. This property significantly prunes the search space.
  • Efficient Intersection Computation: The algorithm efficiently computes the costs of optimal paths to these critical intersections using a dynamic programming approach that iterates through blocks and maintains a sorted list of diagonals. The appendentry and trace functions are central to this process.

The worst-case running time is cubic in the coding lengths of the inputs ($O(k^3)$ where $k$ is the maximum coding length), which is a significant improvement over $O(n^2)$ when the coding length is much smaller than the original series length.

Datasets

The experiments were conducted using seven datasets from the UCR repository: HandOutlines, InlineSkate, CinCECGtorso, Haptics, Mallat, Phoneme, and StarLight Curves. These datasets were chosen because they contain time series with lengths of at least 1000. For the experiments, time series were compressed to various run-length encoded forms using a best piecewise constant approximation with a specified number of runs, controlled by a "space-saving ratio."

Key Insights and Ideas

  • Exploiting Structure is Key: The paper demonstrates that by understanding the structure of optimal warping paths in the context of run-length encoded data, we can avoid computing the entire $O(n^2)$ DTW matrix. The concept of "diagonal-conformity" is a powerful insight.
  • RLE is a Natural Fit for DTW: Time series with long constant segments are common in real-world data. RLE is a natural compression for such data, and this algorithm directly exploits that structure for exact DTW.
  • Trade-off Between Compression and Computation: The algorithm's efficiency is directly tied to the degree of compression achieved by RLE. Higher compression (more runs) leads to faster computation, but the overhead of managing intersections can make it slower than standard DTW for very low compression ratios.
  • BDTW as a Heuristic: The paper highlights that existing algorithms like BDTW (Blocked DTW) are heuristics that provide bounds but are not always exact. This work provides an exact alternative that can be faster than BDTW when the RLE is efficient.

Key Results

  • Exact DTW on RLE: The paper presents the first exact algorithm for DTW on run-length encoded time series.
  • Runtime Dependency: The algorithm's running time is $O(\kappa)$, where $\kappa$ is the number of intersections between block boundaries and block diagonals. In the worst case, this is $O(k^2l + kl^2)$, where $k$ and $l$ are the coding lengths of the two time series. This is significantly faster than $O(n^2)$ when $k, l \ll n$.
  • Experimental Speedups: Experiments show that the proposed RLEDTW algorithm achieves substantial speedups (up to 100x faster than standard DTW) for time series with high space-saving ratios (i.e., short run-length encodings).
  • Comparison to Alternatives:
    • AWarp and SDTW were found to be slower than standard DTW, likely due to their design for specific cases (e.g., runs of zeros).
  • BDTW (a heuristic) showed significant speedups but at the cost of accuracy, with error percentages increasing with space-saving ratio.
  • RLEDTW was the fastest exact algorithm for high space-saving ratios, outperforming even BDTW in these scenarios.

Limitations and Future Work

  • Overhead for Low Compression: The paper acknowledges that for very low space-saving ratios (i.e., many runs), the overhead of computing intersections can make RLEDTW slower than standard DTW. The authors suggest that a simple $O(kn)$ dynamic program might be faster in such cases.
  • Theoretical Questions: An open question is whether an $O(\max(k, l)^{3-\epsilon})$-time algorithm exists or can be excluded assuming the Strong Exponential Time Hypothesis (SETH).
  • Other Compressions: The authors suggest exploring DTW with respect to other compression techniques, similar to work done for other string problems.

Summary updated

Abstract

Dynamic Time Warping (DTW) is a well-known similarity measure for time series. The standard dynamic programming approach to compute the DTW distance of two length-n time series, however, requires $$O(n^2)$$ O ( n 2 ) time, which is often too slow for real-world applications. Therefore, many heuristics have been proposed to speed up the DTW computation. These are often based on lower bounding techniques, approximating the DTW distance, or considering special input data such as binary or piecewise constant time series. In this paper, we present a first exact algorithm to compute the DTW distance of two run-length encoded time series whose running time only depends on the encoding lengths of the inputs. The worst-case running time is cubic in the encoding length. In experiments we show that our algorithm is indeed fast for time series with short encoding lengths.
BibTeX
@inproceedings{froese2023fast,
  author = {Vincent Froese and Brijnesh J. Jain and Maciej Rymar and Mathias Weller},
  title = {{Fast Exact Dynamic Time Warping on Run-Length Encoded Time Series}},
  booktitle = {Algorithmica},
  year = {2023},
  url = {https://openreview.net/forum?id=U1eU6jtOK1},
}
Figure