public void detect_no_anomalies_if_factors_are_set_to_max_value()
        {
            var strategy      = new OnlineNormalStrategy(Double.MaxValue, Double.MaxValue);
            var anomalyResult = strategy.Detect(data, (0, int.MaxValue));

            var expected = Enumerable.Empty <(int, Anomaly)>();

            anomalyResult.SequenceEqual(expected).ShouldBeTrue();
        }
        private VerificationResult CreateAnomalyChecksAndRunEverything(
            DataFrame data,
            IMetricsRepository repository,
            Check otherCheck,
            IEnumerable <IAnalyzer <IMetric> > additionalRequiredAnalyzers)
        {
            // We only want to use historic data with the EU tag for the anomaly checks since the new
            // data point is from the EU marketplace
            var filterEU = new Dictionary <string, string> {
                { "marketplace", "EU" }
            };

            // We only want to use data points before the date time associated with the current
            // data point and only ones that are from 2018
            var afterDateTime  = CreateDate(2018, 1, 1);
            var beforeDateTime = CreateDate(2018, 8, 1);

            // Config for the size anomaly check
            var sizeAnomalyCheckConfig = new AnomalyCheckConfig(CheckLevel.Error, "Size only increases",
                                                                filterEU, afterDateTime, beforeDateTime);
            var sizeAnomalyDetectionStrategy = new AbsoluteChangeStrategy(0);

            // Config for the mean sales anomaly check
            var meanSalesAnomalyCheckConfig = new AnomalyCheckConfig(
                CheckLevel.Warning,
                "Sales mean within 2 standard deviations",
                filterEU,
                afterDateTime,
                beforeDateTime
                );

            var meanSalesAnomalyDetectionStrategy = new OnlineNormalStrategy(upperDeviationFactor: 2, lowerDeviationFactor: Option <double> .None,
                                                                             ignoreAnomalies: false);

            // ResultKey to be used when saving the results of this run
            var currentRunResultKey =
                new ResultKey(CreateDate(2018, 8, 1), new Dictionary <string, string> {
                { "marketplace", "EU" }
            });


            return(new VerificationSuite()
                   .OnData(data)
                   .AddCheck(otherCheck)
                   .AddRequiredAnalyzers(additionalRequiredAnalyzers)
                   .UseRepository(repository)
                   // Add the Size anomaly check
                   .AddAnomalyCheck(sizeAnomalyDetectionStrategy, Initializers.Size(), sizeAnomalyCheckConfig)
                   // Add the Mean sales anomaly check
                   .AddAnomalyCheck(meanSalesAnomalyDetectionStrategy, Initializers.Mean("sales"),
                                    meanSalesAnomalyCheckConfig)
                   // Save new data point in the repository after we calculated everything
                   .SaveOrAppendResult(currentRunResultKey)
                   .Run());
        }
        public void detect_all_anomalies_if_no_interval_specified()
        {
            var strategy = new OnlineNormalStrategy(3.5, 3.5, 0.2);

            var anomalyResult = strategy.Detect(data, (0, int.MaxValue));

            var expected = Enumerable.Range(20, 11)
                           .Select(value => (value, new Anomaly(data[value], 1.0)));

            anomalyResult.SequenceEqual(expected).ShouldBeTrue();
        }
        public void doesn_t_ignore_anomalies_in_calculation_if_not_wanted()
        {
            var strategy  = new OnlineNormalStrategy(1.5, 1.5, 0.2, false);
            var data      = new[] { 1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 1.0 };
            var lastPoint = strategy.ComputeStatsAndAnomalies(data, (0, int.MaxValue)).Last();

            var mean   = data.Average();
            var stdDev = StdDev(data);

            lastPoint.Mean.ShouldBe(mean);
            (Math.Abs(lastPoint.StdDev - stdDev) < stdDev * 0.001).ShouldBeTrue();
        }
        public void ignore_upper_factor_if_none_is_given()
        {
            var strategy = new OnlineNormalStrategy(1.5, Option <double> .None);

            var anomalyResult = strategy.Detect(data, (0, int.MaxValue));

            var expected = Enumerable
                           .Range(21, 9)
                           .Where(x => x % 2 != 0)
                           .Select(value => (value, new Anomaly(data[value], 1.0)));

            anomalyResult.SequenceEqual(expected).ShouldBeTrue();
        }
 public OnlineNormalStrategyTest()
 {
     _strategy = new OnlineNormalStrategy(1.5, 1.5, 0.2);
 }