Trading Dev AcademyFree quant education

Free lesson · Charts & patterns

Quantitative recognition II: shapelets and constrained dynamic time warping

Open interactive lessonPractice calculationsExplore labs

Start with the idea

Aligned distance compares the same positions. Dynamic time warping can pair nearby positions when a shape unfolds at a slightly different speed.

Symbols, units & horizon
  • x_i,s_j: scaled observation and template values at positions i,j
  • D_ij: minimum accumulated squared mismatch through that cell
  • n,m: observation and template lengths
  • b: nonnegative integer alignment band
  • d: square root of total path cost
  • D_00: zero starting cost
  • other boundary cells: positive infinity, meaning unreachable
  • all sequence values: dimensionless in the example

When and why to use this

Use constrained shape matching to retrieve prior motifs or build interpretable classifier features. Treat the distance as a measurement, then test its relation to subsequent net payoffs.

  • Template: an example sequence. Shapelet: a short sequence used as a discriminative feature. Its distance can become one column in a classifier.
  • First use a common documented scale. A raw-price distance mostly measures price level; aggressive per-window normalization can discard economically useful amplitude.
  • DTW fills a grid. Each cell adds its local mismatch to the cheapest reachable predecessor: diagonal, up or left.
  • Limit timing distortion with a fixed band. A band of zero on equal-length inputs permits only aligned pairs. Also consider run-length or slope restrictions.
  • For a shapelet bank, slide each template across the observed window and retain its minimum permitted distance. Record the matching start/end times and template length.
  • Discover templates, clusters, normalization and distance thresholds inside the training fold. A prototype selected using a future price continuation leaks its outcome unless the entire example belongs to training.
  • A small DTW distance may pair economically different movements. Inspect the alignment path, amplitude, duration and surrounding volatility.
  • Keep length comparable or document a score normalization. This lesson uses square root of total squared path cost; it is not aligned RMSE or an average-cost-optimal DTW objective.
Dij=(xi−sj)2+min⁡(Di−1,j−1,Di−1,j,Di,j−1),d=Dnm,|i−j|≤b
Model assumptions, derivation and arithmetic

Quantitative recognition II: shapelets and constrained dynamic time warping

  1. Initialize D00=0 and all other boundary entries to infinity. Exclude cells outside the band.
  2. For x=[0,1,1], s=[0,0,1], band=1: a path pairs positions (1,1),(1,2),(2,3),(3,3).
  3. Every paired value agrees, so each squared cost is zero. Total cost and its square root are zero.
  4. Aligned comparison instead has one mismatch of 1. Its RMSE is √(1/3), about .57735. A zero warped distance does not mean identical sequences.
Work it by hand

At a cell with local mismatch squared=.25 and predecessor costs [.4,.1,.3], the update is .25+.1=.35. The band limits which such cells can be used.

Use the rule

  • Start with aligned distance and a fixed template bank.
  • Tune a small set of bands and lengths using chronological validation.
  • Inspect false matches and test whether timing-flexible matching adds value over the aligned baseline.

Before moving on

Draw a tiny cost grid, recover its path and audit the timestamps of every template.

Python implementation

Self-contained teaching example. Python 3.10+; dependencies and input conventions are shown in the code and notation. Run in your own Python environment.

from math import sqrt, inf

def constrained_dtw(x,s,band=1):
    if not x or not s or not isinstance(band,int) or band<0: raise ValueError("Nonempty sequences and integer band required")
    n,m=len(x),len(s)
    d=[[inf]*(m+1) for _ in range(n+1)]; d[0][0]=0
    for i in range(1,n+1):
        for j in range(max(1,i-band),min(m,i+band)+1):
            d[i][j]=(x[i-1]-s[j-1])**2+min(d[i-1][j-1],d[i-1][j],d[i][j-1])
    return sqrt(d[n][m])

def shapelet_feature(observed,template,band=1):
    m=len(template)
    if m==0 or len(observed)<m: raise ValueError("Template must fit the observed window")
    return min(constrained_dtw(observed[k:k+m],template,band) for k in range(len(observed)-m+1))

print(constrained_dtw([0,1,1],[0,0,1],1))

Continue learning

Candles, Structures & Pattern Research — all lessons
  1. First principles: what a candle actually records
  2. Doji, hammer, shooting star and long-body bars
  3. Engulfing, inside bars and multi-candle sequences
  4. Trends, ranges, breakouts and chart structures
  5. Indicators as arithmetic: ATR, moving averages, RSI and bands
  6. Known strategy families: from chart idea to complete rules
  7. Pattern recognition: rules, features, shapelets and image models
  8. Quantitative recognition I: build a causal candle feature table
  9. Quantitative recognition II: shapelets and constrained dynamic time warping
  10. Quantitative recognition III: causal encoders and contrastive learning
  11. Quantitative recognition IV: GAF images, CNNs, transformers and visual-model audits
  12. Quantitative recognition V: calibrate, abstain and test the complete strategy
  13. Research review: what the evidence does and does not establish

Quantitative finance and development glossary · Python resources and libraries · Research sources and limitations