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.NaNsare treated according to the configuredNaNStrategyand ties are handled using the selectedTiesStrategy. Configuration settings are supplied in optional constructor arguments. Defaults areNaNStrategy.FAILEDandTiesStrategy.AVERAGE, respectively.When using
TiesStrategy.RANDOM, a generator of random values in[0, x)can be supplied as aIntUnaryOperatorargument; 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.RANDOMis 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.FAILEDandTiesStrategy.AVERAGE.NaturalRanking(IntUnaryOperator randomIntFunction)Creates an instance withNaNStrategy.FAILED,TiesStrategy.RANDOMand the given the source of random index data.NaturalRanking(NaNStrategy nanStrategy)Creates an instance with the specified @nanStrategyandTiesStrategy.AVERAGE.NaturalRanking(NaNStrategy nanStrategy, IntUnaryOperator randomIntFunction)Creates an instance with the specified @nanStrategy,TiesStrategy.RANDOMand the given the source of random index data.NaturalRanking(NaNStrategy nanStrategy, TiesStrategy tiesStrategy)Creates an instance with the specified @nanStrategyand the specified @tiesStrategy.NaturalRanking(TiesStrategy tiesStrategy)Creates an instance withNaNStrategy.FAILEDand the specified @tiesStrategy.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description double[]apply(double[] data)Rankdatausing the natural ordering on floating-point values, with NaN values handled according tonanStrategyand ties resolved usingtiesStrategy.NaNStrategygetNanStrategy()Return theNaNStrategy.TiesStrategygetTiesStrategy()Return theTiesStrategy.
-
-
-
Constructor Detail
-
NaturalRanking
public NaturalRanking()
Creates an instance withNaNStrategy.FAILEDandTiesStrategy.AVERAGE.
-
NaturalRanking
public NaturalRanking(TiesStrategy tiesStrategy)
Creates an instance withNaNStrategy.FAILEDand the specified @tiesStrategy.If the ties strategy is
RANDOMa 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 @nanStrategyandTiesStrategy.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 @nanStrategyand the specified @tiesStrategy.If the ties strategy is
RANDOMa 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.RANDOMand the given the source of random index data.- Parameters:
randomIntFunction- Source of random index data. Function maps positivexrandomly 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.RANDOMand the given the source of random index data.- Parameters:
nanStrategy- NaNStrategy to use.randomIntFunction- Source of random index data. Function maps positivexrandomly 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)
Rankdatausing the natural ordering on floating-point values, with NaN values handled according tonanStrategyand ties resolved usingtiesStrategy.- Specified by:
applyin interfaceFunction<double[],double[]>- Specified by:
applyin 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 selectedNaNStrategyisFAILEDand aDouble.NaNis encountered in the input data.
-
-