Class Quantile


  • public final class Quantile
    extends Object
    Provides quantile computation.

    For values of length n:

    • The result is NaN if n = 0.
    • The result is values[0] if n = 1.
    • Otherwise the result is computed using the Quantile.EstimationMethod.

    Computation of multiple quantiles and will handle duplicate and unordered probabilities. Passing ordered probabilities is recommended if the order is already known as this can improve efficiency; for example using uniform spacing through the array data, or to identify extreme values from the data such as [0.001, 0.999].

    This implementation respects the ordering imposed by Double.compare(double, double) for NaN values. If a NaN occurs in the selected positions in the fully sorted values then the result is NaN.

    The NaNPolicy can be used to change the behaviour on NaN values.

    Instances of this class are immutable and thread-safe.

    Since:
    1.1
    See Also:
    with(NaNPolicy), Quantile (Wikipedia)
    • Method Detail

      • withCopy

        public Quantile withCopy​(boolean v)
        Return an instance with the configured copy behaviour. If false then the input array will be modified by the call to evaluate the quantiles; otherwise the computation uses a copy of the data.
        Parameters:
        v - Value.
        Returns:
        an instance
      • with

        public Quantile with​(NaNPolicy v)
        Return an instance with the configured NaNPolicy.

        Note: This implementation respects the ordering imposed by Double.compare(double, double) for NaN values: NaN is considered greater than all other values, and all NaN values are equal. The NaNPolicy changes the computation of the statistic in the presence of NaN values.

        • NaNPolicy.INCLUDE: NaN values are moved to the end of the data; the size of the data includes the NaN values and the quantile will be NaN if any value used for quantile interpolation is NaN.
        • NaNPolicy.EXCLUDE: NaN values are moved to the end of the data; the size of the data excludes the NaN values and the quantile will never be NaN for non-zero size. If all data are NaN then the size is zero and the result is NaN.
        • NaNPolicy.ERROR: An exception is raised if the data contains NaN values.

        Note that the result is identical for all policies if no NaN values are present.

        Parameters:
        v - Value.
        Returns:
        an instance
      • probabilities

        public static double[] probabilities​(int n)
        Generate n evenly spaced probabilities in the range [0, 1].
         1/(n + 1), 2/(n + 1), ..., n/(n + 1)
         
        Parameters:
        n - Number of probabilities.
        Returns:
        the probabilities
        Throws:
        IllegalArgumentException - if n < 1
      • probabilities

        public static double[] probabilities​(int n,
                                             double p1,
                                             double p2)
        Generate n evenly spaced probabilities in the range [p1, p2].
         w = p2 - p1
         p1 + w/(n + 1), p1 + 2w/(n + 1), ..., p1 + nw/(n + 1)
         
        Parameters:
        n - Number of probabilities.
        p1 - Lower probability.
        p2 - Upper probability.
        Returns:
        the probabilities
        Throws:
        IllegalArgumentException - if n < 1; if the probabilities are not in the range [0, 1]; or p2 <= p1.
      • evaluate

        public double evaluate​(double[] values,
                               double p)
        Evaluate the p-th quantile of the values.

        Note: This method may partially sort the input values if not configured to copy the input data.

        Performance

        It is not recommended to use this method for repeat calls for different quantiles within the same values. The evaluate(double[], double...) method should be used which provides better performance.

        Parameters:
        values - Values.
        p - Probability for the quantile to compute.
        Returns:
        the quantile
        Throws:
        IllegalArgumentException - if the probability p is not in the range [0, 1]
        See Also:
        evaluate(double[], double...)
      • evaluate

        public double[] evaluate​(double[] values,
                                 double... p)
        Evaluate the p-th quantiles of the values.

        Note: This method may partially sort the input values if not configured to copy the input data.

        Parameters:
        values - Values.
        p - Probabilities for the quantiles to compute.
        Returns:
        the quantiles
        Throws:
        IllegalArgumentException - if any probability p is not in the range [0, 1]; or no probabilities are specified.
      • evaluate

        public double evaluate​(int[] values,
                               double p)
        Evaluate the p-th quantile of the values.

        Note: This method may partially sort the input values if not configured to copy the input data.

        Performance

        It is not recommended to use this method for repeat calls for different quantiles within the same values. The evaluate(int[], double...) method should be used which provides better performance.

        Parameters:
        values - Values.
        p - Probability for the quantile to compute.
        Returns:
        the quantile
        Throws:
        IllegalArgumentException - if the probability p is not in the range [0, 1]
        See Also:
        evaluate(int[], double...)
      • evaluate

        public double[] evaluate​(int[] values,
                                 double... p)
        Evaluate the p-th quantiles of the values.

        Note: This method may partially sort the input values if not configured to copy the input data.

        Parameters:
        values - Values.
        p - Probabilities for the quantiles to compute.
        Returns:
        the quantiles
        Throws:
        IllegalArgumentException - if any probability p is not in the range [0, 1]; or no probabilities are specified.
      • evaluate

        public double evaluate​(int n,
                               IntToDoubleFunction values,
                               double p)
        Evaluate the p-th quantile of the values.

        This method can be used when the values of known size are already sorted.

        
         short[] x = ...
         Arrays.sort(x);
         double q = Quantile.withDefaults().evaluate(x.length, i -> x[i], 0.05);
         
        Parameters:
        n - Size of the values.
        values - Values function.
        p - Probability for the quantile to compute.
        Returns:
        the quantile
        Throws:
        IllegalArgumentException - if size < 0; or if the probability p is not in the range [0, 1].
      • evaluate

        public double[] evaluate​(int n,
                                 IntToDoubleFunction values,
                                 double... p)
        Evaluate the p-th quantiles of the values.

        This method can be used when the values of known size are already sorted.

        
         short[] x = ...
         Arrays.sort(x);
         double[] q = Quantile.withDefaults().evaluate(x.length, i -> x[i], 0.25, 0.5, 0.75);
         
        Parameters:
        n - Size of the values.
        values - Values function.
        p - Probabilities for the quantiles to compute.
        Returns:
        the quantiles
        Throws:
        IllegalArgumentException - if size < 0; if any probability p is not in the range [0, 1]; or no probabilities are specified.