Class Median
- java.lang.Object
-
- org.apache.commons.statistics.descriptive.Median
-
public final class Median extends Object
Returns the median of the available values.For values of length
n, letk = n / 2:- The result is
NaNifn = 0. - The result is
values[k]ifnis odd. - The result is
(values[k - 1] + values[k]) / 2ifnis even.
This implementation respects the ordering imposed by
Double.compare(double, double)forNaNvalues. If aNaNoccurs in the selected positions in the fully sorted values then the result isNaN.The
NaNPolicycan be used to change the behaviour onNaNvalues.Instances of this class are immutable and thread-safe.
Support for
longarraysThe result on
longvalues can be returned as adoubleor alongusing aStatisticResult.The
doubleresult is the closest representable value following floating-point rounding rules. This may be outside the range defined by the minimum and maximum of the input array following rounding to a 53-bit floating point representation. For example the median of an array containing onlyLong.MAX_VALUEas adoubleis 263, which is the closest representation of 263 - 1.The
longresult is returned using the nearest whole number. In the event of ties the result is rounded towards positive infinity. For example the median of[2, 3]is 3, and of[-2, -3]is -2. This value will always be within the range defined by the minimum and maximum of the input array. Due to interpolation it may be a value not observed in the input values.If the array length
nis zero the result as adoubleisNaNand the result as alongwill raise anArithmeticException.- Since:
- 1.1
- See Also:
with(NaNPolicy), Median (Wikipedia)
- The result is
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description doubleevaluate(double[] values)Evaluate the median.doubleevaluate(int[] values)Evaluate the median.StatisticResultevaluate(long[] values)Evaluate the median.doubleevaluateRange(double[] values, int from, int to)Evaluate the median of the specified range.doubleevaluateRange(int[] values, int from, int to)Evaluate the median of the specified range.StatisticResultevaluateRange(long[] values, int from, int to)Evaluate the median of the specified range.Medianwith(NaNPolicy v)Return an instance with the configuredNaNPolicy.MedianwithCopy(boolean v)Return an instance with the configured copy behaviour.static MedianwithDefaults()Return a new instance with the default options.
-
-
-
Method Detail
-
withDefaults
public static Median withDefaults()
Return a new instance with the default options.Note: The default options configure for processing in-place and including
NaNvalues in the data. This is the most efficient mode and has the smallest memory consumption.- Returns:
- the median implementation
- See Also:
withCopy(boolean),with(NaNPolicy)
-
withCopy
public Median withCopy(boolean v)
Return an instance with the configured copy behaviour. Iffalsethen the input array will be modified by the call to evaluate the median; otherwise the computation uses a copy of the data.- Parameters:
v- Value.- Returns:
- an instance
-
with
public Median with(NaNPolicy v)
Return an instance with the configuredNaNPolicy.Note: This implementation respects the ordering imposed by
Double.compare(double, double)forNaNvalues:NaNis considered greater than all other values, and allNaNvalues are equal. TheNaNPolicychanges the computation of the statistic in the presence ofNaNvalues.NaNPolicy.INCLUDE:NaNvalues are moved to the end of the data; the size of the data includes theNaNvalues and the median will beNaNif any value used for median interpolation isNaN.NaNPolicy.EXCLUDE:NaNvalues are moved to the end of the data; the size of the data excludes theNaNvalues and the median will never beNaNfor non-zero size. If all data areNaNthen the size is zero and the result isNaN.NaNPolicy.ERROR: An exception is raised if the data containsNaNvalues.
Note that the result is identical for all policies if no
NaNvalues are present.- Parameters:
v- Value.- Returns:
- an instance
-
evaluate
public double evaluate(double[] values)
Evaluate the median.Note: This method may partially sort the input values if not configured to
copythe input data.- Parameters:
values- Values.- Returns:
- the median
- Throws:
IllegalArgumentException- if the values contain NaN and the configuration isNaNPolicy.ERROR- See Also:
with(NaNPolicy)
-
evaluateRange
public double evaluateRange(double[] values, int from, int to)
Evaluate the median of the specified range.Note: This method may partially sort the input values if not configured to
copythe input data.- Parameters:
values- Values.from- Inclusive start of the range.to- Exclusive end of the range.- Returns:
- the median
- Throws:
IllegalArgumentException- if the values contain NaN and the configuration isNaNPolicy.ERRORIndexOutOfBoundsException- if the sub-range is out of bounds- Since:
- 1.2
- See Also:
with(NaNPolicy)
-
evaluate
public double evaluate(int[] values)
Evaluate the median.Note: This method may partially sort the input values if not configured to
copythe input data.- Parameters:
values- Values.- Returns:
- the median
-
evaluateRange
public double evaluateRange(int[] values, int from, int to)
Evaluate the median of the specified range.Note: This method may partially sort the input values if not configured to
copythe input data.- Parameters:
values- Values.from- Inclusive start of the range.to- Exclusive end of the range.- Returns:
- the median
- Throws:
IndexOutOfBoundsException- if the sub-range is out of bounds- Since:
- 1.2
-
evaluate
public StatisticResult evaluate(long[] values)
Evaluate the median.If the input length is even the result requires interpolation of two values. The returned median will interpolate the
doubleorlongresult on demand. This is more efficient when only one result is required. Consumers of the result should store the appropriate evaluated value if repeated use is required.Note: This method may partially sort the input values if not configured to
copythe input data.- Parameters:
values- Values.- Returns:
- the median
- Since:
- 1.3
-
evaluateRange
public StatisticResult evaluateRange(long[] values, int from, int to)
Evaluate the median of the specified range.If the input sub-range length is even the result requires interpolation of two values. The returned median will interpolate the
doubleorlongresult on demand. This is more efficient when only one result is required. Consumers of the result should store the appropriate evaluated value if repeated use is required.Note: This method may partially sort the input values if not configured to
copythe input data.- Parameters:
values- Values.from- Inclusive start of the range.to- Exclusive end of the range.- Returns:
- the median
- Throws:
IndexOutOfBoundsException- if the sub-range is out of bounds- Since:
- 1.3
-
-