Package io.jenetics.stat
Class IntMomentStatistics
- java.lang.Object
-
- io.jenetics.stat.IntMomentStatistics
-
- All Implemented Interfaces:
IntConsumer
public class IntMomentStatistics extends Object implements IntConsumer
A state object for collecting statistics such as count, min, max, sum, mean, variance, skewness and kurtosis. The design of this class is similar to the design of theIntSummaryStatistics
class.This class is designed to work with (though does not require) streams. For example, you can compute moments-statistics on a stream of ints with:
For a non int stream, you can use a collector:final IntStream stream = ... final IntMomentStatistics statistics = stream.collect( IntMomentStatistics::new, IntMomentStatistics::accept, IntMomentStatistics::combine );
final Stream<SomeObject> stream = ... final IntMomentStatistics statistics = stream .collect(toIntMomentStatistics(v -> v.intValue()));
- Since:
- 3.0
- Version:
- 6.0
- See Also:
IntSummaryStatistics
,IntMoments
, Computing Higher-Order Moments Online- Implementation Note:
- This implementation is not thread safe. However, it is safe to use
toIntMomentStatistics(ToIntFunction)
on a parallel stream, because the parallel implementation ofStream.collect()
provides the necessary partitioning, isolation, and merging of results for safe and efficient parallel execution.
-
-
Constructor Summary
Constructors Constructor Description IntMomentStatistics()
Create an empty moments object.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
accept(int value)
Records a new value into the moments informationIntMomentStatistics
combine(IntMomentStatistics other)
Combine twoIntMoments
statistic objects.long
count()
Returns the count of values recorded.double
kurtosis()
Return the kurtosis of values recorded, orDouble.NaN
if less than four values have been recorded.int
max()
Return the maximum value recorded, orInteger.MIN_VALUE
if no values have been recorded.double
mean()
Return the arithmetic mean of values recorded, orDouble.NaN
if no values have been recorded.int
min()
Return the minimum value recorded, orInteger.MAX_VALUE
if no values have been recorded.boolean
sameState(IntMomentStatistics other)
Compares the state of twoIntMomentStatistics
objects.double
skewness()
Return the skewness of values recorded, orDouble.NaN
if less than two values have been recorded.long
sum()
Return the sum of values recorded, or zero if no values have been recorded.IntMoments
toIntMoments()
Return anIntMoments
object from the current statistics,static <T> Collector<T,?,IntMomentStatistics>
toIntMomentStatistics(ToIntFunction<? super T> mapper)
Return aCollector
which applies an int-producing mapping function to each input element, and returns moments-statistics for the resulting values.String
toString()
double
variance()
Return the variance of values recorded, orDouble.NaN
if no values have been recorded.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.function.IntConsumer
andThen
-
-
-
-
Constructor Detail
-
IntMomentStatistics
public IntMomentStatistics()
Create an empty moments object.
-
-
Method Detail
-
accept
public void accept(int value)
Records a new value into the moments information- Specified by:
accept
in interfaceIntConsumer
- Parameters:
value
- the inputvalue
-
combine
public IntMomentStatistics combine(IntMomentStatistics other)
Combine twoIntMoments
statistic objects.- Parameters:
other
- the otherIntMoments
statistics to combine withthis
one.- Returns:
this
statistics object- Throws:
NullPointerException
- if the other statistical summary isnull
.
-
min
public int min()
Return the minimum value recorded, orInteger.MAX_VALUE
if no values have been recorded.- Returns:
- the minimum value, or
Integer.MAX_VALUE
if none
-
max
public int max()
Return the maximum value recorded, orInteger.MIN_VALUE
if no values have been recorded.- Returns:
- the maximum value, or
Integer.MIN_VALUE
if none
-
sum
public long sum()
Return the sum of values recorded, or zero if no values have been recorded.- Returns:
- the sum of values, or zero if none
-
sameState
public boolean sameState(IntMomentStatistics other)
Compares the state of twoIntMomentStatistics
objects. This is a replacement for theObject.equals(Object)
which is not advisable to implement for this mutable object. If two object have the same state, it has still the same state when updated with the same value.final IntMomentStatistics ims1 = ...; final IntMomentStatistics ims2 = ...; if (ims1.sameState(ims2)) { final int value = random.nextInt(1_000_000); ims1.accept(value); ims2.accept(value); assert ims1.sameState(ims2); assert ims2.sameState(ims1); assert ims1.sameState(ims1); }
- Parameters:
other
- the other object for the test- Returns:
true
thethis
and theother
objects have the same state,false
otherwise- Since:
- 3.7
-
toIntMoments
public IntMoments toIntMoments()
Return anIntMoments
object from the current statistics,- Returns:
- an
IntMoments
object from the current statistics - Since:
- 3.9
-
toIntMomentStatistics
public static <T> Collector<T,?,IntMomentStatistics> toIntMomentStatistics(ToIntFunction<? super T> mapper)
Return aCollector
which applies an int-producing mapping function to each input element, and returns moments-statistics for the resulting values.final Stream<SomeObject> stream = ... final IntMomentStatistics statistics = stream .collect(toIntMomentStatistics(v -> v.intValue()));
- Type Parameters:
T
- the type of the input elements- Parameters:
mapper
- a mapping function to apply to each element- Returns:
- a
Collector
implementing the moments-statistics reduction - Throws:
NullPointerException
- if the givenmapper
isnull
-
count
public long count()
Returns the count of values recorded.- Returns:
- the count of recorded values
-
mean
public double mean()
Return the arithmetic mean of values recorded, orDouble.NaN
if no values have been recorded.- Returns:
- the arithmetic mean of values, or zero if none
-
variance
public double variance()
Return the variance of values recorded, orDouble.NaN
if no values have been recorded.- Returns:
- the variance of values, or
NaN
if none
-
skewness
public double skewness()
Return the skewness of values recorded, orDouble.NaN
if less than two values have been recorded.- Returns:
- the skewness of values, or
NaN
if less than two values have been recorded - See Also:
- Skewness
-
kurtosis
public double kurtosis()
Return the kurtosis of values recorded, orDouble.NaN
if less than four values have been recorded.- Returns:
- the kurtosis of values, or
NaN
if less than four values have been recorded - See Also:
- Kurtosis
-
-