Esempio n. 1
0
 public OutColumn(Scalar <float> input,
                  int confidence,
                  int pvalueHistoryLength,
                  AnomalySide side)
     : base(new Reconciler(confidence, pvalueHistoryLength, side), input)
 {
     Input = input;
 }
Esempio n. 2
0
 public Reconciler(
     int confidence,
     int pvalueHistoryLength,
     AnomalySide side)
 {
     _confidence          = confidence;
     _pvalueHistoryLength = pvalueHistoryLength;
     _side = side;
 }
Esempio n. 3
0
 /// <summary>
 /// Perform SSA spike detection over a column of time series data. See <see cref="SsaSpikeEstimator"/>.
 /// </summary>
 public static Vector <double> SsaSpikeDetect(
     this Scalar <float> input,
     int confidence,
     int changeHistoryLength,
     int trainingWindowSize,
     int seasonalityWindowSize,
     AnomalySide side            = AnomalySide.TwoSided,
     ErrorFunction errorFunction = ErrorFunction.SignedDifference
     ) => new OutColumn(input, confidence, changeHistoryLength, trainingWindowSize, seasonalityWindowSize, side, errorFunction);
Esempio n. 4
0
        public static IEstimator <ITransformer> _DetectIidSpike(this MLContext MLContext, JToken componentObject)
        {
            string      outputColumn  = componentObject.Value <string>("OutputColumnName");
            string      inputColumn   = componentObject.Value <string>("InputColumnName");
            int         confidence    = componentObject.Value <int>("Confidence");
            int         pValueHistory = componentObject.Value <int>("PvalueHistoryLength");
            AnomalySide side          = Enum.Parse <AnomalySide>(componentObject.Value <string>("AnomalySide"));

            return(MLContext.Transforms.DetectIidSpike(outputColumn, inputColumn, confidence, pValueHistory, side));
        }
Esempio n. 5
0
 public OutColumn(Scalar <float> input,
                  int confidence,
                  int pvalueHistoryLength,
                  int trainingWindowSize,
                  int seasonalityWindowSize,
                  AnomalySide side,
                  ErrorFunction errorFunction)
     : base(new Reconciler(confidence, pvalueHistoryLength, trainingWindowSize, seasonalityWindowSize, side, errorFunction), input)
 {
     Input = input;
 }
Esempio n. 6
0
        public static IEstimator <ITransformer> _DetectSpikeBySsa(this MLContext MLContext, JToken componentObject)
        {
            string        outputColumn      = componentObject.Value <string>("OutputColumnName");
            string        inputColumn       = componentObject.Value <string>("InputColumnName");
            int           confidence        = componentObject.Value <int>("Confidence");
            int           pValueHistory     = componentObject.Value <int>("PvalueHistoryLength");
            int           trainingWindow    = componentObject.Value <int>("TrainingWindowSize");
            int           seasonalityWindow = componentObject.Value <int>("SeasonalityWindowSize");
            AnomalySide   side          = Enum.Parse <AnomalySide>(componentObject.Value <string>("AnomalySide"));
            ErrorFunction errorFunction = Enum.Parse <ErrorFunction>(componentObject.Value <string>("ErrorFunction"));

            return(MLContext.Transforms.DetectSpikeBySsa(outputColumn, inputColumn, confidence, pValueHistory,
                                                         trainingWindow, seasonalityWindow, side, errorFunction));
        }
Esempio n. 7
0
 public Reconciler(
     int confidence,
     int pvalueHistoryLength,
     int trainingWindowSize,
     int seasonalityWindowSize,
     AnomalySide side,
     ErrorFunction errorFunction)
 {
     _confidence            = confidence;
     _pvalueHistoryLength   = pvalueHistoryLength;
     _trainingWindowSize    = trainingWindowSize;
     _seasonalityWindowSize = seasonalityWindowSize;
     _side          = side;
     _errorFunction = errorFunction;
 }
 /// <summary>
 /// Create a new instance of <see cref="SsaSpikeEstimator"/>
 /// </summary>
 /// <param name="env">Host Environment.</param>
 /// <param name="outputColumnName">Name of the column resulting from the transformation of <paramref name="inputColumnName"/>.</param>
 /// <param name="confidence">The confidence for spike detection in the range [0, 100].</param>
 /// <param name="pvalueHistoryLength">The size of the sliding window for computing the p-value.</param>
 /// <param name="trainingWindowSize">The number of points from the beginning of the sequence used for training.</param>
 /// <param name="seasonalityWindowSize">An upper bound on the largest relevant seasonality in the input time-series.</param>
 /// <param name="inputColumnName">Name of column to transform. If set to <see langword="null"/>, the value of the <paramref name="outputColumnName"/> will be used as source.
 /// The vector contains Alert, Raw Score, P-Value as first three values.</param>
 /// <param name="side">The argument that determines whether to detect positive or negative anomalies, or both.</param>
 /// <param name="errorFunction">The function used to compute the error between the expected and the observed value.</param>
 internal SsaSpikeEstimator(IHostEnvironment env,
                            string outputColumnName,
                            int confidence,
                            int pvalueHistoryLength,
                            int trainingWindowSize,
                            int seasonalityWindowSize,
                            string inputColumnName      = null,
                            AnomalySide side            = AnomalySide.TwoSided,
                            ErrorFunction errorFunction = ErrorFunction.SignedDifference)
     : this(env, new SsaSpikeDetector.Options
 {
     Source = inputColumnName ?? outputColumnName,
     Name = outputColumnName,
     Confidence = confidence,
     PvalueHistoryLength = pvalueHistoryLength,
     TrainingWindowSize = trainingWindowSize,
     SeasonalWindowSize = seasonalityWindowSize,
     Side = side,
     ErrorFunction = errorFunction
 })
 {
 }
