Esempio n. 1
0
        public SimpleMovingQuantileEstimator(int windowSize, int k,
                                             MovingQuantileEstimatorInitStrategy initStrategy = MovingQuantileEstimatorInitStrategy.QuantileApproximation)
        {
            Assertion.Positive(nameof(windowSize), windowSize);
            Assertion.InRangeInclusive(nameof(k), k, 0, windowSize - 1);

            this.windowSize   = windowSize;
            this.k            = k;
            this.initStrategy = initStrategy;
            values            = new double[windowSize];
        }
        private void DoTest(ISequentialQuantileEstimator estimator, MovingQuantileEstimatorInitStrategy initStrategy, int totalElementCount,
                            int windowSize, int k,
                            Func <int, double> generator)
        {
            double[] source = Enumerable.Range(0, totalElementCount).Select(generator).ToArray();

            var outputBuilder = new StringBuilder();

            for (int i = 0; i < source.Length; i++)
            {
                double[] windowElements = source.Take(i + 1).TakeLast(windowSize).ToArray();
                estimator.Add(source[i]);

                if (DiagnosticsMode)
                {
                    outputBuilder.AppendLine($"i = {i}");
                    outputBuilder.AppendLine(
                        $"Data = [{string.Join(", ", windowElements.Select(x => x.ToString(TestCultureInfo.Instance)))}]");
                    if (estimator is PartitioningHeapsMovingQuantileEstimator partitioningHeapsMovingQuantileEstimator)
                    {
                        outputBuilder.AppendLine($"Heap = [{partitioningHeapsMovingQuantileEstimator.Dump()}]");
                    }
                    outputBuilder.AppendLine();
                }

                if (initStrategy == MovingQuantileEstimatorInitStrategy.OrderStatistics && k >= windowElements.Length)
                {
                    Assert.Throws <IndexOutOfRangeException>(() => estimator.GetQuantile());
                }
                else
                {
                    double actual = estimator.GetQuantile();
                    Array.Sort(windowElements);
                    double expected = initStrategy switch
                    {
                        MovingQuantileEstimatorInitStrategy.QuantileApproximation => windowElements[windowElements.Length * k / windowSize],
                        MovingQuantileEstimatorInitStrategy.OrderStatistics => windowElements[Math.Min(windowElements.Length - 1, k)],
                        _ => throw new ArgumentOutOfRangeException()
                    };
                    Assert.Equal(expected, actual);
                }
            }

            if (DiagnosticsMode)
            {
                Output.WriteLine(outputBuilder.ToString());
            }
        }
    }
        public PartitioningHeapsMovingQuantileEstimator(int windowSize, int k,
                                                        MovingQuantileEstimatorInitStrategy initStrategy = MovingQuantileEstimatorInitStrategy.QuantileApproximation)
        {
            Assertion.Positive(nameof(windowSize), windowSize);
            Assertion.InRangeInclusive(nameof(k), k, 0, windowSize - 1);

            this.windowSize = windowSize;
            this.k          = k;
            probability     = Probability.NaN;
            h = new double[windowSize];
            heapToElementIndex = new int[windowSize];
            elementToHeapIndex = new int[windowSize];

            lowerHeapMaxSize  = k;
            this.initStrategy = initStrategy;
            rootHeapIndex     = k;
        }
Esempio n. 4
0
 protected override ISequentialQuantileEstimator CreateEstimator(int windowSize, int k,
                                                                 MovingQuantileEstimatorInitStrategy initStrategy)
 {
     return(new PartitioningHeapsMovingQuantileEstimator(windowSize, k, initStrategy));
 }
 protected abstract ISequentialQuantileEstimator CreateEstimator(int windowSize, int k,
                                                                 MovingQuantileEstimatorInitStrategy initStrategy);