timeSeriesLofAD
Create a machine learning local outlier factor model for anomaly detection in time series data
Since R2026a
Description
Add-On Required: This feature requires the Time Series Anomaly Detection for MATLAB add-on.
Create a machine learning local outlier factor model for anomaly detection in time series data
timeSeriesLofAD creates an time-series anomaly detector based
on the machine learning Local Outlier Factor algorithm in Statistics and Machine Learning Toolbox™. This algorithm is detects anomalies based on the relative density of an
observation with respect to the surrounding neighborhood.
When the relative density is high, indicating many similar points nearby, the algorithm identifies the point as normal.
When the relative density is low, indicating few similar points nearby, the algorithm identifies the point as a local outlier.
For more information on the model that TimeSeriesLofAD creates, see
TimeSeriesLOFDetectorecto. For more information on the function that
timeSeriesLofAD is based on, see lof in Statistics and Machine Learning Toolbox.
creates a detector = timeSeriesLofAD(numChannels)TimeSeriesLOFDetector model for time series data with
numChannels input channels.
detector = timeSeriesLofAD(
sets additional options using one or more name-value arguments.numChannels,Name=Value)
For example, detector = timeSeriesLofAD(3,DetectionWindowLength=20),
meaning that the method . detector =
timeSeriesLofAD(3,DetectionWindowLength=20) creates a three-channel detector
model with a detection window length of 20.
Examples
Load the file sineWaveAnomalyData.mat, which contains two sets of synthetic 3-channel sinusoidal signals.
sineWaveNormal contains 10 sinusoids of stable frequency and amplitude. Each signal has a series of small-amplitude impact-like imperfections. The signals have different lengths and initial phases. sineWaveAbnormal contains 3 sinusoids that contain the same normal data as sineWaveNormal, but that also include anomalous data.
load sineWaveAnomalyData.mat sineWaveNormal sineWaveAbnormal
Plot input signals
Plot all 3 channels of the first three anomalous signals.
s1 = 3; tiledlayout("vertical") ax = zeros(s1,1); for kj = 1:s1 ax(kj) = nexttile; plot(sineWaveAbnormal{kj}) title("Anomalous Signals") end

sineWaveAbnormal contains three signals, all of the same length. Each signal in the set has one or more anomalies.
All channels of the first signal have an abrupt change in frequency that lasts for a finite time.
The second signal has a finite-duration amplitude change in one of its channels.
The third signal has spikes at random times in all channels.
Create Detector
Use the timeSeriesLofAD detector to create a robust random cut forest detector with 3 channels and using "kdtree" and "cityblock" as the Search method / Distance metric pair.
detector_tslof = timeSeriesLofAD(3,SearchMethod="kdtree",Distance="cityblock")
detector_tslof =
TimeSeriesLOFDetector with properties:
NumNeighbors: []
Distance: "cityblock"
BucketSize: 50
CacheSize: 1000
Cov: []
Exponent: []
IncludeTies: 0
SearchMethod: "kdtree"
NumChannels: 3
IsTrained: 0
WindowLength: 10
TrainingStride: 1
DetectionStride: 10
Threshold: []
ThresholdMethod: "kSigma"
ThresholdParameter: 3
ThresholdFunction: []
Normalization: "zscore"
FeatureExtraction: 1
Train Detector
Train the detector using the normal data.
detector_tslof = train(detector_tslof,sineWaveNormal);
View the threshold that train computes and saves within detector_tslof. This computed value is influenced by random factors, such as which subsets of the data are used for training, and can change somewhat for different training sessions and different machines.
thresh = detector_tslof.Threshold
thresh = 1.2772
Plot Anomaly Scores
Plot the histogram of the anomaly scores for the normal data. Each score is calculated over a single detection window. The threshold, plotted as a vertical line, does not always completely bound the scores.
plotHistogram(detector_tslof,sineWaveNormal);

Use Detector to Identify Anomalies
Use the detect function to determine the anomaly scores for the anomalous data. Then, plot the anomaly scores of the normal and anomalous data together.
results = detect(detector_tslof, sineWaveAbnormal);
results is a cell array that contains three tables, one table for each signal. Each cell table contains three variables: WindowLabel, WindowAnomalyScore, and WindowStartIndices.
View the contents of the five rows between 10 and 15 of the third table.
results_table3 = results{3};
results_t3_rows10to15 = results_table3(10:15,:)results_t3_rows10to15=6×3 table
Labels AnomalyScores StartIndices
______ _____________ ____________
false 0.98275 91
false 1.0275 101
true 34.426 111
false 0.9937 121
false 1.0497 131
false 1.0424 141
The results indicate an anomaly in the third window of this set. The anomaly score is significantly higher than the scores for the other windows..
Plot Anomaly Score Distributions
Plot a histogram that shows the anomaly scores for both sets of data together, along with the threshold, for comparison.
plotHistogram(detector_tslof,sineWaveNormal,sineWaveAbnormal)

