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

public Class Percentile

extends AbstractUnivariateStatistic
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Known Direct Subclasses
org.apache.commons.math3.stat.descriptive.rank.Median
Imports
java.io.Serializable, java.util.Arrays, .BitSet, org.apache.commons.math3.exception.MathIllegalArgumentException, .MathUnsupportedOperationException, .NullArgumentException, .OutOfRangeException, org.apache.commons.math3.exception.util.LocalizedFormats, org.apache.commons.math3.stat.descriptive.AbstractUnivariateStatistic, org.apache.commons.math3.stat.ranking.NaNStrategy, org.apache.commons.math3.util.FastMath, .KthSelector, .MathArrays, .MathUtils, .MedianOf3PivotingStrategy, .PivotingStrategyInterface, .Precision

Provides percentile computation.

There are several commonly used methods for estimating percentiles (a.k.a. quantiles) based on sample data. For large samples, the different methods agree closely, but when sample sizes are small, different methods will give significantly different results. The algorithm implemented here works as follows:

  1. Let n be the length of the (sorted) array and 0 < p <= 100 be the desired percentile.
  2. If n = 1 return the unique array element (regardless of the value of p); otherwise
  3. Compute the estimated percentile position pos = p * (n + 1) / 100 and the difference, d between pos and floor(pos) (i.e. the fractional part of pos).
  4. If pos < 1 return the smallest element in the array.
  5. Else if pos >= n return the largest element in the array.
  6. Else let lower be the element in position floor(pos) in the array and let upper be the next element in the array. Return lower + d * (upper - lower)

To compute percentiles, the data must be at least partially ordered. Input arrays are copied and recursively partitioned using an ordering definition. The ordering used by Arrays.sort(double[]) is the one determined by java.lang.Double#compareTo(Double). This ordering makes Double.NaN larger than any other value (including Double.POSITIVE_INFINITY). Therefore, for example, the median (50th percentile) of {0, 1, 2, 3, 4, Double.NaN} evaluates to 2.5.

Since percentile estimation usually involves interpolation between array elements, arrays containing NaN or infinite values will often result in NaN or infinite values returned.

Further, to include different estimation types such as R1, R2 as mentioned in Quantile page(wikipedia), a type specific NaN handling strategy is used to closely match with the typically observed results from popular tools like R(R1-R9), Excel(R7).

Since 2.2, Percentile uses only selection instead of complete sorting and caches selection algorithm state between calls to the various evaluate methods. This greatly improves efficiency, both for a single percentile and multiple percentile computations. To maximize performance when multiple percentiles are computed based on the same data, users should set the data array once using either one of the evaluate(double[], double) or setData(double[]) methods and thereafter evaluate(double) with just the percentile provided.

Note that this implementation is not synchronized. If multiple threads access an instance of this class concurrently, and at least one of the threads invokes the increment() or clear() method, it must be synchronized externally.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
Percentile.EstimationType

An enum for various estimation strategies of a percentile referred in wikipedia on quantile with the names of enum matching those of types mentioned in wikipedia.

Field Summary

Modifier and TypeField and Description
private int[]
cachedPivots

Cached pivots.

private final Percentile.EstimationType
estimationType

Any of the EstimationTypes such as CM can be used.

private final KthSelector
kthSelector

Default KthSelector used with default pivoting strategy

private static final int
MAX_CACHED_LEVELS

Maximum number of partitioning pivots cached (each level double the number of pivots).

private final NaNStrategy
nanStrategy

NaN Handling of the input as defined by NaNStrategy

private static final int
PIVOTS_HEAP_LENGTH

Maximum number of cached pivots in the pivots cached array

private double
quantile

Determines what percentile is computed when evaluate() is activated with no quantile argument

private static final long
serialVersionUID

Serializable version identifier

Constructor Summary

AccessConstructor and Description
public
Percentile()

Constructs a Percentile with the following defaults.

public
Percentile(final double
the quantile
quantile
)

Constructs a Percentile with the specific quantile value and the following

public
Percentile(final Percentile
the Percentile instance to copy
original
)

Copy constructor, creates a new Percentile identical to the original

protected
Percentile(final double
the quantile to be computed
quantile
,
final Percentile.EstimationType
one of the percentile estimation types
estimationType
,
final NaNStrategy
one of NaNStrategy to handle with NaNs
nanStrategy
,
final KthSelector
a KthSelector to use for pivoting during search
kthSelector
)

Constructs a Percentile with the specific quantile value, EstimationType, NaNStrategy and KthSelector.

Method Summary

Modifier and TypeMethod and Description
public Percentile
copy()

Implements abstract org.apache.commons.math3.stat.descriptive.AbstractUnivariateStatistic.copy.

