Send Feedback
Skip to content

Anomaly Detection

This page describes the Anomaly Detection parameters as part of AI libs.

Finding discords (unexpected events, faulty sensors) in time series data is a feature of high importance for fields such as finance, IoT, and healthcare, where identifying repeating trends, anomalies, or correlations in time-dependent data is critical.

.ai.tss.anomaly

The .ai.tss.anomaly function conducts an anonaly detection and returns the matrix profile for m sized anomalies.

Z-normalization ensures that subsequences are compared on a standardized scale, removing the effects of shifts in mean and variance. This allows for more accurate detection of similar temporal patterns across varying time series.

Parameters

Name Type(s) Description
ts short[] | int[] | long[] | float[] | real[] The data vector
m short | long | int The number of nearest neighbors to return
sp short | long | int The number of nearest neighbors to return
opts dict Advanced options (optional):
- lookahead (default m): distance to look ahead in time series and prune candidates early
- normalize (default true): if true, applies z-normalization to timeseries windows.
- bsf (default false): if true, returns best-so-far value along with matrix profile.

Returns

Type Description
float[] Approximate matrix profile for m sized anomalies
(float[]; long) Approximate matrix profile for m sized anomalies and highest discord rank if opts`bsf is true

Example

q).ai:use`kx.ai
q)ts:100?1f;
q).ai.tss.anomaly[ts;5;6;enlist[`bsf]!enlist 1b]

0 0 0 0 0 0 1.646094 2.320798 3.12591 3.34024 3.061833 3.041805 ..
3.89505

.ai.tss.anomalyi

The .ai.tss.anomalyi function calculates anomaly rank on last window of timeseries. It can be used for online anomaly detection.

By normalizing at each step, it produces consistent distance values that highlight true structural similarities rather than raw value differences.

Parameters

Name Type(s) Description
ts short[] | int[] | long[] | float[] | real[] The data vector
m short | long | int The number of nearest neighbors to return
bsf short | long | int | float | real Highest rank in previous anomaly detection
opts dict Advanced options (optional):
- normalize (default true): if true, applies z-normalization to timeseries windows.

Returns

Type Description
(float;float) Distance for last window and new best-so-far value

Example

q)ts:100?1f;
q)(amp;bsf):.ai.tss.anomaly[ts;5;6;enlist[`bsf]!enlist 1b]
q)ts,:rand 1f;
q).ai.tss.anomalyi[ts;5;bsf;::]
3.485367
3.89505