public class Covariance extends Object
The covariance matrix is stored in a SymmetricPositiveMatrix
object.
The covariance columns/rows are described by a list of parameter descriptors.
Modifier and Type | Field and Description |
---|---|
protected static String |
DEFAULT_FIELD_SEPARATOR
Default field separator for the
toString() methods. |
protected static String |
DEFAULT_NAME_SEPARATOR
Default name separator for the
toString() methods. |
Constructor and Description |
---|
Covariance(SymmetricPositiveMatrix covarianceMatrixIn)
Creates a new instance using the specified covariance matrix.
|
Covariance(SymmetricPositiveMatrix covarianceMatrixIn,
Collection<ParameterDescriptor> parameterDescriptorsIn)
Creates a new instance using the specified covariance matrix and parameter descriptors.
|
Modifier and Type | Method and Description |
---|---|
Covariance |
add(SymmetricPositiveMatrix m)
Adds the symmetric positive semi-definite matrix M to this covariance matrix and returns a
new Covariance instance associated with the computed matrix and the same
parameter descriptors.
|
Covariance |
copy()
Gets a copy of this Covariance instance.
|
boolean |
equals(Object object) |
double |
getCorrelationCoefficient(int row,
int column)
Gets the correlation coefficient ρi,j for the specified row and column
indexes.
|
double |
getCorrelationCoefficient(ParameterDescriptor parameterDescriptor1,
ParameterDescriptor parameterDescriptor2)
Gets the correlation coefficient ρi,j associated with the specified parameter
descriptors, the first parameter* descriptor being mapped to the rows of the covariance
matrix (index "i"), while the second parameter descriptor is mapped to the columns of the
covariance matrix (index "j").
|
SymmetricPositiveMatrix |
getCorrelationCoefficientsMatrix()
Gets the correlation matrix.
|
SymmetricPositiveMatrix |
getCovarianceMatrix()
Gets the covariance matrix.
|
double |
getMahalanobisDistance(RealVector point)
Gets the Mahalanobis distance of a point with respect to this covariance matrix, assuming its
mean value is zero.
|
double |
getMahalanobisDistance(RealVector point,
RealVector mean)
Gets the Mahalanobis distance of a point with respect to this covariance matrix.
|
ParameterDescriptor |
getParameterDescriptor(int index)
Gets the parameter descriptors associated with the specified row/column of the covariance
matrix.
|
List<ParameterDescriptor> |
getParameterDescriptors()
Gets the parameter descriptors associated with the rows/columns of the covariance matrix.
|
int |
getSize()
Gets the size of the covariance matrix.
|
double |
getStandardDeviation(int index)
Gets the standard deviation σi = sqrt(Ci,i) for the
specified row/column index.
|
double |
getStandardDeviation(ParameterDescriptor parameterDescriptor)
Gets the standard deviation σi = sqrt(Ci,i) associated
with the specified parameter descriptor.
|
DiagonalMatrix |
getStandardDeviationMatrix()
Gets the standard deviation matrix.
|
Covariance |
getSubCovariance(Collection<ParameterDescriptor> selectedParameterDescriptors)
Extracts the parts of the covariance associated with the specified parameter descriptors.
|
Covariance |
getSubCovariance(int[] indices)
Extracts the parts of the covariance associated with the specified row/column indices.
|
double |
getVariance(int index)
Gets the variance σi = Ci,i for the specified
row/column index.
|
double |
getVariance(ParameterDescriptor parameterDescriptor)
Gets the variance σi = Ci,i associated with the
specified parameter descriptor.
|
DiagonalMatrix |
getVarianceMatrix()
Gets the variance matrix.
|
int |
hashCode() |
protected int |
indexOf(ParameterDescriptor parameterDescriptor)
Gets the index of a given parameter descriptor in the list stored internally.
|
Covariance |
positiveScalarMultiply(double d)
Multiplies this covariance matrix by a positive scalar.
|
Covariance |
quadraticMultiplication(RealMatrix m)
Gets the result of the quadratic multiplication M×C×MT, where C is
this covariance matrix and M is the provided matrix, and associates the computed matrix with
default parameter descriptors.
|
Covariance |
quadraticMultiplication(RealMatrix m,
boolean isTranspose)
Gets the result of the quadratic multiplication M×C×MT, where C is
this covariance matrix and M or MT is the provided matrix, and associates the
computed matrix with default parameter descriptors.
|
Covariance |
quadraticMultiplication(RealMatrix m,
Collection<ParameterDescriptor> newParameterDescriptors)
Gets the result of the quadratic multiplication M×C×MT, where C is
this covariance matrix and M is the provided matrix, and associates the computed matrix with
the specified parameter descriptors.
|
Covariance |
quadraticMultiplication(RealMatrix m,
Collection<ParameterDescriptor> newParameterDescriptors,
boolean isTranspose)
Gets the result of the quadratic multiplication M×C×MT, where C is
this covariance matrix and M or MT is the provided matrix, and associates the
computed matrix with the specified parameter descriptors.
|
String |
toString()
Returns a string representation of this instance which includes the name of the class and the
names of the associated parameter descriptors (the covariance matrix itself is not printed).
|
String |
toString(RealMatrixFormat realMatrixFormat)
Returns a string representation of this instance which includes the name of the class (if
requested), the names of the associated parameter descriptors and the the covariance matrix
(if the specified matrix format is not
null ). |
String |
toString(RealMatrixFormat realMatrixFormat,
String nameSeparator,
String fieldSeparator,
boolean printClassName,
boolean reverseOrder)
Returns a string representation of this instance which includes the name of the class (if
requested), the names of the associated parameter descriptors and the the covariance matrix
(if the specified matrix format is not
null ). |
protected static final String DEFAULT_FIELD_SEPARATOR
toString()
methods.protected static final String DEFAULT_NAME_SEPARATOR
toString()
methods.public Covariance(SymmetricPositiveMatrix covarianceMatrixIn)
The rows/columns of the covariance matrix are automatically associated with default parameter descriptors. These descriptors are comprised of a single field descriptor ( PARAMETER_NAME), mapped to the string "p" + i, where i is the index of the corresponding row/column in the covariance matrix.
covarianceMatrixIn
- the covariance matrixNullPointerException
- if the provided covariance matrix is null
ParameterUtils.buildDefaultParameterDescriptors(int)
public Covariance(SymmetricPositiveMatrix covarianceMatrixIn, Collection<ParameterDescriptor> parameterDescriptorsIn)
The provided parameter descriptors are automatically associated with the rows/colums of the
covariance matrix (in the order they are returned by the collection's iterator). The number
of parameter descriptors must match the size the covariance matrix. If the provided
collection is null
or empty, default parameter descriptors are used instead. These
default descriptors are comprised of a single field descriptor (
PARAMETER_NAME), mapped to the string
"p" + i, where i is the index of the corresponding row/column in the covariance matrix.
Note that this constructor performs a shallow copy of the provided parameter descriptors. The covariance matrix itself is passed by reference.
covarianceMatrixIn
- the covariance matrixparameterDescriptorsIn
- the parameter descriptors associated with the covariance matrixIllegalArgumentException
- if the provided covariance matrix is null
, if the number of parameter
descriptors does not match the size of the covariance
matrix, or if the collection of parameter descriptors contains any duplicateParameterUtils.buildDefaultParameterDescriptors(int)
protected int indexOf(ParameterDescriptor parameterDescriptor)
parameterDescriptor
- the parameter descriptor whose index must be retrievedIllegalArgumentException
- if the parameter descriptor is not associated with this covariance matrixpublic int getSize()
public SymmetricPositiveMatrix getCovarianceMatrix()
A covariance matrix is symmetric positive semi-definite by definition. Whether these properties are actually respected depends on the implementation of SymmetricPositiveMatrix used to store the matrix.
Note that this method provides a direct access to the covariance matrix stored internally, which is possibly mutable.
public List<ParameterDescriptor> getParameterDescriptors()
Note that this methods returns a shallow copy of the list of parameter descriptors stored internally. However, this list provides a direct access to the parameter descriptors stored by this instance, which are mutable.
public ParameterDescriptor getParameterDescriptor(int index)
Note that this method provides a direct access to the parameter descriptors stored internally, which are mutable.
index
- the row/column index of the parameter descriptor to be retrievedpublic Covariance getSubCovariance(int[] indices)
This method extracts the specified rows/columns associated in order to build a new Covariance instance. The provided index array may contain duplicates, but an exception will be thrown if it any of the indices is not a valid row/column index. This method can be used to extract any submatrix and/or to perform a symmetric reordering of the rows/columns of the covariance matrix.
Important:
Since a parameter descriptor cannot be associated with multiple rows/columns of the
covariance, the provided index array must not contain any duplicate (an exception will be
thrown if that occurs).
indices
- the indices of the rows/columns to be extractedIllegalArgumentException
- if one of the specified indices is not a valid row/column index for this covariance
matrix, or the provided index array contains any duplicatepublic Covariance getSubCovariance(Collection<ParameterDescriptor> selectedParameterDescriptors)
This method extracts the rows/columns associated with the specified parameter descriptors in order to build a new Covariance instance. The provided collection may contain duplicates, but an exception will be thrown if any of the parameter descriptors is not associated with this covariance. This method can be used to extract any submatrix and/or to perform a symmetric reordering of the rows/columns of the covariance matrix.
Important:
Since a parameter descriptor cannot be associated with multiple rows/columns of the
covariance matrix, the provided collection must not contain any duplicate (an exception will
be thrown if that occurs).
selectedParameterDescriptors
- the parameter descriptors associated with the rows/columns to be extractedIllegalArgumentException
- if one of the provided parameter descriptors is not associated with this covariance
matrix, or if the provided collection contains any duplicatepublic DiagonalMatrix getVarianceMatrix()
The variance matrix is a diagonal matrix which contains the diagonal elements of the covariance matrix.
public double getVariance(int index)
index
- the row/column indexIllegalArgumentException
- if the provided index is not validpublic double getVariance(ParameterDescriptor parameterDescriptor)
parameterDescriptor
- the parameter descriptorIllegalArgumentException
- if the provided collection is null
or if the parameter descriptor is not
associated with this covariance matrixpublic DiagonalMatrix getStandardDeviationMatrix()
The standard deviation matrix is a diagonal matrix which contains the square root of the diagonal elements of this covariance matrix.
public double getStandardDeviation(int index)
index
- the row/column indexOutOfRangeException
- if the provided index is not validpublic double getStandardDeviation(ParameterDescriptor parameterDescriptor)
parameterDescriptor
- the parameter descriptorIllegalArgumentException
- if the parameter descriptor is not associated with this covariance matrixpublic SymmetricPositiveMatrix getCorrelationCoefficientsMatrix()
The correlation matrix is a symmetric positive matrix which contains the correlation coefficients of this covariance matrix. The correlation coefficients for the diagonal elements are equal to 1 by definition. For off-diagonal elements, they are equal to Ci,j/σiσj, where σi and σj are the standard deviations for the ith and jth elements.
public double getCorrelationCoefficient(int row, int column)
If i is equal to j, ρi,j is equal to 1 by definition. Otherwise, ρi,j is equal to Ci,j / (σi*σj). If the division cannot be computed because σi*σj is too small, ρi,j is set to 0.
row
- the row indexcolumn
- the column indexIllegalArgumentException
- if one of the provided row/column indexes is not validpublic double getCorrelationCoefficient(ParameterDescriptor parameterDescriptor1, ParameterDescriptor parameterDescriptor2)
If i is equal to j, ρi,j is equal to 1 by definition. Otherwise, ρi,j is equal to Ci,j / (σi*σj). If the division cannot be computed because σi*σj is too small, ρi,j is set to 0.
parameterDescriptor1
- the first parameter descriptorparameterDescriptor2
- the second parameter descriptorIllegalArgumentException
- if one of the parameter descriptors is not associated with this covariance matrixpublic Covariance quadraticMultiplication(RealMatrix m)
m
- the matrix MNullArgumentException
- if the matrix M is @null
DimensionMismatchException
- if this matrix and the matrices M and MT are not multiplication compatiblepublic Covariance quadraticMultiplication(RealMatrix m, boolean isTranspose)
m
- the matrix MisTranspose
- if true
, assume the matrix provided is MT, otherwise assume it is MNullArgumentException
- if the matrix M is @null
DimensionMismatchException
- if this matrix and the matrices M and MT are not multiplication compatiblepublic Covariance quadraticMultiplication(RealMatrix m, Collection<ParameterDescriptor> newParameterDescriptors)
m
- the matrix MnewParameterDescriptors
- the parameter descriptors to be associated with the computed covariance matrixNullArgumentException
- if the matrix M is @null
DimensionMismatchException
- if this matrix and the matrices M and MT are not multiplication compatibleIllegalArgumentException
- if the number of parameter descriptors does not match the size of the covariance
matrix, or if the collection of parameter descriptors contains any duplicatepublic Covariance quadraticMultiplication(RealMatrix m, Collection<ParameterDescriptor> newParameterDescriptors, boolean isTranspose)
m
- the matrix MnewParameterDescriptors
- the parameter descriptors to be associated with the computed covariance matrixisTranspose
- if true
, assume the matrix provided is MT, otherwise assume it is MNullArgumentException
- if the matrix M is @null
DimensionMismatchException
- if this matrix and the matrix M or MT is not multiplication compatibleIllegalArgumentException
- if the provided matrix M is null
, if the number of parameter descriptors does
not match the size of the covariance matrix, or if the collection of parameter
descriptors contains any duplicatepublic Covariance add(SymmetricPositiveMatrix m)
m
- the covariance matrix M to be addedNullArgumentException
- if the matrix M is @null
MatrixDimensionMismatchException
- if the two matrices are not addition compatiblepublic Covariance positiveScalarMultiply(double d)
d
- the scalar by which the matrix is multiplied (≥0)NotPositiveException
- if the provided scalar is negativepublic double getMahalanobisDistance(RealVector point)
The Mahalanobis distance is defined by:
dM(P) = sqrt(PT x C-1 x P)
with P the point and C the covariance matrix (centered on 0).
point
- the point PSingularMatrixException
- if the covariance matrix is singularDimensionMismatchException
- if the provided vector do not have the same size as the covariance matrixArithmeticException
- if the computed Mahalanobis distance is negativepublic double getMahalanobisDistance(RealVector point, RealVector mean)
The Mahalanobis distance is defined by:
dM(P,M) = sqrt((P-M)T x C-1 x (P-M))
with P the point and C the covariance matrix centered on M.
point
- the pointmean
- the mean value of the covarianceSingularMatrixException
- if the covariance matrix is singularDimensionMismatchException
- if the provided vectors have different sizes, or if they do not have the same size as
the covariance matrixArithmeticException
- if the computed Mahalanobis distance is negativepublic String toString()
public String toString(RealMatrixFormat realMatrixFormat)
null
).realMatrixFormat
- the format to use when printing the covariance matrixpublic String toString(RealMatrixFormat realMatrixFormat, String nameSeparator, String fieldSeparator, boolean printClassName, boolean reverseOrder)
null
).realMatrixFormat
- the format to use when printing the covariance matrixnameSeparator
- the string to use as a separator between the names of the parameter descriptorsfieldSeparator
- the string to use as a separator between the field values of a parameter descriptorprintClassName
- whether or not the name of this class should be printedreverseOrder
- whether or not the field values of each parameter descriptor should be printed in
reverse orderpublic Covariance copy()
This method performs a shallow copy of the parameter descriptors list and a deep copy of the covariance matrix.
ParameterDescriptor.copy()
Copyright © 2023 CNES. All rights reserved.