Class IntStatistics
- All Implemented Interfaces:
IntConsumer
int
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 StatisticsConfiguration
Configuration options for computation of statistics.private final IntConsumer
The consumer of values.private long
Count of values recorded.private final IntMax
TheIntMax
implementation.private final IntMin
TheIntMin
implementation.private final FirstMoment
The moment implementation.private static final String
Error message for non configured statistics.private final Product
TheProduct
implementation.private final IntSum
TheIntSum
implementation.private final SumOfLogs
TheSumOfLogs
implementation.private final IntSumOfSquares
TheIntSumOfSquares
implementation.private static final String
Error message for an unsupported statistic. -
Constructor Summary
ConstructorsConstructorDescriptionIntStatistics
(long count, IntMin min, IntMax max, FirstMoment moment, IntSum sum, Product product, IntSumOfSquares sumOfSquares, SumOfLogs sumOfLogs, StatisticsConfiguration config) Create an instance. -
Method Summary
Modifier and TypeMethodDescriptionvoid
accept
(int value) Updates the state of the statistics to reflect the addition ofvalue
.static IntStatistics.Builder
Returns a new builder configured to create instances to compute the specifiedstatistics
.combine
(IntStatistics other) Combines the state of theother
statistics into this one.private static IntConsumer
composeAsInt
(DoubleConsumer... consumers) Chain theconsumers
into a single compositeIntConsumer
.getAsBigInteger
(Statistic statistic) Gets the value of the specifiedstatistic
as aBigInteger
.double
getAsDouble
(Statistic statistic) Gets the value of the specifiedstatistic
as adouble
.int
Gets the value of the specifiedstatistic
as anint
.long
Gets the value of the specifiedstatistic
as along
.long
getCount()
Return the count of values recorded.private StatisticResult
Gets the geometric mean.private StatisticResult
Gets the kurtosis.private StatisticResult
getMean()
Gets the mean.Gets a supplier for the value of the specifiedstatistic
.private StatisticResult
Gets the skewness.private StatisticResult
Gets the standard deviation.private StatisticResult
Gets the variance.private StatisticResult
getVarianceOrStd
(boolean std) Gets the variance or standard deviation.boolean
isSupported
(Statistic statistic) Check if the specifiedstatistic
is supported.static IntStatistics
Returns a new instance configured to compute the specifiedstatistics
populated using the inputvalues
.static IntStatistics
Returns a new instance configured to compute the specifiedstatistics
.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.IntConsumer
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
TheIntMin
implementation. -
max
TheIntMax
implementation. -
moment
The moment implementation. May be any instance ofFirstMoment
. This implementation uses only the third and fourth moments. -
sum
TheIntSum
implementation. -
product
TheProduct
implementation. -
sumOfSquares
TheIntSumOfSquares
implementation. -
sumOfLogs
TheSumOfLogs
implementation. -
config
Configuration options for computation of statistics.
-
-
Constructor Details
-
IntStatistics
IntStatistics(long count, IntMin min, IntMax max, FirstMoment moment, IntSum sum, Product product, IntSumOfSquares sumOfSquares, SumOfLogs sumOfLogs, StatisticsConfiguration config) Create an instance.- Parameters:
count
- Count of values.min
- IntMin implementation.max
- IntMax implementation.moment
- Moment implementation.sum
- IntSum implementation.product
- Product implementation.sumOfSquares
- Sum of squares implementation.sumOfLogs
- Sum of logs implementation.config
- Statistics configuration.
-
-
Method Details
-
composeAsInt
Chain theconsumers
into a single compositeIntConsumer
. Ignore anynull
consumer.- Parameters:
consumers
- Consumers.- Returns:
- a composed consumer (or null)
-
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 nostatistics
to compute.
-
of
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
int[]
data:IntStatistics stats = IntStatistics.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
Returns a new builder configured to create instances to compute the specifiedstatistics
.Use this method to create an instance populated with an array of
int[]
data using theIntStatistics.Builder.build(int...)
method:int[] data = ... IntStatistics stats = IntStatistics.builder( Statistic.MIN, Statistic.MAX, Statistic.VARIANCE) .build(data);
The builder can be used to create multiple instances of
IntStatistics
to be used in parallel, or on separate arrays ofint[]
data. These may becombined
. For example:int[][] data = ... IntStatistics.Builder builder = IntStatistics.builder( Statistic.MIN, Statistic.MAX, Statistic.VARIANCE); IntStatistics stats = Arrays.stream(data) .parallel() .map(builder::build) .reduce(IntStatistics::combine) .get();
The builder can be used to create a
Collector
for repeat use on multiple data:IntStatistics.Builder builder = IntStatistics.builder( Statistic.MIN, Statistic.MAX, Statistic.VARIANCE); Collector<int[], IntStatistics, IntStatistics> collector = Collector.of(builder::build, (s, d) -> s.combine(builder.build(d)), IntStatistics::combine); // Repeated int[][] data = ... IntStatistics 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(int value) Updates the state of the statistics to reflect the addition ofvalue
.- Specified by:
accept
in interfaceIntConsumer
- Parameters:
value
- Value.
-
getCount
public long getCount()Return the count of values recorded.- Returns:
- the count of values
-
isSupported
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
Gets the value of the specifiedstatistic
as adouble
.- Parameters:
statistic
- Statistic.- Returns:
- the value
- Throws:
IllegalArgumentException
- if thestatistic
is not supported- See Also:
-
getAsInt
Gets the value of the specifiedstatistic
as anint
.Use this method to access the
int
result for exact integer statistics, for exampleStatistic.MIN
.Note: This method may throw an
ArithmeticException
if the result overflows anint
.- Parameters:
statistic
- Statistic.- Returns:
- the value
- Throws:
IllegalArgumentException
- if thestatistic
is not supportedArithmeticException
- if theresult
overflows anint
or is not finite- See Also:
-
getAsLong
Gets the value of the specifiedstatistic
as along
.Use this method to access the
long
result for exact integer statistics, for exampleStatistic.SUM
for acount
less than or equal to 232.Note: This method may throw an
ArithmeticException
if the result overflows anlong
.- Parameters:
statistic
- Statistic.- Returns:
- the value
- Throws:
IllegalArgumentException
- if thestatistic
is not supportedArithmeticException
- if theresult
overflows anlong
or is not finite- See Also:
-
getAsBigInteger
Gets the value of the specifiedstatistic
as aBigInteger
.Use this method to access the
BigInteger
result for exact integer statistics, for exampleStatistic.SUM_OF_SQUARES
.Note: This method may throw an
ArithmeticException
if the result is not finite.- Parameters:
statistic
- Statistic.- Returns:
- the value
- Throws:
IllegalArgumentException
- if thestatistic
is not supportedArithmeticException
- if theresult
is not finite- See Also:
-
getResult
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:
-
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
Gets the mean.- Returns:
- a mean supplier (or null if unsupported)
-
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)
-
getVarianceOrStd
Gets the variance or standard deviation.- Parameters:
std
- Flag to control if the statistic is the standard deviation.- Returns:
- a variance/standard deviation supplier (or null if unsupported)
-
combine
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
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:
-