Top Description Inners Fields Constructors Methods
org.apache.commons.math3.stat.ranking

public Class NaturalRanking

extends Object
implements RankingAlgorithm
Class Inheritance
All Implemented Interfaces
org.apache.commons.math3.stat.ranking.RankingAlgorithm
Imports
java.util.ArrayList, .Arrays, .Iterator, .List, org.apache.commons.math3.exception.MathInternalError, .NotANumberException, org.apache.commons.math3.random.RandomDataGenerator, .RandomGenerator, org.apache.commons.math3.util.FastMath

Ranking based on the natural ordering on doubles.

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 RandomGenerator may be supplied as a constructor argument.

Examples:

Input data: (20, 17, 30, 42.3, 17, 50, Double.NaN, Double.NEGATIVE_INFINITY, 17)
NaNStrategyTiesStrategy rank(data)
default (NaNs maximal) default (ties averaged) (5, 3, 6, 7, 3, 8, 9, 1, 3)
default (NaNs 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)

Since
2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
NaturalRanking.IntDoublePair

Represents the position of a double value in an ordering.

Field Summary

Modifier and TypeField and Description
public static final NaNStrategy
DEFAULT_NAN_STRATEGY

default NaN strategy

public static final TiesStrategy
DEFAULT_TIES_STRATEGY

default ties strategy

private final NaNStrategy
nanStrategy

NaN strategy - defaults to NaNs maximal

private final RandomDataGenerator
randomData

Deprecated as a consequence of RandomDataGenerator being deprecated.
Source of random data - used only when ties strategy is RANDOM
private final TiesStrategy
tiesStrategy

Ties strategy - defaults to ties averaged

Constructor Summary

AccessConstructor and Description
public
NaturalRanking()

Create a NaturalRanking with default strategies for handling ties and NaNs.

public
NaturalRanking(TiesStrategy
the TiesStrategy to use
tiesStrategy
)

Create a NaturalRanking with the given TiesStrategy.

public
NaturalRanking(NaNStrategy
the NaNStrategy to use
nanStrategy
)

Create a NaturalRanking with the given NaNStrategy.

public
NaturalRanking(NaNStrategy
NaNStrategy to use
nanStrategy
,
TiesStrategy
TiesStrategy to use
tiesStrategy
)

Create a NaturalRanking with the given NaNStrategy and TiesStrategy.

public
NaturalRanking(RandomGenerator
source of random data
randomGenerator
)

Create a NaturalRanking with TiesStrategy.RANDOM and the given RandomGenerator as the source of random data.

public
NaturalRanking(NaNStrategy
NaNStrategy to use
nanStrategy
,
RandomGenerator
source of random data
randomGenerator
)

Create a NaturalRanking with the given NaNStrategy, TiesStrategy.RANDOM and the given source of random data.

Method Summary

Modifier and TypeMethod and Description
private boolean

Returns:

true iff ranks contains one or more NaNs
containsNaNs
(NaturalRanking.IntDoublePair[]
array to be searched for NaNs
ranks
)

Checks for presence of NaNs in ranks.

private void
fill(double[]
array to modify
data
,
List<Integer>
list of index values to set
tiesTrace
,
double
value to set
value
)

Setsdata[i] = value for each i in tiesTrace.

private List<Integer>

Returns:

list of indexes i such that ranks[i] = NaN
getNanPositions
(NaturalRanking.IntDoublePair[]
array to search for NaNs
ranks
)

Returns a list of indexes where ranks is NaN.

public NaNStrategy

Returns:

returns the NaNStrategy
getNanStrategy
()

Return the NaNStrategy

public TiesStrategy

Returns:

the TiesStrategy
getTiesStrategy
()

Return the TiesStrategy

public double[]

Returns:

array of ranks
rank
(double[]
array to be ranked
data
)

Implements org.apache.commons.math3.stat.ranking.RankingAlgorithm.rank.

Rank data using the natural ordering on Doubles, with NaN values handled according to nanStrategy and ties resolved using tiesStrategy.

private void
recodeNaNs(NaturalRanking.IntDoublePair[]
array to recode
ranks
,
double
the value to replace NaNs with
value
)

Recodes NaN values to the given value.

private NaturalRanking.IntDoublePair[]

