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

Shapelet Pattern Classifier

Install and import#

bash
npm install fintech-algorithms
ts
import { classify } from "fintech-algorithms/geometric-chart-patterns/pattern-matching/shapelet-pattern-classifier";

Signature#

classify(series, shapelet, threshold, belowLabel)

Labels a series by how closely its best-matching window resembles a shapelet. It slides the z-normalized shapelet across the series, takes the smallest Euclidean distance, and turns that into a class by comparing it against a threshold.

Parameters#

NameTypeNotes
seriesnumber[]The series being classified, in order. Needs at least three finite values and must be no shorter than the shapelet.
min_length: 3 · finite: true
nulls: reject
shapeletnumber[]The discriminative subsequence to look for. Needs at least two finite values and cannot be constant.
min_length: 2 · finite: true
nulls: reject
thresholdnumberThe distance at or below which the series counts as a match, normally the cut point learned by train. Must be finite and nonnegative.
min: 0 · finite: true
belowLabelbooleanThe class assigned to series at or below the threshold. Series above it get the opposite label.
optional

Returns#

{ distance: number; best_start: number; profile: Array<number | null>; threshold: number; matched: boolean; predicted_label: boolean }

An object giving the winning distance and the best_start index it was found at, the full profile of one distance per window start with null where a window was constant, the threshold used, matched for whether the distance fell at or below it, and the resulting predicted_label.

Errors#

  • When threshold is negative or not a finite number — throws Error
  • When series holds fewer than three values or shapelet fewer than two, or any value is not a finite number — throws Error
  • When shapelet is longer than series — throws Error
  • When shapelet is constant, so its z-normalization is undefined — throws Error
  • When every window of series is constant, leaving no distance to take — throws Error

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

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#

series
[99.870299, 100.065403, 100.108942, 100.094882, 100.306776, 100.33253]

Showing 6 of 48 elements.

shapelet
[0, 1, 3, 6, 8, 6]

Showing 6 of 10 elements.

threshold
0.6024320464983021

Call#

classify(series, shapelet, threshold, belowLabel)

Returns#

object with 6 fields: distance, best_start, profile, threshold, matched, predicted_label

{
  "distance": 8.81773713357542e-15,
  "best_start": 20,
  "profile": [
    4.4429515304009595,
    4.021249553691412,
    3.1883762365539186,
    2.4542268418798887,
    2.742051350159151,
    2.8389981766588135
  ],
  "threshold": 0.6024320464983021,
  "matched": true,
  "predicted_label": true
}

Other exports#

This module also exports train, distance. Every module additionally exports run as an alias of its primary function, and a meta object carrying its catalog id, domain, family, shape and article URL.

Diagrams#

Shapelet Pattern Classifier — algorithm anatomy
Shapelet Pattern Classifier — article hero
Shapelet Pattern Classifier — calculation ledger
Shapelet Pattern Classifier — causal timeline
Shapelet Pattern Classifier — failure boundary
Shapelet Pattern Classifier — method comparison

Calculation flow#

Reasoning Flow — Shapelet Pattern Classifier
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#