Tutorial: Dynamic Time Warping With KDB X

Tutorial: Dynamic time warping (DTW) with KDB-X

作者

Ryan Siegler

Data Scientist

ポイント

  1. Dynamic Time Warping (DTW) flexes the time axis to identify similarities, even when events occur at different times or speeds.
  2. Powerful sequences with tunable parameters help detect anomalies, align signals, and uncover hidden patterns.
  3. DTW is widely applicable across industries including Finance, Manufacturing, Aerospace and Defense.

If you’ve ever tried to compare two pieces of time series data, you’ll know it’s not always as simple as checking if the lines overlap. Markets shift, sensors drift, and heart rates spike at different times. However, with Dynamic time warping (DTW), analysts can now identify similar shapes in sequences, even when they’re stretched or misaligned.

In this article, we’ll get hands-on with DTW using the KDB-X AI libs module. We’ll analyze NYSE stock data for our example and also discuss how it can be applied in other industries.

Dynamic Time Warping

Why DTW?

Traditional similarity metrics, such as Euclidean distance, assume that sequences line up neatly in time, but that’s rarely the case:

  • A stock rally for one ticker might happen hours before another
  • A turbine sensor may vibrate just before a fault, while a “normal” unit indicates the same pattern with a delay
  • An ECG trace could show the same heartbeat pattern at slightly different speeds

DTW flexes the time axis to find alignments, measuring similarity based on shape rather than rigid time steps.

Getting started

The KDB-X AI libs module provides DTW with simple functions, such as .ai.dtw.search, .ai.dtw.searchRange, and .ai.dtw.filterSearch. In this tutorial, we will run a small subset, but you can also explore the entire notebook via our GitHub tutorial.

Load the AI libs module:

q
.ai:use`kx.ai

Let’s explore our dataset:

q
first trade
msgType | 220 
sequenceNo| 58765 
time | 0D07:00:00.105862814 
sym | `TMF 
tradeId | 24476 
price | 4.74 
volume | 4000

We can see a single trade message for the symbol TMF executed at $4.74 for 4,000 shares at 7:00:00.

Next, we will create a query vector that will define the pattern we wish to match within our NYSE dataset, specifically in the price column.

q
vector:10*abs sums neg[0.5]+25?1f

In the above:

  • 25?1f: Generates 25 random float numbers between 0 and 1
  • neg[0.5]: Returns -0.5
  • sums: Computes the cumulative sum
  • abs: Tables the absolute value
  • Finally, we multiply by 10*

The result is a random pattern that fluctuates around zero, mimicking price fluctuations we may want to detect in our time-series data.

DTW search

We run our first query using .ai.dtw.search to perform a DTW search on the price column of the trade table.

The .ai.dtw.search function compares the query pattern against the time series data, identifying the top five most similar subsequences based on DTW distance. The fourth parameter, 0.1, represents the window or the ratio of the query size allowed during warping. The larger the window, the more flexible the search becomes, but also the more time-consuming.

q
`distances`indexes!.ai.dtw.search[;vector;5;0.1;::] trade`price
distances| 1.519624 1.766677 1.773423 1.78443 1.794284
indexes  | 3802228  24797    2314123  2746995 4067262

Let’s set the window to a higher value of  0.8, to get greater time warping into our search.

q
`distances`indexes!.ai.dtw.search[;vector;5;0.8;::] trade`price
distances| 1.265552 1.354349 1.446201 1.468039 1.515346
indexes  | 2303698  632384   632383   2303699  3388567 

Tuning parameters

The DTW search functions .ai.dtw.searchRange and ai.dtw.filterSearch allows for some additional fine-tuning:

  • k: The maximum number of best matches to return (ai.dtw.filterSearch, ai.dtw.Search)
  • cutoff: Threshold similarity, reducing weak matches
  • returnMatches: Lets you decide whether to bring back the aligned subsequences for inspection.

The DTW searchRange query provides additional flexibility in setting a maximum distance. In this example, we set it to 1.6, ensuring the search will only return matches within those parameters.

q
`distances`indexes!.ai.dtw.searchRange[;vector;0.2;1.6;::] trade`price
distances| 1.265552 1.468039 1.507729 1.519624 1.520065 1.592965
indexes  | 2303698  2303699  632384   3802228  3388567  2620562

The DTW filteredSearch query sets a maximum results cap. Here we limit the results to 3, and ensure returned results are <1.6.

q
`distances`indexes!.ai.dtw.filterSearch[;vector;3;0.2;1.6;::] trade`price
distances| 1.265552 1.468039 1.507729
indexes  | 2303698  2303699  632384

Beyond Wall Street

While this tutorial relies on NYSE data, DTW is a universal pattern matcher that can be applied across various industries.

For example:

  • Manufacturing: Detect anomalies in machine vibration before failure
  • Aerospace & Defense: Identify repeat signal patterns in radar/sonar streams or match flight telemetry against known mission profiles
  • Healthcare: Compare patient ECG traces or gait cycles over time
  • IoT: Spot recurring usage patterns in smart meter data
  • Speech recognition: DTW has long been used to align spoken words against templates.

Dynamic time warping fills a gap that traditional similarity measures leave behind. By stretching and compressing the time axis, it surfaces meaningful patterns hidden in noisy, misaligned data.

If you enjoyed this blog and would like to explore other examples, you can visit our GitHub repository. You can also begin your journey with KDB-X by signing up for the KDB-X Community Edition Public Preview.

Customer Stories

Discover richer, actionable insights for faster, better informed decision making

資本市場

AxiはKXを使用して、ストリーミング・データをリアルタイムかつ大規模に取り込み、分析し、可視化しています。

詳細を読む 概要 Axi
資本市場

ADSSはKXリアルタイムデータプラットフォームを活用し、変革的成長戦略を加速させます。

詳細を読む 概要 ADSS
資本市場

10年以上にわたってKXの顧客である同社は、KXのチームとリアルタイムデータベースを信頼し、技術的な観点からクラウドへの移行を容易にできる確信がありました。

詳細を読む 概要 日本の投資銀行


AIによるイノベーションを加速する、KXのデモをお客様に合わせてご提供します。

当社のチームが以下の実現をサポートします:

  • ストリーミング、リアルタイム、および過去データに最適化された設計
  • エンタープライズ向けのスケーラビリティ、耐障害性、統合性、そして高度な分析機能
  • 幅広い開発言語との統合に対応する充実したツール群

専門担当者によるデモをリクエスト

*」は必須フィールドを示します

このフィールドは入力チェック用です。変更しないでください。

本フォームを送信いただくと、KXの製品・サービス、お知らせ、イベントに関する営業・マーケティング情報をお受け取りいただけます。プライバシーポリシーからお手続きいただくことで購読解除も可能です。当社の個人情報の収集・使用に関する詳しい情報については、プライバシーポリシーをご覧ください。

タイムシリーズ分野におけるG2認定リーダー

// social // social