Generate and return the Fractional rank of the input scalar string or contiguous
array
of rank 1
in ascending order or in the order specified by the input procedure isSorted()
using the Quicksort algorithm such that array(rank)
will be in ascending order (or in the requested order as specified by isSorted()
.
This kind of ranking of values is widely known as fractional (1 2.5 2.5 4
) ranking.
In Fractional ranking, items that compare equal receive the same ranking number, which is the mean of what they would have under ordinal rankings; equivalently, the ranking number of 1 plus the number of items ranked above it plus half the number of items equal to it.
This strategy has the property that the sum of the ranking numbers is the same as under ordinal ranking.
For this reason, it is used in computing Borda counts and ranking statistics (e.g., Spearman Correlation).
Thus if A ranks ahead of B and C (which compare equal) which are both ranked ahead of D, then A gets ranking number 1
(first), B and C each get ranking number 2.5
(average of joint second/third) and D gets ranking number 4
(fourth).
That is, if A < B == C < D
, then the sequence ABCD
has the Fractional ranking 1223
.
Example:
Suppose the data set is 1.0, 1.0, 2.0, 3.0, 3.0, 4.0, 5.0, 5.0, 5.0
.
The ordinal ranks are 1, 2, 3, 4, 5, 6, 7, 8, 9
.
For v = 1.0
, the Fractional rank is the average of the ordinal ranks: (1 + 2) / 2 = 1.5
.
In a similar manner, for v = 5.0
, the Fractional rank is (7 + 8 + 9) / 3 = 8.0
.
Thus the Fractional ranks are: 1.5, 1.5, 3.0, 4.5, 4.5, 6.0, 8.0, 8.0, 8.0
- Parameters
-
[in] | array | : The input contiguous array of rank 1 of either
-
type css_pdt (parameterized container of string of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU)) or,
-
type css_type (container of string of default kind SK) or,
-
type
character of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU) of arbitrary length type parameter or,
-
type
integer of kind any supported by the processor (e.g., IK, IK8, IK16, IK32, or IK64) or,
-
type
logical of kind any supported by the processor (e.g., LK) or,
-
type
complex of kind any supported by the processor (e.g., CK, CK32, CK64, or CK128) or,
-
type
real of kind any supported by the processor (e.g., RK, RK32, RK64, or RK128),
or,
-
a scalar of type
character of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU) of arbitrary length type parameter,
whose elements rankings will be computed and returned. |
| isSorted | : The external user-specified function that takes two input scalar arguments of the same type and kind as the input array .
It returns a scalar logical of default kind LK that is .true. if the first input scalar argument is sorted with respect to the second input argument according to the user-defined sorting condition within isSorted() , otherwise, it is .false. .
If array is a Fortran string (i.e., a scalar character ), then both input arguments to isSorted() are single character(1,SKG) where SKG is the kind of array .
The following illustrates the generic interface of isSorted() when the rank of the input array is 1 , function isSorted(a,b) result (sorted)
TYPE(KIND) , intent(in) :: a, b
logical(LK) :: sorted
end function
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
integer, parameter RK The default real kind in the ParaMonte library: real64 in Fortran, c_double in C-Fortran Interoperati...
integer, parameter LK The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
integer, parameter CK The default complex kind in the ParaMonte library: real64 in Fortran, c_double_complex in C-Fortran I...
integer, parameter IK The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
integer, parameter SK The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
where TYPE(KIND) represents the type and kind of the input argument array , which can be one of the following,
character(*, SK) , intent(in) :: a, b
integer(IK) , intent(in) :: a, b
logical(LK) , intent(in) :: a, b
complex(CK) , intent(in) :: a, b
real(RK) , intent(in) :: a, b
type(css_type) , intent(in) :: a, b
type(css_pdt(SK)) , intent(in) :: a, b
This module contains the derived types for generating allocatable containers of scalar,...
This is the css_pdt parameterized type for generating instances of container of scalar of string obje...
This is the css_type type for generating instances of container of scalar of string objects.
where the kinds SK , IK , LK , CK , RK , can refer to any kind type parameter that is supported by the processor.
The following illustrates the generic interface of isSorted() when the input array is a scalar string, function isSorted(a,b) result (sorted)
character(1,SKG), intent(in) :: a, b
logical(LK) :: sorted
end function
where SKG represents the kind of the input string argument array .
This user-defined equivalence check is extremely useful where a user-defined sorting criterion other than simple ascending order is needed, for example, when the case-sensitivity of an input string or array of strings is irrelevant or when sorting of the absolute values matters excluding the signs of the numbers, or when descending order is desired.
In such cases, user can define a custom sorting condition within the user-defined external function isSorted to achieve the goal.
(optional, the default sorting condition is ascending order, that is a < b .) |
- Returns
rank(1:size(array)
: The output contiguous
array of rank 1
of type real
of default kind RK containing the ranks of the corresponding elements of array
.
The size of rank
must match that of array
(or its length type parameter if array
is a scalar string).
Read rank(i)
as the Fractional rank of the i
th element of array
.
Possible calling interfaces ⛓
Generate and return the Fractional rank of the input scalar string or contiguous array of rank 1 in a...
This module contains procedures and generic interfaces for obtaining various rankings of elements of ...
- Warning
- Note that the definition of
isSorted()
, if present, must be such that isSorted() .and. .not. isSorted()
is equivalent to an equality check for two elements of the input array
. This equality check is used to identify ties within the Standard ranking of the input array
.
-
The
pure
procedure(s) documented herein become impure
when the ParaMonte library is compiled with preprocessor macro CHECK_ENABLED=1
.
By default, these procedures are pure
in release
build and impure
in debug
and testing
builds.
-
The procedures under this generic interface are always
impure
when the input argument isSorted
is present.
- See also
- setSelected
getRankDense
setRankDense
getRankOrdinal
setRankOrdinal
getRankFractional
setRankFractional
getRankStandard
setRankStandard
getRankModified
setRankModified
setSorted
setSorted
Example usage ⛓
12 integer(IK) ,
parameter :: NP
= 5_IK
17 real(RK) ,
allocatable ::
rank(:)
18 character(:, SK) ,
allocatable :: string_SK
19 character(
9, SK) ,
allocatable :: vector_SK(:)
20 real(RK) :: vector_RK(NP)
21 integer(IK) :: vector_IK(NP)
22 logical(LK) :: vector_LK(NP)
24 type(display_type) :: disp
37 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
38 call disp%show(
"!Sort string of characters in ascending order.")
39 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
42 string_SK
= SK_
"ParaMonte"
43 call allocateRank(
len(string_SK))
47 call disp%show( string_SK, deliml
= SK_
"""" )
48 call disp%show(
"rank = getRankFractional(string_SK)")
50 call disp%show(
"getTrimmedTZ(getStr(rank, format = SK_'(*(g0,:,', '))'))")
55 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
56 call disp%show(
"!Sort array of strings of the same length in ascending order.")
57 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
60 vector_SK
= [
"ParaMonte" &
74 call allocateRank(
size(vector_SK))
77 call disp%show( vector_SK, deliml
= SK_
"""" )
78 call disp%show(
"rank = getRankFractional(vector_SK)")
80 call disp%show(
"getTrimmedTZ(getStr(rank, format = SK_'(*(g0,:,', '))'))")
85 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
86 call disp%show(
"!Sort vector of integers of arbitrary kinds in ascending order.")
87 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
90 call allocateRank(
size(vector_IK))
94 call disp%show(
"rank = getRankFractional(vector_IK)")
96 call disp%show(
"getTrimmedTZ(getStr(rank, format = SK_'(*(g0,:,', '))'))")
100 call allocateRank(
size(vector_IK))
102 call disp%show(
"vector_IK = [1_IK, 2_IK, 3_IK, 2_IK, 1_IK]")
103 vector_IK
= [
1_IK,
2_IK,
3_IK,
2_IK,
1_IK]
104 call disp%show(
"rank = getRankFractional(vector_IK)")
106 call disp%show(
"getTrimmedTZ(getStr(rank, format = SK_'(*(g0,:,', '))'))")
111 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
112 call disp%show(
"!Sort vector of logicals of arbitrary kinds in ascending order.")
113 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
116 call allocateRank(
size(vector_LK))
120 call disp%show(
"rank = getRankFractional(vector_LK)")
122 call disp%show(
"getTrimmedTZ(getStr(rank, format = SK_'(*(g0,:,', '))'))")
127 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
128 call disp%show(
"!Sort arrays of reals of arbitrary kinds in ascending order.")
129 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
132 call allocateRank(
size(vector_RK))
136 call disp%show(
"rank = getRankFractional(vector_RK)")
138 call disp%show(
"getTrimmedTZ(getStr(rank, format = SK_'(*(g0,:,', '))'))")
143 real,
allocatable :: vector_RK(:)
144 vector_RK
= [
1.0,
1.0,
2.0,
3.0,
3.0,
4.0,
5.0,
5.0,
5.0]
145 call allocateRank(
size(vector_RK))
149 call disp%show(
"rank = getRankFractional(vector_RK)")
151 call disp%show(
"getTrimmedTZ(getStr(rank, format = SK_'(*(g0,:,', '))'))")
157 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
158 call disp%show(
"!Sort according to an input user-defined comparison function.")
159 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
162 vector_IK
= int([(i, i
= 1_IK, NP)],
kind = IK)
163 call allocateRank(
size(vector_IK))
165 call disp%show(
"!Sort in DESCENDING (decreasing) order via an input custom-designed `isSorted()` function.")
169 call disp%show(
"rank = getRankFractional(vector_IK, isSorted_IK)")
171 call disp%show(
"getTrimmedTZ(getStr(rank, format = SK_'(*(g0,:,', '))'))")
175 call random_number(vector_RK); vector_RK
= vector_RK
- 0.5_RK
176 call allocateRank(
size(vector_RK))
178 call disp%show(
"!Sort in ascending order solely based on the magnitude of numbers using a custom comparison function.")
182 call disp%show(
"rank = getRankFractional(vector_RK, isSorted_RK)")
184 call disp%show(
"getTrimmedTZ(getStr(rank, format = SK_'(*(g0,:,', '))'))")
188 string_SK
= "ParaMonte"
189 call allocateRank(
len(string_SK))
191 call disp%show(
"!Sort string in ascending order without case-sensitivity via a custom-designed input comparison function.")
194 call disp%show( string_SK, deliml
= SK_
"""" )
195 call disp%show(
"rank = getRankFractional(string_SK, isSorted_SK)")
197 call disp%show(
"getTrimmedTZ(getStr(rank, format = SK_'(*(g0,:,', '))'))")
203 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
204 call disp%show(
"!Sort array of strings of varying length in ascending order.")
205 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
210 type(css_pdt) ,
allocatable :: cssvec(:)
211 cssvec
=[
css_pdt(
"ParaMonte")
&
224 call allocateRank(
size(cssvec))
227 call disp%show( cssvec, deliml
= SK_
"""" )
228 call disp%show(
"rank = getRankFractional(cssvec)")
230 call disp%show(
"getTrimmedTZ(getStr(rank, format = SK_'(*(g0,:,', '))'))")
238 function isSorted_IK(a,b)
result(isSorted)
240 integer(IK) ,
intent(in) :: a, b
241 logical(LK) :: isSorted
245 function isSorted_RK(a,b)
result(isSorted)
247 integer(IK) ,
intent(in) :: a, b
248 logical(LK) :: isSorted
249 isSorted
= abs(a)
< abs(b)
252 function isSorted_SK(a,b)
result(isSorted)
255 character(
1, SK),
intent(in) :: a, b
256 logical(LK) :: isSorted
260 subroutine allocateRank(size)
261 integer,
intent(in) :: size
262 if (
allocated(rank))
deallocate(rank)
Return a uniform random scalar or contiguous array of arbitrary rank of randomly uniformly distribute...
This is a generic method of the derived type display_type with pass attribute.
This is a generic method of the derived type display_type with pass attribute.
Generate and return the input string where the uppercase English alphabets are all converted to lower...
Generate and return a vector of single-characters each element of which corresponds to one character ...
Generate and return the conversion of the input value to an output Fortran string,...
This module contains classes and procedures for computing various statistical quantities related to t...
This module contains classes and procedures for input/output (IO) or generic display operations on st...
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
This module contains the uncommon and hardly representable ASCII characters as well as procedures for...
This module contains classes and procedures for various string manipulations and inquiries.
This module contains the generic procedures for converting values of different types and kinds to For...
Generate and return an object of type display_type.
Example Unix compile command via Intel ifort
compiler ⛓
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
Example Windows Batch compile command via Intel ifort
compiler ⛓
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
Example Unix / MinGW compile command via GNU gfortran
compiler ⛓
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
Example output ⛓
112.,
3.5,
8.,
3.5,
1.,
7.,
6.,
9.,
5.
20"ParaMonte",
"V.2 ",
"is ",
"a ",
"Parallel ",
"Monte ",
"Carlo ",
"and ",
"a ",
"Machine ",
"Learning ",
"Library. "
236.,
8.,
12.,
9.5,
7.,
5.,
1.,
11.,
9.5,
4.,
2.,
3.
38vector_IK
= [
1_IK,
2_IK,
3_IK,
2_IK,
1_IK]
62-0.49830622565118388E-1,
-0.23114565137606047,
-0.77879096741973353E-1,
-0.32193846805061599,
-0.40356560967758748E-1
69+1.00000000,
+1.00000000,
+2.00000000,
+3.00000000,
+3.00000000,
+4.00000000,
+5.00000000,
+5.00000000,
+5.00000000
721.5,
1.5,
3.,
4.5,
4.5,
6.,
8.,
8.,
8.
92+0.22262409075725609,
+0.34416981305582595,
+0.39872403657714039,
+0.30275170649637972,
+0.14569932787074158
1047.,
1.5,
8.,
1.5,
4.,
6.,
5.,
9.,
3.
- Test:
- test_pm_arrayRank
- Bug:
Status: Unresolved
Source: Intel Classic Fortran Compiler ifort
version 2021.5
Description: See pm_arraySplit for the description of a relevant bug in PDT name aliasing when compiled with Intel ifort 2021.5 that also applies to this module.
Remedy (as of ParaMonte Library version 2.0.0): See pm_arraySplit for the remedy.
- Todo:
- Low Priority: The current bypass for the PDT name aliasing bug can be reverted back to PDT name aliasing once the ifort bug is resolved.
- Todo:
- Low Priority: A test should be implemented for arrays of size that can be represented only by an IKD integer.
Final Remarks ⛓
If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.
-
If you use any parts or concepts from this library to any extent, please acknowledge the usage by citing the relevant publications of the ParaMonte library.
-
If you regenerate any parts/ideas from this library in a programming environment other than those currently supported by this ParaMonte library (i.e., other than C, C++, Fortran, MATLAB, Python, R), please also ask the end users to cite this original ParaMonte library.
This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.
- Copyright
- Computational Data Science Lab
- Author:
- Amir Shahmoradi, April 21, 2017, 1:54 AM, Institute for Computational Engineering and Sciences (ICES), The University of Texas Austin
Definition at line 2703 of file pm_arrayRank.F90.