Implements org.apache.commons.math3.stat.descriptive.UnivariateStatistic.copy.

Returns a copy of the statistic with the same internal state.

public static void
copy(final Percentile
Percentile to copy
source
,
final Percentile
Percentile to copy to
dest
)
Deprecated as of 3.4 this method does not work anymore, as it fails to copy internal states between instances configured with different estimation type, NaN handling strategies and kthSelector, it therefore always throw MathUnsupportedOperationException

Copies source to dest.

private static double[]

Returns:

copy of a slice of the original array
copyOf
(final double[]
the input array
values
,
final int
start index of the array to include
begin
,
final int
number of elements to include from begin
length
)

Make a copy of the array for the slice defined by array part from [begin, begin+length)

public double

Returns:

the value of the statistic applied to the stored data
evaluate
(final double
the percentile value to compute
p
)

Returns the result of evaluating the statistic over the stored data.

public double

Returns:

the percentile value or Double.NaN if the array is empty
evaluate
(final double[]
input array of values
values
,
final double
the percentile value to compute
p
)

Returns an estimate of the pth percentile of the values in the values array.

public double

Returns:

the percentile value
evaluate
(final double[]
the input array
values
,
final int
index of the first array element to include
start
,
final int
the number of elements to include
length
)

Implements abstract org.apache.commons.math3.stat.descriptive.AbstractUnivariateStatistic.evaluate.

Implements org.apache.commons.math3.stat.descriptive.UnivariateStatistic.evaluate.

Returns an estimate of the quantileth percentile of the designated values in the values array.

public double

Returns:

the percentile value
evaluate
(final double[]
array of input values
values
,
final int
the first (0-based) element to include in the computation
begin
,
final int
the number of array elements to include
length
,
final double
the percentile to compute
p
)

Returns an estimate of the pth percentile of the values in the values array, starting with the element in (0-based) position begin in the array and including length values.

public Percentile.EstimationType

Returns:

the estimationType set
getEstimationType
()

Get the estimation type used for computation.

public KthSelector

Returns:

the kthSelector set
getKthSelector
()

Get the kthSelector used for computation.

public NaNStrategy

Returns:

NaN Handling strategy set during construction
getNaNStrategy
()

Get the NaN Handling strategy used for computation.

public PivotingStrategyInterface

Returns:

the pivoting strategy set
getPivotingStrategy
()

Get the PivotingStrategyInterface used in KthSelector for computation.

private int[]

Returns:

cached pivots or a newly created one
getPivots
(final double[]
array containing the input numbers
values
)

Get pivots which is either cached or a newly created one

public double

Returns:

quantile set while construction or setQuantile(double)
getQuantile
()

Returns the value of the quantile field (determines what percentile is computed when evaluate() is called with no quantile argument).

protected double[]

Returns:

work array sliced from values in the range [begin,begin+length)
getWorkArray
(final double[]
the array of numbers
values
,
final int
index to start reading the array
begin
,
final int
the length of array to be read from the begin index
length
)

Get the work array to operate.

pack-priv int

Returns:

the index of the median element chosen between the first, the middle and the last element of the array slice
medianOf3
(final double[]
data array
work
,
final int
index of the first element of the slice
begin
,
final int
index after the last element of the slice
end
)
Deprecated Please refrain from using this method (as it wont take effect) and instead use Percentile#withKthSelector(newKthSelector) if required.

Select a pivot index as the median of three

Note

With the effect of allowing KthSelector to be set on Percentile instances(thus indirectly PivotingStrategy) this method wont take effect any more and hence is unsupported.

private static double[]

Returns:

the copy of the sliced array after removing the removedValue
removeAndSlice
(final double[]
the input array
values
,
final int
start index of the array to include
begin
,
final int
number of elements to include from begin
length
,
final double
the value to be removed from the sliced array
removedValue
)

Remove the occurrence of a given value in a copied slice of array defined by the array part from [begin, begin+length).

private static double[]

Returns:

the copy of sliced array with replaced values
replaceAndSlice
(final double[]
the input array
values
,
final int
start index of the array to include
begin
,
final int
number of elements to include from begin
length
,
final double
the value to be replaced with
original
,
final double
the value to be used for replacement
replacement
)

Replace every occurrence of a given value with a replacement value in a copied slice of array defined by array part from [begin, begin+length).

public void
setData(final double[]
data array to store (may be null to remove stored data)
values
)

Overrides org.apache.commons.math3.stat.descriptive.AbstractUnivariateStatistic.setData.

Set the data array.

public void
setData(final double[]
data array to store
values
,
final int
the index of the first element to include
begin
,
final int
the number of elements to include
length
)

