Open
Description
Sometimes you don't need the exact ranks of elements in a vector but what quantile each value is in. For quintiles you would return an integer from 1 to 5 corresponding to each element. Such partial ranking ought to be faster than full ranking, especially when the number of groups is small compared to the size of the vector. The interface could be
pure function quantile(x,ngroups) result(igroup)
real, intent(in) :: x(:) ! data to be grouped
integer, intent(in) :: ngroups ! # of groups
integer :: igroup(size(x)) ! returned values will be between 1 and ngroups, inclusive
end function quantile
There was a Stack Overflow thread Efficient algorithm of partial sorting into N unsorted groups
In finance, researchers commonly group stocks into quintiles or deciles based on some characteristic, such as price/earnings ratio, and then study future performance by quintile. The exact ranks of stocks are not needed for such studies.