Class Percentile
- java.lang.Object
- 
- org.apache.commons.math.stat.descriptive.AbstractUnivariateStatistic
- 
- org.apache.commons.math.stat.descriptive.rank.Percentile
 
 
- 
- All Implemented Interfaces:
- java.io.Serializable,- UnivariateStatistic
 - Direct Known Subclasses:
- Median
 
 public class Percentile extends AbstractUnivariateStatistic implements java.io.Serializable 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: - Let nbe the length of the (sorted) array and0 < p <= 100be the desired percentile.
- If n = 1return the unique array element (regardless of the value ofp); otherwise
- Compute the estimated percentile position
 pos = p * (n + 1) / 100and the difference,dbetweenposandfloor(pos)(i.e. the fractional part ofpos). Ifpos >= nreturn the largest element in the array; otherwise
- Let lowerbe the element in positionfloor(pos)in the array and letupperbe the next element in the array. Returnlower + 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 byDouble.compareTo(Double). This ordering makesDouble.NaNlarger than any other value (includingDouble.POSITIVE_INFINITY). Therefore, for example, the median (50th percentile) of{0, 1, 2, 3, 4, Double.NaN}evaluates to2.5.Since percentile estimation usually involves interpolation between array elements, arrays containing NaNor infinite values will often result inNaNor infinite values returned.Since 2.2, Percentile implementation uses only selection instead of complete sorting and caches selection algorithm state between calls to the various evaluatemethods when several percentiles are to be computed on the same data. This greatly improves efficiency, both for single percentile and multiple percentiles computations. However, it also induces a need to be sure the data at one call toevaluateis the same as the data with the cached algorithm state from the previous calls. Percentile does this by checking the array reference itself and a checksum of its content by default. If the user already knows he callsevaluateon an immutable array, he can save the checking time by calling theevaluatemethods that do notNote 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()orclear()method, it must be synchronized externally.- Version:
- $Revision: 1006299 $ $Date: 2010-10-10 16:47:17 +0200 (dim. 10 oct. 2010) $
- See Also:
- Serialized Form
 
- 
- 
Constructor SummaryConstructors Constructor Description Percentile()Constructs a Percentile with a default quantile value of 50.0.Percentile(double p)Constructs a Percentile with the specific quantile value.Percentile(Percentile original)Copy constructor, creates a newPercentileidentical to theoriginal
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Percentilecopy()Returns a copy of the statistic with the same internal state.static voidcopy(Percentile source, Percentile dest)Copies source to dest.doubleevaluate(double p)Returns the result of evaluating the statistic over the stored data.doubleevaluate(double[] values, double p)Returns an estimate of thepth percentile of the values in thevaluesarray.doubleevaluate(double[] values, int start, int length)Returns an estimate of thequantileth percentile of the designated values in thevaluesarray.doubleevaluate(double[] values, int begin, int length, double p)Returns an estimate of thepth percentile of the values in thevaluesarray, starting with the element in (0-based) positionbeginin the array and includinglengthvalues.doublegetQuantile()Returns the value of the quantile field (determines what percentile is computed when evaluate() is called with no quantile argument).voidsetData(double[] values)Set the data array.voidsetData(double[] values, int begin, int length)Set the data array.voidsetQuantile(double p)Sets the value of the quantile field (determines what percentile is computed when evaluate() is called with no quantile argument).- 
Methods inherited from class org.apache.commons.math.stat.descriptive.AbstractUnivariateStatisticevaluate, evaluate, getData, getDataRef, test, test
 
- 
 
- 
- 
- 
Constructor Detail- 
Percentilepublic Percentile() Constructs a Percentile with a default quantile value of 50.0.
 - 
Percentilepublic Percentile(double p) Constructs a Percentile with the specific quantile value.- Parameters:
- p- the quantile
- Throws:
- java.lang.IllegalArgumentException- if p is not greater than 0 and less than or equal to 100
 
 - 
Percentilepublic Percentile(Percentile original) Copy constructor, creates a newPercentileidentical to theoriginal- Parameters:
- original- the- Percentileinstance to copy
 
 
- 
 - 