Esempio n. 9
0
        protected SequentialAnomalyDetectionTransformBase(IHostEnvironment env, ModelLoadContext ctx, string name, IDataView input)
            : base(env, ctx, name, input)
        {
            // *** Binary format ***
            // <base>
            // byte: _martingale
            // byte: _alertingScore
            // byte: _anomalySide
            // Double: _powerMartingaleEpsilon
            // Double: _alertThreshold

            byte temp;

            temp = ctx.Reader.ReadByte();
            Host.CheckDecode(Enum.IsDefined(typeof(MartingaleType), temp));
            Martingale = (MartingaleType)temp;

            temp = ctx.Reader.ReadByte();
            Host.CheckDecode(Enum.IsDefined(typeof(AlertingScore), temp));
            ThresholdScore = (AlertingScore)temp;

            Host.CheckDecode(Martingale != MartingaleType.None || ThresholdScore != AlertingScore.MartingaleScore);
            Host.CheckDecode(WindowSize > 0 || ThresholdScore == AlertingScore.RawScore);

            temp = ctx.Reader.ReadByte();
            Host.CheckDecode(Enum.IsDefined(typeof(AnomalySide), temp));
            Side = (AnomalySide)temp;

            PowerMartingaleEpsilon = ctx.Reader.ReadDouble();
            Host.CheckDecode(0 < PowerMartingaleEpsilon && PowerMartingaleEpsilon < 1);

            AlertThreshold = ctx.Reader.ReadDouble();
            Host.CheckDecode(AlertThreshold >= 0);
            Host.CheckDecode(ThresholdScore != AlertingScore.PValueScore || (0 <= AlertThreshold && AlertThreshold <= 1));

            _outputLength  = GetOutputLength(ThresholdScore, Host);
            _wrappedSchema = CreateSchema(base.Schema, OutputColumnName, _outputLength);
        }
        private protected SequentialAnomalyDetectionTransformBase(int windowSize, int initialWindowSize, string inputColumnName, string outputColumnName, string name, IHostEnvironment env,
                                                                  AnomalySide anomalySide, MartingaleType martingale, AlertingScore alertingScore, Double powerMartingaleEpsilon,
                                                                  Double alertThreshold)
            : base(Contracts.CheckRef(env, nameof(env)).Register(name), windowSize, initialWindowSize, outputColumnName, inputColumnName, new VectorType(NumberDataViewType.Double, GetOutputLength(alertingScore, env)))
        {
            Host.CheckUserArg(Enum.IsDefined(typeof(MartingaleType), martingale), nameof(ArgumentsBase.Martingale), "Value is undefined.");
            Host.CheckUserArg(Enum.IsDefined(typeof(AnomalySide), anomalySide), nameof(ArgumentsBase.Side), "Value is undefined.");
            Host.CheckUserArg(Enum.IsDefined(typeof(AlertingScore), alertingScore), nameof(ArgumentsBase.AlertOn), "Value is undefined.");
            Host.CheckUserArg(martingale != MartingaleType.None || alertingScore != AlertingScore.MartingaleScore, nameof(ArgumentsBase.Martingale), "A martingale type should be specified if alerting is based on the martingale score.");
            Host.CheckUserArg(windowSize > 0 || alertingScore == AlertingScore.RawScore, nameof(ArgumentsBase.AlertOn),
                              "When there is no windowed buffering (i.e., " + nameof(ArgumentsBase.WindowSize) + " = 0), the alert can be generated only based on the raw score (i.e., "
                              + nameof(ArgumentsBase.AlertOn) + " = " + nameof(AlertingScore.RawScore) + ")");
            Host.CheckUserArg(0 < powerMartingaleEpsilon && powerMartingaleEpsilon < 1, nameof(ArgumentsBase.PowerMartingaleEpsilon), "Should be in (0,1).");
            Host.CheckUserArg(alertThreshold >= 0, nameof(ArgumentsBase.AlertThreshold), "Must be non-negative.");
            Host.CheckUserArg(alertingScore != AlertingScore.PValueScore || (0 <= alertThreshold && alertThreshold <= 1), nameof(ArgumentsBase.AlertThreshold), "Must be in [0,1].");

            ThresholdScore         = alertingScore;
            Side                   = anomalySide;
            Martingale             = martingale;
            PowerMartingaleEpsilon = powerMartingaleEpsilon;
            AlertThreshold         = alertThreshold;
            OutputLength           = GetOutputLength(ThresholdScore, Host);
        }
        private protected SequentialAnomalyDetectionTransformBase(IHostEnvironment env, ModelLoadContext ctx, string name)
            : base(Contracts.CheckRef(env, nameof(env)).Register(name), ctx)
        {
            // *** Binary format ***
            // <base>
            // byte: _martingale
            // byte: _alertingScore
            // byte: _anomalySide
            // Double: _powerMartingaleEpsilon
            // Double: _alertThreshold

            byte temp;

            temp = ctx.Reader.ReadByte();
            Host.CheckDecode(Enum.IsDefined(typeof(MartingaleType), temp));
            Martingale = (MartingaleType)temp;

            temp = ctx.Reader.ReadByte();
            Host.CheckDecode(Enum.IsDefined(typeof(AlertingScore), temp));
            ThresholdScore = (AlertingScore)temp;

            Host.CheckDecode(Martingale != MartingaleType.None || ThresholdScore != AlertingScore.MartingaleScore);
            Host.CheckDecode(WindowSize > 0 || ThresholdScore == AlertingScore.RawScore);

            temp = ctx.Reader.ReadByte();
            Host.CheckDecode(Enum.IsDefined(typeof(AnomalySide), temp));
            Side = (AnomalySide)temp;

            PowerMartingaleEpsilon = ctx.Reader.ReadDouble();
            Host.CheckDecode(0 < PowerMartingaleEpsilon && PowerMartingaleEpsilon < 1);

            AlertThreshold = ctx.Reader.ReadDouble();
            Host.CheckDecode(AlertThreshold >= 0);
            Host.CheckDecode(ThresholdScore != AlertingScore.PValueScore || (0 <= AlertThreshold && AlertThreshold <= 1));

            OutputLength = GetOutputLength(ThresholdScore, Host);
        }
