Class Median
- java.lang.Object
-
- org.apache.commons.statistics.descriptive.Median
-
public final class Median extends java.lang.Object
Returns the median of the available values.For values of length
n
, letk = n / 2
:- The result is
NaN
ifn = 0
. - The result is
values[k]
ifn
is odd. - The result is
(values[k - 1] + values[k]) / 2
ifn
is even.
This implementation respects the ordering imposed by
Double.compare(double, double)
forNaN
values. If aNaN
occurs in the selected positions in the fully sorted values then the result isNaN
.The
NaNPolicy
can be used to change the behaviour onNaN
values.Instances of this class are immutable and thread-safe.
- Since:
- 1.1
- See Also:
with(NaNPolicy)
, Median (Wikipedia)
- The result is
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
copy
Flag to indicate if the data should be copied.private static Median
DEFAULT
Default instance.private NaNPolicy
nanPolicy
NaN policy for floating point data.private NaNTransformer
nanTransformer
Transformer for NaN data.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description double
evaluate(double[] values)
Evaluate the median.double
evaluate(int[] values)
Evaluate the median.Median
with(NaNPolicy v)
Return an instance with the configuredNaNPolicy
.Median
withCopy(boolean v)
Return an instance with the configured copy behaviour.static Median
withDefaults()
Return a new instance with the default options.
-
-
-
Field Detail
-
DEFAULT
private static final Median DEFAULT
Default instance.
-
copy
private final boolean copy
Flag to indicate if the data should be copied.
-
nanPolicy
private final NaNPolicy nanPolicy
NaN policy for floating point data.
-
nanTransformer
private final NaNTransformer nanTransformer
Transformer for NaN data.
-
-
Constructor Detail
-
Median
private Median(boolean copy, NaNPolicy nanPolicy)
- Parameters:
copy
- Flag to indicate if the data should be copied.nanPolicy
- NaN policy.
-
-
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
NaN
values 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. Iffalse
then 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)
forNaN
values:NaN
is considered greater than all other values, and allNaN
values are equal. TheNaNPolicy
changes the computation of the statistic in the presence ofNaN
values.NaNPolicy.INCLUDE
:NaN
values are moved to the end of the data; the size of the data includes theNaN
values and the median will beNaN
if any value used for median interpolation isNaN
.NaNPolicy.EXCLUDE
:NaN
values are moved to the end of the data; the size of the data excludes theNaN
values and the median will never beNaN
for non-zero size. If all data areNaN
then the size is zero and the result isNaN
.NaNPolicy.ERROR
: An exception is raised if the data containsNaN
values.
Note that the result is identical for all policies if no
NaN
values 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
copy
the input data.- Parameters:
values
- Values.- Returns:
- the median
-
evaluate
public double evaluate(int[] values)
Evaluate the median.Note: This method may partially sort the input values if not configured to
copy
the input data.- Parameters:
values
- Values.- Returns:
- the median
-
-