Overrides org.apache.commons.math3.stat.descriptive.AbstractUnivariateStatistic.setData.

Set the data array.

public void
setQuantile(final double
a value between 0 < p <= 100
p
)

Sets the value of the quantile field (determines what percentile is computed when evaluate() is called with no quantile argument).

public Percentile

Returns:

a new instance, with changed estimation type
withEstimationType
(final Percentile.EstimationType
estimation type for the new instance
newEstimationType
)

Build a new instance similar to the current one except for the estimation type.

public Percentile

Returns:

a new instance, with changed KthSelector
withKthSelector
(final KthSelector
KthSelector for the new instance
newKthSelector
)

Build a new instance similar to the current one except for the kthSelector instance specifically set.

public Percentile

Returns:

a new instance, with changed NaN handling strategy
withNaNStrategy
(final NaNStrategy
NaN strategy for the new instance
newNaNStrategy
)

Build a new instance similar to the current one except for the NaN handling strategy.

Inherited from org.apache.commons.math3.stat.descriptive.AbstractUnivariateStatistic:
evaluateevaluategetDatagetDataReftesttesttesttest

Field Detail

cachedPivotsback to summary
private int[] cachedPivots

Cached pivots.

estimationTypeback to summary
private final Percentile.EstimationType estimationType

Any of the EstimationTypes such as CM can be used.

kthSelectorback to summary
private final KthSelector kthSelector

Default KthSelector used with default pivoting strategy

MAX_CACHED_LEVELSback to summary
private static final int MAX_CACHED_LEVELS

Maximum number of partitioning pivots cached (each level double the number of pivots).

nanStrategyback to summary
private final NaNStrategy nanStrategy

NaN Handling of the input as defined by NaNStrategy

PIVOTS_HEAP_LENGTHback to summary
private static final int PIVOTS_HEAP_LENGTH

Maximum number of cached pivots in the pivots cached array

quantileback to summary
private double quantile

Determines what percentile is computed when evaluate() is activated with no quantile argument

serialVersionUIDback to summary
private static final long serialVersionUID

Serializable version identifier

Constructor Detail

Percentileback to summary
public Percentile()

Constructs a Percentile with the following defaults.

Percentileback to summary
public Percentile(final double quantile) throws MathIllegalArgumentException

Constructs a Percentile with the specific quantile value and the following

Parameters
quantile:double

the quantile

Exceptions
MathIllegalArgumentException:
if p is not greater than 0 and less than or equal to 100
Percentileback to summary
public Percentile(final Percentile original) throws NullArgumentException

Copy constructor, creates a new Percentile identical to the original

Parameters
original:Percentile

the Percentile instance to copy

Exceptions
NullArgumentException:
if original is null
Percentileback to summary
protected Percentile(final double quantile, final Percentile.EstimationType estimationType, final NaNStrategy nanStrategy, final KthSelector kthSelector) throws MathIllegalArgumentException

Constructs a Percentile with the specific quantile value, EstimationType, NaNStrategy and KthSelector.

Parameters
quantile:double

the quantile to be computed

estimationType:Percentile.EstimationType

one of the percentile estimation types

nanStrategy:NaNStrategy

one of NaNStrategy to handle with NaNs

kthSelector:KthSelector

a KthSelector to use for pivoting during search

Exceptions
MathIllegalArgumentException:
if p is not within (0,100]
NullArgumentException:
if type or NaNStrategy passed is null

Method Detail

copyback to summary
public Percentile copy()

Implements abstract org.apache.commons.math3.stat.descriptive.AbstractUnivariateStatistic.copy.

Implements org.apache.commons.math3.stat.descriptive.UnivariateStatistic.copy.

Doc from org.apache.commons.math3.stat.descriptive.AbstractUnivariateStatistic.copy.

Returns a copy of the statistic with the same internal state.

Returns:Percentile

Doc from org.apache.commons.math3.stat.descriptive.UnivariateStatistic.copy.

a copy of the statistic

Annotations
@Override
copyback to summary
public static void copy(final Percentile source, final Percentile dest) throws MathUnsupportedOperationException

Deprecated

as of 3.4 this method does not work anymore, as it fails to copy internal states between instances configured with different estimation type, NaN handling strategies and kthSelector, it therefore always throw MathUnsupportedOperationException

Copies source to dest.

Parameters
source:Percentile

Percentile to copy

dest:Percentile

Percentile to copy to

Annotations
@Deprecated
Exceptions
MathUnsupportedOperationException:
always thrown since 3.4
copyOfback to summary
private static double[] copyOf(final double[] values, final int begin, final int length)

Make a copy of the array for the slice defined by array part from [begin, begin+length)

