Class DoubleStatistics
- java.lang.Object
-
- org.apache.commons.statistics.descriptive.DoubleStatistics
-
- All Implemented Interfaces:
DoubleConsumer
public final class DoubleStatistics extends Object implements DoubleConsumer
Statistics fordouble
values.This class provides combinations of individual statistic implementations in the
org.apache.commons.statistics.descriptive
package.Supports up to 263 (exclusive) observations. This implementation does not check for overflow of the count.
- Since:
- 1.1
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DoubleStatistics.Builder
A builder forDoubleStatistics
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
accept(double value)
Updates the state of the statistics to reflect the addition ofvalue
.static DoubleStatistics.Builder
builder(Statistic... statistics)
Returns a new builder configured to create instances to compute the specifiedstatistics
.DoubleStatistics
combine(DoubleStatistics other)
Combines the state of theother
statistics into this one.double
getAsDouble(Statistic statistic)
Gets the value of the specifiedstatistic
as adouble
.long
getCount()
Return the count of values recorded.StatisticResult
getResult(Statistic statistic)
Gets a supplier for the value of the specifiedstatistic
.boolean
isSupported(Statistic statistic)
Check if the specifiedstatistic
is supported.static DoubleStatistics
of(Set<Statistic> statistics, double... values)
Returns a new instance configured to compute the specifiedstatistics
populated using the inputvalues
.static DoubleStatistics
of(Statistic... statistics)
Returns a new instance configured to compute the specifiedstatistics
.DoubleStatistics
setConfiguration(StatisticsConfiguration v)
Sets the statistics configuration.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.function.DoubleConsumer
andThen
-
-
-
-
Method Detail
-
of
public static DoubleStatistics of(Statistic... statistics)
Returns a new instance configured to compute the specifiedstatistics
.The statistics will be empty and so will return the default values for each computed statistic.
- Parameters:
statistics
- Statistics to compute.- Returns:
- the instance
- Throws:
IllegalArgumentException
- if there are nostatistics
to compute.
-
of
public static DoubleStatistics of(Set<Statistic> statistics, double... values)
Returns a new instance configured to compute the specifiedstatistics
populated using the inputvalues
.Use this method to create an instance populated with a (variable) array of
double[]
data:DoubleStatistics stats = DoubleStatistics.of( EnumSet.of(Statistic.MIN, Statistic.MAX), 1, 1, 2, 3, 5, 8, 13);
- Parameters:
statistics
- Statistics to compute.values
- Values.- Returns:
- the instance
- Throws:
IllegalArgumentException
- if there are nostatistics
to compute.
-
builder
public static DoubleStatistics.Builder builder(Statistic... statistics)
Returns a new builder configured to create instances to compute the specifiedstatistics
.Use this method to create an instance populated with an array of
double[]
data using theDoubleStatistics.Builder.build(double...)
method:double[] data = ... DoubleStatistics stats = DoubleStatistics.builder( Statistic.MIN, Statistic.MAX, Statistic.VARIANCE) .build(data);
The builder can be used to create multiple instances of
DoubleStatistics
to be used in parallel, or on separate arrays ofdouble[]
data. These may becombined
. For example:double[][] data = ... DoubleStatistics.Builder builder = DoubleStatistics.builder( Statistic.MIN, Statistic.MAX, Statistic.VARIANCE); DoubleStatistics stats = Arrays.stream(data) .parallel() .map(builder::build) .reduce(DoubleStatistics::combine) .get();
The builder can be used to create a
Collector
for repeat use on multiple data:DoubleStatistics.Builder builder = DoubleStatistics.builder( Statistic.MIN, Statistic.MAX, Statistic.VARIANCE); Collector<double[], DoubleStatistics, DoubleStatistics> collector = Collector.of(builder::build, (s, d) -> s.combine(builder.build(d)), DoubleStatistics::combine); // Repeated double[][] data = ... DoubleStatistics stats = Arrays.stream(data).collect(collector);
- Parameters:
statistics
- Statistics to compute.- Returns:
- the builder
- Throws:
IllegalArgumentException
- if there are nostatistics
to compute.
-
accept
public void accept(double value)
Updates the state of the statistics to reflect the addition ofvalue
.- Specified by:
accept
in interfaceDoubleConsumer
- Parameters:
value
- Value.
-
getCount
public long getCount()
Return the count of values recorded.- Returns:
- the count of values
-
isSupported
public boolean isSupported(Statistic statistic)
Check if the specifiedstatistic
is supported.Note: This method will not return
false
if the argument isnull
.- Parameters:
statistic
- Statistic.- Returns:
true
if supported- Throws:
NullPointerException
- if thestatistic
isnull
- See Also:
getAsDouble(Statistic)
-
getAsDouble
public double getAsDouble(Statistic statistic)
Gets the value of the specifiedstatistic
as adouble
.- Parameters:
statistic
- Statistic.- Returns:
- the value
- Throws:
IllegalArgumentException
- if thestatistic
is not supported- See Also:
isSupported(Statistic)
,getResult(Statistic)
-
getResult
public StatisticResult getResult(Statistic statistic)
Gets a supplier for the value of the specifiedstatistic
.The returned function will supply the correct result after calls to
accept
orcombine
further values intothis
instance.This method can be used to perform a one-time look-up of the statistic function to compute statistics as values are dynamically added.
- Parameters:
statistic
- Statistic.- Returns:
- the supplier
- Throws:
IllegalArgumentException
- if thestatistic
is not supported- See Also:
isSupported(Statistic)
,getAsDouble(Statistic)
-
combine
public DoubleStatistics combine(DoubleStatistics other)
Combines the state of theother
statistics into this one. Onlythis
instance is modified by thecombine
operation.The
other
instance must be compatible. This istrue
if theother
instance returnstrue
forisSupported(Statistic)
for all values of theStatistic
enum which are supported bythis
instance.Note that this operation is not symmetric. It may be possible to perform
a.combine(b)
but notb.combine(a)
. In the event that theother
instance is not compatible then an exception is raised before any state is modified.- Parameters:
other
- Another set of statistics to be combined.- Returns:
this
instance after combiningother
.- Throws:
IllegalArgumentException
- if theother
is not compatible
-
setConfiguration
public DoubleStatistics setConfiguration(StatisticsConfiguration v)
Sets the statistics configuration.These options only control the final computation of statistics. The configuration will not affect compatibility between instances during a
combine
operation.Note: These options will affect any future computation of statistics. Supplier functions that have been previously created will not be updated with the new configuration.
- Parameters:
v
- Value.- Returns:
this
instance- Throws:
NullPointerException
- if the value is null- See Also:
getResult(Statistic)
-
-