A matrix profile is a vector that records, for every subsequence of length m in a time series, the z-normalized Euclidean distance to its nearest neighbor elsewhere in the same series, together with an index pointing to that neighbor. In anomaly detection, it turns the search for unusual shapes into a simple rule: high values mark discords, subsequences unlike anything else, and low values mark motifs, patterns that repeat.
The concept was introduced in 2016 by Eamonn Keogh’s research group at the University of California, Riverside. It has become a standard tool for time series anomaly detection because it is exact, nearly parameter-free and domain-agnostic: the only essential setting is the subsequence length.
How a matrix profile is computed
Conceptually, the method slides a window of length m along a series of n points and compares each window with every other window. The full set of comparisons forms a distance matrix of roughly n by n entries; the matrix profile keeps only the minimum of each row, which is why it needs linear memory instead of quadratic.
Two details make the result meaningful. Each subsequence is z-normalized before comparison, so shape matters rather than offset or amplitude, and an exclusion zone around each window, commonly m/4 to m/2 points wide, prevents a subsequence from matching its own slightly shifted copy, which is known as a trivial match.
The table below describes the outputs that a matrix profile computation produces.
| Output | Content | Use in analysis |
|---|---|---|
| Matrix profile | Nearest-neighbor distance per subsequence | Ranks windows by how unusual they are |
| Matrix profile index | Position of each nearest neighbor | Links each window to its best match |
| Left matrix profile | Nearest match among earlier windows only | Supports streaming and causal detection |
| Right matrix profile | Nearest match among later windows only | Used to find time series chains |
Algorithms for computing the matrix profile
A naive computation would cost O(n squared times m) operations, which is impractical for long recordings. The published algorithms reduce this cost by reusing dot products between overlapping windows or by using the fast Fourier transform through the MASS distance routine.
| Algorithm | Approach | Typical use |
|---|---|---|
| STAMP | Repeated FFT-based distance profiles | Anytime exact computation |
| STOMP | Incremental dot-product updates, O(n squared) | Fast exact batch computation |
| SCRIMP++ | Diagonal-order approximation then refinement | Good approximations within seconds |
| SCAMP | Numerically stable GPU and cluster implementation | Very long series in production |
| STAMPI | Incremental updates as new points arrive | Online monitoring of data streams |
Because each diagonal of the distance matrix can be processed independently, matrix profile computation parallelizes well across GPUs. Open-source libraries such as STUMPY for Python make these algorithms available with a few lines of code, including multidimensional variants.
Discords, motifs and other derived results
The value of a matrix profile lies in the many tasks it supports from a single computation. A time series discord, the subsequence with the largest nearest-neighbor distance, is one of the most competitive definitions of a collective anomaly in a time series, and it requires no training labels.
| Task | What is read from the profile | Example result |
|---|---|---|
| Discord discovery | Highest profile values | An irregular heartbeat in an ECG |
| Motif discovery | Lowest profile values and their index | A repeated machine cycle |
| Semantic segmentation | Arc counts from the index (FLUSS) | The moment a patient changes activity |
| AB-join | Distances between two different series | Behavior present in one period but not another |
| Time series chains | Left and right index links | A pattern that drifts slowly over time |
Compared with neighboring detectors such as the local outlier factor or an isolation forest, the matrix profile works directly on raw subsequences rather than on engineered features. Compared with forecasting-based anomaly detection, it needs no model of normal behavior, but it also cannot predict future values.
Choosing the subsequence length and thresholds
The subsequence length m is the decisive setting, and it should match the duration of the behavior of interest, such as one heartbeat, one machine cycle or one day of readings. A window that is too short detects noise spikes, while one that is too long dilutes a brief anomaly inside normal data.
- Use domain knowledge first: the natural period of the process is usually the best starting length.
- Try several lengths: the pan matrix profile computes results across a range of m values.
- Set thresholds on distance: maximum distance for z-normalized windows is 2 times the square root of m, so normalized scores can be compared across lengths.
- Inspect the top discords visually: a short review of the highest peaks quickly reveals artifacts and data errors.
For alerting, practitioners often combine the profile with dynamic thresholding or compare each new window only with history, using the left matrix profile, so that a detector never looks into the future.
Limitations and failure modes
The best-known weakness is the twin freak problem: if the same anomaly occurs twice, each copy finds the other as a close neighbor and both receive low profile values. Variants that use the k-th nearest neighbor or compare only against a clean reference set reduce this risk.
Further limitations follow from the design. Z-normalization hides anomalies that differ only in level or amplitude, such as a sensor stuck at a constant offset, and flat segments create numerical instability that libraries must handle explicitly. Exact computation also remains quadratic in series length, so billions of points require GPU clusters or approximate algorithms such as DAMP.
In plain words
Picture a long recording from a sensor. Cut it into short overlapping pieces. For each piece, look for the most similar piece anywhere else in the recording. Write down how close that best match is.
That list of scores is the matrix profile. A piece with a very close twin is ordinary. A piece with no good match anywhere is suspicious, because nothing else in the data looks like it. The method needs only one choice: how long each piece should be.
A real-world example
A water utility monitors pump vibration at one reading per second across several pumping stations. Engineers know that a normal pump cycle lasts about five minutes, so they set the subsequence length to 300 points and compute the matrix profile over the previous 30 days of data on a single GPU.
Most cycles have nearly identical neighbors, with low profile values. The top discord stands out clearly: a cycle in which a faint high-frequency oscillation appears just after startup. Engineers inspect the pump and find an early bearing defect that had not yet raised any vibration amplitude alarm.
The team then runs an incremental version on live data, comparing each new cycle with the left matrix profile. Unusual cycles now trigger a review within minutes, which allows maintenance to be scheduled before an unplanned outage.