Parameters
values:double[]

the input array

begin:int

start index of the array to include

length:int

number of elements to include from begin

Returns:double[]

copy of a slice of the original array

evaluateback to summary
public double evaluate(final double p) throws MathIllegalArgumentException

Returns the result of evaluating the statistic over the stored data.

The stored array is the one which was set by previous calls to setData(double[])

Parameters
p:double

the percentile value to compute

Returns:double

the value of the statistic applied to the stored data

Exceptions
MathIllegalArgumentException:
if p is not a valid quantile value (p must be greater than 0 and less than or equal to 100)
evaluateback to summary
public double evaluate(final double[] values, final double p) throws MathIllegalArgumentException

Returns an estimate of the pth percentile of the values in the values array.

Calls to this method do not modify the internal quantile state of this statistic.

  • Returns Double.NaN if values has length 0
  • Returns (for any value of p) values[0] if values has length 1
  • Throws MathIllegalArgumentException if values is null or p is not a valid quantile value (p must be greater than 0 and less than or equal to 100)

See Percentile for a description of the percentile estimation algorithm used.

Parameters
values:double[]

input array of values

p:double

the percentile value to compute

Returns:double

the percentile value or Double.NaN if the array is empty

Exceptions
MathIllegalArgumentException:
if values is null or p is invalid
evaluateback to summary
public double evaluate(final double[] values, final int start, final int length) throws MathIllegalArgumentException

Implements abstract org.apache.commons.math3.stat.descriptive.AbstractUnivariateStatistic.evaluate.

Implements org.apache.commons.math3.stat.descriptive.UnivariateStatistic.evaluate.

Returns an estimate of the quantileth percentile of the designated values in the values array. The quantile estimated is determined by the quantile property.

  • Returns Double.NaN if length = 0
  • Returns (for any value of quantile) values[begin] if length = 1
  • Throws MathIllegalArgumentException if values is null, or start or length is invalid

See Percentile for a description of the percentile estimation algorithm used.

Parameters
values:double[]

the input array

start:int

index of the first array element to include

length:int

the number of elements to include

Returns:double

the percentile value

Annotations
@Override
Exceptions
MathIllegalArgumentException:
if the parameters are not valid
evaluateback to summary
public double evaluate(final double[] values, final int begin, final int length, final double p) throws MathIllegalArgumentException

Returns an estimate of the pth percentile of the values in the values array, starting with the element in (0-based) position begin in the array and including length values.

Calls to this method do not modify the internal quantile state of this statistic.

  • Returns Double.NaN if length = 0
  • Returns (for any value of p) values[begin] if length = 1
  • Throws MathIllegalArgumentException if values is null , begin or length is invalid, or p is not a valid quantile value (p must be greater than 0 and less than or equal to 100)

See Percentile for a description of the percentile estimation algorithm used.

Parameters
values:double[]

array of input values

begin:int

the first (0-based) element to include in the computation

length:int

the number of array elements to include

p:double

the percentile to compute

Returns:double

the percentile value

Exceptions
MathIllegalArgumentException:
if the parameters are not valid or the input array is null
getEstimationTypeback to summary
public Percentile.EstimationType getEstimationType()

Get the estimation type used for computation.

Returns:Percentile.EstimationType

the estimationType set

getKthSelectorback to summary
public KthSelector getKthSelector()

Get the kthSelector used for computation.

Returns:KthSelector

the kthSelector set

getNaNStrategyback to summary
public NaNStrategy getNaNStrategy()

Get the NaN Handling strategy used for computation.

Returns:NaNStrategy

NaN Handling strategy set during construction

getPivotingStrategyback to summary
public PivotingStrategyInterface getPivotingStrategy()

Get the PivotingStrategyInterface used in KthSelector for computation.

Returns:PivotingStrategyInterface

the pivoting strategy set

getPivotsback to summary
private int[] getPivots(final double[] values)

Get pivots which is either cached or a newly created one

Parameters
values:double[]

array containing the input numbers

Returns:int[]

cached pivots or a newly created one

getQuantileback to summary
public double getQuantile()

Returns the value of the quantile field (determines what percentile is computed when evaluate() is called with no quantile argument).

Returns:double

quantile set while construction or setQuantile(double)

getWorkArrayback to summary
protected double[] getWorkArray(final double[] values, final int begin, final int length)

Get the work array to operate. Makes use of prior storedData if it exists or else do a check on NaNs and copy a subset of the array defined by begin and length parameters. The set nanStrategy will be used to either retain/remove/replace any NaNs present before returning the resultant array.

Parameters
values:double[]

the array of numbers

begin:int

index to start reading the array

length:int

the length of array to be read from the begin index

