DeepANT<T>

Models & Types · AiDotNet.TimeSeries.AnomalyDetection

Implements DeepAnT (Munir et al. 2019), unsupervised anomaly detection in time series by forecasting.

Inheritance: TimeSeriesModelBase<T> → DeepANT<T>

Implements: IGeneratedParameterRegistrar<T>

For Beginners

DeepAnT learns what “normal” looks like in your time series, then flags anything unusual as an anomaly. It works by:

  1. Learning to predict the next value based on past values
  2. Comparing actual values to predictions
  3. Marking large prediction errors as anomalies

How It Works

DeepAnT has two modules. The time-series predictor, a convolutional network, reads a history window of w values and predicts the next p_w values; it is trained on the series’ own overlapping windows with the mean absolute error and stochastic gradient descent, on the series normalized with statistics of the training data. The anomaly detector scores each observed value by its Euclidean distance from the prediction, and flags scores above a threshold. No labels are used, and anomalies need not be removed from the training data.

The paper fixes the predictor’s structure (two convolution and max-pooling blocks with 32 filters and ReLU, a fully connected layer and a linear output), the loss, the optimizer and the defaults taken from its Table 1 (window 45 and threshold 0.5, the A1 benchmark). It does not state the kernel, pooling and hidden widths, the dropout, the normalization, the learning rate or the training length, and no author code was released. Those follow the PyTorch implementation at github.com/datacubeR/DeepAnt (kernel 3, pooling 2, 40 hidden units, dropout 0.25, min-max scaling, batches of 32, 30 epochs), except the optimizer, where the paper’s SGD replaces that code’s Adam; its learning rate, which neither source gives, is Keras’ SGD default of 0.01.

Example

using AiDotNet;
using AiDotNet.Data.Loaders;
using AiDotNet.TimeSeries.AnomalyDetection;
using AiDotNet.Tensors.LinearAlgebra;

double[] series =
{
    120, 135, 148, 160, 155, 170, 180, 195, 210, 198, 220, 235,
    140, 155, 165, 178, 172, 190, 200, 215, 230, 218, 245, 260
};
var x = new Matrix<double>(series.Length, 1);
for (int i = 0; i < series.Length; i++) x[i, 0] = i;

var result = await new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureModel(new DeepANT<double>())
    .ConfigureDataLoader(DataLoaders.FromMatrixVector(x, new Vector<double>(series)))
    .BuildAsync();

var forecast = result.Predict(new Matrix<double>(6, 1));
Console.WriteLine($"DeepANT: forecast {forecast.Length} steps.");

Constructors

Constructor Summary
DeepANT(DeepANTOptions<T> options) Initializes a new instance of the DeepAnT model.

Methods

Method Summary
ModelMetadata<T> GetModelMetadata()
T PredictSingle(Vector<T> input) The predicted value after a window of WindowSize raw values.
Vector<T> ComputeAnomalyScores(Vector<T> data) The anomaly score of every value after the first WindowSize: the Euclidean distance (§IV-B) between the value and its one-step prediction from the WindowSize values before it, in the normalized units the threshold uses.
Vector<T> Forecast(Vector<T> history, int steps) Forecasts the steps values after history: each is the one-step prediction from the last WindowSize values, appended to the history for the next.
Vector<T> Predict(Matrix<T> input) Predicts the series.
bool[] DetectAnomalies(Vector<T> data) Flags every value after the first WindowSize whose anomaly score exceeds AnomalyThreshold.