Esempio n. 1
0
        public static double CalculateMValue([NotNull] Statistics originalStatistics)
        {
            try
            {
                var s = new Statistics(originalStatistics.WithoutOutliers());

                double mValue = 0;

                double binSize = s.GetOptimalBinSize();
                if (Abs(binSize) < 1e-9)
                {
                    binSize = 1;
                }
                while (true)
                {
                    var histogram = HistogramBuilder.Adaptive.BuildWithFixedBinSize(s.GetValues(), 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 > s.Max - s.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)
            }
        }