-
Notifications
You must be signed in to change notification settings - Fork 10.9k
StatsExplained
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.
double mean = Stats.meanOf(values);
double mean = doubleStream.average().getAsDouble();
double max = doubleStream.max().getAsDouble();
double max = Double.max(doubleA, doubleB); // Java 8+ only
double max = Doubles.max(doubleA, doubleB, doubleC);
double max = Doubles.max(doubleArray);
double max = immutableDoubleArray.stream().max().getAsDouble();
double max = Collections.max(collectionOfDouble);
double max = Ordering.natural().max(iterableOfDouble);
double sum = doubleStream.sum();
double sum = Arrays.stream(doubleArray).sum();
double sum = Stats.of(values).sum();
DoubleSummaryStatistics stats = doubleStream.summaryStatistics();
double mean = stats.getAverage();
double max = stats.getMax();
Stats stats = Stats.of(values);
double mean = stats.mean();
double max = stats.max();
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.
double stddev = Stats.of(values).populationStandardDeviation();
double stddev = primitiveStream.collect(toStats()).populationStandardDeviation();
(The toStats()
method is statically imported from Stats
.)
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.
StatsAccumulator accum = new StatsAccumulator();
...
// any number of times, over time
accum.add(value); // or addAll
...
double mean = accum.mean();
double stddev = accum.sampleStandardDeviation();
// or use accum.snapshot() to get an immutable Stats instance
double median = Quantiles.median().compute(values);
double percentile95 = Quantiles.percentiles().index(95).compute(values);
Map<Integer, Double> largeValues =
Quantiles.scale(1000).indexes(900, 990, 999).compute(values);
double p99 = largeValues.get(990); // for example
PairedStatsAccumulator accum = new PairedStatsAccumulator();
for (...) {
...
accum.add(x, y);
}
double correl = accum.pearsonsCorrelationCoefficient();
PairedStatsAccumulator accum = new PairedStatsAccumulator();
for (...) {
...
accum.add(x, y);
}
LinearTransformation bestFit = accum.leastSquaresFit();
double slope = bestFit.slope();
double yIntercept = bestFit.transform(0);
double estimateXWhenYEquals5 = bestFit.inverse().transform(5);
- Introduction
- Basic Utilities
- Collections
- Graphs
- Caches
- Functional Idioms
- Concurrency
- Strings
- Networking
- Primitives
- Ranges
- I/O
- Hashing
- EventBus
- Math
- Reflection
- Releases
- Tips
- Glossary
- Mailing List
- Stack Overflow
- Android Overview
- Footprint of JDK/Guava data structures