Method Detail- 
setDatapublic void setData(double[] values) Set the data array.The stored value is a copy of the parameter array, not the array itself - Overrides:
- setDatain class- AbstractUnivariateStatistic
- Parameters:
- values- data array to store (may be null to remove stored data)
- See Also:
- AbstractUnivariateStatistic.evaluate()
 
 - 
setDatapublic void setData(double[] values, int begin, int length)Set the data array.- Overrides:
- setDatain class- AbstractUnivariateStatistic
- Parameters:
- values- data array to store
- begin- the index of the first element to include
- length- the number of elements to include
- See Also:
- AbstractUnivariateStatistic.evaluate()
 
 - 
evaluatepublic double evaluate(double p) Returns the result of evaluating the statistic over the stored data.The stored array is the one which was set by previous calls to - Parameters:
- p- the percentile value to compute
- Returns:
- the value of the statistic applied to the stored data
 
 - 
evaluatepublic double evaluate(double[] values, double p)Returns an estimate of thepth percentile of the values in thevaluesarray.Calls to this method do not modify the internal quantilestate of this statistic.- Returns Double.NaNifvalueshas length0
- Returns (for any value of p)values[0]ifvalueshas length1
- Throws IllegalArgumentExceptionifvaluesis null or p is not a valid quantile value (p must be greater than 0 and less than or equal to 100)
 See Percentilefor a description of the percentile estimation algorithm used.- Parameters:
- values- input array of values
- p- the percentile value to compute
- Returns:
- the percentile value or Double.NaN if the array is empty
- Throws:
- java.lang.IllegalArgumentException- if- valuesis null or p is invalid
 
- Returns 
 - 
evaluatepublic double evaluate(double[] values, int start, int length)Returns an estimate of thequantileth percentile of the designated values in thevaluesarray. The quantile estimated is determined by thequantileproperty.- Returns Double.NaNiflength = 0
- Returns (for any value of quantile)values[begin]iflength = 1
- Throws IllegalArgumentExceptionifvaluesis null, orstartorlengthis invalid
 See Percentilefor a description of the percentile estimation algorithm used.- Specified by:
- evaluatein interface- UnivariateStatistic
- Specified by:
- evaluatein class- AbstractUnivariateStatistic
- Parameters:
- values- the input array
- start- index of the first array element to include
- length- the number of elements to include
- Returns:
- the percentile value
- Throws:
- java.lang.IllegalArgumentException- if the parameters are not valid
 
- Returns 
 - 
evaluatepublic double evaluate(double[] values, int begin, int length, double p)Returns an estimate of thepth percentile of the values in thevaluesarray, starting with the element in (0-based) positionbeginin the array and includinglengthvalues.Calls to this method do not modify the internal quantilestate of this statistic.- Returns Double.NaNiflength = 0
- Returns (for any value of p)values[begin]iflength = 1
- Throws IllegalArgumentExceptionifvaluesis null ,beginorlengthis invalid, orpis not a valid quantile value (p must be greater than 0 and less than or equal to 100)
 See Percentilefor a description of the percentile estimation algorithm used.- Parameters:
- values- array of input values
- p- the percentile to compute
- begin- the first (0-based) element to include in the computation
- length- the number of array elements to include
- Returns:
- the percentile value
- Throws:
- java.lang.IllegalArgumentException- if the parameters are not valid or the input array is null
 
- Returns 
 - 
getQuantilepublic double getQuantile() Returns the value of the quantile field (determines what percentile is computed when evaluate() is called with no quantile argument).- Returns:
- quantile
 
 - 
setQuantilepublic void setQuantile(double p) Sets the value of the quantile field (determines what percentile is computed when evaluate() is called with no quantile argument).- Parameters:
- p- a value between 0 < p <= 100
- Throws:
- java.lang.IllegalArgumentException- if p is not greater than 0 and less than or equal to 100
 
 - 
copypublic Percentile copy() Returns a copy of the statistic with the same internal state.- Specified by:
- copyin interface- UnivariateStatistic
- Specified by:
- copyin class- AbstractUnivariateStatistic
- Returns:
- a copy of the statistic
 
 - 
copypublic static void copy(Percentile source, Percentile dest) Copies source to dest.Neither source nor dest can be null. - Parameters:
- source- Percentile to copy
- dest- Percentile to copy to
- Throws:
- java.lang.NullPointerException- if either source or dest is null
 
 
- 
 
-