Top Description Fields Constructors Methods
org.apache.commons.math3.stat.correlation

public Class Covariance

extends Object
Class Inheritance
Known Direct Subclasses
org.apache.commons.math3.stat.correlation.StorelessCovariance
Imports
org.apache.commons.math3.exception.MathIllegalArgumentException, .NotStrictlyPositiveException, org.apache.commons.math3.exception.util.LocalizedFormats, org.apache.commons.math3.linear.RealMatrix, .BlockRealMatrix, org.apache.commons.math3.stat.descriptive.moment.Mean, .Variance

Computes covariances for pairs of arrays or columns of a matrix.

The constructors that take RealMatrix or double[][] arguments generate covariance matrices. The columns of the input matrices are assumed to represent variable values.

The constructor argument biasCorrected determines whether or not computed covariances are bias-corrected.

Unbiased covariances are given by the formula

cov(X, Y) = Σ[(xi - E(X))(yi - E(Y))] / (n - 1) where E(X) is the mean of X and E(Y) is the mean of the Y values.

Non-bias-corrected estimates use n in place of n - 1

Since
2.0

Field Summary

Modifier and TypeField and Description
private final RealMatrix
covarianceMatrix

covariance matrix

private final int
n

Number of observations (length of covariate vectors)

Constructor Summary

AccessConstructor and Description
public
Covariance()

Create a Covariance with no data

public
Covariance(double[][]
rectangular array with columns representing covariates
data
,
boolean
true means covariances are bias-corrected
biasCorrected
)

Create a Covariance matrix from a rectangular array whose columns represent covariates.

public
Covariance(double[][]
rectangular array with columns representing covariates
data
)

Create a Covariance matrix from a rectangular array whose columns represent covariates.

public
Covariance(RealMatrix
matrix with columns representing covariates
matrix
,
boolean
true means covariances are bias-corrected
biasCorrected
)

Create a covariance matrix from a matrix whose columns represent covariates.

public
Covariance(RealMatrix
matrix with columns representing covariates
matrix
)

Create a covariance matrix from a matrix whose columns represent covariates.

Method Summary

Modifier and TypeMethod and Description
private void
checkSufficientData(final RealMatrix
matrix to check
matrix
)

Throws MathIllegalArgumentException if the matrix does not have at least one column and two rows.

protected RealMatrix

Returns:

covariance matrix
computeCovarianceMatrix
(RealMatrix
input matrix (must have at least one column and two rows)
matrix
,
boolean
determines whether or not covariance estimates are bias-corrected
biasCorrected
)

Compute a covariance matrix from a matrix whose columns represent covariates.

protected RealMatrix

Returns:

covariance matrix
computeCovarianceMatrix
(RealMatrix
input matrix (must have at least one column and two rows)
matrix
)

Create a covariance matrix from a matrix whose columns represent covariates.

protected RealMatrix

Returns:

covariance matrix
computeCovarianceMatrix
(double[][]
input array (must have at least one column and two rows)
data
,
boolean
determines whether or not covariance estimates are bias-corrected
biasCorrected
)

Compute a covariance matrix from a rectangular array whose columns represent covariates.

protected RealMatrix

Returns:

covariance matrix
computeCovarianceMatrix
(double[][]
input array (must have at least one column and two rows)
data
)

Create a covariance matrix from a rectangular array whose columns represent covariates.

public double

Returns:

returns the covariance for the two arrays
covariance
(final double[]
first data array
xArray
,
final double[]
second data array
yArray
,
boolean
if true, returned value will be bias-corrected
biasCorrected
)

Computes the covariance between the two arrays.

public double

Returns:

returns the covariance for the two arrays
covariance
(final double[]
first data array
xArray
,
final double[]
second data array
yArray
)

Computes the covariance between the two arrays, using the bias-corrected formula.

public RealMatrix

Returns:

covariance matrix
getCovarianceMatrix
()

Returns the covariance matrix

public int

Returns:

number of observations
getN
()

Returns the number of observations (length of covariate vectors)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

covarianceMatrixback to summary
private final RealMatrix covarianceMatrix

covariance matrix

nback to summary
private final int n

Number of observations (length of covariate vectors)

Constructor Detail

Covarianceback to summary
public Covariance()

Create a Covariance with no data

Covarianceback to summary
public Covariance(double[][] data, boolean biasCorrected) throws MathIllegalArgumentException, NotStrictlyPositiveException

Create a Covariance matrix from a rectangular array whose columns represent covariates.

The biasCorrected parameter determines whether or not covariance estimates are bias-corrected.

The input array must be rectangular with at least one column and two rows.

Parameters
data:double[][]

rectangular array with columns representing covariates

biasCorrected:boolean

true means covariances are bias-corrected

Exceptions
MathIllegalArgumentException:
if the input data array is not rectangular with at least two rows and one column.
NotStrictlyPositiveException:
if the input data array is not rectangular with at least one row and one column.
Covarianceback to summary
public Covariance(double[][] data) throws MathIllegalArgumentException, NotStrictlyPositiveException

Create a Covariance matrix from a rectangular array whose columns represent covariates.

