static void Benchmark(object obj, uint iterations, string suffix = null)
        {
            var bench   = new BenchShark(true);
            var result  = bench.EvaluateDecoratedTasks(obj, iterations);
            var results = result.FastestEvaluations.Select(x =>
            {
                var series = x.Iterations.Select(it => (double)it.ElapsedTicks).ToArray();
                Array.Sort(series);
                var summary = SortedArrayStatistics.FiveNumberSummary(series);
                var ms      = ArrayStatistics.MeanStandardDeviation(series);
                return(new { x.Name, Mean = ms.Item1, StdDev = ms.Item2, Min = summary[0], Q1 = summary[1], Median = summary[2], Q3 = summary[3], Max = summary[4] });
            }).ToArray();
            var top     = results[0];
            var managed = results.Single(x => x.Name.StartsWith("Managed"));
            var label   = string.IsNullOrEmpty(suffix) ? obj.GetType().FullName : string.Concat(obj.GetType().FullName, ": ", suffix);

            results.Select(x => new
            {
                x.Name,
                Mean           = Math.Round(x.Mean), StdDev = Math.Round(x.StdDev),
                Min            = Math.Round(x.Min), Q1 = Math.Round(x.Q1), Median = Math.Round(x.Median), Q3 = Math.Round(x.Q3), Max = Math.Round(x.Max),
                TopSlowdown    = Math.Round(x.Median / top.Median, 2),
                ManagedSpeedup = Math.Round(managed.Median / x.Median, 2)
            }).Dump(label);
        }
        /// <summary>
        /// Check for the interquartile range and also detect outliers
        /// </summary>
        /// <param name="data"></param>
        /// <param name="word"></param>
        static void  GetQuartilesCheck(double[] data, string word)
        {
            Array.Sort(data);

            var result = new double[5];

            result = SortedArrayStatistics.FiveNumberSummary(data);

            var min    = result[0];
            var q1     = result[1];
            var median = result[2];
            var q3     = result[3];
            var max    = result[4];

            var iqr = q3 - q1;

            var c1 = q1 - (1.5 * iqr);

            var c2 = q3 + (1.5 * iqr);

            foreach (var number in data)
            {
                if (number < c1 || number > c2)
                {
                    Console.WriteLine($"outlier detected. {word}");
                }
            }
        }
Exemple #3
0
        private void Compute()
        {
            double minValue = Math.Floor(_rawData.Min());
            double maxValue = Math.Ceiling(_rawData.Max());

            int binCount = (int)(maxValue - minValue);
            int binWidth = (int)Math.Round(binCount / 20.0);

            if (binWidth < 1)
            {
                binWidth = 1;
            }

            var histogram = new Histogram(_rawData, (binCount / binWidth) + 2, minValue - binWidth, maxValue + binWidth);

            _chartValues = new ChartValues <ObservablePoint>();

            for (int i = 0; i < histogram.BucketCount; i++)
            {
                _chartValues.Add(new ObservablePoint(
                                     (histogram[i].LowerBound + histogram[i].UpperBound) / 2.0,
                                     histogram[i].Count / _rawData.Count));
            }


            var dataArray = _rawData.ToArray();

            this.MedianValue     = SortedArrayStatistics.Median(dataArray);
            this.LowerConfidence = SortedArrayStatistics.Percentile(dataArray, 5);
            this.UpperConfidence = SortedArrayStatistics.Percentile(dataArray, 95);

            this.XMax = histogram.UpperBound;
            this.XMin = histogram.LowerBound;
        }
Exemple #4
0
        private static void UniformDistributionTest(double[] sampleArr, double lowerBound, double upperBound)
        {
            Array.Sort(sampleArr);
            RunningStatistics runningStats = new(sampleArr);

            // Skewness should be pretty close to zero (evenly distributed samples)
            Assert.True(Math.Abs(runningStats.Skewness) < 0.01);

            // Mean test.
            double range          = upperBound - lowerBound;
            double expectedMean   = lowerBound + (range / 2.0);
            double meanErr        = expectedMean - runningStats.Mean;
            double maxExpectedErr = range / 1000.0;

            Assert.True(Math.Abs(meanErr) < maxExpectedErr);

            // Test a range of centile/quantile values.
            for (double tau = 0; tau <= 1.0; tau += 0.1)
            {
                double quantile         = SortedArrayStatistics.Quantile(sampleArr, tau);
                double expectedQuantile = lowerBound + (tau * range);
                double quantileError    = expectedQuantile - quantile;

                Assert.True(Math.Abs(quantileError) < maxExpectedErr);
            }
        }
