public class LongMomentStatistics extends Object implements LongConsumer, IntConsumer
LongSummaryStatistics class.
This class is designed to work with (though does not require) streams. For example, you can compute moments-statistics on a stream of longs with:
final LongStream stream = ...
final LongMomentStatistics statistics = stream.collect(
LongMomentStatistics::new,
LongMomentStatistics::accept,
LongMomentStatistics::combine
);
final Stream<SomeObject> stream = ...
final LongMomentStatistics statistics = stream
.collect(toLongMomentStatistics(v -> v.longValue()));
Implementation note:
This implementation is not thread safe. However, it is safe to use
toLongMomentStatistics(ToLongFunction) on a parallel stream, because the parallel
implementation of Stream.collect()
provides the necessary partitioning, isolation, and merging of results for
safe and efficient parallel execution.
LongSummaryStatistics,
LongMoments,
Computing Higher-Order Moments Online| Constructor and Description |
|---|
LongMomentStatistics()
Create an empty moments object.
|
| Modifier and Type | Method and Description |
|---|---|
void |
accept(int value)
Records a new value into the moments information
|
void |
accept(long value)
Records a new value into the moments information
|
LongMomentStatistics |
combine(LongMomentStatistics other)
Combine two
LongMoments statistic objects. |
long |
getCount()
Returns the count of values recorded.
|
double |
getKurtosis()
Return the kurtosis of values recorded, or
Double.NaN if less
than four values have been recorded. |
long |
getMax()
Return the maximum value recorded, or
Long.MIN_VALUE if no
values have been recorded. |
double |
getMean()
Return the arithmetic mean of values recorded, or
Double.NaN if
no values have been recorded. |
long |
getMin()
Return the minimum value recorded, or
Long.MAX_VALUE if no
values have been recorded. |
double |
getSkewness()
Return the skewness of values recorded, or
Double.NaN if less
than two values have been recorded. |
long |
getSum()
Return the sum of values recorded, or zero if no values have been
recorded.
|
double |
getVariance()
Return the variance of values recorded, or
Double.NaN if no
values have been recorded. |
boolean |
sameState(LongMomentStatistics other)
Compares the state of two
LongMomentStatistics objects. |
static <T> Collector<T,?,LongMomentStatistics> |
toLongMomentStatistics(ToLongFunction<? super T> mapper)
Return a
Collector which applies an long-producing mapping
function to each input element, and returns moments-statistics for the
resulting values. |
String |
toString() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitandThenandThenpublic LongMomentStatistics()
public void accept(long value)
accept in interface LongConsumervalue - the input valuepublic void accept(int value)
accept in interface IntConsumervalue - the input valuepublic LongMomentStatistics combine(LongMomentStatistics other)
LongMoments statistic objects.other - the other LongMoments statistics to combine with
this one.this statistics objectNullPointerException - if the other statistical summary
is null.public long getMin()
Long.MAX_VALUE if no
values have been recorded.Long.MAX_VALUE if nonepublic long getMax()
Long.MIN_VALUE if no
values have been recorded.Long.MIN_VALUE if nonepublic long getSum()
public boolean sameState(LongMomentStatistics other)
LongMomentStatistics objects. This is
a replacement for the Object.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 LongMomentStatistics lms1 = ...;
final LongMomentStatistics lms2 = ...;
if (lms1.sameState(lms2)) {
final long value = random.nextInt(1_000_000);
lms1.accept(value);
lms2.accept(value);
assert lms1.sameState(lms2);
assert lms2.sameState(lms1);
assert lms1.sameState(lms1);
}other - the other object for the testtrue the this and the other objects have
the same state, false otherwisepublic static <T> Collector<T,?,LongMomentStatistics> toLongMomentStatistics(ToLongFunction<? super T> mapper)
Collector which applies an long-producing mapping
function to each input element, and returns moments-statistics for the
resulting values.
final Stream<SomeObject> stream = ...
final LongMomentStatistics statistics = stream
.collect(toLongMomentStatistics(v -> v.longValue()));T - the type of the input elementsmapper - a mapping function to apply to each elementCollector implementing the moments-statistics reductionNullPointerException - if the given mapper is
nullpublic long getCount()
public double getMean()
Double.NaN if
no values have been recorded.public double getVariance()
Double.NaN if no
values have been recorded.NaN if nonepublic double getSkewness()
Double.NaN if less
than two values have been recorded.NaN if less than two values
have been recordedpublic double getKurtosis()
Double.NaN if less
than four values have been recorded.NaN if less than four values
have been recorded© 2007-2017 Franz Wilhelmstötter (2017-04-28 16:50)