Inferensys

Glossary

Dynamic Time Warping (DTW)

An algorithm for measuring the similarity between two temporal sequences that may vary in speed, aligning them non-linearly to compare transaction patterns even when they are stretched or compressed in time.
Legal team reviewing EU AI Act compliance documents on laptop in modern office, coffee cups and papers on table, casual meeting.
SEQUENCE ALIGNMENT

What is Dynamic Time Warping (DTW)?

Dynamic Time Warping (DTW) is a non-linear alignment algorithm that measures the similarity between two temporal sequences that may vary in speed or timing, making it essential for comparing transaction patterns that are stretched, compressed, or shifted in time.

Dynamic Time Warping (DTW) is an algorithm that computes an optimal match between two time-dependent sequences by non-linearly warping the time axis. Unlike Euclidean distance, which rigidly compares points at identical timestamps, DTW finds a minimal-cost alignment path through a distance matrix, allowing it to recognize that two spending patterns are fundamentally similar even when one unfolds over days and the other over weeks.

In fraud detection, DTW is applied to compare a user's current transaction sequence against their historical behavioral template or known fraudulent archetypes. By calculating a sequence anomaly score from the warping distance, the system flags deviations in temporal rhythm—such as accelerated purchase velocity or shifted transaction timing—that indicate account takeover or automated bot activity, even when individual transaction amounts appear normal.

CORE MECHANISMS

Key Characteristics of DTW

Dynamic Time Warping is defined by several distinct computational properties that make it uniquely suited for comparing temporal sequences in fraud detection. These characteristics address the challenges of non-linear time distortion and variable-length transaction patterns.

01

Non-Linear Temporal Alignment

Unlike Euclidean distance, which performs a rigid one-to-one comparison, DTW finds an optimal non-linear alignment between two sequences. It computes a warping path that maps indices from one sequence to another, allowing a single point to map to multiple points. This handles local time scaling, where a fraudulent sequence may be a stretched or compressed version of a legitimate pattern, such as a slow-and-low probing attack versus a rapid smash-and-grab.

02

Cost Matrix and Warping Path

The algorithm constructs an N x M cumulative cost matrix, where each cell (i, j) represents the minimal alignment cost between the first i elements of sequence A and the first j elements of sequence B. The optimal warping path is the route through this matrix that minimizes total cost, constrained by:

  • Monotonicity: The path cannot go backward in time.
  • Continuity: The path steps only to adjacent cells.
  • Boundary conditions: The path starts at (1,1) and ends at (N,M).
03

Sakoe-Chiba Band Constraint

To prevent pathological warpings and reduce computational complexity from O(N²) to O(N*W), a Sakoe-Chiba band or Itakura parallelogram global constraint is applied. This restricts the warping path to a diagonal corridor of width W in the cost matrix. In fraud detection, this prevents a single transaction from being aligned with an entire day's worth of history, enforcing a physically plausible maximum time distortion.

04

Distance Measure Agnosticism

DTW is a meta-algorithm that operates on a user-defined local cost function d(i,j). While Euclidean distance is common for scalar time series like transaction amounts, the framework supports:

  • Cosine similarity for high-dimensional feature vectors.
  • Edit distance for sequences of discrete merchant category codes.
  • Learned metrics from siamese neural networks. This flexibility allows DTW to compare raw dollar amounts, behavioral embeddings, or symbolic sequences with equal rigor.
05

Step Pattern and Slope Weighting

The local continuity constraints, or step pattern, dictate the allowed transitions in the warping path. Common patterns include symmetric, asymmetric, and slope-weighted variants. Slope weighting penalizes diagonal steps differently from horizontal or vertical ones, controlling the warping path's stiffness. A higher penalty on horizontal moves prevents a short sequence from being stretched to match a long one, which is critical for comparing a brief fraudulent session against a full day of normal activity.

06

Unsupervised Anomaly Scoring

In a fraud context, DTW is often used as a k-nearest neighbors (k-NN) anomaly detector. A user's recent transaction sequence is compared against a library of their own historical sequences or a cluster of normal peer sequences. The DTW distance to the k-th nearest neighbor becomes the anomaly score. A sudden spike in this score indicates a behavioral shift that does not conform to any previously seen temporal pattern, triggering an alert without requiring labeled fraud examples.

DYNAMIC TIME WARPING

Frequently Asked Questions

Clear, technically precise answers to the most common questions about Dynamic Time Warping and its application in financial fraud detection.

Dynamic Time Warping (DTW) is an algorithm for measuring the similarity between two temporal sequences that may vary in speed or timing. Unlike Euclidean distance, which rigidly compares points at identical time indices, DTW non-linearly warps the time axis to find an optimal alignment. It constructs a cost matrix where each cell (i, j) represents the cumulative distance between the i-th point of sequence A and the j-th point of sequence B, subject to boundary, continuity, and monotonicity constraints. A dynamic programming recursion—typically D(i,j) = d(i,j) + min(D(i-1,j), D(i,j-1), D(i-1,j-1))—finds the minimum-cost warping path. The final DTW distance is the value at D(n,m), quantifying dissimilarity after accounting for temporal distortions. This makes it invaluable for comparing transaction sequences where fraudsters deliberately vary timing to evade rigid pattern matching.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.