java.lang.Object
org.bzdev.math.stats.CovarianceMatrix
- Direct Known Subclasses:
CovarianceMatrix.Population
,CovarianceMatrix.Sample
Covariance matrix.
This class provides the covariance matrix for a vector of measurements
or random variables measured or evaluated multiple times. The inner
classes are subclasses of this class:
-
Sample
is used to compute the covariance matrix for the case in which the dataset whose covariance matrix is computed is a representative sample. -
Population
is used to compute the covariance matrix for the case where the dataset represents every case being computed.
For a row vector X of measurements or random values, the covariance matrix Σ is defined by
Σ = E[(X-E[X])(X - E[X])T]where E[X] is the expectation value of X.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
Class to compute the covariance matrix for a total set of values, as opposed to a sample of values.static final class
Class to compute a sample covariance matrix. -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
CovarianceMatrix
(double[][] arrays, int n) Constructor given an array containing the initial data used to compute a covariance matrix.protected
CovarianceMatrix
(int n) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(double[] values) Add a new vector of values.void
Assert that more values will not be added to the covariance-matrix computation.protected abstract double
The elements of the variance array that is computed will be scaled by a correction factor.double[][]
Get the correlation matrix corresponding to this covariance matrix.void
getCorrelationMatrix
(double[][] result) Get the correlation matrix corresponding to this covariance matrix, storing the result in an array.int
Get the number of rows and colums for this covariance matrix.double[][]
Get the covariance matrix.double[]
getMeans()
Get the mean values of the variables associated with this covariance matrix.
-
Field Details
-
count
protected long countThe number of data vectors used.
-
-
Constructor Details
-
CovarianceMatrix
protected CovarianceMatrix(int n) Constructor. The methodadd(double[])
must be called repeatedly to provide the dataset used to compute the covariance matrix and mean array.- Parameters:
n
- the number of variables
-
CovarianceMatrix
Constructor given an array containing the initial data used to compute a covariance matrix. Note: the initial dataset will be used to estimate the mean for purposes of reducing floating-point errors. More dataset can be added using theadd(double[])
method.- Parameters:
arrays
- an array, each component of which is an array containing the values for n variablesn
- the number of variables- Throws:
IllegalArgumentException
- the array length was incorrect
-
-
Method Details
-
getCorrection
protected abstract double getCorrection()The elements of the variance array that is computed will be scaled by a correction factor. This factor should be 1.0 when the dataset represents a total population. For a sample of a population, the factor is m/(m-1) where m is the sample size.The subclasses
CovarianceMatrix.Sample
andCovarianceMatrix.Population
provide suitable implementations of this method for those cases.- Returns:
- the correction factor; Double.NaN if the factor cannot be computed due to too few entries
-
add
Add a new vector of values. The vector's length must be at least n, where n is the number of variables.- Parameters:
values
- a vector of values (the first n entries will be used)- Throws:
IllegalStateException
- method may no longer be calledIllegalArgumentException
- the array length was incorrect
-
addsComplete
public void addsComplete()Assert that more values will not be added to the covariance-matrix computation. Calling this method results in the methodgetMatrix()
returning the same array repeatedly instead of a fresh copy.- See Also:
-
getMatrix
Get the covariance matrix. The value returned must not be modified when this method is called afteraddsComplete()
has been called.- Returns:
- the covariance matrix
- Throws:
IllegalStateException
- no data is yet availableIllegalArgumentException
- the array length was incorrect
-
getCorrelationMatrix
public void getCorrelationMatrix(double[][] result) Get the correlation matrix corresponding to this covariance matrix, storing the result in an array.- Parameters:
result
- an array to store the result.
-
getCorrelationMatrix
public double[][] getCorrelationMatrix()Get the correlation matrix corresponding to this covariance matrix. Each call will produce a new matrix.- Returns:
- the correlation matrix
-
getMeans
Get the mean values of the variables associated with this covariance matrix. The value returned must not be modified when this method is called afteraddsComplete()
has been called.- Returns:
- the mean values
- Throws:
IllegalStateException
- no data is yet available
-
getDimension
public int getDimension()Get the number of rows and colums for this covariance matrix. The covariance matrix is an N by N matrix, where N is the number of variables associated with the matrix. N is set when the constructor is called.- Returns:
- the number of rows and columns
-