Top Description Fields Constructors Methods
com.google.common.math

public final Class Stats

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Annotations
@J2ktIncompatible
@GwtIncompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, .Preconditions.checkState, com.google.common.math.DoubleUtils.ensureNonNegative, .StatsAccumulator.calculateNewMeanNonFinite, com.google.common.primitives.Doubles.isFinite, java.lang.Double.NaN, .Double.doubleToLongBits, .Double.isNaN

A bundle of statistical summary values -- sum, count, mean/average, min and max, and several forms of variance -- that were computed from a single set of zero or more floating-point values.

There are two ways to obtain a Stats instance:

Static convenience methods called meanOf are also provided for users who wish to calculate only the mean.

Java 8+ users: If you are not using any of the variance statistics, you may wish to use built-in JDK libraries instead of this class.

Authors
Pete Gillin, Kevin Bourrillion
Since
20.0

Field Summary

Modifier and TypeField and Description
pack-priv static final int
BYTES

The size of byte array representation in bytes.

private final long
private final double
private final double
private final double
private static final long
private final double

Constructor Summary

AccessConstructor and Description
pack-priv
Stats(long count, double mean, double sumOfSquaresOfDeltas, double min, double max)

Internal constructor.

Method Summary

Modifier and TypeMethod and Description
public long
count()

Returns the number of values.

