fintech-algorithms
Using a coding agent? Give it the skill: npx skills add IslamBaraka90/Fintech-Algorithms-Library What it does →

Dynamic-Time-Warping Pattern Match

Install and import#

bash
npm install fintech-algorithms
ts
import { match } from "fintech-algorithms/geometric-chart-patterns/pattern-matching/dynamic-time-warping-pattern-match";

Signature#

match(query, candidate, radius, normalize)

Measures how far a query is from a candidate under dynamic time warping, which lets the two series stretch against each other in time instead of comparing them bar for bar. It returns the distance together with the warping path and the full cost matrix that produced it.

Parameters#

NameTypeNotes
querynumber[]The reference series. Needs at least two finite values.
min_length: 2 · finite: true
nulls: reject
candidatenumber[]The series being compared against the query. Needs at least two finite values; it does not have to be the same length.
min_length: 2 · finite: true
nulls: reject
radiusnumberSakoe-Chiba band half-width, in bars. Defaults to the longer of the two series, which leaves the warping path unconstrained. Must be an integer no smaller than the length difference.
optional
normalizebooleanWhether to z-normalize both inputs before warping, which compares shape rather than level. It is passed after the optional radius argument, the Sakoe-Chiba band half-width, which defaults to the length of the longer input and so leaves the path unconstrained.
optional

Returns#

{ distance: number; path_rms: number; path_length: number; path: number[][]; radius: number; normalized: boolean; cost_matrix: Array<Array<number | null>> }

An object holding distance, the square root of the accumulated squared cost, path_rms, the root of that same total divided by the number of aligned pairs, and path_length, that count. path lists the aligned index pairs from start to end, radius and normalized echo what was applied, and cost_matrix gives the accumulated cost per cell with cells outside the band as null.

Errors#

  • When query or candidate holds fewer than two values, or any value is not a finite number — throws Error
  • When radius is not an integer, or is smaller than the difference in length between the two inputs — throws Error
  • When normalization is on and either input is constant — throws Error
  • When the band admits no warping path reaching the final cell — throws Error

Complexity: time O(n * m), space O(n * m).

Worked example#

executed This entry is a thin wrapper its test never calls directly, so it was invoked with the arguments its shared implementation received. Real output, not asserted against a published figure.

Input#

query
[0, 0.5, 1.6, 3.2, 5, 6.1]

Showing 6 of 16 elements.

candidate
[0, 0.2, 0.7, 1.5, 2.7, 4.2]

Showing 6 of 20 elements.

radius
5

Call#

match(query, candidate, radius, normalize)

Returns#

object with 7 fields: distance, path_rms, path_length, path, radius, normalized, cost_matrix

{
  "distance": 0.8828777062798496,
  "path_rms": 0.19265971040078095,
  "path_length": 21,
  "path": [
    [0, 0],
    [1, 1],
    [1, 2]
  ],
  "radius": 5,
  "normalized": true,
  "cost_matrix": [
    [
      0.005392023895877613,
      0.036191498358618325,
      0.22166492791858747,
      0.9254782337120473,
      3.0318664648048,
      7.946270727248203
    ],
    [
      0.03942879310652065,
      0.012185604161087042,
      0.04202654074278329,
      0.3796047955087834,
      1.8038541385002964,
      5.641239716500235
    ],
    [
      0.6048075127728977,
      0.4344897279549777,
      0.16795856695383304,
      0.04221119401162229,
      0.4340784939436583,
      2.370346476931225
    ]
  ]
}

Diagrams#

Dynamic-Time-Warping Pattern Match — algorithm anatomy
Dynamic-Time-Warping Pattern Match — article hero
Dynamic-Time-Warping Pattern Match — calculation ledger
Dynamic-Time-Warping Pattern Match — causal timeline
Dynamic-Time-Warping Pattern Match — failure boundary
Dynamic-Time-Warping Pattern Match — method comparison

Calculation flow#

Reasoning Flow — Dynamic-Time-Warping Pattern Match
flowchart LR
    A["Validate sequence roles and clocks"] --> B["Apply declared local normalization"]
    B --> C["Enumerate eligible search space"]
    C --> D["Calculate distance, path, profile, or gain"]
    D --> E{"Candidate eligible?"}
    E -->|No| R["INELIGIBLE with reason"]
    E -->|Yes| F["Apply exclusion, radius, threshold, and tie rules"]
    F --> G["Return result plus complete diagnostics"]
    G --> H["Evaluate separately from financial usefulness"]

How it works#

This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.

Read the article →

References#

The rest of the Pattern Matching family#