Class StorelessCovariance
- java.lang.Object
-
- org.apache.commons.math3.stat.correlation.Covariance
-
- org.apache.commons.math3.stat.correlation.StorelessCovariance
-
public class StorelessCovariance extends Covariance
Covariance implementation that does not require input data to be stored in memory. The size of the covariance matrix is specified in the constructor. Specific elements of the matrix are incrementally updated with calls to incrementRow() or increment Covariance().This class is based on a paper written by Philippe Pébay: Formulas for Robust, One-Pass Parallel Computation of Covariances and Arbitrary-Order Statistical Moments, 2008, Technical Report SAND2008-6212, Sandia National Laboratories.
Note: the underlying covariance matrix is symmetric, thus only the upper triangular part of the matrix is stored and updated each increment.
- Since:
- 3.0
-
-
Field Summary
Fields Modifier and Type Field Description private StorelessBivariateCovariance[]covMatrixthe square covariance matrix (upper triangular part)private intdimensiondimension of the square covariance matrix
-
Constructor Summary
Constructors Constructor Description StorelessCovariance(int dim)Create a bias corrected covariance matrix with a given dimension.StorelessCovariance(int dim, boolean biasCorrected)Create a covariance matrix with a given number of rows and columns and the indicated bias correction.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidappend(StorelessCovariance sc)Appendsscto this, effectively aggregating the computations inscwith this.doublegetCovariance(int xIndex, int yIndex)Get the covariance for an individual element of the covariance matrix.RealMatrixgetCovarianceMatrix()Returns the covariance matrixdouble[][]getData()Return the covariance matrix as two-dimensional array.private StorelessBivariateCovariancegetElement(int i, int j)Gets the element at index (i, j) from the covariance matrixintgetN()ThisCovariancemethod is not supported by aStorelessCovariance, since the number of bivariate observations does not have to be the same for different pairs of covariates - i.e., N as defined inCovariance.getN()is undefined.voidincrement(double[] data)Increment the covariance matrix with one row of data.private intindexOf(int i, int j)Returns the index (i, j) translated into the one-dimensional array used to store the upper triangular part of the symmetric covariance matrix.private voidinitializeMatrix(boolean biasCorrected)Initialize the internal two-dimensional array ofStorelessBivariateCovarianceinstances.private voidsetElement(int i, int j, StorelessBivariateCovariance cov)Sets the covariance element at index (i, j) in the covariance matrix-
Methods inherited from class org.apache.commons.math3.stat.correlation.Covariance
computeCovarianceMatrix, computeCovarianceMatrix, computeCovarianceMatrix, computeCovarianceMatrix, covariance, covariance
-
-
-
-
Field Detail
-
covMatrix
private StorelessBivariateCovariance[] covMatrix
the square covariance matrix (upper triangular part)
-
dimension
private int dimension
dimension of the square covariance matrix
-
-
Constructor Detail
-
StorelessCovariance
public StorelessCovariance(int dim)
Create a bias corrected covariance matrix with a given dimension.- Parameters:
dim- the dimension of the square covariance matrix
-
StorelessCovariance
public StorelessCovariance(int dim, boolean biasCorrected)Create a covariance matrix with a given number of rows and columns and the indicated bias correction.- Parameters:
dim- the dimension of the covariance matrixbiasCorrected- iftruethe covariance estimate is corrected for bias, i.e. n-1 in the denominator, otherwise there is no bias correction, i.e. n in the denominator.
-
-
Method Detail
-
initializeMatrix
private void initializeMatrix(boolean biasCorrected)
Initialize the internal two-dimensional array ofStorelessBivariateCovarianceinstances.- Parameters:
biasCorrected- if the covariance estimate shall be corrected for bias
-
indexOf
private int indexOf(int i, int j)Returns the index (i, j) translated into the one-dimensional array used to store the upper triangular part of the symmetric covariance matrix.- Parameters:
i- the row indexj- the column index- Returns:
- the corresponding index in the matrix array
-
getElement
private StorelessBivariateCovariance getElement(int i, int j)
Gets the element at index (i, j) from the covariance matrix- Parameters:
i- the row indexj- the column index- Returns:
- the
StorelessBivariateCovarianceelement at the given index
-
setElement
private void setElement(int i, int j, StorelessBivariateCovariance cov)Sets the covariance element at index (i, j) in the covariance matrix- Parameters:
i- the row indexj- the column indexcov- theStorelessBivariateCovarianceelement to be set
-
getCovariance
public double getCovariance(int xIndex, int yIndex) throws NumberIsTooSmallExceptionGet the covariance for an individual element of the covariance matrix.- Parameters:
xIndex- row index in the covariance matrixyIndex- column index in the covariance matrix- Returns:
- the covariance of the given element
- Throws:
NumberIsTooSmallException- if the number of observations in the cell is < 2
-
increment
public void increment(double[] data) throws DimensionMismatchExceptionIncrement the covariance matrix with one row of data.- Parameters:
data- array representing one row of data.- Throws:
DimensionMismatchException- if the length ofrowDatadoes not match with the covariance matrix
-
append
public void append(StorelessCovariance sc) throws DimensionMismatchException
Appendsscto this, effectively aggregating the computations inscwith this. After invoking this method, covariances returned should be close to what would have been obtained by performing all of theincrement(double[])operations inscdirectly on this.- Parameters:
sc- externally computed StorelessCovariance to add to this- Throws:
DimensionMismatchException- if the dimension of sc does not match this- Since:
- 3.3
-
getCovarianceMatrix
public RealMatrix getCovarianceMatrix() throws NumberIsTooSmallException
Returns the covariance matrix- Overrides:
getCovarianceMatrixin classCovariance- Returns:
- covariance matrix
- Throws:
NumberIsTooSmallException- if the number of observations in a cell is < 2
-
getData
public double[][] getData() throws NumberIsTooSmallExceptionReturn the covariance matrix as two-dimensional array.- Returns:
- a two-dimensional double array of covariance values
- Throws:
NumberIsTooSmallException- if the number of observations for a cell is < 2
-
getN
public int getN() throws MathUnsupportedOperationExceptionThisCovariancemethod is not supported by aStorelessCovariance, since the number of bivariate observations does not have to be the same for different pairs of covariates - i.e., N as defined inCovariance.getN()is undefined.- Overrides:
getNin classCovariance- Returns:
- nothing as this implementation always throws a
MathUnsupportedOperationException - Throws:
MathUnsupportedOperationException- in all cases
-
-