Returns:double[]

work array sliced from values in the range [begin,begin+length)

Exceptions
MathIllegalArgumentException:
if values or indices are invalid
medianOf3back to summary
pack-priv int medianOf3(final double[] work, final int begin, final int end)

Deprecated

Please refrain from using this method (as it wont take effect) and instead use Percentile#withKthSelector(newKthSelector) if required.

Select a pivot index as the median of three

Note

With the effect of allowing KthSelector to be set on Percentile instances(thus indirectly PivotingStrategy) this method wont take effect any more and hence is unsupported.

Parameters
work:double[]

data array

begin:int

index of the first element of the slice

end:int

index after the last element of the slice

Returns:int

the index of the median element chosen between the first, the middle and the last element of the array slice

Annotations
@Deprecated
removeAndSliceback to summary
private static double[] removeAndSlice(final double[] values, final int begin, final int length, final double removedValue)

Remove the occurrence of a given value in a copied slice of array defined by the array part from [begin, begin+length).

Parameters
values:double[]

the input array

begin:int

start index of the array to include

length:int

number of elements to include from begin

removedValue:double

the value to be removed from the sliced array

Returns:double[]

the copy of the sliced array after removing the removedValue

replaceAndSliceback to summary
private static double[] replaceAndSlice(final double[] values, final int begin, final int length, final double original, final double replacement)

Replace every occurrence of a given value with a replacement value in a copied slice of array defined by array part from [begin, begin+length).

Parameters
values:double[]

the input array

begin:int

start index of the array to include

length:int

number of elements to include from begin

original:double

the value to be replaced with

replacement:double

the value to be used for replacement

Returns:double[]

the copy of sliced array with replaced values

setDataback to summary
public void setData(final double[] values)

Overrides org.apache.commons.math3.stat.descriptive.AbstractUnivariateStatistic.setData.

Doc from org.apache.commons.math3.stat.descriptive.AbstractUnivariateStatistic.setData.

Set the data array.

The stored value is a copy of the parameter array, not the array itself.

Parameters
values:double[]

data array to store (may be null to remove stored data)

Annotations
@Override
setDataback to summary
public void setData(final double[] values, final int begin, final int length) throws MathIllegalArgumentException

Overrides org.apache.commons.math3.stat.descriptive.AbstractUnivariateStatistic.setData.

Doc from org.apache.commons.math3.stat.descriptive.AbstractUnivariateStatistic.setData.

Set the data array. The input array is copied, not referenced.

Parameters
values:double[]

data array to store

begin:int

the index of the first element to include

length:int

the number of elements to include

Annotations
@Override
Exceptions
MathIllegalArgumentException:
if values is null or the indices are not valid
setQuantileback to summary
public void setQuantile(final double p) throws MathIllegalArgumentException

Sets the value of the quantile field (determines what percentile is computed when evaluate() is called with no quantile argument).

Parameters
p:double

a value between 0 < p <= 100

Exceptions
MathIllegalArgumentException:
if p is not greater than 0 and less than or equal to 100
withEstimationTypeback to summary
public Percentile withEstimationType(final Percentile.EstimationType newEstimationType)

Build a new instance similar to the current one except for the estimation type.

This method is intended to be used as part of a fluent-type builder pattern. Building finely tune instances should be done as follows:

  Percentile customized = new Percentile(quantile).
                          withEstimationType(estimationType).
                          withNaNStrategy(nanStrategy).
                          withKthSelector(kthSelector);

If any of the withXxx method is omitted, the default value for the corresponding customization parameter will be used.

Parameters
newEstimationType:Percentile.EstimationType

estimation type for the new instance

Returns:Percentile

a new instance, with changed estimation type

Exceptions
NullArgumentException:
when newEstimationType is null
withKthSelectorback to summary
public Percentile withKthSelector(final KthSelector newKthSelector)

Build a new instance similar to the current one except for the kthSelector instance specifically set.

This method is intended to be used as part of a fluent-type builder pattern. Building finely tune instances should be done as follows:

  Percentile customized = new Percentile(quantile).
                          withEstimationType(estimationType).
                          withNaNStrategy(nanStrategy).
                          withKthSelector(newKthSelector);

If any of the withXxx method is omitted, the default value for the corresponding customization parameter will be used.

Parameters
newKthSelector:KthSelector

KthSelector for the new instance

Returns:Percentile

a new instance, with changed KthSelector

Exceptions
NullArgumentException:
when newKthSelector is null
withNaNStrategyback to summary
public Percentile withNaNStrategy(final NaNStrategy newNaNStrategy)

Build a new instance similar to the current one except for the NaN handling strategy.

