Class CovarianceMatrix

java.lang.Object
edu.cmu.tetrad.data.CovarianceMatrix
All Implemented Interfaces:
DataModel, ICovarianceMatrix, KnowledgeTransferable, VariableSource, TetradSerializable, Serializable
Direct Known Subclasses:
CorrelationMatrix

public class CovarianceMatrix extends Object implements ICovarianceMatrix
Stores a covariance matrix together with variable names and sample size, intended as a representation of a data set. When constructed from a continuous data set, the matrix is not checked for positive definiteness; however, when a covariance matrix is supplied, its positive definiteness is always checked. If the sample size is less than the number of variables, the positive definiteness is "spot-checked"--that is, checked for various submatrices.
Version:
$Id: $Id
Author:
josephramsey
See Also:
  • Constructor Details

    • CovarianceMatrix

      public CovarianceMatrix(DataSet dataSet)
      Constructs a new covariance matrix from the given data set.
      Parameters:
      dataSet - a DataSet object
      Throws:
      IllegalArgumentException - if this is not a continuous data set.
    • CovarianceMatrix

      public CovarianceMatrix(DataSet dataSet, boolean biasCorrected)

      Constructor for CovarianceMatrix.

      Parameters:
      dataSet - a DataSet object
      biasCorrected - a boolean
    • CovarianceMatrix

      public CovarianceMatrix(List<Node> variables, Matrix matrix, int sampleSize)
      Protected constructor to construct a new covariance matrix using the supplied continuous variables and the the given symmetric, positive definite matrix and sample size. The number of variables must equal the dimension of the array.
      Parameters:
      variables - the list of variables (in order) for the covariance matrix.
      matrix - an square array of containing covariances.
      sampleSize - the sample size of the data for these covariances.
      Throws:
      IllegalArgumentException - if the given matrix is not symmetric (to a tolerance of 1.e-5) and positive definite, if the number of variables does not equal the dimension of m, or if the sample size is not positive.
    • CovarianceMatrix

      public CovarianceMatrix(List<Node> variables, double[][] matrix, int sampleSize)

      Constructor for CovarianceMatrix.

      Parameters:
      variables - a List object
      matrix - an array of objects
      sampleSize - a int
    • CovarianceMatrix

      public CovarianceMatrix(CovarianceMatrix covMatrix)
      Copy constructor.
      Parameters:
      covMatrix - a CovarianceMatrix object
    • CovarianceMatrix

      public CovarianceMatrix(ICovarianceMatrix covMatrix)

      Constructor for CovarianceMatrix.

      Parameters:
      covMatrix - a ICovarianceMatrix object
  • Method Details

    • serializableInstance

      public static ICovarianceMatrix serializableInstance()
      Generates a simple exemplar of this class to test serialization.
      Returns:
      a ICovarianceMatrix object
    • getVariables

      public final List<Node> getVariables()

      Getter for the field variables.

      Specified by:
      getVariables in interface ICovarianceMatrix
      Specified by:
      getVariables in interface VariableSource
      Returns:
      the list of variables (unmodifiable).
    • setVariables

      public void setVariables(List<Node> variables)
      Sets the list of Node variables for the covariance matrix.
      Specified by:
      setVariables in interface ICovarianceMatrix
      Parameters:
      variables - a list of Node objects representing the variables to be set in the covariance matrix
    • getVariableNames

      public final List<String> getVariableNames()

      getVariableNames.

      Specified by:
      getVariableNames in interface ICovarianceMatrix
      Specified by:
      getVariableNames in interface VariableSource
      Returns:
      the variable names, in order.
    • getVariableName

      public final String getVariableName(int index)
      Retrieves the name of the variable at the specified index from the covariance matrix.
      Specified by:
      getVariableName in interface ICovarianceMatrix
      Parameters:
      index - the index of the variable whose name is to be retrieved
      Returns:
      the name of the variable at the specified index
    • getDimension

      public final int getDimension()

      getDimension.

      Specified by:
      getDimension in interface ICovarianceMatrix
      Returns:
      the dimension of the covariance matrix.
    • getSampleSize

      public final int getSampleSize()
      The size of the sample used to calculated this covariance matrix.
      Specified by:
      getSampleSize in interface ICovarianceMatrix
      Returns:
      The sample size (> 0).
    • setSampleSize

      public final void setSampleSize(int sampleSize)
      Sets the sample size used in the covariance matrix.
      Specified by:
      setSampleSize in interface ICovarianceMatrix
      Parameters:
      sampleSize - the sample size to be set
    • getName

      public final String getName()
      Gets the name of the covariance matrix.
      Specified by:
      getName in interface DataModel
      Specified by:
      getName in interface ICovarianceMatrix
      Returns:
      a String object
    • setName

      public final void setName(String name)
      Sets the name of the covariance matrix.

      Sets the name of the covariance matrix.

      Specified by:
      setName in interface DataModel
      Specified by:
      setName in interface ICovarianceMatrix
      Parameters:
      name - the new name of the covariance matrix
    • getKnowledge

      public final Knowledge getKnowledge()

      Getter for the field knowledge.

      Specified by:
      getKnowledge in interface ICovarianceMatrix
      Specified by:
      getKnowledge in interface KnowledgeTransferable
      Returns:
      the knowledge associated with this data.
    • setKnowledge

      public final void setKnowledge(Knowledge knowledge)
      Sets the knowledge associated with the ICovarianceMatrix.

      Associates knowledge with this data.

      Specified by:
      setKnowledge in interface ICovarianceMatrix
      Specified by:
      setKnowledge in interface KnowledgeTransferable
      Parameters:
      knowledge - the knowledge to set
    • getSubmatrix

      public final ICovarianceMatrix getSubmatrix(int[] indices)

      getSubmatrix.

      Specified by:
      getSubmatrix in interface ICovarianceMatrix
      Parameters:
      indices - an array of objects
      Returns:
      a submatrix of the covariance matrix with variables in the given order.
    • getSubmatrix

      public final ICovarianceMatrix getSubmatrix(List<String> submatrixVarNames)

      getSubmatrix.

      Specified by:
      getSubmatrix in interface ICovarianceMatrix
      Parameters:
      submatrixVarNames - a List object
      Returns:
      a ICovarianceMatrix object
    • getSubmatrix

      public final CovarianceMatrix getSubmatrix(String[] submatrixVarNames)

      getSubmatrix.

      Specified by:
      getSubmatrix in interface ICovarianceMatrix
      Parameters:
      submatrixVarNames - an array of String objects
      Returns:
      a submatrix of this matrix, with variables in the given order.
    • getValue

      public final double getValue(int i, int j)
      Retrieves the value from the covariance matrix at the specified row and column indices.
      Specified by:
      getValue in interface ICovarianceMatrix
      Parameters:
      i - the row index
      j - the column index
      Returns:
      the value at the specified indices in the matrix
    • getSize

      public final int getSize()

      getSize.

      Specified by:
      getSize in interface ICovarianceMatrix
      Returns:
      the size of the square matrix.
    • getMatrix

      public final Matrix getMatrix()

      getMatrix.

      Specified by:
      getMatrix in interface ICovarianceMatrix
      Returns:
      a the covariance matrix (not a copy).
    • setMatrix

      public void setMatrix(Matrix matrix)
      Sets the covariance matrix.
      Specified by:
      setMatrix in interface ICovarianceMatrix
      Parameters:
      matrix - the matrix to set
    • select

      public final void select(Node variable)
      Selects a specified variable in the covariance matrix.
      Specified by:
      select in interface ICovarianceMatrix
      Parameters:
      variable - the node variable to be selected
    • clearSelection

      public final void clearSelection()

      clearSelection.

      Specified by:
      clearSelection in interface ICovarianceMatrix
    • isSelected

      public final boolean isSelected(Node variable)
      Checks if the specified node is selected in the covariance matrix.
      Specified by:
      isSelected in interface ICovarianceMatrix
      Parameters:
      variable - the node to check for selection
      Returns:
      true if the node is selected, false otherwise
    • getSelectedVariableNames

      public final List<String> getSelectedVariableNames()

      getSelectedVariableNames.

      Specified by:
      getSelectedVariableNames in interface ICovarianceMatrix
      Returns:
      a List object
    • toString

      public final String toString()
      Prints out the matrix
      Specified by:
      toString in interface DataModel
      Specified by:
      toString in interface ICovarianceMatrix
      Overrides:
      toString in class Object
      Returns:
      a String object
    • isContinuous

      public boolean isContinuous()

      isContinuous.

      Specified by:
      isContinuous in interface DataModel
      Returns:
      true if the data model is continuous, false otherwise.
    • isDiscrete

      public boolean isDiscrete()

      isDiscrete.

      Specified by:
      isDiscrete in interface DataModel
      Returns:
      true if the data model is discrete, false otherwise.
    • isMixed

      public boolean isMixed()

      isMixed.

      Specified by:
      isMixed in interface DataModel
      Returns:
      true if the data model is mixed continuous/discrete, false otherwise.
    • getSelection

      public Matrix getSelection(int[] rows, int[] cols)
      Returns a submatrix based on the specified rows and columns.
      Specified by:
      getSelection in interface ICovarianceMatrix
      Parameters:
      rows - an array of integers representing the row indices to be included in the selection.
      cols - an array of integers representing the column indices to be included in the selection.
      Returns:
      a Matrix object containing the selected rows and columns.
    • getVariable

      public Node getVariable(String name)
      Retrieves a Node instance from the covariance matrix corresponding to the specified variable name.
      Specified by:
      getVariable in interface DataModel
      Specified by:
      getVariable in interface ICovarianceMatrix
      Parameters:
      name - the name of the variable to retrieve
      Returns:
      the Node associated with the specified variable name
    • copy

      public DataModel copy()

      copy.

      Specified by:
      copy in interface DataModel
      Returns:
      a copy of the data model.
    • setValue

      public void setValue(int i, int j, double v)
      Sets the value of the covariance matrix at the specified indices.
      Specified by:
      setValue in interface ICovarianceMatrix
      Parameters:
      i - the row index
      j - the column index
      v - the value to set at the specified indices
    • removeVariables

      public void removeVariables(List<String> remaining)
      Removes variables from the covariance matrix, retaining only the variables specified in the provided list.
      Specified by:
      removeVariables in interface ICovarianceMatrix
      Parameters:
      remaining - a list of variable names to retain in the covariance matrix.
    • isSingular

      public boolean isSingular()

      isSingular.

      Returns:
      a boolean