Example #1
0
        public static Quartiles FromUnsorted(IReadOnlyList <double> values)
        {
            QuantileEstimatorHelper.CheckArguments(values, 0);
            var sortedValues = values.CopyToArray();

            Array.Sort(sortedValues);
            return(FromSorted(sortedValues));
        }
Example #2
0
        public static Quartiles FromSorted([NotNull] IReadOnlyList <double> values)
        {
            QuantileEstimatorHelper.CheckArguments(values, 0);

            double GetQuantile(double q) => SimpleQuantileEstimator.Instance.GetQuantileFromSorted(values, q);

            double q0 = GetQuantile(0.00);
            double q1 = GetQuantile(0.25);
            double q2 = GetQuantile(0.50);
            double q3 = GetQuantile(0.75);
            double q4 = GetQuantile(1.00);

            return(new Quartiles(q0, q1, q2, q3, q4));
        }
        /// <summary>
        /// Calculates the requested quantile from the set of values
        /// </summary>
        /// <remarks>
        /// The implementation is expected to be consistent with the one from Excel.
        /// It's a quite common to export bench output into .csv for further analysis
        /// And it's a good idea to have same results from all tools being used.
        /// </remarks>
        /// <param name="data">Sequence of the values to be calculated</param>
        /// <param name="probability">Value in range [0;1]</param>
        /// <returns>Quantile from the set of values</returns>
        // Based on: http://stackoverflow.com/a/8137526
        public double GetQuantileFromSorted(IReadOnlyList <double> data, double probability)
        {
            QuantileEstimatorHelper.CheckArguments(data, probability);

            // DONTTOUCH: the following code was taken from http://stackoverflow.com/a/8137526 and it is proven
            // to work in the same way the excel's counterpart does.
            // So it's better to leave it as it is unless you do not want to reimplement it from scratch:)
            double realIndex = probability * (data.Count - 1);
            int    index     = (int)realIndex;
            double frac      = realIndex - index;

            if (index + 1 < data.Count)
            {
                return(data[index] * (1 - frac) + data[index + 1] * frac);
            }
            return(data[index]);
        }
Example #4
0
        public double GetQuantileFromSorted(IReadOnlyList <double> data, double probability)
        {
            QuantileEstimatorHelper.CheckArguments(data, probability);

            int    n = data.Count;
            double a = (n + 1) * probability, b = (n + 1) * (1 - probability);
            var    distribution = new BetaDistribution(a, b);

            double result       = 0;
            double betaCdfRight = 0;

            for (int j = 0; j < data.Count; j++)
            {
                double betaCdfLeft = betaCdfRight;
                betaCdfRight = distribution.Cdf((j + 1) * 1.0 / n);
                result      += (betaCdfRight - betaCdfLeft) * data[j];
            }
            return(result);
        }