public static double Calculate([NotNull] double[] values) { try { var clearedValues = TukeyOutlierDetector.Create(values).WithoutAllOutliers(values).ToList(); int n = clearedValues.Count; var quartiles = Quartiles.Create(clearedValues); var moments = Moments.Create(clearedValues); double mValue = 0; double binSize = AdaptiveHistogramBuilder.GetOptimalBinSize(n, moments.StandardDeviation); if (Abs(binSize) < 1e-9) { binSize = 1; } while (true) { var histogram = HistogramBuilder.Adaptive.Build(clearedValues, binSize); var x = new List <int> { 0 }; x.AddRange(histogram.Bins.Select(bin => bin.Count)); x.Add(0); int sum = 0; for (int i = 1; i < x.Count; i++) { sum += Abs(x[i] - x[i - 1]); } mValue = Max(mValue, sum * 1.0 / x.Max()); if (binSize > quartiles.Max - quartiles.Min) { break; } binSize *= 2.0; } return(mValue); } catch (Exception) { return(1); // In case of any bugs, we return 1 because it's an invalid value (mValue is always >= 2) } }
public void Run() { var simple = SimpleQuantileEstimator.Instance; var hd = HarrellDavisQuantileEstimator.Instance; var detectors = new[] { new Detector("TukeySimple", values => TukeyOutlierDetector.Create(values, quantileEstimator: simple)), new Detector("TukeyHd", values => TukeyOutlierDetector.Create(values, quantileEstimator: hd)), new Detector("MadSimple", values => MadOutlierDetector.Create(values, quantileEstimator: simple)), new Detector("MadHd", values => MadOutlierDetector.Create(values, quantileEstimator: hd)), new Detector("DoubleMadSimple", values => DoubleMadOutlierDetector.Create(values, quantileEstimator: simple)), new Detector("DoubleMadHd", values => DoubleMadOutlierDetector.Create(values, quantileEstimator: hd)) }; Case.DumpBaseSample(); BuildTable(detectors, new[] { new Case(1, 0), new Case(2, 0), new Case(3, 0) }); BuildTable(detectors, new[] { new Case(0, 1), new Case(0, 2), new Case(0, 3) }); BuildTable(detectors, new[] { new Case(1, 1), new Case(2, 2), new Case(3, 3) }); }
public void TukeyOutlierDetectorHdQeTest([NotNull] string testDataKey) => Check(HdQeTestDataMap[testDataKey], values => TukeyOutlierDetector.Create(values, quantileEstimator: HarrellDavisQuantileEstimator.Instance));
public void TukeyOutlierDetectorSimpleQeTest([NotNull] string testDataKey) => Check(SimpleQeTestDataMap[testDataKey], values => TukeyOutlierDetector.Create(values, quantileEstimator: SimpleQuantileEstimator.Instance));