Esempio n. 12
0
 /// <summary>
 /// Create a new instance of <see cref="IidSpikeEstimator"/>
 /// </summary>
 /// <param name="env">Host Environment.</param>
 /// <param name="outputColumnName">Name of the column resulting from the transformation of <paramref name="inputColumnName"/>.
 /// Column is a vector of type double and size 4. The vector contains Alert, Raw Score, P-Value as first three values.</param>
 /// <param name="confidence">The confidence for spike detection in the range [0, 100].</param>
 /// <param name="pvalueHistoryLength">The size of the sliding window for computing the p-value.</param>
 /// <param name="inputColumnName">Name of column to transform. If set to <see langword="null"/>, the value of the <paramref name="outputColumnName"/> will be used as source.</param>
 /// <param name="side">The argument that determines whether to detect positive or negative anomalies, or both.</param>
 internal IidSpikeEstimator(IHostEnvironment env, string outputColumnName, int confidence, int pvalueHistoryLength, string inputColumnName, AnomalySide side = AnomalySide.TwoSided)
     : base(Contracts.CheckRef(env, nameof(env)).Register(nameof(IidSpikeDetector)),
            new IidSpikeDetector(env, new IidSpikeDetector.Options
 {
     Name                = outputColumnName,
     Source              = inputColumnName,
     Confidence          = confidence,
     PvalueHistoryLength = pvalueHistoryLength,
     Side                = side
 }))
 {
 }
Esempio n. 13
0
 /// <summary>
 /// Perform IID spike detection over a column of time series data. See <see cref="IidSpikeEstimator"/>.
 /// </summary>
 public static Vector <double> IidSpikeDetect(
     this Scalar <float> input,
     int confidence,
     int pvalueHistoryLength,
     AnomalySide side = AnomalySide.TwoSided
     ) => new OutColumn(input, confidence, pvalueHistoryLength, side);