This method is intended to be used as part of a fluent-type builder pattern. Building finely tune instances should be done as follows:

  Percentile customized = new Percentile(quantile).
                          withEstimationType(estimationType).
                          withNaNStrategy(nanStrategy).
                          withKthSelector(kthSelector);

If any of the withXxx method is omitted, the default value for the corresponding customization parameter will be used.

Parameters
newNaNStrategy:NaNStrategy

NaN strategy for the new instance

Returns:Percentile

a new instance, with changed NaN handling strategy

Exceptions
NullArgumentException:
when newNaNStrategy is null
org.apache.commons.math3.stat.descriptive.rank back to summary

public sealed Enum Percentile.EstimationType

extends Enum<Percentile.EstimationType>
Class Inheritance

An enum for various estimation strategies of a percentile referred in wikipedia on quantile with the names of enum matching those of types mentioned in wikipedia.

Each enum corresponding to the specific type of estimation in wikipedia implements the respective formulae that specializes in the below aspects

Users can now create Percentile by explicitly passing this enum; such as by invoking Percentile#withEstimationType(EstimationType)

References:

  1. Wikipedia on quantile
  2. Hyndman, R. J. and Fan, Y. (1996) Sample quantiles in statistical packages, American Statistician 50, 361–365
  3. R-Manual

Field Summary

Modifier and TypeField and Description
public static final Percentile.EstimationType
LEGACY

This is the default type used in the Percentile.This method has the following formulae for index and estimates \( \begin{align} &index = (N+1)p\ \\ &estimate = x_{\lceil h\,-\,1/2 \rceil} \\ &minLimit = 0 \\ &maxLimit = 1 \\ \end{align}\)

private final String
name

Hides java.lang.Enum.name.

Simple name such as R-1, R-2 corresponding to those in wikipedia.
public static final Percentile.EstimationType
R_1

The method R_1 has the following formulae for index and estimates \( \begin{align} &index= Np + 1/2\, \\ &estimate= x_{\lceil h\,-\,1/2 \rceil} \\ &minLimit = 0 \\ \end{align}\)

public static final Percentile.EstimationType
R_2

The method R_2 has the following formulae for index and estimates \( \begin{align} &index= Np + 1/2\, \\ &estimate=\frac{x_{\lceil h\,-\,1/2 \rceil} + x_{\lfloor h\,+\,1/2 \rfloor}}{2} \\ &minLimit = 0 \\ &maxLimit = 1 \\ \end{align}\)

public static final Percentile.EstimationType
R_3

The method R_3 has the following formulae for index and estimates \( \begin{align} &index= Np \\ &estimate= x_{\lfloor h \rceil}\, \\ &minLimit = 0.5/N \\ \end{align}\)

public static final Percentile.EstimationType
R_4

The method R_4 has the following formulae for index and estimates \( \begin{align} &index= Np\, \\ &estimate= x_{\lfloor h \rfloor} + (h - \lfloor h \rfloor) (x_{\lfloor h \rfloor + 1} - x_{\lfloor h \rfloor}) \\ &minLimit = 1/N \\ &maxLimit = 1 \\ \end{align}\)

public static final Percentile.EstimationType
R_5

The method R_5 has the following formulae for index and estimates \( \begin{align} &index= Np + 1/2\\ &estimate= x_{\lfloor h \rfloor} + (h - \lfloor h \rfloor) (x_{\lfloor h \rfloor + 1} - x_{\lfloor h \rfloor}) \\ &minLimit = 0.5/N \\ &maxLimit = (N-0.5)/N \end{align}\)

public static final Percentile.EstimationType
R_6

The method R_6 has the following formulae for index and estimates \( \begin{align} &index= (N + 1)p \\ &estimate= x_{\lfloor h \rfloor} + (h - \lfloor h \rfloor) (x_{\lfloor h \rfloor + 1} - x_{\lfloor h \rfloor}) \\ &minLimit = 1/(N+1) \\ &maxLimit = N/(N+1) \\ \end{align}\)

Note

This method computes the index in a manner very close to the default Commons Math Percentile existing implementation.

public static final Percentile.EstimationType
R_7

The method R_7 implements Microsoft Excel style computation has the following formulae for index and estimates.

public static final Percentile.EstimationType
R_8

The method R_8 has the following formulae for index and estimates \( \begin{align} &index = (N + 1/3)p + 1/3 \\ &estimate = x_{\lfloor h \rfloor} + (h - \lfloor h \rfloor) (x_{\lfloor h \rfloor + 1} - x_{\lfloor h \rfloor}) \\ &minLimit = (2/3)/(N+1/3) \\ &maxLimit = (N-1/3)/(N+1/3) \\ \end{align}\)

