コード例 #1
0
        public int CkmsHigh()
        {
            double[] p   = new double[] { 95 };
            int      res = 0;

            for (int i = 0; i < _allocCount; i++)
            {
                var obj = new ConstantErrorBasicCKMSBuilder(0.000001, p);
                obj.AddValue(1d);
                obj.AddValue(2d);
                res += obj.GetHashCode();
            }

            return(res);
        }
        public void should_work_with_random_uniform_distribution()
        {
            var random             = new Random();
            var desiredPercentiles = new double[] { 10, 20, 30, 40, 50, 60, 70, 80, 90 };
            var builder            = new ConstantErrorBasicCKMSBuilder(0.00001, desiredPercentiles);
            var referenceBuilder   = new NearestRankBuilder(desiredPercentiles);

            for (var i = 0; i < 10000; ++i)
            {
                var value = random.NextDouble() * 100;
                builder.AddValue(value);
                referenceBuilder.AddValue(value);
            }

            var percentiles          = builder.GetPercentiles().ToList();
            var referencePercentiles = referenceBuilder.GetPercentiles().ToList();

            Console.WriteLine("CKMS basic histogram");
            var squaredErrors = new List <double>();

            for (var i = 0; i < 9; ++i)
            {
                var deltaToPercentile = Math.Abs(percentiles[i].Value - referencePercentiles[i].Value);
                Console.WriteLine("[" + percentiles[i].Rank + "] => " + percentiles[i].Value + " (" + deltaToPercentile + ")");
                deltaToPercentile.ShouldBeLessThan(0.15);
                squaredErrors.Add(Math.Pow(deltaToPercentile, 2));
            }
            Console.WriteLine("MSE: " + squaredErrors.Average());
        }
        public void should_handle_simple_range()
        {
            var expectedPercentiles = new[]
            {
                new Percentile(10, 10),
                new Percentile(20, 20),
                new Percentile(30, 30),
                new Percentile(40, 40),
                new Percentile(50, 50),
                new Percentile(60, 60),
                new Percentile(70, 70),
                new Percentile(80, 80),
                new Percentile(90, 90)
            };
            var builder = new ConstantErrorBasicCKMSBuilder(0.001, Constants.DefaultPercentiles);

            foreach (var i in Enumerable.Range(1, 100).Shuffle())
            {
                builder.AddValue(i);
            }

            var percentiles = builder.GetPercentiles().ToList();

            percentiles.ShouldBeEquivalentTo(expectedPercentiles, true);
        }
        public void  should_handle_hard_case()
        {
            var values = new double[] { 2, 7, 1, 10, 8, 9, 3, 4, 5, 6 };
            var expectedPercentiles = new[]
            {
                new Percentile(10, 1),
                new Percentile(20, 2),
                new Percentile(30, 3),
                new Percentile(40, 4),
                new Percentile(50, 5),
                new Percentile(60, 6),
                new Percentile(70, 7),
                new Percentile(80, 8),
                new Percentile(90, 9)
            };
            var builder = new ConstantErrorBasicCKMSBuilder(0.00000001, Constants.DefaultPercentiles);

            foreach (var datum in values.ToArray())
            {
                builder.AddValue(datum);
            }

            var percentiles = builder.GetPercentiles().ToList();

            percentiles.ShouldBeEquivalentTo(expectedPercentiles, true);
        }
        public void should_work_with_sample_data(SampleFile sampleFile)
        {
            var builder = new ConstantErrorBasicCKMSBuilder(0.000000000000000001, new[] { 80, 90, 99, 99.9, 99.99, 99.999 });

            foreach (var line in File.ReadAllLines(sampleFile.Filename))
            {
                var value = double.Parse(line, CultureInfo.InvariantCulture);
                builder.AddValue(value);
            }

            builder.GetPercentiles().Select(p => (int)p.Value).ShouldBeEquivalentTo(sampleFile.ExpectedValues);
        }
        public void should_return_percentiles_for_given_data(Expectation expectation)
        {
            var builder = new ConstantErrorBasicCKMSBuilder(0.00000001, expectation.DesiredPercentiles);

            foreach (var datum in expectation.Values.Shuffle().ToArray())
            {
                builder.AddValue(datum);
            }

            var percentiles = builder.GetPercentiles().ToList();

            percentiles.ShouldBeEquivalentTo(expectation.ExpectedPercentiles, true);
        }