Shapelet Pattern Classifier
Install and import#
npm install fintech-algorithmsimport { 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#
| Name | Type | Notes |
|---|---|---|
series | number[] | 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 |
shapelet | number[] | The discriminative subsequence to look for. Needs at least two finite values and cannot be constant. min_length: 2 · finite: true nulls: reject |
threshold | number | The 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 |
belowLabel | boolean | The 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#
[99.870299, 100.065403, 100.108942, 100.094882, 100.306776, 100.33253]Showing 6 of 48 elements.
[0, 1, 3, 6, 8, 6]Showing 6 of 10 elements.
0.6024320464983021Call#
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#
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.
References#
- Foundations of Technical Analysis — Andrew W. Lo, Harry Mamaysky, and Jiang Wang
- SciPy `find_peaks` — SciPy project
- The Probability of Backtest Overfitting — David H. Bailey, Jonathan M. Borwein, Marcos López de Prado, and Qiji Jim Zhu
- Time Series Shapelets: A New Primitive for Data Mining — Lexiang Ye and Eamonn Keogh
- Applicability decision