Class DoubleStatistics
- All Implemented Interfaces:
DoubleConsumer
double 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 -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate StatisticsConfigurationConfiguration options for computation of statistics.private final DoubleConsumerThe consumer of values.private longCount of values recorded.private final MaxTheMaximplementation.private final MinTheMinimplementation.private final FirstMomentThe moment implementation.private static final StringError message for non configured statistics.private final ProductTheProductimplementation.private final SumTheSumimplementation.private final SumOfLogsTheSumOfLogsimplementation.private final SumOfSquaresTheSumOfSquaresimplementation.private static final StringError message for an unsupported statistic. -
Constructor Summary
ConstructorsConstructorDescriptionDoubleStatistics(long count, Min min, Max max, FirstMoment moment, Sum sum, Product product, SumOfSquares sumOfSquares, SumOfLogs sumOfLogs, StatisticsConfiguration config) Create an instance. -
Method Summary
Modifier and TypeMethodDescriptionvoidaccept(double value) Updates the state of the statistics to reflect the addition ofvalue.static DoubleStatistics.BuilderReturns a new builder configured to create instances to compute the specifiedstatistics.combine(DoubleStatistics other) Combines the state of theotherstatistics into this one.doublegetAsDouble(Statistic statistic) Gets the value of the specifiedstatisticas adouble.longgetCount()Return the count of values recorded.private StatisticResultGets the geometric mean.private StatisticResultGets the kurtosis.private StatisticResultgetMean()Gets the mean.Gets a supplier for the value of the specifiedstatistic.private StatisticResultGets the skewness.private StatisticResultGets the standard deviation.private StatisticResultGets the variance.booleanisSupported(Statistic statistic) Check if the specifiedstatisticis supported.static DoubleStatisticsReturns a new instance configured to compute the specifiedstatisticspopulated using the inputvalues.static DoubleStatisticsReturns a new instance configured to compute the specifiedstatistics.static DoubleStatisticsReturns a new instance configured to compute the specifiedstatisticspopulated using the specified range ofvalues.Sets the statistics configuration.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface DoubleConsumer
andThen
-
Field Details
-
NO_CONFIGURED_STATISTICS
Error message for non configured statistics.- See Also:
-
UNSUPPORTED_STATISTIC
Error message for an unsupported statistic.- See Also:
-
count
private long countCount of values recorded. -
consumer
The consumer of values. -
min
-
max
-
moment
The moment implementation. May be any instance ofFirstMoment. -
sum
-
product
-
sumOfSquares
TheSumOfSquaresimplementation. -
sumOfLogs
-
config
Configuration options for computation of statistics.
-
-
Constructor Details
-
DoubleStatistics
DoubleStatistics(long count, Min min, Max max, FirstMoment moment, Sum sum, Product product, SumOfSquares sumOfSquares, SumOfLogs sumOfLogs, StatisticsConfiguration config) Create an instance.- Parameters:
count- Count of values.min- Min implementation.max- Max implementation.moment- Moment implementation.sum- Sum implementation.product- Product implementation.sumOfSquares- Sum of squares implementation.sumOfLogs- Sum of logs implementation.config- Statistics configuration.
-
-
Method Details
-
of
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 nostatisticsto compute.
-
of
Returns a new instance configured to compute the specifiedstatisticspopulated 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 nostatisticsto compute.
-
ofRange
public static DoubleStatistics ofRange(Set<Statistic> statistics, double[] values, int from, int to) Returns a new instance configured to compute the specifiedstatisticspopulated using the specified range ofvalues.Use this method to create an instance populated with part of an array of
double[]data, e.g. to use the first half of the data:double[] data = ... DoubleStatistics stats = DoubleStatistics.of( EnumSet.of(Statistic.MIN, Statistic.MAX), data, 0, data.length / 2);- Parameters:
statistics- Statistics to compute.values- Values.from- Inclusive start of the range.to- Exclusive end of the range.- Returns:
- the instance
- Throws:
IllegalArgumentException- if there are nostatisticsto compute.IndexOutOfBoundsException- if the sub-range is out of bounds- Since:
- 1.2
-
builder
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
DoubleStatisticsto 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
Collectorfor 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 nostatisticsto compute.
-
accept
public void accept(double value) Updates the state of the statistics to reflect the addition ofvalue.- Specified by:
acceptin interfaceDoubleConsumer- Parameters:
value- Value.
-
getCount
public long getCount()Return the count of values recorded.- Returns:
- the count of values
-
isSupported
Check if the specifiedstatisticis supported.Note: This method will not return
falseif the argument isnull.- Parameters:
statistic- Statistic.- Returns:
trueif supported- Throws:
NullPointerException- if thestatisticisnull- See Also:
-
getAsDouble
Gets the value of the specifiedstatisticas adouble.- Parameters:
statistic- Statistic.- Returns:
- the value
- Throws:
IllegalArgumentException- if thestatisticis not supported- See Also:
-
getResult
Gets a supplier for the value of the specifiedstatistic.The returned function will supply the correct result after calls to
acceptorcombinefurther values intothisinstance.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 thestatisticis not supported- See Also:
-
getGeometricMean
Gets the geometric mean.- Returns:
- a geometric mean supplier (or null if unsupported)
-
getKurtosis
Gets the kurtosis.- Returns:
- a kurtosis supplier (or null if unsupported)
-
getMean
-
getSkewness
Gets the skewness.- Returns:
- a skewness supplier (or null if unsupported)
-
getStandardDeviation
Gets the standard deviation.- Returns:
- a standard deviation supplier (or null if unsupported)
-
getVariance
Gets the variance.- Returns:
- a variance supplier (or null if unsupported)
-
combine
Combines the state of theotherstatistics into this one. Onlythisinstance is modified by thecombineoperation.The
otherinstance must be compatible. This istrueif theotherinstance returnstrueforisSupported(Statistic)for all values of theStatisticenum which are supported bythisinstance.Note that this operation is not symmetric. It may be possible to perform
a.combine(b)but notb.combine(a). In the event that theotherinstance is not compatible then an exception is raised before any state is modified.- Parameters:
other- Another set of statistics to be combined.- Returns:
thisinstance after combiningother.- Throws:
IllegalArgumentException- if theotheris not compatible
-
setConfiguration
Sets the statistics configuration.These options only control the final computation of statistics. The configuration will not affect compatibility between instances during a
combineoperation.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:
thisinstance- Throws:
NullPointerException- if the value is null- See Also:
-