The histogram uses different colors for the normal (Data 1) and anomalous (Data 2) data. Both types of data appear to the left of the threshold. To the right of threshold, Data 2 is prevalent.
Plot Detected Anomalies
Plot the detected anomalies of the third abnormal signal set.
plot(detector_tslof,sineWaveAbnormal{3})
The top plot shows an overlay of red where the anomalies occur. The bottom plot shows how effective the threshold is at dividing the normal from the abnormal scores for Signal set 3.
Input Arguments
Number of input channels in each time series, specified as a positive integer. All time series inputs must have the same number of channels.
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN, where Name is
the argument name and Value is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: detector = timeSeriesLofAD(3,DetectionWindowLength=20) sets
the length of the detection window to 20.
Window
Window length of each time series segment, specified as a positive integer.
Stride length of sliding window in training stage, specified as a positive integer.
Stride length of sliding window in detection stage, specified as a positive
integer. DetectionStride controls the number of overlapped
samples. Increasing the amount of overlap provides higher resolution, but at higher
computational cost.
If you do not specify DetectionStride,
timeSeriesLofAD sets the stride length to the value of
WindowLength to create non-overlapping windows.
Feature Extraction
Enable feature extraction option, specified as 1(true) or
0(false).
When
FeatureExtractionis enabled, the software extracts statistical features to use during training. Extracted features are generally cleaner and more meaningful than raw data, but the extraction process can be computationally expensive.When
FeatureExtractionis disabled, the training process uses only raw data points that are combined into a single flattened vector. Raw data is likely to be noisier and include random disturbances. However, if you have a relatively small number of signals and they already represent useful features, then the computational costFeatureExtractionmay not be worth the benefit.
Threshold
Method for computing the detection threshold, specified as one of these:
"kSigma"— Sigma-based standard deviation of the normalized anomaly scores, calculated as mean + k + standard deviation. The parameter k determines how many standard deviations above the mean the threshold is set. The value of k is specified byThresholdParameter."contaminationFraction"— Percentage of anomalies within a specified fraction of windows, measured over the entire training set. The fraction value is specified byThresholdParameter."max"— Maximum window loss measured over the entire training data set and multiplied byThresholdParameter."mean"— Mean window loss measured over the entire training data set and multiplied byThresholdParameter."median"— Median window loss measured over the entire training data set and multiplied byThresholdParameter."manual"— Manual detection threshold value based onThreshold."customFunction"— Custom detection threshold method based onThresholdFunction.
If you specify ThresholdMethod, you can also specify ThresholdParameter, Threshold, or ThresholdParameter. The available threshold parameter depends on the specified detection method.
Parameter for determining the detection threshold, specified as a numeric scalar.
The way you specify ThresholdParameter depends on the specified value for ThresholdMethod. This list describes the specification of ThresholdParameter for each possible value of ThresholdMethod.
"kSigma"— SpecifyThresholdParameteras a positive numeric scalar. If you do not specifyThresholdParameter, the detector sets the threshold to3."contaminationFraction"— SpecifyThresholdParameteras a as a nonnegative scalar less than 0.5. For example, if you specify"contaminationFraction"as0.05, then the threshold is set to identify the top 5% of the anomaly scores as anomalous. If you do not specifyThresholdParameter, the detector sets the threshold to 0.01."max","mean", or"median"— SpecifyThresholdParameteras a positive numeric scalar. If you do not specifyThresholdParameter, the detector sets the threshold to1."customFunction"or"manual"—ThresholdParameterdoes not apply.
Detection threshold that separates the normal anomaly scores from the anomalous anomaly scores, specified as a scalar. During the detection process, the software assigns anomaly labels according to this threshold.
The source of the Threshold value depends on the setting of
ThresholdMethod.
If
ThresholdMethodis"manual", you set the value.If
ThresholdMethodis"customFunction", the function you specify inThresholdFunctioncomputes the value.For other values of
ThresholdMethod, specifyThresholdParameteras the input to the specified method. The software uses this method to compute the threshold value.
Function for computing a custom detection threshold, specified as a function
handle. This argument applies only when ThresholdMethod is
specified as "customFunction".
The function must have one input and one output.
The input must be the vector of the anomaly scores.
The output must contain a scalar corresponding to the detection threshold.
For more information about how the detector uses the threshold to detect
anomalies, see Threshold.
This property can be set only during object creation and, after training, by using
the updateDetector
function.
Local Outlier Factor Options
Nearest neighbor search method, specified as "kdtree" or
"exhaustive".
"kdtree"— This method uses the Kd-tree algorithm to find nearest neighbors. This option is valid when the distance metric (Distance) is one of the following:"euclidean"— Euclidean distance"cityblock"— City block distance"minkowski"— Minkowski distance"chebychev"— Chebychev distance
"exhaustive"— This method uses the"exhaustive"search algorithm to find nearest neighbors. This method finds nearest neighbors by computing the distance values from all points in the time series to each point in the neighborhood.
Number of nearest neighbors when SearchMethod is
"kdtree", specified as a positive integer.
Distance metric, specified as a string, with possible values listed here.
| Value | Description |
|---|---|
"euclidean" | Euclidean distance |
"fasteuclidean" | Euclidean distance using an algorithm that usually saves time
when the number of elements in a data point exceeds 10.
|
"mahalanobis" | Mahalanobis distance — You can specify the covariance matrix for
the Mahalanobis distance by using the |
"minkowski" | Minkowski distance — You can specify the exponent of the
Minkowski distance by using the |
"chebychev" | Chebychev distance (maximum coordinate difference) |
"cityblock" | City block distance |
"correlation" | One minus the sample correlation between observations (treated as sequences of values) |
"cosine" | One minus the cosine of the included angle between observations (treated as vectors) |
"spearman" | One minus the sample Spearman's rank correlation between observations (treated as sequences of values) |
If you want to use the Kd-tree algorithm
(), then
SearchMethod="kdtree"Distance must be "euclidean",
"cityblock", "minkowski", or
"chebychev".
For more information on the various distance metrics, see Distance Metrics.
Example: Distance="fasteuclidean"
Minkowski distance exponent, specified as a positive scalar value. This argument
is valid only when Distance is
"minkowski".
Example: Exponent=3
Tie inclusion flag indicating whether the software includes all the neighbors
whose distance values are equal to the kth smallest distance,
specified as logical 0 (false) or
1 (true). If IncludeTies
is true, the software includes all of these neighbors. Otherwise,
the software includes exactly k neighbors.
Example: IncludeTies=true
Maximum number of data points in the leaf node of the
Kd-tree, specified as a positive integer value. This argument is
valid only when SearchMethod is
"kdtree".
Example: BucketSize=40
Size of the Gram matrix in megabytes, specified as a positive scalar or
"maximal". For information about the Gram matrix, see the
Algorithms section of lof. The
timeSeriesLofAD function can use a Gram matrix when the
Distance name-value argument is
"fasteuclidean".
When CacheSize is "maximal",
timeSeriesLofAD attempts to allocate enough memory for an entire
intermediate matrix whose size is MX-by-MX,
where MX is the number of rows of the input data,
X or Tbl. CacheSize
does not have to be large enough for an entire intermediate matrix, but must be at
least large enough to hold an MX-by-1 vector. Otherwise,
timeSeriesLofAD uses the "euclidean"
distance.
If Distance is "fasteuclidean" and
CacheSize is too large or "maximal",
timeSeriesLofAD might attempt to allocate a Gram matrix that
exceeds the available memory. In this case, MATLAB® issues an error.
Example: CacheSize="maximal"
Covariance matrix, specified as a positive definite matrix of scalar values
representing the covariance matrix when the function computes the Mahalanobis
distance. This argument is valid only when Distance is
"mahalanobis".
Normalization
Normalization technique for training and testing, specified as "zscore",
"range", or "off".
"range"— Rescale the data range to [0,1]."zscore"— Distance from a data point to the mean in terms of standard deviation"off"— Do not normalize the data.
The data to which Normalization is applied depends whether
FeatureExtraction is enabled.
If
FeatureExtractionis enabled, then normalization is applied to the features.If
FeatureExtractionis disabled, then normalization is applied to the raw data.
If all the input data values are the same (the data is constant), then normalization
returns zeros. For example, if X is a vector containing all equal
values, then normalize(X) returns a vector of the same size that
contains all zeros.
For more information on normalization methods, see normalize.
Output Arguments
Anomaly detector model, returned as a TimeSeriesLOFDetector object.
Version History
Introduced in R2026a
See Also
train | detect | plot | plotHistogram | updateDetector
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)