Returns:

array with NaN-valued entries removed
removeNaNs
(NaturalRanking.IntDoublePair[]
input array
ranks
)

Returns an array that is a copy of the input array with IntDoublePairs having NaN values removed.

private void
resolveTie(double[]
array of ranks
ranks
,
List<Integer>
list of indices where ranks is constant -- that is, for any i and j in TiesTrace, ranks[i] == ranks[j]
tiesTrace
)

Resolve a sequence of ties, using the configured TiesStrategy.

private void
restoreNaNs(double[]
array to modify
ranks
,
List<Integer>
list of index values to set to Double.NaN
nanPositions
)

Set ranks[i] = Double.NaN for each i in nanPositions.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_NAN_STRATEGYback to summary
public static final NaNStrategy DEFAULT_NAN_STRATEGY

default NaN strategy

DEFAULT_TIES_STRATEGYback to summary
public static final TiesStrategy DEFAULT_TIES_STRATEGY

default ties strategy

nanStrategyback to summary
private final NaNStrategy nanStrategy

NaN strategy - defaults to NaNs maximal

randomDataback to summary
private final RandomDataGenerator randomData

Deprecated

as a consequence of RandomDataGenerator being deprecated. See corresponding docs for further information.

Source of random data - used only when ties strategy is RANDOM

tiesStrategyback to summary
private final TiesStrategy tiesStrategy

Ties strategy - defaults to ties averaged

Constructor Detail

NaturalRankingback to summary
public NaturalRanking()

Create a NaturalRanking with default strategies for handling ties and NaNs.

NaturalRankingback to summary
public NaturalRanking(TiesStrategy tiesStrategy)

Create a NaturalRanking with the given TiesStrategy.

Parameters
tiesStrategy:TiesStrategy

the TiesStrategy to use

NaturalRankingback to summary
public NaturalRanking(NaNStrategy nanStrategy)

Create a NaturalRanking with the given NaNStrategy.

Parameters
nanStrategy:NaNStrategy

the NaNStrategy to use

NaturalRankingback to summary
public NaturalRanking(NaNStrategy nanStrategy, TiesStrategy tiesStrategy)

Create a NaturalRanking with the given NaNStrategy and TiesStrategy.

Parameters
nanStrategy:NaNStrategy

NaNStrategy to use

tiesStrategy:TiesStrategy

TiesStrategy to use

NaturalRankingback to summary
public NaturalRanking(RandomGenerator randomGenerator)

Create a NaturalRanking with TiesStrategy.RANDOM and the given RandomGenerator as the source of random data.

Parameters
randomGenerator:RandomGenerator

source of random data

NaturalRankingback to summary
public NaturalRanking(NaNStrategy nanStrategy, RandomGenerator randomGenerator)

Create a NaturalRanking with the given NaNStrategy, TiesStrategy.RANDOM and the given source of random data.

Parameters
nanStrategy:NaNStrategy

NaNStrategy to use

randomGenerator:RandomGenerator

source of random data

Method Detail

containsNaNsback to summary
private boolean containsNaNs(NaturalRanking.IntDoublePair[] ranks)

Checks for presence of NaNs in ranks.

Parameters
ranks:NaturalRanking.IntDoublePair[]

array to be searched for NaNs

Returns:boolean

true iff ranks contains one or more NaNs

fillback to summary
private void fill(double[] data, List<Integer> tiesTrace, double value)

Setsdata[i] = value for each i in tiesTrace.

Parameters
data:double[]

array to modify

tiesTrace:List<Integer>

list of index values to set

value:double

value to set

getNanPositionsback to summary
private List<Integer> getNanPositions(NaturalRanking.IntDoublePair[] ranks)

Returns a list of indexes where ranks is NaN.

Parameters
ranks:NaturalRanking.IntDoublePair[]

array to search for NaNs

Returns:List<Integer>

list of indexes i such that ranks[i] = NaN

getNanStrategyback to summary
public NaNStrategy getNanStrategy()

Return the NaNStrategy

Returns:NaNStrategy

returns the NaNStrategy

getTiesStrategyback to summary
public TiesStrategy getTiesStrategy()

Return the TiesStrategy

Returns:TiesStrategy

the TiesStrategy

