public class Quantile extends Object implements DoubleConsumer
Raj JAIN and Imrich CHLAMTAC:
The P2 Algorithm for Dynamic Calculation of Quantiles and
Histograms Without Storing Observations
[Communications
of the ACM; October 1985, Volume 28, Number 10]
This class is designed to work with (though does not require) streams. For example, you can compute the quantile with:
final DoubleStream stream = ...
final Quantile quantile = stream.collect(
() -> new Quantile(0.23),
Quantile::accept,
Quantile::combine
);
Implementation note:
This implementation is not thread safe. However, it is safe to use 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.
Using this class in the collect method of an parallel stream can
lead to an reduced accuracy of the quantile value. Since this implementation
is an estimation algorithm, combining the estimations will only work for
large streams (size >> 1000).
| Constructor and Description |
|---|
Quantile(double quantile)
Create a new quantile accumulator with the given value.
|
| Modifier and Type | Method and Description |
|---|---|
void |
accept(double value) |
Quantile |
combine(Quantile other)
Combine two
Quantile objects. |
double |
getQuantile()
Return the quantile
this object has been parametrized
with. |
long |
getSamples()
Return the number of samples the quantile value was calculated of.
|
double |
getValue()
Return the computed quantile value.
|
void |
reset()
Reset this object to its initial state.
|
boolean |
sameState(Quantile other)
Compares the state of two
Quantile objects. |
static <T> Collector<T,?,Quantile> |
toMedian(ToDoubleFunction<? super T> mapper)
Return a
Collector which applies an double-producing mapping
function to each input element, and returns the median for the resulting
values. |
static <T> Collector<T,?,Quantile> |
toQuantile(double quantile,
ToDoubleFunction<? super T> mapper)
Return a
Collector which applies an double-producing mapping
function to each input element, and returns quantiles for the resulting
values. |
String |
toString() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitandThenpublic Quantile(double quantile)
quantile - the wished quantile value.IllegalArgumentException - if the quantile is not in the
range [0, 1].public void reset()
public double getQuantile()
this object has been parametrized
with.this object has been parametrized
withpublic double getValue()
public long getSamples()
public void accept(double value)
accept in interface DoubleConsumerpublic Quantile combine(Quantile other)
Quantile objects.other - the other Quantile object to combinethisNullPointerException - if the other object is
null.IllegalArgumentException - if the getQuantile()
of the other object differs from this one.public boolean sameState(Quantile other)
Quantile 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 Quantile q1 = ...;
final Quantile q2 = ...;
if (q1.sameState(q2)) {
final double value = random.nextDouble();
q1.accept(value);
q2.accept(value);
assert q1.sameState(q2);
assert q2.sameState(q1);
assert q1.sameState(q1);
}other - the other object for the testtrue the this and the other objects have
the same state, false otherwisepublic static <T> Collector<T,?,Quantile> toQuantile(double quantile, ToDoubleFunction<? super T> mapper)
Collector which applies an double-producing mapping
function to each input element, and returns quantiles for the resulting
values.
final Stream<SomeObject> stream = ...
final Quantile quantile = stream
.collect(toQuantile(0.25, v -> v.doubleValue()));T - the type of the input elementsquantile - the wished quantile value.mapper - a mapping function to apply to each elementCollector implementing the quantiles reductionNullPointerException - if the given mapper is
nullIllegalArgumentException - if the quantile is not in the
range [0, 1].public static <T> Collector<T,?,Quantile> toMedian(ToDoubleFunction<? super T> mapper)
Collector which applies an double-producing mapping
function to each input element, and returns the median for the resulting
values.
final Stream<SomeObject> stream = ...
final Quantile median = stream.collect(toMedian(v -> v.doubleValue()));T - the type of the input elementsmapper - a mapping function to apply to each elementCollector implementing the quantiles reductionNullPointerException - if the given mapper is
null© 2007-2017 Franz Wilhelmstötter (2017-04-28 16:50)