As per Ref [2,3] this approach is most recommended as it provides an approximate median-unbiased estimate regardless of distribution.

public static final Percentile.EstimationType
R_9

The method R_9 has the following formulae for index and estimates \( \begin{align} &index = (N + 1/4)p + 3/8\\ &estimate = x_{\lfloor h \rfloor} + (h - \lfloor h \rfloor) (x_{\lfloor h \rfloor + 1} - x_{\lfloor h \rfloor}) \\ &minLimit = (5/8)/(N+1/4) \\ &maxLimit = (N-3/8)/(N+1/4) \\ \end{align}\)

Constructor Summary

AccessConstructor and Description
private
EstimationType(final String
name of estimation type as per wikipedia
type
)

Constructor

Method Summary

Modifier and TypeMethod and Description
protected double

Returns:

estimated percentile
estimate
(final double[]
array of numbers to be used for finding the percentile
work
,
final int[]
an earlier populated cache if exists; will be used
pivotsHeap
,
final double
indicated positional index prior computed from calling index(double, int)
pos
,
final int
size of array considered
length
,
final KthSelector
a KthSelector used for pivoting during search
selector
)

Estimation based on Kth selection.

protected double

Returns:

estimated percentile
evaluate
(final double[]
array of numbers to be used for finding the percentile
work
,
final int[]
a prior cached heap which can speed up estimation
pivotsHeap
,
final double
the pth quantile to be computed
p
,
final KthSelector
a KthSelector used for pivoting during search
selector
)

Evaluate method to compute the percentile for a given bounded array using earlier computed pivots heap.

public double

Returns:

estimated percentile
evaluate
(final double[]
array of numbers to be used for finding the percentile
work
,
final double
the pth quantile to be computed
p
,
final KthSelector
a KthSelector used for pivoting during search
selector
)

Evaluate method to compute the percentile for a given bounded array.

pack-priv String

Returns:

the name
getName
()

Gets the name of the enum

protected abstract double

Returns:

a computed real valued index as explained in the wikipedia
index
(final double
the pth quantile
p
,
final int
the total number of array elements in the work array
length
)

Finds the index of array that can be used as starting index to estimate percentile.

