Python has a set of functions in several popular packages to compute Quantiles. For example, if you want to find Quartiles (or quarters) within a numeric array – i.e. values within the array which are 25%, 50% and 75% of the input data range. Or, if you want Quintiles (or fifths), which are 20%, 40%, 60%, and 80% of the input data range. Quantile is a more generic term in statistics for finding values within a data distribution.
Python Quantile functions take an array as input, along with one or more floating-point values, each in the range of 0.0 to 1.0. For instance, providing a single value of 0.75 returns an array element such that 75% of the array elements are smaller than this array element and 25% are larger. This is accomplished without sorting the array, in linear time.
Python Quantile function can also accept an array of floating-point values. For example, 0.25, 0.5, 0.75. In this case, it returns array values for the 25%, 50% and 75% of all array values. Once again, this is accomplished without sorting the array, by using a comparison-based Selection algorithm, in linear time. This is faster than sorting, which is O(nlgn) – i.e. slower than linear time.
Quantile is implemented using the QuickSelect or Median-of-Medians algorithms, which are both comparison-based – i.e. compare array elements to each other.
Radix Quantile
Quantile functionality can also be implemented using Radix methodology instead of using comparisons.
Quantile function, is simply a different interface to the Selection algorithm. Selection algorithm uses an integer array index. However, Quantile needs, a floating-point value instead. The value of 0.0 means finding the minimum value within the array. The value of 1.0 means finding the maximum value. The minimum is at index of zero if the array was sorted in ascending order. The maximum is at index of (length_of_array – 1).
Thus, the floating-point in the range of 0.0 to 1.0 can be transformed into an integer index by scaling it by (length_of_array – 1). This integer index is then passed to the Radix Selection algorithm to achieve Radix Quantile. When an array of floating-point values are used, each in the range of 0.0 to 1.0, they can be scaled in the same manner, achieving multi-value Radix Quantile (similar to k[]-th Selection).
This version of Radix Quantile returns Quantile values within the input array, without sorting the array or even modifying a substantial portion of it.
Radix Quantile with Partitioning
Comparison-based algorithms use partitioning to achieve Selection. This means, partitioning is achieved as a byproduct of doing Selection.
Radix Selection, on the other hand, does not require use of partitioning to achieve Selection. Doing partitioning is additional unnecessary work for Radix Selection. However, if we want to not only determine the Quantile values but also to partition array elements in between these values, then the extra partitioning work is required. This variant is Radix Quantile with Partitioning.
Radix Quantile with Partitioning uses an MSD Radix Sort variant. While processing the input array based on the first digit, the entire array is sorted using the most significant digit, as is normally done by the MSD Radix Sort algorithm. However, for the next and subsequent digits, only the bins where the Quantile floating-point values (scaled to integer indexes) land are processed further. Only these bins are sorted fully by the next digit. Less work than sorting is achieved by only processing these particular bins, instead of processing all of the bins.
In other words, sorting by the most significant digit provides a coarse sorting operation. Sorting by the subsequent digits refines the result by sorting only around the Quantile values – i.e. around the Quantile boundaries – refining these boundaries.
Implementation
The initial implementation of Radix Quantile functions are in the HPCsharp (C#) nuget package, in QuantileRadix.cs source file. Both with Partitioning and without are implemented there for an input array of unsigned integers. Other data types would be fairly simple to support, as is done in HPCsharp for In-Place MSD Radix Sort, which support all numeric data types and user defined types. Let me know which ones you would like to be added.
C++ implemented in the ParallelAlgorithm (C++) package coming shortly…
Performance
The following Table shows C# implementation performance for an array of 10 Million 32-bit unsigned integers with three data distributions.
| Algorithm | Random Data | Presorted Data | Constant Data |
| Array.Sort | 17 M/sec | 186 M/sec | 186 M/sec |
| QuickSelect | 142 M/sec | 149 M/sec | 143 M/sec |
| LSD Radix Sort | 117 M/sec | 124 M/sec | 184 M/sec |
| Radix Quantile | 822 M/sec | 684 M/sec | 807 M/sec |
| Radix Quantile with Partitioning | 216 M/sec | 137 M/sec | 68 M/sec |
QuickSelect is substantially faster than Array.Sort for random data, since it’s linear on average. LSD Radix Sort is substantially faster than Array.Sort for random data and competitive for constant data distribution. Radix Quantile is the fastest algorithm for every data distribution. It is over 5X faster than QuickSelect for random data. It is faster because it performs less work because it does not partition the data to achieve its result.
Radix Quantile with Partitioning is substantially slower than Radix Quantile because it performs additional work to partition the data. It is faster than QuickSelect for random data, and is nearly the same speed for presorted data.