Class NaturalRanking
- java.lang.Object
-
- org.apache.commons.statistics.ranking.NaturalRanking
-
- All Implemented Interfaces:
Function<double[],double[]>
,UnaryOperator<double[]>
,RankingAlgorithm
public class NaturalRanking extends Object implements RankingAlgorithm
Ranking based on the natural ordering on floating-point values.NaNs
are treated according to the configuredNaNStrategy
and ties are handled using the selectedTiesStrategy
. Configuration settings are supplied in optional constructor arguments. Defaults areNaNStrategy.FAILED
andTiesStrategy.AVERAGE
, respectively.When using
TiesStrategy.RANDOM
, a generator of random values in[0, x)
can be supplied as aIntUnaryOperator
argument; otherwise a default is created on-demand. The source of randomness can be supplied using a method reference. The following example creates a ranking with NaN values with the highest ranking and ties resolved randomly:NaturalRanking ranking = new NaturalRanking(NaNStrategy.MAXIMAL, new SplittableRandom()::nextInt);
Note: Using
TiesStrategy.RANDOM
is not thread-safe due to the mutable generator of randomness. Instances not using random resolution of ties are thread-safe.Examples:
Examples Input data: [20, 17, 30, 42.3, 17, 50, Double.NaN, Double.NEGATIVE_INFINITY, 17] NaNStrategy TiesStrategy rank(data)
MAXIMAL default (ties averaged) [5, 3, 6, 7, 3, 8, 9, 1, 3] MAXIMAL MINIMUM [5, 2, 6, 7, 2, 8, 9, 1, 2] MINIMAL default (ties averaged] [6, 4, 7, 8, 4, 9, 1.5, 1.5, 4] REMOVED SEQUENTIAL [5, 2, 6, 7, 3, 8, 1, 4] MINIMAL MAXIMUM [6, 5, 7, 8, 5, 9, 2, 2, 5] MINIMAL MAXIMUM [6, 5, 7, 8, 5, 9, 2, 2, 5] - Since:
- 1.1
-
-
Constructor Summary
Constructors Constructor Description NaturalRanking()
Creates an instance withNaNStrategy.FAILED
andTiesStrategy.AVERAGE
.NaturalRanking(IntUnaryOperator randomIntFunction)
Creates an instance withNaNStrategy.FAILED
,TiesStrategy.RANDOM
and the given the source of random index data.NaturalRanking(NaNStrategy nanStrategy)
Creates an instance with the specified @nanStrategy
andTiesStrategy.AVERAGE
.NaturalRanking(NaNStrategy nanStrategy, IntUnaryOperator randomIntFunction)
Creates an instance with the specified @nanStrategy
,TiesStrategy.RANDOM
and the given the source of random index data.NaturalRanking(NaNStrategy nanStrategy, TiesStrategy tiesStrategy)
Creates an instance with the specified @nanStrategy
and the specified @tiesStrategy
.NaturalRanking(TiesStrategy tiesStrategy)
Creates an instance withNaNStrategy.FAILED
and the specified @tiesStrategy
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description double[]
apply(double[] data)
Rankdata
using the natural ordering on floating-point values, with NaN values handled according tonanStrategy
and ties resolved usingtiesStrategy
.NaNStrategy
getNanStrategy()
Return theNaNStrategy
.TiesStrategy
getTiesStrategy()
Return theTiesStrategy
.
-
-
-
Constructor Detail
-
NaturalRanking
public NaturalRanking()
Creates an instance withNaNStrategy.FAILED
andTiesStrategy.AVERAGE
.
-
NaturalRanking
public NaturalRanking(TiesStrategy tiesStrategy)
Creates an instance withNaNStrategy.FAILED
and the specified @tiesStrategy
.If the ties strategy is
RANDOM
a default source of randomness is used to resolve ties.- Parameters:
tiesStrategy
- TiesStrategy to use.- Throws:
NullPointerException
- if the strategy isnull
-
NaturalRanking
public NaturalRanking(NaNStrategy nanStrategy)
Creates an instance with the specified @nanStrategy
andTiesStrategy.AVERAGE
.- Parameters:
nanStrategy
- NaNStrategy to use.- Throws:
NullPointerException
- if the strategy isnull
-
NaturalRanking
public NaturalRanking(NaNStrategy nanStrategy, TiesStrategy tiesStrategy)
Creates an instance with the specified @nanStrategy
and the specified @tiesStrategy
.If the ties strategy is
RANDOM
a default source of randomness is used to resolve ties.- Parameters:
nanStrategy
- NaNStrategy to use.tiesStrategy
- TiesStrategy to use.- Throws:
NullPointerException
- if any strategy isnull
-
NaturalRanking
public NaturalRanking(IntUnaryOperator randomIntFunction)
Creates an instance withNaNStrategy.FAILED
,TiesStrategy.RANDOM
and the given the source of random index data.- Parameters:
randomIntFunction
- Source of random index data. Function maps positivex
randomly to[0, x)
- Throws:
NullPointerException
- if the source of randomness isnull
-
NaturalRanking
public NaturalRanking(NaNStrategy nanStrategy, IntUnaryOperator randomIntFunction)
Creates an instance with the specified @nanStrategy
,TiesStrategy.RANDOM
and the given the source of random index data.- Parameters:
nanStrategy
- NaNStrategy to use.randomIntFunction
- Source of random index data. Function maps positivex
randomly to[0, x)
- Throws:
NullPointerException
- if the strategy or source of randomness arenull
-
-
Method Detail
-
getNanStrategy
public NaNStrategy getNanStrategy()
Return theNaNStrategy
.- Returns:
- the strategy for handling NaN
-
getTiesStrategy
public TiesStrategy getTiesStrategy()
Return theTiesStrategy
.- Returns:
- the strategy for handling ties
-
apply
public double[] apply(double[] data)
Rankdata
using the natural ordering on floating-point values, with NaN values handled according tonanStrategy
and ties resolved usingtiesStrategy
.- Specified by:
apply
in interfaceFunction<double[],double[]>
- Specified by:
apply
in interfaceRankingAlgorithm
- Parameters:
data
- Array of data to be ranked.- Returns:
- an array of ranks corresponding to the elements of the input array
- Throws:
IllegalArgumentException
- if the selectedNaNStrategy
isFAILED
and aDouble.NaN
is encountered in the input data.
-
-