Esempio n. 14
0
 /// <summary>
 /// Create <see cref="IidSpikeEstimator"/>, which predicts spikes in
 /// <a href="https://en.wikipedia.org/wiki/Independent_and_identically_distributed_random_variables"> independent identically distributed (i.i.d.)</a>
 /// time series based on adaptive kernel density estimations and martingale scores.
 /// </summary>
 /// <param name="catalog">The transform's catalog.</param>
 /// <param name="outputColumnName">Name of the column resulting from the transformation of <paramref name="inputColumnName"/>.
 /// The column data is a vector of <see cref="System.Double"/>. The vector contains 3 elements: alert (non-zero value means a spike), raw score, and p-value.</param>
 /// <param name="inputColumnName">Name of column to transform. The column data must be <see cref="System.Single"/>.
 /// If set to <see langword="null"/>, the value of the <paramref name="outputColumnName"/> will be used as source.</param>
 /// <param name="confidence">The confidence for spike detection in the range [0, 100].</param>
 /// <param name="pvalueHistoryLength">The size of the sliding window for computing the p-value.</param>
 /// <param name="side">The argument that determines whether to detect positive or negative anomalies, or both.</param>
 /// <example>
 /// <format type="text/markdown">
 /// <![CDATA[
 /// [!code-csharp[DetectIidSpike](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectIidSpikeBatchPrediction.cs)]
 /// ]]>
 /// </format>
 /// </example>
 public static IidSpikeEstimator DetectIidSpike(this TransformsCatalog catalog, string outputColumnName, string inputColumnName,
                                                int confidence, int pvalueHistoryLength, AnomalySide side = AnomalySide.TwoSided)
 => new IidSpikeEstimator(CatalogUtils.GetEnvironment(catalog), outputColumnName, confidence, pvalueHistoryLength, inputColumnName, side);
Esempio n. 15
0
 /// <summary>
 /// Create <see cref="SsaSpikeEstimator"/>, which predicts spikes in time series
 /// using <a href="https://en.wikipedia.org/wiki/Singular_spectrum_analysis">Singular Spectrum Analysis (SSA)</a>.
 /// </summary>
 /// <param name="catalog">The transform's catalog.</param>
 /// <param name="outputColumnName">Name of the column resulting from the transformation of <paramref name="inputColumnName"/>.
 /// The column data is a vector of <see cref="System.Double"/>. The vector contains 3 elements: alert (non-zero value means a spike), raw score, and p-value.</param>
 /// <param name="inputColumnName">Name of column to transform. The column data must be <see cref="System.Single"/>.
 /// If set to <see langword="null"/>, the value of the <paramref name="outputColumnName"/> will be used as source.</param>
 /// <param name="confidence">The confidence for spike detection in the range [0, 100].</param>
 /// <param name="pvalueHistoryLength">The size of the sliding window for computing the p-value.</param>
 /// <param name="trainingWindowSize">The number of points from the beginning of the sequence used for training.</param>
 /// <param name="seasonalityWindowSize">An upper bound on the largest relevant seasonality in the input time-series.</param>
 /// <param name="side">The argument that determines whether to detect positive or negative anomalies, or both.</param>
 /// <param name="errorFunction">The function used to compute the error between the expected and the observed value.</param>
 /// <example>
 /// <format type="text/markdown">
 /// <![CDATA[
 /// [!code-csharp[DetectSpikeBySsa](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectSpikeBySsaBatchPrediction.cs)]
 /// ]]>
 /// </format>
 /// </example>
 public static SsaSpikeEstimator DetectSpikeBySsa(this TransformsCatalog catalog, string outputColumnName, string inputColumnName, int confidence, int pvalueHistoryLength,
                                                  int trainingWindowSize, int seasonalityWindowSize, AnomalySide side = AnomalySide.TwoSided, ErrorFunction errorFunction = ErrorFunction.SignedDifference)
 => new SsaSpikeEstimator(CatalogUtils.GetEnvironment(catalog), outputColumnName, confidence, pvalueHistoryLength, trainingWindowSize, seasonalityWindowSize, inputColumnName, side, errorFunction);
Esempio n. 16
0
 public static IidSpikeEstimator DetectIidSpike(this TransformsCatalog catalog, string outputColumnName, string inputColumnName,
                                                int confidence, int pvalueHistoryLength, AnomalySide side = AnomalySide.TwoSided)
 => DetectIidSpike(catalog, outputColumnName, inputColumnName, (double)confidence, pvalueHistoryLength, side);