Exemple #5
0
        public static void UniformDistributionTest(
            double[] sampleArr, double minValue, double maxValue)
        {
            Array.Sort(sampleArr);
            RunningStatistics runningStats = new RunningStatistics(sampleArr);

            // Skewness should be pretty close to zero (evenly distributed samples)
            Assert.True(Math.Abs(runningStats.Skewness) <= 0.01);

            // Mean test.
            double range          = maxValue - minValue;
            double expectedMean   = minValue + (range / 2.0);
            double meanErr        = expectedMean - runningStats.Mean;
            double maxExpectedErr = range / 1000.0;

            Assert.True(Math.Abs(meanErr) <= maxExpectedErr);

            // Test a range of centile/quantile values.
            for (double tau = 0; tau <= 1.0; tau += 0.1)
            {
                double quantile         = SortedArrayStatistics.Quantile(sampleArr, tau);
                double expectedQuantile = minValue + (tau * range);
                double quantileError    = expectedQuantile - quantile;
                Assert.True(Math.Abs(quantileError) <= maxExpectedErr);
            }

            // Test that no samples are outside the defined range.
            for (int i = 0; i < sampleArr.Length; i++)
            {
                Assert.True(sampleArr[i] >= minValue && sampleArr[i] < maxValue);
            }
        }