The input array must be rectangular with at least one column and two rows

Parameters
data:double[][]

rectangular array with columns representing covariates

Exceptions
MathIllegalArgumentException:
if the input data array is not rectangular with at least two rows and one column.
NotStrictlyPositiveException:
if the input data array is not rectangular with at least one row and one column.
Covarianceback to summary
public Covariance(RealMatrix matrix, boolean biasCorrected) throws MathIllegalArgumentException

Create a covariance matrix from a matrix whose columns represent covariates.

The biasCorrected parameter determines whether or not covariance estimates are bias-corrected.

The matrix must have at least one column and two rows

Parameters
matrix:RealMatrix

matrix with columns representing covariates

biasCorrected:boolean

true means covariances are bias-corrected

Exceptions
MathIllegalArgumentException:
if the input matrix does not have at least two rows and one column
Covarianceback to summary
public Covariance(RealMatrix matrix) throws MathIllegalArgumentException

Create a covariance matrix from a matrix whose columns represent covariates.

The matrix must have at least one column and two rows

Parameters
matrix:RealMatrix

matrix with columns representing covariates

Exceptions
MathIllegalArgumentException:
if the input matrix does not have at least two rows and one column

Method Detail

checkSufficientDataback to summary
private void checkSufficientData(final RealMatrix matrix) throws MathIllegalArgumentException

Throws MathIllegalArgumentException if the matrix does not have at least one column and two rows.

Parameters
matrix:RealMatrix

matrix to check

Exceptions
MathIllegalArgumentException:
if the matrix does not contain sufficient data to compute covariance
computeCovarianceMatrixback to summary
protected RealMatrix computeCovarianceMatrix(RealMatrix matrix, boolean biasCorrected) throws MathIllegalArgumentException

Compute a covariance matrix from a matrix whose columns represent covariates.

Parameters
matrix:RealMatrix

input matrix (must have at least one column and two rows)

biasCorrected:boolean

determines whether or not covariance estimates are bias-corrected

Returns:RealMatrix

covariance matrix

Exceptions
MathIllegalArgumentException:
if the matrix does not contain sufficient data
computeCovarianceMatrixback to summary
protected RealMatrix computeCovarianceMatrix(RealMatrix matrix) throws MathIllegalArgumentException

Create a covariance matrix from a matrix whose columns represent covariates. Covariances are computed using the bias-corrected formula.

Parameters
matrix:RealMatrix

input matrix (must have at least one column and two rows)

Returns:RealMatrix

covariance matrix

Exceptions
MathIllegalArgumentException:
if matrix does not contain sufficient data
See Also
Covariance
computeCovarianceMatrixback to summary
protected RealMatrix computeCovarianceMatrix(double[][] data, boolean biasCorrected) throws MathIllegalArgumentException, NotStrictlyPositiveException

Compute a covariance matrix from a rectangular array whose columns represent covariates.

Parameters
data:double[][]

input array (must have at least one column and two rows)

biasCorrected:boolean

determines whether or not covariance estimates are bias-corrected

Returns:RealMatrix

covariance matrix

Exceptions
MathIllegalArgumentException:
if the data array does not contain sufficient data
NotStrictlyPositiveException:
if the input data array is not rectangular with at least one row and one column.
computeCovarianceMatrixback to summary
protected RealMatrix computeCovarianceMatrix(double[][] data) throws MathIllegalArgumentException, NotStrictlyPositiveException

Create a covariance matrix from a rectangular array whose columns represent covariates. Covariances are computed using the bias-corrected formula.

Parameters
data:double[][]

input array (must have at least one column and two rows)

Returns:RealMatrix

covariance matrix

Exceptions
MathIllegalArgumentException:
if the data array does not contain sufficient data
NotStrictlyPositiveException:
if the input data array is not rectangular with at least one row and one column.
See Also
Covariance
covarianceback to summary
public double covariance(final double[] xArray, final double[] yArray, boolean biasCorrected) throws MathIllegalArgumentException

Computes the covariance between the two arrays.

Array lengths must match and the common length must be at least 2.

Parameters
xArray:double[]

first data array

yArray:double[]

second data array

biasCorrected:boolean

if true, returned value will be bias-corrected

Returns:double

returns the covariance for the two arrays

Exceptions
MathIllegalArgumentException:
if the arrays lengths do not match or there is insufficient data
covarianceback to summary
public double covariance(final double[] xArray, final double[] yArray) throws MathIllegalArgumentException

Computes the covariance between the two arrays, using the bias-corrected formula.

Array lengths must match and the common length must be at least 2.

Parameters
xArray:double[]

first data array

yArray:double[]

second data array

Returns:double

returns the covariance for the two arrays

Exceptions
MathIllegalArgumentException:
if the arrays lengths do not match or there is insufficient data
getCovarianceMatrixback to summary
public RealMatrix getCovarianceMatrix()

Returns the covariance matrix

Returns:RealMatrix

covariance matrix

getNback to summary
public int getN()

Returns the number of observations (length of covariate vectors)

Returns:int

number of observations