Skip to content

StatsExplained

cpovirk edited this page Apr 9, 2026 · 11 revisions

Computing statistical values

This page lists a number of common statistical computations and how to perform them, often making use of the statistical support libraries in com.google.common.math.

In the following examples, a variable with a name like intArray or collectionOfDouble is of the type implied by that name. The identifier values can represent an int[], long[], double[], Collection<? extends Number>, or can be replaced with primitive varargs. (In some cases, even more variations may be accepted; check Javadoc for full details.)

Links to named classes are given at the bottom of the page.

Mean (only) of existing values

doublemean = Stats.meanOf(values);
doublemean = doubleStream.average().getAsDouble();

Maximum (only) of existing values (ditto for minimum)

doublemax = doubleStream.max().getAsDouble();
doublemax = Double.max(doubleA, doubleB);
doublemax = Doubles.max(doubleA, doubleB, doubleC);
doublemax = Doubles.max(doubleArray);
doublemax = immutableDoubleArray.stream().max().getAsDouble();
doublemax = Collections.max(collectionOfDouble);
doublemax = Ordering.natural().max(iterableOfDouble);

Sum (only) of existing values

doublesum = doubleStream.sum();
doublesum = Arrays.stream(doubleArray).sum();
doublesum = Stats.of(values).sum();

Both mean and maximum of existing values

DoubleSummaryStatisticsstats = doubleStream.summaryStatistics();
doublemean = stats.getAverage();
doublemax = stats.getMax();
Statsstats = Stats.of(values);
doublemean = stats.mean();
doublemax = stats.max();

Standard deviation of existing values

Choose between populationStandardDeviation and sampleStandardDeviation; see the Javadoc of these methods to understand the difference. You can get other statistics, such as mean, min, and max, from the same Stats instance.

doublestddev = Stats.of(values).populationStandardDeviation();
doublestddev = primitiveStream.collect(toStats()).populationStandardDeviation();

(The toStats() method is statically imported from Stats.)

Mean and sample standard deviation of incoming values

This approach is useful when you don't want to store up all the values in advance. Instead, create an "acccumulator", and as you get the values you can feed them in and then discard them.

StatsAccumulatoraccum = newStatsAccumulator();
...
// any number of times, over timeaccum.add(value); // or addAll
...
doublemean = accum.mean();
doublestddev = accum.sampleStandardDeviation();
// or use accum.snapshot() to get an immutable Stats instance

Median (only) of existing values

doublemedian = Quantiles.median().compute(values);

95th percentile of existing values

doublepercentile95 = Quantiles.percentiles().index(95).compute(values);

Find the 90th, 99th, and 99.9th percentile

Map<Integer, Double> largeValues =
Quantiles.scale(1000).indexes(900, 990, 999).compute(values);
doublep99 = largeValues.get(990); // for example

Find the statistical correlation between two sets of values

PairedStatsAccumulatoraccum = newPairedStatsAccumulator();
for (...) {
...
accum.add(x, y);
}
doublecorrel = accum.pearsonsCorrelationCoefficient();

Find a linear approximation for a set of ordered pairs

PairedStatsAccumulatoraccum = newPairedStatsAccumulator();
for (...) {
...
accum.add(x, y);
}
LinearTransformationbestFit = accum.leastSquaresFit();
doubleslope = bestFit.slope();
doubleyIntercept = bestFit.transform(0);
doubleestimateXWhenYEquals5 = bestFit.inverse().transform(5);

Links to classes used in these examples

Clone this wiki locally