public static Percentile.EstimationType
public static Percentile.EstimationType[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

LEGACYback to summary
public static final Percentile.EstimationType LEGACY

This is the default type used in the Percentile.This method has the following formulae for index and estimates
\( \begin{align} &index = (N+1)p\ \\ &estimate = x_{\lceil h\,-\,1/2 \rceil} \\ &minLimit = 0 \\ &maxLimit = 1 \\ \end{align}\)

nameback to summary
private final String name

Hides java.lang.Enum.name.

Simple name such as R-1, R-2 corresponding to those in wikipedia.

R_1back to summary
public static final Percentile.EstimationType R_1

The method R_1 has the following formulae for index and estimates
\( \begin{align} &index= Np + 1/2\, \\ &estimate= x_{\lceil h\,-\,1/2 \rceil} \\ &minLimit = 0 \\ \end{align}\)

R_2back to summary
public static final Percentile.EstimationType R_2

The method R_2 has the following formulae for index and estimates
\( \begin{align} &index= Np + 1/2\, \\ &estimate=\frac{x_{\lceil h\,-\,1/2 \rceil} + x_{\lfloor h\,+\,1/2 \rfloor}}{2} \\ &minLimit = 0 \\ &maxLimit = 1 \\ \end{align}\)

R_3back to summary
public static final Percentile.EstimationType R_3

The method R_3 has the following formulae for index and estimates
\( \begin{align} &index= Np \\ &estimate= x_{\lfloor h \rceil}\, \\ &minLimit = 0.5/N \\ \end{align}\)

R_4back to summary
public static final Percentile.EstimationType R_4

The method R_4 has the following formulae for index and estimates
\( \begin{align} &index= Np\, \\ &estimate= x_{\lfloor h \rfloor} + (h - \lfloor h \rfloor) (x_{\lfloor h \rfloor + 1} - x_{\lfloor h \rfloor}) \\ &minLimit = 1/N \\ &maxLimit = 1 \\ \end{align}\)

R_5back to summary
public static final Percentile.EstimationType R_5

The method R_5 has the following formulae for index and estimates
\( \begin{align} &index= Np + 1/2\\ &estimate= x_{\lfloor h \rfloor} + (h - \lfloor h \rfloor) (x_{\lfloor h \rfloor + 1} - x_{\lfloor h \rfloor}) \\ &minLimit = 0.5/N \\ &maxLimit = (N-0.5)/N \end{align}\)

R_6back to summary
public static final Percentile.EstimationType R_6

The method R_6 has the following formulae for index and estimates
\( \begin{align} &index= (N + 1)p \\ &estimate= x_{\lfloor h \rfloor} + (h - \lfloor h \rfloor) (x_{\lfloor h \rfloor + 1} - x_{\lfloor h \rfloor}) \\ &minLimit = 1/(N+1) \\ &maxLimit = N/(N+1) \\ \end{align}\)

Note

This method computes the index in a manner very close to the default Commons Math Percentile existing implementation. However the difference to be noted is in picking up the limits with which first element (p<1(N+1)) and last elements (p>N/(N+1))are done. While in default case; these are done with p=0 and p=1 respectively.

R_7back to summary
public static final Percentile.EstimationType R_7

The method R_7 implements Microsoft Excel style computation has the following formulae for index and estimates.
\( \begin{align} &index = (N-1)p + 1 \\ &estimate = x_{\lfloor h \rfloor} + (h - \lfloor h \rfloor) (x_{\lfloor h \rfloor + 1} - x_{\lfloor h \rfloor}) \\ &minLimit = 0 \\ &maxLimit = 1 \\ \end{align}\)

R_8back to summary
public static final Percentile.EstimationType R_8

The method R_8 has the following formulae for index and estimates
\( \begin{align} &index = (N + 1/3)p + 1/3 \\ &estimate = x_{\lfloor h \rfloor} + (h - \lfloor h \rfloor) (x_{\lfloor h \rfloor + 1} - x_{\lfloor h \rfloor}) \\ &minLimit = (2/3)/(N+1/3) \\ &maxLimit = (N-1/3)/(N+1/3) \\ \end{align}\)

As per Ref [2,3] this approach is most recommended as it provides an approximate median-unbiased estimate regardless of distribution.

R_9back to summary
public static final Percentile.EstimationType R_9

The method R_9 has the following formulae for index and estimates
\( \begin{align} &index = (N + 1/4)p + 3/8\\ &estimate = x_{\lfloor h \rfloor} + (h - \lfloor h \rfloor) (x_{\lfloor h \rfloor + 1} - x_{\lfloor h \rfloor}) \\ &minLimit = (5/8)/(N+1/4) \\ &maxLimit = (N-3/8)/(N+1/4) \\ \end{align}\)

Constructor Detail

EstimationTypeback to summary
private EstimationType(final String type)

Constructor

Parameters
type:String

name of estimation type as per wikipedia

Method Detail

estimateback to summary
protected double estimate(final double[] work, final int[] pivotsHeap, final double pos, final int length, final KthSelector selector)

Estimation based on Kth selection. This may be overridden in specific enums to compute slightly different estimations.

Parameters
work:double[]

array of numbers to be used for finding the percentile

pivotsHeap:int[]

an earlier populated cache if exists; will be used

pos:double

indicated positional index prior computed from calling index(double, int)

length:int

size of array considered

selector:KthSelector

a KthSelector used for pivoting during search

Returns:double

estimated percentile

evaluateback to summary
protected double evaluate(final double[] work, final int[] pivotsHeap, final double p, final KthSelector selector)

Evaluate method to compute the percentile for a given bounded array using earlier computed pivots heap.
This basically calls the index and then estimate functions to return the estimated percentile value.

Parameters
work:double[]

array of numbers to be used for finding the percentile

pivotsHeap:int[]

a prior cached heap which can speed up estimation

p:double

the pth quantile to be computed

selector:KthSelector

a KthSelector used for pivoting during search

Returns:double

estimated percentile

Exceptions
OutOfRangeException:
if p is out of range
NullArgumentException:
if work array is null
evaluateback to summary
public double evaluate(final double[] work, final double p, final KthSelector selector)

Evaluate method to compute the percentile for a given bounded array. This basically calls the index and then estimate functions to return the estimated percentile value. Please note that this method does not make use of cached pivots.

Parameters
work:double[]

array of numbers to be used for finding the percentile

p:double

the pth quantile to be computed

selector:KthSelector

a KthSelector used for pivoting during search

Returns:double

estimated percentile

Exceptions
OutOfRangeException:
if length or p is out of range
NullArgumentException:
if work array is null
getNameback to summary
pack-priv String getName()

Gets the name of the enum

Returns:String

the name

indexback to summary
protected abstract double index(final double p, final int length)

Finds the index of array that can be used as starting index to estimate percentile. The calculation of index calculation is specific to each EstimationType.

Parameters
p:double

the pth quantile

length:int

the total number of array elements in the work array

Returns:double

a computed real valued index as explained in the wikipedia

valueOfback to summary
public static Percentile.EstimationType valueOf(String name)
valuesback to summary
public static Percentile.EstimationType[] values()