Exemple #6
0
        public void ThrowsOnNullData()
        {
            double[] data = null;

            // ReSharper disable InvokeAsExtensionMethod
            Assert.That(() => Statistics.Minimum(data), Throws.Exception);
            Assert.That(() => Statistics.Maximum(data), Throws.Exception);
            Assert.That(() => Statistics.Mean(data), Throws.Exception);
            Assert.That(() => Statistics.Median(data), Throws.Exception);
            Assert.That(() => Statistics.Quantile(data, 0.3), Throws.Exception);
            Assert.That(() => Statistics.Variance(data), Throws.Exception);
            Assert.That(() => Statistics.StandardDeviation(data), Throws.Exception);
            Assert.That(() => Statistics.PopulationVariance(data), Throws.Exception);
            Assert.That(() => Statistics.PopulationStandardDeviation(data), Throws.Exception);
            Assert.That(() => Statistics.Covariance(data, data), Throws.Exception);
            Assert.That(() => Statistics.PopulationCovariance(data, data), Throws.Exception);
            // ReSharper restore InvokeAsExtensionMethod

            Assert.That(() => SortedArrayStatistics.Minimum(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.Minimum(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.Maximum(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.OrderStatistic(data, 1), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.Median(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.LowerQuartile(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.UpperQuartile(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.Percentile(data, 30), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.Quantile(data, 0.3), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.QuantileCustom(data, 0.3, 0, 0, 1, 0), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.QuantileCustom(data, 0.3, QuantileDefinition.Nearest), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.InterquartileRange(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.FiveNumberSummary(data), Throws.Exception.TypeOf <NullReferenceException>());

            Assert.That(() => ArrayStatistics.Minimum(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => ArrayStatistics.Maximum(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => ArrayStatistics.OrderStatisticInplace(data, 1), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => ArrayStatistics.Mean(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => ArrayStatistics.Variance(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => ArrayStatistics.StandardDeviation(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => ArrayStatistics.PopulationVariance(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => ArrayStatistics.PopulationStandardDeviation(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => ArrayStatistics.Covariance(data, data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => ArrayStatistics.PopulationCovariance(data, data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => ArrayStatistics.MedianInplace(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => ArrayStatistics.QuantileInplace(data, 0.3), Throws.Exception.TypeOf <NullReferenceException>());

            Assert.That(() => StreamingStatistics.Minimum(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => StreamingStatistics.Maximum(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => StreamingStatistics.Mean(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => StreamingStatistics.Variance(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => StreamingStatistics.StandardDeviation(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => StreamingStatistics.PopulationVariance(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => StreamingStatistics.PopulationStandardDeviation(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => StreamingStatistics.Covariance(data, data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => StreamingStatistics.PopulationCovariance(data, data), Throws.Exception.TypeOf <NullReferenceException>());
        }
    static void Main()
    {
        double[] numbers = new double[] { 1, 2, 3, 4, 5 };
        double   a       = SortedArrayStatistics.Minimum(numbers);
        double   b       = SortedArrayStatistics.LowerQuartile(numbers);
        double   c       = SortedArrayStatistics.Median(numbers);
        double   d       = SortedArrayStatistics.UpperQuartile(numbers);
        double   e       = SortedArrayStatistics.Maximum(numbers);

        Console.WriteLine("{0}\n{1}\n{2}\n{3}\n{4}\n", a, b, c, d, e);
    }
Exemple #8
0
 public void MaximumOfEmptyMustBeNaN()
 {
     Assert.That(Statistics.Maximum(new double[0]), Is.NaN);
     Assert.That(Statistics.Maximum(new[] { 2d }), Is.Not.NaN);
     Assert.That(ArrayStatistics.Maximum(new double[0]), Is.NaN);
     Assert.That(ArrayStatistics.Maximum(new[] { 2d }), Is.Not.NaN);
     Assert.That(SortedArrayStatistics.Maximum(new double[0]), Is.NaN);
     Assert.That(SortedArrayStatistics.Maximum(new[] { 2d }), Is.Not.NaN);
     Assert.That(StreamingStatistics.Maximum(new double[0]), Is.NaN);
     Assert.That(StreamingStatistics.Maximum(new[] { 2d }), Is.Not.NaN);
 }
Exemple #9
0
        public void ThrowsOnNullData()
        {
            double[] data = null;

            Assert.Throws <ArgumentNullException>(() => Statistics.Minimum(data));
            Assert.Throws <ArgumentNullException>(() => Statistics.Maximum(data));
            Assert.Throws <ArgumentNullException>(() => Statistics.Mean(data));
            Assert.Throws <ArgumentNullException>(() => Statistics.Median(data));
            Assert.Throws <ArgumentNullException>(() => Statistics.Quantile(data, 0.3));
            Assert.Throws <ArgumentNullException>(() => Statistics.Variance(data));
            Assert.Throws <ArgumentNullException>(() => Statistics.StandardDeviation(data));
            Assert.Throws <ArgumentNullException>(() => Statistics.PopulationVariance(data));
            Assert.Throws <ArgumentNullException>(() => Statistics.PopulationStandardDeviation(data));
            Assert.Throws <ArgumentNullException>(() => Statistics.Covariance(data, data));
            Assert.Throws <ArgumentNullException>(() => Statistics.PopulationCovariance(data, data));

            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.Minimum(data));
            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.Maximum(data));
            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.OrderStatistic(data, 1));
            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.Median(data));
            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.LowerQuartile(data));
            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.UpperQuartile(data));
            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.Percentile(data, 30));
            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.Quantile(data, 0.3));
            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.QuantileCustom(data, 0.3, 0, 0, 1, 0));
            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.QuantileCustom(data, 0.3, QuantileDefinition.Nearest));
            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.InterquartileRange(data));
            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.FiveNumberSummary(data));

            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.Minimum(data));
            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.Maximum(data));
            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.OrderStatisticInplace(data, 1));
            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.Mean(data));
            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.Variance(data));
            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.StandardDeviation(data));
            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.PopulationVariance(data));
            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.PopulationStandardDeviation(data));
            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.Covariance(data, data));
            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.PopulationCovariance(data, data));
            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.MedianInplace(data));
            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.QuantileInplace(data, 0.3));

            Assert.Throws <ArgumentNullException>(() => StreamingStatistics.Minimum(data));
            Assert.Throws <ArgumentNullException>(() => StreamingStatistics.Maximum(data));
            Assert.Throws <ArgumentNullException>(() => StreamingStatistics.Mean(data));
            Assert.Throws <ArgumentNullException>(() => StreamingStatistics.Variance(data));
            Assert.Throws <ArgumentNullException>(() => StreamingStatistics.StandardDeviation(data));
            Assert.Throws <ArgumentNullException>(() => StreamingStatistics.PopulationVariance(data));
            Assert.Throws <ArgumentNullException>(() => StreamingStatistics.PopulationStandardDeviation(data));
            Assert.Throws <ArgumentNullException>(() => StreamingStatistics.Covariance(data, data));
            Assert.Throws <ArgumentNullException>(() => StreamingStatistics.PopulationCovariance(data, data));
        }
Exemple #10
0
        public void Median_CodeplexIssue5667()
        {
            var seq = File.ReadLines("./data/Codeplex-5667.csv").Select(double.Parse);

            Assert.AreEqual(1.0, Statistics.Median(seq));

            var array = seq.ToArray();

            Assert.AreEqual(1.0, ArrayStatistics.MedianInplace(array));

            Array.Sort(array);
            Assert.AreEqual(1.0, SortedArrayStatistics.Median(array));
        }
Exemple #11
0
        public void MinimumMaximumOnShortSequence()
        {
            var samples = new[] { -1.0, 5, 0, -3, 10, -0.5, 4 };

            Assert.That(Statistics.Minimum(samples), Is.EqualTo(-3), "Min");
            Assert.That(Statistics.Maximum(samples), Is.EqualTo(10), "Max");
            Assert.That(ArrayStatistics.Minimum(samples), Is.EqualTo(-3), "Min");
            Assert.That(ArrayStatistics.Maximum(samples), Is.EqualTo(10), "Max");
            Assert.That(StreamingStatistics.Minimum(samples), Is.EqualTo(-3), "Min");
            Assert.That(StreamingStatistics.Maximum(samples), Is.EqualTo(10), "Max");

            Array.Sort(samples);
            Assert.That(SortedArrayStatistics.Minimum(samples), Is.EqualTo(-3), "Min");
            Assert.That(SortedArrayStatistics.Maximum(samples), Is.EqualTo(10), "Max");
        }
Exemple #12
0
        public void QuantileR2InverseCDFAverageOnShortSequence(double tau, double expected)
        {
            // R: quantile(c(-1,5,0,-3,10,-0.5,4,0.2,1,6),probs=c(0,1,0.5,0.2,0.7,0.01,0.99,0.52,0.325),type=2)
            // Mathematica: Not Supported

            var samples = new[] { -1, 5, 0, -3, 10, -0.5, 4, 0.2, 1, 6 };

            Assert.AreEqual(expected, Statistics.QuantileCustom(samples, tau, QuantileDefinition.R2), 1e-14);
            Assert.AreEqual(expected, Statistics.QuantileCustomFunc(samples, QuantileDefinition.R2)(tau), 1e-14);

            Assert.AreEqual(expected, ArrayStatistics.QuantileCustomInplace(samples, tau, QuantileDefinition.InverseCDFAverage), 1e-14);

            Array.Sort(samples);
            Assert.AreEqual(expected, SortedArrayStatistics.QuantileCustom(samples, tau, QuantileDefinition.InverseCDFAverage), 1e-14);
        }
Exemple #13
0
        public void QuantileR9NormalOnShortSequence(double tau, double expected)
        {
            // R: quantile(c(-1,5,0,-3,10,-0.5,4,0.2,1,6),probs=c(0,1,0.5,0.2,0.7,0.01,0.99,0.52,0.325),type=9)
            // Mathematica: Quantile[{-1,5,0,-3,10,-1/2,4,1/5,1,6},{0,1,1/2,1/5,7/10,1/100,99/100,13/25,13/40},{{3/8,1/4},{0,1}}]
            var samples = new[] { -1, 5, 0, -3, 10, -0.5, 4, 0.2, 1, 6 };

            Assert.AreEqual(expected, Statistics.QuantileCustom(samples, tau, QuantileDefinition.R9), 1e-14);
            Assert.AreEqual(expected, Statistics.QuantileCustomFunc(samples, QuantileDefinition.R9)(tau), 1e-14);

            Assert.AreEqual(expected, ArrayStatistics.QuantileCustomInplace(samples, tau, QuantileDefinition.Normal), 1e-14);
            Assert.AreEqual(expected, ArrayStatistics.QuantileCustomInplace(samples, tau, 3 / 8d, 1 / 4d, 0d, 1d), 1e-14);

            Array.Sort(samples);
            Assert.AreEqual(expected, SortedArrayStatistics.QuantileCustom(samples, tau, QuantileDefinition.Normal), 1e-14);
            Assert.AreEqual(expected, SortedArrayStatistics.QuantileCustom(samples, tau, 3 / 8d, 1 / 4d, 0d, 1d), 1e-14);
        }
Exemple #14
0
        public void DoesNotThrowOnEmptyData()
        {
            double[] data = new double[0];

            Assert.DoesNotThrow(() => Statistics.Minimum(data));
            Assert.DoesNotThrow(() => Statistics.Maximum(data));
            Assert.DoesNotThrow(() => Statistics.Mean(data));
            Assert.DoesNotThrow(() => Statistics.Median(data));
            Assert.DoesNotThrow(() => Statistics.Quantile(data, 0.3));
            Assert.DoesNotThrow(() => Statistics.Variance(data));
            Assert.DoesNotThrow(() => Statistics.StandardDeviation(data));
            Assert.DoesNotThrow(() => Statistics.PopulationVariance(data));
            Assert.DoesNotThrow(() => Statistics.PopulationStandardDeviation(data));

            Assert.DoesNotThrow(() => SortedArrayStatistics.Minimum(data));
            Assert.DoesNotThrow(() => SortedArrayStatistics.Maximum(data));
            Assert.DoesNotThrow(() => SortedArrayStatistics.OrderStatistic(data, 1));
            Assert.DoesNotThrow(() => SortedArrayStatistics.Median(data));
            Assert.DoesNotThrow(() => SortedArrayStatistics.LowerQuartile(data));
            Assert.DoesNotThrow(() => SortedArrayStatistics.UpperQuartile(data));
            Assert.DoesNotThrow(() => SortedArrayStatistics.Percentile(data, 30));
            Assert.DoesNotThrow(() => SortedArrayStatistics.Quantile(data, 0.3));
            Assert.DoesNotThrow(() => SortedArrayStatistics.QuantileCustom(data, 0.3, 0, 0, 1, 0));
            Assert.DoesNotThrow(() => SortedArrayStatistics.QuantileCustom(data, 0.3, QuantileDefinition.Nearest));
            Assert.DoesNotThrow(() => SortedArrayStatistics.InterquartileRange(data));
            Assert.DoesNotThrow(() => SortedArrayStatistics.FiveNumberSummary(data));

            Assert.DoesNotThrow(() => ArrayStatistics.Minimum(data));
            Assert.DoesNotThrow(() => ArrayStatistics.Maximum(data));
            Assert.DoesNotThrow(() => ArrayStatistics.OrderStatisticInplace(data, 1));
            Assert.DoesNotThrow(() => ArrayStatistics.Mean(data));
            Assert.DoesNotThrow(() => ArrayStatistics.Variance(data));
            Assert.DoesNotThrow(() => ArrayStatistics.StandardDeviation(data));
            Assert.DoesNotThrow(() => ArrayStatistics.PopulationVariance(data));
            Assert.DoesNotThrow(() => ArrayStatistics.PopulationStandardDeviation(data));
            Assert.DoesNotThrow(() => ArrayStatistics.MedianInplace(data));
            Assert.DoesNotThrow(() => ArrayStatistics.QuantileInplace(data, 0.3));

            Assert.DoesNotThrow(() => StreamingStatistics.Minimum(data));
            Assert.DoesNotThrow(() => StreamingStatistics.Maximum(data));
            Assert.DoesNotThrow(() => StreamingStatistics.Mean(data));
            Assert.DoesNotThrow(() => StreamingStatistics.Variance(data));
            Assert.DoesNotThrow(() => StreamingStatistics.StandardDeviation(data));
            Assert.DoesNotThrow(() => StreamingStatistics.PopulationVariance(data));
            Assert.DoesNotThrow(() => StreamingStatistics.PopulationStandardDeviation(data));
        }
Exemple #15
0
        public void OrderStatisticsOnShortSequence()
        {
            // -3 -1 -0.5 0  1  4 5 6 10
            var samples = new[] { -1, 5, 0, -3, 10, -0.5, 4, 1, 6 };

            var f = Statistics.OrderStatisticFunc(samples);

            Assert.That(f(0), Is.NaN, "Order-0 (bad)");
            Assert.That(f(1), Is.EqualTo(-3), "Order-1");
            Assert.That(f(2), Is.EqualTo(-1), "Order-2");
            Assert.That(f(3), Is.EqualTo(-0.5), "Order-3");
            Assert.That(f(7), Is.EqualTo(5), "Order-7");
            Assert.That(f(8), Is.EqualTo(6), "Order-8");
            Assert.That(f(9), Is.EqualTo(10), "Order-9");
            Assert.That(f(10), Is.NaN, "Order-10 (bad)");

            Assert.That(Statistics.OrderStatistic(samples, 0), Is.NaN, "Order-0 (bad)");
            Assert.That(Statistics.OrderStatistic(samples, 1), Is.EqualTo(-3), "Order-1");
            Assert.That(Statistics.OrderStatistic(samples, 2), Is.EqualTo(-1), "Order-2");
            Assert.That(Statistics.OrderStatistic(samples, 3), Is.EqualTo(-0.5), "Order-3");
            Assert.That(Statistics.OrderStatistic(samples, 7), Is.EqualTo(5), "Order-7");
            Assert.That(Statistics.OrderStatistic(samples, 8), Is.EqualTo(6), "Order-8");
            Assert.That(Statistics.OrderStatistic(samples, 9), Is.EqualTo(10), "Order-9");
            Assert.That(Statistics.OrderStatistic(samples, 10), Is.NaN, "Order-10 (bad)");

            Assert.That(ArrayStatistics.OrderStatisticInplace(samples, 0), Is.NaN, "Order-0 (bad)");
            Assert.That(ArrayStatistics.OrderStatisticInplace(samples, 1), Is.EqualTo(-3), "Order-1");
            Assert.That(ArrayStatistics.OrderStatisticInplace(samples, 2), Is.EqualTo(-1), "Order-2");
            Assert.That(ArrayStatistics.OrderStatisticInplace(samples, 3), Is.EqualTo(-0.5), "Order-3");
            Assert.That(ArrayStatistics.OrderStatisticInplace(samples, 7), Is.EqualTo(5), "Order-7");
            Assert.That(ArrayStatistics.OrderStatisticInplace(samples, 8), Is.EqualTo(6), "Order-8");
            Assert.That(ArrayStatistics.OrderStatisticInplace(samples, 9), Is.EqualTo(10), "Order-9");
            Assert.That(ArrayStatistics.OrderStatisticInplace(samples, 10), Is.NaN, "Order-10 (bad)");

            Array.Sort(samples);
            Assert.That(SortedArrayStatistics.OrderStatistic(samples, 0), Is.NaN, "Order-0 (bad)");
            Assert.That(SortedArrayStatistics.OrderStatistic(samples, 1), Is.EqualTo(-3), "Order-1");
            Assert.That(SortedArrayStatistics.OrderStatistic(samples, 2), Is.EqualTo(-1), "Order-2");
            Assert.That(SortedArrayStatistics.OrderStatistic(samples, 3), Is.EqualTo(-0.5), "Order-3");
            Assert.That(SortedArrayStatistics.OrderStatistic(samples, 7), Is.EqualTo(5), "Order-7");
            Assert.That(SortedArrayStatistics.OrderStatistic(samples, 8), Is.EqualTo(6), "Order-8");
            Assert.That(SortedArrayStatistics.OrderStatistic(samples, 9), Is.EqualTo(10), "Order-9");
            Assert.That(SortedArrayStatistics.OrderStatistic(samples, 10), Is.NaN, "Order-10 (bad)");
        }
Exemple #16
0
        public static void TestDistribution(IGaussianDistribution <double> dist, double mean, double stdDev)
        {
            // Take a set of samples.
            const int sampleCount = 10_000_000;

            double[] sampleArr = new double[sampleCount];

            for (int i = 0; i < sampleCount; i++)
            {
                sampleArr[i] = dist.Sample();
            }

            // Sort the ample so that we can use SortedArrayStatistics.
            Array.Sort(sampleArr);

            //// Test a range of centile/quantile values.
            double lowerBound = -5;
            double upperBound = 5;

            double tauStep = (upperBound - lowerBound) / 30.0;

            for (double tau = 0; tau <= 1.0; tau += 0.1)
            {
                // Notes.
                // Here we calc the tau'th quartile over a range of values in he interval [0,1],
                // the resulting quantile is the sample value (and CDF x-axis value) at which the
                // CDF crosses tau on the y-axis.
                //
                // We then take that sample x-axis value, pass it through the CDF function for the
                // gaussian to obtain the expected y value at that x, and compare with tau.

                // Determine the x value at which tau (as a proportion) of samples are <= x.
                double sample_x = SortedArrayStatistics.Quantile(sampleArr, tau);

                // Put sample_x into the gaussian CDF function, to obtain a CDF y coord..
                double cdf_y = 0.5 * SpecialFunctions.Erfc((mean - sample_x) / (stdDev * Constants.Sqrt2));

                // Compare the expected and actual CDF y values.
                double y_error = Math.Abs(tau - cdf_y);
                Assert.IsTrue(y_error < 0.0005);
            }
        }
Exemple #17
0
        public void MedianOnShortSequence()
        {
            // R: median(c(-1,5,0,-3,10,-0.5,4,0.2,1,6))
            // Mathematica: Median[{-1,5,0,-3,10,-1/2,4,1/5,1,6}]
            var even = new[] { -1, 5, 0, -3, 10, -0.5, 4, 0.2, 1, 6 };

            Assert.AreEqual(0.6d, Statistics.Median(even), 1e-14);
            Assert.AreEqual(0.6d, ArrayStatistics.MedianInplace(even), 1e-14);
            Array.Sort(even);
            Assert.AreEqual(0.6d, SortedArrayStatistics.Median(even), 1e-14);

            // R: median(c(-1,5,0,-3,10,-0.5,4,0.2,1))
            // Mathematica: Median[{-1,5,0,-3,10,-1/2,4,1/5,1}]
            var odd = new[] { -1, 5, 0, -3, 10, -0.5, 4, 0.2, 1 };

            Assert.AreEqual(0.2d, Statistics.Median(odd), 1e-14);
            Assert.AreEqual(0.2d, ArrayStatistics.MedianInplace(odd), 1e-14);
            Array.Sort(even);
            Assert.AreEqual(0.2d, SortedArrayStatistics.Median(odd), 1e-14);
        }
Exemple #18
0
        /// <summary>
        /// 稳健算法Qn
        /// </summary>
        /// <param name="array">为要传入的数组</param>
        /// <returns></returns>
        public static double RobustAnalysisS(double[] array, int type, int resultNum = 0, double eps = 1e-7)
        {
            int    v      = type == 0 ? 1 : resultNum - 1; //自由度
            double eta    = etas[v - 1];                   // η
            double xi     = xis[v - 1];                    //ξ
            double w_init = SortedArrayStatistics.Median(array);
            double w_t    = w_init;
            double w_t_old;
            double psi;//ψ
            double w_t_sum;
            int    step = 0;

            do
            {
                double[] tempArray = array.Clone() as double[];
                step++;
                //Console.WriteLine(step);

                w_t_sum = 0;
                psi     = eta * w_t;
                // WriteLine(psi); // 输出ψ
                for (int i = 0; i < tempArray.Length; i++)
                {
                    if (tempArray[i] > psi)
                    {
                        tempArray[i] = psi;
                    }
                    w_t_sum += tempArray[i] * tempArray[i];
                }
                w_t_old = w_t;
                w_t     = xi * Math.Sqrt(w_t_sum / tempArray.Length); // 计算 w*
            }while (Math.Abs(w_t - w_t_old) > eps);                   // 比较这次w*的变化

            // WriteLine(w_t); // 输出w*

            return(w_t);
        }
        public static void TestDistribution(ISampler <float> sampler, float mean, float stdDev)
        {
            // Take a set of samples.
            const int sampleCount = 10_000_000;

            float[] sampleArr = new float[sampleCount];

            for (int i = 0; i < sampleCount; i++)
            {
                sampleArr[i] = sampler.Sample();
            }

            // Sort the samples so that we can use SortedArrayStatistics.
            Array.Sort(sampleArr);

            for (float tau = 0; tau <= 1f; tau += 0.1f)
            {
                // Notes.
                // Here we calc the tau'th quartile over a range of values in he interval [0,1],
                // the resulting quantile is the sample value (and CDF x-axis value) at which the
                // CDF crosses tau on the y-axis.
                //
                // We then take that sample x-axis value, pass it through the CDF function for the
                // Gaussian to obtain the expected y value at that x, and compare with tau.

                // Determine the x value at which tau (as a proportion) of samples are <= x.
                float sample_x = SortedArrayStatistics.Quantile(sampleArr, tau);

                // Put sample_x into the Gaussian CDF function, to obtain a CDF y coord..
                double cdf_y = (0.5 * SpecialFunctions.Erfc((mean - sample_x) / (stdDev * Constants.Sqrt2)));

                // Compare the expected and actual CDF y values.
                double y_error = Math.Abs(tau - cdf_y);
                Assert.True(y_error < 0.0005);
            }
        }