Apache Commons logo Apache Commons Statistics

Apache Commons Statistics

Apache Commons Statistics provides utilities for statistical applications.

Descriptive statistics can be computed on array data or using the Java Stream API, for example:

int[] values = {1, 1, 2, 3, 5, 8, 13, 21};
double v = IntVariance.of(values).getAsDouble();   // 49.929

// A builder for specified statistics to allow
// parallel computation on a stream of values
IntStatistics.Builder builder = IntStatistics.builder(
    Statistic.MIN, Statistic.MAX, Statistic.MEAN);
IntStatistics stats =
    Stream.of("one", "two", "three", "four")
    .parallel()
    .mapToInt(String::length)
    .collect(builder::build, IntConsumer::accept, IntStatistics::combine);

stats.getAsInt(Statistic.MIN);       // 3
stats.getAsInt(Statistic.MAX);       // 5
stats.getAsDouble(Statistic.MEAN);   // 15.0 / 4

Support is provided for commonly used continuous and discrete distributions, for example:

TDistribution t = TDistribution.of(29);
double lowerTail = t.cumulativeProbability(-2.656);   // P(T(29) <= -2.656)
double upperTail = t.survivalProbability(2.75);       // P(T(29) > 2.75)

PoissonDistribution p = PoissonDistribution.of(4.56);
int x = p.inverseCumulativeProbability(0.99);

Hypothesis testing can be performed for various statistical tests, for example:

double[] math    = {53, 69, 65, 65, 67, 79, 86, 65, 62, 69};   // mean = 68.0
double[] science = {75, 65, 68, 63, 55, 65, 73, 45, 51, 52};   // mean = 61.2

SignificanceResult result = TTest.withDefaults()
                                 .with(AlternativeHypothesis.GREATER_THAN)
                                 .pairedTest(math, science);
result.getPValue();    // 0.05764
result.reject(0.05);   // false

For more examples and advanced usage, see the user guide.

Download Apache Commons Statistics

Releases

Download the latest release of Apache Commons Statistics.