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

public final Class PairedStats

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, java.lang.Double.NaN, .Double.doubleToLongBits, .Double.isNaN

An immutable value object capturing some basic statistics about a collection of paired double values (e.g. points on a plane). Build instances with PairedStatsAccumulator#snapshot.
Author
Pete Gillin
Since
20.0

Field Summary

Modifier and TypeField and Description
private static final int
BYTES

The size of byte array representation in bytes.

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

Constructor Summary

AccessConstructor and Description
pack-priv
PairedStats(Stats xStats, Stats yStats, double sumOfProductsOfDeltas)

Internal constructor.

Method Summary

Modifier and TypeMethod and Description
public long
count()

Returns the number of pairs in the dataset.

private static double
ensureInUnitRange(double value)

private static double
ensurePositive(double value)

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 PairedStats
fromByteArray(byte[] byteArray)

Creates a PairedStats 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 LinearTransformation
leastSquaresFit()

Returns a linear transformation giving the best fit to the data according to Ordinary Least Squares linear regression of y as a function of x.

public double
public double
populationCovariance()

Returns the population covariance of the values.

public double
sampleCovariance()

Returns the sample covariance of the values.

pack-priv double
public byte[]
toByteArray()

Gets a byte array representation of this instance.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public Stats
xStats()

Returns the statistics on the x values alone.

public Stats
yStats()

Returns the statistics on the y values alone.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

BYTESback to summary
private static final int BYTES

The size of byte array representation in bytes.

serialVersionUIDback to summary
private static final long serialVersionUID
sumOfProductsOfDeltasback to summary
private final double sumOfProductsOfDeltas
xStatsback to summary
private final Stats xStats
yStatsback to summary
private final Stats yStats

Constructor Detail

PairedStatsback to summary
pack-priv PairedStats(Stats xStats, Stats yStats, double sumOfProductsOfDeltas)

Internal constructor. Users should use PairedStatsAccumulator#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.

  • Both xStats and yStats must have the same count.
  • If that count is 1, sumOfProductsOfDeltas must be exactly 0.0.
  • If that count is more than 1, sumOfProductsOfDeltas must be finite.

Method Detail

countback to summary
public long count()

Returns the number of pairs in the dataset.

ensureInUnitRangeback to summary
private static double ensureInUnitRange(double value)
ensurePositiveback to summary
private static double ensurePositive(double value)
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 PairedStatsAccumulator().addAll(first).snapshot(), if both were obtained by calling snapshot() on the same PairedStatsAccumulator 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 PairedStats fromByteArray(byte[] byteArray)

Creates a PairedStats 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
leastSquaresFitback to summary
public LinearTransformation leastSquaresFit()

Returns a linear transformation giving the best fit to the data according to Ordinary Least Squares linear regression of y as a function of x. The count must be greater than one, and either the x or y data must have a non-zero population variance (i.e. xStats().populationVariance() > 0.0 || yStats().populationVariance() > 0.0). The result is guaranteed to be horizontal if there is variance in the x data but not the y data, and vertical if there is variance in the y data but not the x data.

This fit minimizes the root-mean-square error in y as a function of x. This error is defined as the square root of the mean of the squares of the differences between the actual y values of the data and the values predicted by the fit for the x values (i.e. it is the square root of the mean of the squares of the vertical distances between the data points and the best fit line). For this fit, this error is a fraction sqrt(1 - R*R) of the population standard deviation of y, where R is the Pearson's correlation coefficient (as given by pearsonsCorrelationCoefficient()).

The corresponding root-mean-square error in x as a function of y is a fraction sqrt(1/(R*R) - 1) of the population standard deviation of x. This fit does not normally minimize that error: to do that, you should swap the roles of x and y.

Non-finite values

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

Exceptions
IllegalStateException:
if the dataset is empty or contains a single pair of values, or both the x and y dataset must have zero population variance
pearsonsCorrelationCoefficientback to summary
public double pearsonsCorrelationCoefficient()

Returns the Pearson's or product-moment correlation coefficient of the values. The count must greater than one, and the x and y values must both have non-zero population variance (i.e. xStats().populationVariance() > 0.0 && yStats().populationVariance() > 0.0). The result is not guaranteed to be exactly +/-1 even when the data are perfectly (anti-)correlated, due to numerical errors. However, it is guaranteed to be in the inclusive range [-1, +1].

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 pair of values, or either the x and y dataset has zero population variance
populationCovarianceback to summary
public double populationCovariance()

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

This is guaranteed to return zero if the dataset contains a single pair of finite values. It is not guaranteed to return zero when the dataset consists of the same pair of values multiple times, due to numerical errors.

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
sampleCovarianceback to summary
public double sampleCovariance()

Returns the sample covariance of the values. The count must be greater than one.

This is not guaranteed to return zero when the dataset consists of the same pair of values multiple times, due to numerical errors.

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 pair of values
sumOfProductsOfDeltasback to summary
pack-priv double sumOfProductsOfDeltas()
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.

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
xStatsback to summary
public Stats xStats()

Returns the statistics on the x values alone.

yStatsback to summary
public Stats yStats()

Returns the statistics on the y values alone.