Class NaturalRanking
- All Implemented Interfaces:
Function<double[],,double[]> UnaryOperator<double[]>,RankingAlgorithm
NaNs are treated according to the configured
NaNStrategy and ties are handled using the selected
TiesStrategy. Configuration settings are supplied in optional
constructor arguments. Defaults are NaNStrategy.FAILED and
TiesStrategy.AVERAGE, respectively.
When using TiesStrategy.RANDOM, a generator of random values in [0, x)
can be supplied as a IntUnaryOperator 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:
| 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
ConstructorsConstructorDescriptionCreates 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
Modifier and TypeMethodDescriptiondouble[]apply(double[] data) Rankdatausing the natural ordering on floating-point values, with NaN values handled according tonanStrategyand ties resolved usingtiesStrategy.Return theNaNStrategy.Return theTiesStrategy.
-
Constructor Details
-
NaturalRanking
public NaturalRanking()Creates an instance withNaNStrategy.FAILEDandTiesStrategy.AVERAGE. -
NaturalRanking
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
Creates an instance with the specified @nanStrategyandTiesStrategy.AVERAGE.- Parameters:
nanStrategy- NaNStrategy to use.- Throws:
NullPointerException- if the strategy isnull
-
NaturalRanking
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
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
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 Details
-
getNanStrategy
Return theNaNStrategy.- Returns:
- the strategy for handling NaN
-
getTiesStrategy
Return theTiesStrategy.- Returns:
- the strategy for handling ties
-
apply
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.
-