rankback to summary
public double[] rank(double[] data)

Implements org.apache.commons.math3.stat.ranking.RankingAlgorithm.rank.

Rank data using the natural ordering on Doubles, with NaN values handled according to nanStrategy and ties resolved using tiesStrategy.

Parameters
data:double[]

array to be ranked

Returns:double[]

array of ranks

Exceptions
NotANumberException:
if the selected NaNStrategy is FAILED and a Double#NaN is encountered in the input data
recodeNaNsback to summary
private void recodeNaNs(NaturalRanking.IntDoublePair[] ranks, double value)

Recodes NaN values to the given value.

Parameters
ranks:NaturalRanking.IntDoublePair[]

array to recode

value:double

the value to replace NaNs with

removeNaNsback to summary
private NaturalRanking.IntDoublePair[] removeNaNs(NaturalRanking.IntDoublePair[] ranks)

Returns an array that is a copy of the input array with IntDoublePairs having NaN values removed.

Parameters
ranks:NaturalRanking.IntDoublePair[]

input array

Returns:NaturalRanking.IntDoublePair[]

array with NaN-valued entries removed

resolveTieback to summary
private void resolveTie(double[] ranks, List<Integer> tiesTrace)

Resolve a sequence of ties, using the configured TiesStrategy. The input ranks array is expected to take the same value for all indices in tiesTrace. The common value is recoded according to the tiesStrategy. For example, if ranks = <5,8,2,6,2,7,1,2>, tiesTrace = <2,4,7> and tiesStrategy is MINIMUM, ranks will be unchanged. The same array and trace with tiesStrategy AVERAGE will come out <5,8,3,6,3,7,1,3>.

Parameters
ranks:double[]

array of ranks

tiesTrace:List<Integer>

list of indices where ranks is constant -- that is, for any i and j in TiesTrace, ranks[i] == ranks[j]

restoreNaNsback to summary
private void restoreNaNs(double[] ranks, List<Integer> nanPositions)

Set ranks[i] = Double.NaN for each i in nanPositions.

Parameters
ranks:double[]

array to modify

nanPositions:List<Integer>

list of index values to set to Double.NaN

org.apache.commons.math3.stat.ranking back to summary

private Class NaturalRanking.IntDoublePair

extends Object
implements Comparable<NaturalRanking.IntDoublePair>
Class Inheritance
  • java.lang.Object
  • org.apache.commons.math3.stat.ranking.NaturalRanking.IntDoublePair
All Implemented Interfaces
java.lang.Comparable

Represents the position of a double value in an ordering. Comparable interface is implemented so Arrays.sort can be used to sort an array of IntDoublePairs by value. Note that the implicitly defined natural ordering is NOT consistent with equals.

Field Summary

Modifier and TypeField and Description
private final int
position

Original position of the pair

private final double
value

Value of the pair

Constructor Summary

AccessConstructor and Description
pack-priv
IntDoublePair(double
the value of the pair
value
,
int
the original position
position
)

Construct an IntDoublePair with the given value and position.

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

result of Double.compare(value, other.value)
compareTo
(NaturalRanking.IntDoublePair
the other pair to compare this to
other
)

Implements java.lang.Comparable.compareTo.

Compare this IntDoublePair to another pair.

public int

Returns:

position
getPosition
()

Returns the original position of the pair.

public double

Returns:

value
getValue
()

Returns the value of the pair.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

positionback to summary
private final int position

Original position of the pair

valueback to summary
private final double value

Value of the pair

Constructor Detail

IntDoublePairback to summary
pack-priv IntDoublePair(double value, int position)

Construct an IntDoublePair with the given value and position.

Parameters
value:double

the value of the pair

position:int

the original position

Method Detail

compareToback to summary
public int compareTo(NaturalRanking.IntDoublePair other)

Implements java.lang.Comparable.compareTo.

Compare this IntDoublePair to another pair. Only the values are compared.

Parameters
other:NaturalRanking.IntDoublePair

the other pair to compare this to

Returns:int

result of Double.compare(value, other.value)

getPositionback to summary
public int getPosition()

Returns the original position of the pair.

Returns:int

position

getValueback to summary
public double getValue()

Returns the value of the pair.

Returns:double

value