public boolean
equals(Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public static Stats
fromByteArray(byte[] byteArray)

Creates a Stats instance from the given byte representation which was obtained by toByteArray.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.
public double
max()

Returns the highest value in the dataset.

public double
mean()

Returns the arithmetic mean of the values.

public static double
meanOf(Iterable<? extends Number>
a series of values, which will be converted to double values (this may cause loss of precision)
values
)

Returns the arithmetic mean of the values.

public static double
meanOf(Iterator<? extends Number>
a series of values, which will be converted to double values (this may cause loss of precision)
values
)

Returns the arithmetic mean of the values.

public static double
meanOf(double...
a series of values
values
)

Returns the arithmetic mean of the values.

public static double
meanOf(int...
a series of values
values
)

Returns the arithmetic mean of the values.

public static double
meanOf(long...
a series of values, which will be converted to double values (this may cause loss of precision for longs of magnitude over 2^53 (slightly over 9e15))
values
)

Returns the arithmetic mean of the values.

public double
min()

Returns the lowest value in the dataset.

public static Stats
of(Iterable<? extends Number>
a series of values, which will be converted to double values (this may cause loss of precision)
values
)

Returns statistics over a dataset containing the given values.

public static Stats
of(Iterator<? extends Number>
a series of values, which will be converted to double values (this may cause loss of precision)
values
)

Returns statistics over a dataset containing the given values.

public static Stats
of(double...
a series of values
values
)

Returns statistics over a dataset containing the given values.

public static Stats
of(int...
a series of values
values
)

Returns statistics over a dataset containing the given values.

public static Stats
of(long...
a series of values, which will be converted to double values (this may cause loss of precision for longs of magnitude over 2^53 (slightly over 9e15))
values
)

Returns statistics over a dataset containing the given values.

public static Stats
of(DoubleStream
a series of values
values
)

Returns statistics over a dataset containing the given values.

public static Stats
of(IntStream
a series of values
values
)

Returns statistics over a dataset containing the given values.

public static Stats
of(LongStream
a series of values, which will be converted to double values (this may cause loss of precision for longs of magnitude over 2^53 (slightly over 9e15))
values
)

Returns statistics over a dataset containing the given values.

public double
public double
populationVariance()

Returns the population variance of the values.

pack-priv static Stats
readFrom(ByteBuffer
A ByteBuffer with at least BYTES ByteBuffer#remaining, ordered as ByteOrder#LITTLE_ENDIAN, from which a BYTES-long byte representation of this instance is read. In the process increases the position of ByteBuffer by BYTES.
buffer
)

Creates a Stats instance from the byte representation read from the given ByteBuffer.

public double
public double
sampleVariance()

Returns the unbiased sample variance of the values.

public double
sum()

Returns the sum of the values.

pack-priv double
public byte[]
toByteArray()

Gets a byte array representation of this instance.

public static Collector<Number, StatsAccumulator, Stats>
toStats()

Returns a Collector which accumulates statistics from a java.util.stream.Stream of any type of boxed Number into a Stats.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
pack-priv void
writeTo(ByteBuffer
A ByteBuffer with at least BYTES ByteBuffer#remaining, ordered as ByteOrder#LITTLE_ENDIAN, to which a BYTES-long byte representation of this instance is written. In the process increases the position of ByteBuffer by BYTES.
buffer
)

Writes to the given ByteBuffer a byte representation of this instance.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

BYTESback to summary
pack-priv static final int BYTES

The size of byte array representation in bytes.

countback to summary
private final long count
maxback to summary
private final double max
meanback to summary
private final double mean
minback to summary
private final double min
serialVersionUIDback to summary
private static final long serialVersionUID
sumOfSquaresOfDeltasback to summary
private final double sumOfSquaresOfDeltas

Constructor Detail

Statsback to summary
pack-priv Stats(long count, double mean, double sumOfSquaresOfDeltas, double min, double max)

Internal constructor. Users should use of or StatsAccumulator#snapshot.

To ensure that the created instance obeys its contract, the parameters should satisfy the following constraints. This is the callers responsibility and is not enforced here.

  • If count is 0, mean may have any finite value (its only usage will be to get multiplied by 0 to calculate the sum), and the other parameters may have any values (they will not be used).
  • If count is 1, sumOfSquaresOfDeltas must be exactly 0.0 or Double#NaN.

Method Detail

countback to summary
public long count()

Returns the number of values.

equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Note

This tests exact equality of the calculated statistics, including the floating point values. Two instances are guaranteed to be considered equal if one is copied from the other using second = new StatsAccumulator().addAll(first).snapshot(), if both were obtained by calling snapshot() on the same StatsAccumulator without adding any values in between the two calls, or if one is obtained from the other after round-tripping through java serialization. However, floating point rounding errors mean that it may be false for some instances where the statistics are mathematically equal, including instances constructed from the same values in a different order... or (in the general case) even in the same order. (It is guaranteed to return true for instances constructed from the same values in the same order if strictfp is in effect, or if the system architecture guarantees strictfp-like semantics.)

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
fromByteArrayback to summary
public static Stats fromByteArray(byte[] byteArray)

Creates a Stats instance from the given byte representation which was obtained by toByteArray.

Note

No guarantees are made regarding stability of the representation between versions.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

Note

This hash code is consistent with exact equality of the calculated statistics, including the floating point values. See the note on equals for details.

Returns:int

a hash code value for this object

Annotations
@Override
maxback to summary
public double max()

Returns the highest value in the dataset. The count must be non-zero.

Non-finite values

If the dataset contains Double#NaN then the result is Double#NaN. If it contains Double#POSITIVE_INFINITY and not Double#NaN then the result is Double#POSITIVE_INFINITY. If it contains Double#NEGATIVE_INFINITY and finite values only then the result is the highest finite value. If it contains Double#NEGATIVE_INFINITY only then the result is Double#NEGATIVE_INFINITY.

Exceptions
IllegalStateException:
if the dataset is empty
meanback to summary
public double mean()

Returns the arithmetic mean of the values. The count must be non-zero.

If these values are a sample drawn from a population, this is also an unbiased estimator of the arithmetic mean of the population.

Non-finite values

If the dataset contains Double#NaN then the result is Double#NaN. If it contains both Double#POSITIVE_INFINITY and Double#NEGATIVE_INFINITY then the result is Double#NaN. If it contains Double#POSITIVE_INFINITY and finite values only or Double#POSITIVE_INFINITY only, the result is Double#POSITIVE_INFINITY. If it contains Double#NEGATIVE_INFINITY and finite values only or Double#NEGATIVE_INFINITY only, the result is Double#NEGATIVE_INFINITY.

If you only want to calculate the mean, use meanOf instead of creating a Stats instance.

Exceptions
IllegalStateException:
if the dataset is empty
meanOfback to summary
public static double meanOf(Iterable<? extends Number> values)

Returns the arithmetic mean of the values. The count must be non-zero.

The definition of the mean is the same as Stats#mean.

Parameters
values:Iterable<? extends Number>

a series of values, which will be converted to double values (this may cause loss of precision)

Exceptions
IllegalArgumentException:
if the dataset is empty
meanOfback to summary
public static double meanOf(Iterator<? extends Number> values)

Returns the arithmetic mean of the values. The count must be non-zero.

The definition of the mean is the same as Stats#mean.

Parameters
values:Iterator<? extends Number>

a series of values, which will be converted to double values (this may cause loss of precision)

Exceptions
IllegalArgumentException:
if the dataset is empty
meanOfback to summary
public static double meanOf(double... values)

Returns the arithmetic mean of the values. The count must be non-zero.

The definition of the mean is the same as Stats#mean.

Parameters
values:double[]

a series of values

Exceptions
IllegalArgumentException:
if the dataset is empty
meanOfback to summary
public static double meanOf(int... values)

Returns the arithmetic mean of the values. The count must be non-zero.

The definition of the mean is the same as Stats#mean.

Parameters
values:int[]

a series of values

Exceptions
IllegalArgumentException:
if the dataset is empty
meanOfback to summary
public static double meanOf(long... values)

Returns the arithmetic mean of the values. The count must be non-zero.

The definition of the mean is the same as Stats#mean.

Parameters
values:long[]

a series of values, which will be converted to double values (this may cause loss of precision for longs of magnitude over 2^53 (slightly over 9e15))

Exceptions
IllegalArgumentException:
if the dataset is empty
minback to summary
public double min()

Returns the lowest value in the dataset. The count must be non-zero.

Non-finite values

If the dataset contains Double#NaN then the result is Double#NaN. If it contains Double#NEGATIVE_INFINITY and not Double#NaN then the result is Double#NEGATIVE_INFINITY. If it contains Double#POSITIVE_INFINITY and finite values only then the result is the lowest finite value. If it contains Double#POSITIVE_INFINITY only then the result is Double#POSITIVE_INFINITY.

Exceptions
IllegalStateException:
if the dataset is empty
ofback to summary
public static Stats of(Iterable<? extends Number> values)

Returns statistics over a dataset containing the given values.

Parameters
values:Iterable<? extends Number>

a series of values, which will be converted to double values (this may cause loss of precision)

ofback to summary
public static Stats of(Iterator<? extends Number> values)

Returns statistics over a dataset containing the given values. The iterator will be completely consumed by this method.

Parameters
values:Iterator<? extends Number>

a series of values, which will be converted to double values (this may cause loss of precision)

ofback to summary
public static Stats of(double... values)

Returns statistics over a dataset containing the given values.

Parameters
values:double[]

a series of values

ofback to summary
public static Stats of(int... values)

Returns statistics over a dataset containing the given values.

Parameters
values:int[]

a series of values

ofback to summary
public static Stats of(long... values)

Returns statistics over a dataset containing the given values.

Parameters
values:long[]

a series of values, which will be converted to double values (this may cause loss of precision for longs of magnitude over 2^53 (slightly over 9e15))

ofback to summary
public static Stats of(DoubleStream values)

Returns statistics over a dataset containing the given values. The stream will be completely consumed by this method.

If you have a Stream<Double> rather than a DoubleStream, you should collect the values using toStats() instead.

Parameters
values:DoubleStream

a series of values

Since
28.2
ofback to summary
public static Stats of(IntStream values)

Returns statistics over a dataset containing the given values. The stream will be completely consumed by this method.

If you have a Stream<Integer> rather than an IntStream, you should collect the values using toStats() instead.

Parameters
values:IntStream

a series of values

Since
28.2
ofback to summary
public static Stats of(LongStream values)

Returns statistics over a dataset containing the given values. The stream will be completely consumed by this method.

If you have a Stream<Long> rather than a LongStream, you should collect the values using toStats() instead.

Parameters
values:LongStream

a series of values, which will be converted to double values (this may cause loss of precision for longs of magnitude over 2^53 (slightly over 9e15))

Since
28.2
populationStandardDeviationback to summary
public double populationStandardDeviation()

Returns the population standard deviation of the values. The count must be non-zero.

This is guaranteed to return zero if the dataset contains only exactly one finite value. It is not guaranteed to return zero when the dataset consists of the same value multiple times, due to numerical errors. However, it is guaranteed never to return a negative result.

Non-finite values

If the dataset contains any non-finite values (Double#POSITIVE_INFINITY, Double#NEGATIVE_INFINITY, or Double#NaN) then the result is Double#NaN.

Exceptions
IllegalStateException:
if the dataset is empty
populationVarianceback to summary
public double populationVariance()

Returns the population variance of the values. The count must be non-zero.

This is guaranteed to return zero if the dataset contains only exactly one finite value. It is not guaranteed to return zero when the dataset consists of the same value multiple times, due to numerical errors. However, it is guaranteed never to return a negative result.

Non-finite values

If the dataset contains any non-finite values (Double#POSITIVE_INFINITY, Double#NEGATIVE_INFINITY, or Double#NaN) then the result is Double#NaN.

Exceptions
IllegalStateException:
if the dataset is empty
readFromback to summary
pack-priv static Stats readFrom(ByteBuffer buffer)

Creates a Stats instance from the byte representation read from the given ByteBuffer.

Note

No guarantees are made regarding stability of the representation between versions.

Parameters
buffer:ByteBuffer

A ByteBuffer with at least BYTES ByteBuffer#remaining, ordered as ByteOrder#LITTLE_ENDIAN, from which a BYTES-long byte representation of this instance is read. In the process increases the position of ByteBuffer by BYTES.

sampleStandardDeviationback to summary
public double sampleStandardDeviation()

Returns the corrected sample standard deviation of the values. If this dataset is a sample drawn from a population, this is an estimator of the population standard deviation of the population which is less biased than populationStandardDeviation() (the unbiased estimator depends on the distribution). The count must be greater than one.

This is not guaranteed to return zero when the dataset consists of the same value multiple times, due to numerical errors. However, it is guaranteed never to return a negative result.

Non-finite values

If the dataset contains any non-finite values (Double#POSITIVE_INFINITY, Double#NEGATIVE_INFINITY, or Double#NaN) then the result is Double#NaN.

Exceptions
IllegalStateException:
if the dataset is empty or contains a single value
sampleVarianceback to summary
public double sampleVariance()

Returns the unbiased sample variance of the values. If this dataset is a sample drawn from a population, this is an unbiased estimator of the population variance of the population. The count must be greater than one.

This is not guaranteed to return zero when the dataset consists of the same value multiple times, due to numerical errors. However, it is guaranteed never to return a negative result.

Non-finite values

If the dataset contains any non-finite values (Double#POSITIVE_INFINITY, Double#NEGATIVE_INFINITY, or Double#NaN) then the result is Double#NaN.

Exceptions
IllegalStateException:
if the dataset is empty or contains a single value
sumback to summary
public double sum()

Returns the sum of the values.

Non-finite values

If the dataset contains Double#NaN then the result is Double#NaN. If it contains both Double#POSITIVE_INFINITY and Double#NEGATIVE_INFINITY then the result is Double#NaN. If it contains Double#POSITIVE_INFINITY and finite values only or Double#POSITIVE_INFINITY only, the result is Double#POSITIVE_INFINITY. If it contains Double#NEGATIVE_INFINITY and finite values only or Double#NEGATIVE_INFINITY only, the result is Double#NEGATIVE_INFINITY.

sumOfSquaresOfDeltasback to summary
pack-priv double sumOfSquaresOfDeltas()
toByteArrayback to summary
public byte[] toByteArray()

Gets a byte array representation of this instance.

Note

No guarantees are made regarding stability of the representation between versions.

toStatsback to summary
public static Collector<Number, StatsAccumulator, Stats> toStats()

Returns a Collector which accumulates statistics from a java.util.stream.Stream of any type of boxed Number into a Stats. Use by calling boxedNumericStream.collect(toStats()). The numbers will be converted to double values (which may cause loss of precision).

If you have any of the primitive streams DoubleStream, IntStream, or LongStream, you should use the factory method of instead.

Since
28.2
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object

Annotations
@Override
writeToback to summary
pack-priv void writeTo(ByteBuffer buffer)

Writes to the given ByteBuffer a byte representation of this instance.

Note

No guarantees are made regarding stability of the representation between versions.

Parameters
buffer:ByteBuffer

A ByteBuffer with at least BYTES ByteBuffer#remaining, ordered as ByteOrder#LITTLE_ENDIAN, to which a BYTES-long byte representation of this instance is written. In the process increases the position of ByteBuffer by BYTES.