Class MlBayesImObs

java.lang.Object
edu.cmu.tetrad.bayes.MlBayesImObs
All Implemented Interfaces:
BayesIm, Simulator, VariableSource, Im, TetradSerializable, Serializable

public final class MlBayesImObs extends Object implements BayesIm
Stores a table of probabilities for a Bayes net and, together with BayesPm and Dag, provides methods to manipulate this table. The division of labor is as follows. The Dag is responsible for manipulating the basic graphical structure of the Bayes net. Dag also stores and manipulates the names of the nodes in the graph; there are no method in either BayesPm or BayesIm to do this. BayesPm stores and manipulates the *categories* of each node in a DAG, considered as a variable in a Bayes net. The number of categories for a variable can be changed there as well as the names for those categories. This class, BayesIm, stores the actual probability tables which are implied by the structures in the other two classes. The implied parameters take the form of conditional probabilities--e.g., P(N=v0|P1=v1, P2=v2, ...), for all nodes and all combinations of their parent categories. The set of all such probabilities is organized in this class as a three-dimensional table of double values. The first dimension corresponds to the nodes in the Bayes net. For each such node, the second dimension corresponds to a flat list of combinations of parent categories for that node. The third dimension corresponds to the list of categories for that node itself. Two methods allow these values to be set and retrieved:
  • getWordRatio(int nodeIndex, int rowIndex, int colIndex); and,
  • setProbability(int nodeIndex, int rowIndex, int colIndex, int probability).
To determine the index of the node in question, use the method
  • getNodeIndex(Node node).
To determine the index of the row in question, use the method
  • getRowIndex(int[] parentVals).
To determine the order of the parent values for a given node so that you can build the parentVals[] array, use the method
  • getParents(int nodeIndex)
To determine the index of a category, use the method
  • getCategoryIndex(Node node)
in BayesPm. The rest of the methods in this class are easily understood as variants of the methods above.

Thanks to Pucktada Treeratpituk, Frank Wimberly, and Willie Wheeler for advise and earlier versions.> 0

Author:
josephramsey
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
    Constructs a new BayesIm from the given BayesPm, initializing all values as Double.NaN ("?").
    MlBayesImObs(BayesPm bayesPm, int initializationMethod)
    Constructs a new BayesIm from the given BayesPm, initializing values either as MANUAL or RANDOM.
    MlBayesImObs(BayesPm bayesPm, BayesIm oldBayesIm, int initializationMethod)
    Constructs a new BayesIm from the given BayesPm, initializing values either as MANUAL or RANDOM, but using values from the old BayesIm provided where posssible.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    clearRow(int nodeIndex, int rowIndex)
    Assigns random probability values to the child values of this row that add to 1.
    void
    clearTable(int nodeIndex)
    Randomizes every row in the table for the given node index.
    void
     
    boolean
    Returns true iff this Bayes net is equal to the given Bayes net.
     
    Returns the underlying Bayes PM.
    int
    getCorrespondingNodeIndex(int nodeIndex, BayesIm otherBayesIm)
    Returns the index of the given node in the given BayesIm.
    $Description
     
    Returns the list of measured variables.
    getNode(int nodeIndex)
    Returns the name of the given node.
    Returns the name of the given node.
    int
    Returns the index of the given node.
    int
    getNumColumns(int nodeIndex)
    Returns the number of columns.
    int
    Returns the name of the given node.
    int
    getNumParents(int nodeIndex)
    Returns the number of parents for the given node.
    int
     
    int
    getNumRows(int nodeIndex)
    Returns the number of rows.
    int
    getParent(int nodeIndex, int parentIndex)
    Returns the ith parent of the givne node.
    int
    getParentDim(int nodeIndex, int parentIndex)
    Returns the dimension of the given parent for the given node.
    int[]
    getParentDims(int nodeIndex)
    Returns the dimensions of the pararents of the given node.
    int[]
    getParents(int nodeIndex)
    Returns the parents of the given node.
    int
    getParentValue(int nodeIndex, int rowIndex, int colIndex)
    Returns the given parent value.
    int[]
    getParentValues(int nodeIndex, int rowIndex)
    Returns the parents values of the given node.
    double
    getProbability(int rowIndex)
     
    double
    getProbability(int nodeIndex, int rowIndex, int colIndex)
    Returns the probability for the given cell in the given CPT.
    int
    getRowIndex(int nodeIndex, int[] values)
    Returns a row index.
    int[]
    getRowValues(int rowIndex)
     
    Returns the list of variable names.
    Returns the list of variables.
    boolean
    isIncomplete(int nodeIndex)
    Returns true iff the given node has a Double.NaN value in it.
    boolean
    isIncomplete(int nodeIndex, int rowIndex)
    Returns true iff the given row in the given node has a Double.NaN value in it.
    void
    Normalizes all rows in the tables associated with each of node in turn.
    void
    normalizeNode(int nodeIndex)
    Normalizes all rows in the table associated with a given node.
    void
    normalizeRow(int nodeIndex, int rowIndex)
    Normalizes the given row.
    void
    randomizeIncompleteRows(int nodeIndex)
    Randomizes any row in the table for the given node index that has a Double.NaN value in it.
    void
    randomizeRow(int nodeIndex, int rowIndex)
    Assigns random probability values to the child values of this row that add to 1.
    void
    randomizeTable(int nodeIndex)
    Randomizes every row in the table for the given node index.
    Generates a simple exemplar of this class to test serialization.
    void
    setProbability(int rowIndex, double value)
     
    void
    setProbability(int nodeIndex, double[][] probMatrix)
    Sets the probability for the given node.
    void
    setProbability(int nodeIndex, int rowIndex, int colIndex, double value)
    Sets the probability for the given node at a given row and column in the table for that node.
    simulateData(int sampleSize, boolean latentDataSaved)
    Simulates a sample with the given sample size.
    simulateData(DataSet dataSet, boolean latentDataSaved)
    Overwrites the given dataSet with a new simulated dataSet, to avoid allocating memory.
    Prints out the probability table for each variable.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • MlBayesImObs

      public MlBayesImObs(BayesPm bayesPm) throws IllegalArgumentException
      Constructs a new BayesIm from the given BayesPm, initializing all values as Double.NaN ("?").
      Parameters:
      bayesPm - the given Bayes PM. Carries with it the underlying graph model.
      Throws:
      IllegalArgumentException - if the array of nodes provided is not a permutation of the nodes contained in the bayes parametric model provided.
    • MlBayesImObs

      public MlBayesImObs(BayesPm bayesPm, int initializationMethod) throws IllegalArgumentException
      Constructs a new BayesIm from the given BayesPm, initializing values either as MANUAL or RANDOM. If initialized manually, all values will be set to Double.NaN ("?") in each row; if initialized randomly, all values will distributed randomly in each row.
      Parameters:
      bayesPm - the given Bayes PM. Carries with it the underlying graph model.
      initializationMethod - either MANUAL or RANDOM.
      Throws:
      IllegalArgumentException - if the array of nodes provided is not a permutation of the nodes contained in the bayes parametric model provided.
    • MlBayesImObs

      public MlBayesImObs(BayesPm bayesPm, BayesIm oldBayesIm, int initializationMethod) throws IllegalArgumentException
      Constructs a new BayesIm from the given BayesPm, initializing values either as MANUAL or RANDOM, but using values from the old BayesIm provided where posssible. If initialized manually, all values that cannot be retrieved from oldBayesIm will be set to Double.NaN ("?") in each such row; if initialized randomly, all values that cannot be retrieved from oldBayesIm will distributed randomly in each such row.
      Parameters:
      bayesPm - the given Bayes PM. Carries with it the underlying graph model.
      oldBayesIm - an already-constructed BayesIm whose values may be used where possible to initialize this BayesIm. May be null.
      initializationMethod - either MANUAL or RANDOM.
      Throws:
      IllegalArgumentException - if the array of nodes provided is not a permutation of the nodes contained in the bayes parametric model provided.
    • MlBayesImObs

      public MlBayesImObs(BayesIm bayesIm) throws IllegalArgumentException
      Throws:
      IllegalArgumentException
  • Method Details

    • serializableInstance

      public static MlBayesImObs serializableInstance()
      Generates a simple exemplar of this class to test serialization.
    • getBayesPm

      public BayesPm getBayesPm()
      Description copied from interface: BayesIm
      Returns the underlying Bayes PM.
      Specified by:
      getBayesPm in interface BayesIm
      Returns:
      this PM.
    • getDag

      public Graph getDag()
      Description copied from interface: BayesIm
      $Description
      Specified by:
      getDag in interface BayesIm
      Returns:
      the DAG.
    • getNumNodes

      public int getNumNodes()
      Description copied from interface: BayesIm
      Returns the name of the given node.
      Specified by:
      getNumNodes in interface BayesIm
      Returns:
      the number of nodes in the model.
    • getNode

      public Node getNode(int nodeIndex)
      Description copied from interface: BayesIm
      Returns the name of the given node.
      Specified by:
      getNode in interface BayesIm
      Parameters:
      nodeIndex - the index of the node.
      Returns:
      this node.
    • getNode

      public Node getNode(String name)
      Description copied from interface: BayesIm
      Returns the name of the given node.
      Specified by:
      getNode in interface BayesIm
      Parameters:
      name - the name of the node.
      Returns:
      the node.
    • getNodeIndex

      public int getNodeIndex(Node node)
      Description copied from interface: BayesIm
      Returns the index of the given node.
      Specified by:
      getNodeIndex in interface BayesIm
      Parameters:
      node - the given node.
      Returns:
      the index for that node, or -1 if the node is not in the BayesIm.
    • getVariables

      public List<Node> getVariables()
      Description copied from interface: BayesIm
      Returns the list of variables.
      Specified by:
      getVariables in interface BayesIm
      Specified by:
      getVariables in interface VariableSource
      Returns:
      the list of variable for this Bayes net.
    • getMeasuredNodes

      public List<Node> getMeasuredNodes()
      Description copied from interface: BayesIm
      Returns the list of measured variables.
      Specified by:
      getMeasuredNodes in interface BayesIm
      Returns:
      the list of measured variableNodes.
    • getVariableNames

      public List<String> getVariableNames()
      Description copied from interface: BayesIm
      Returns the list of variable names.
      Specified by:
      getVariableNames in interface BayesIm
      Specified by:
      getVariableNames in interface VariableSource
      Returns:
      the list of variable names for this Bayes net.
    • getNumColumns

      public int getNumColumns(int nodeIndex)
      Description copied from interface: BayesIm
      Returns the number of columns.
      Specified by:
      getNumColumns in interface BayesIm
      Parameters:
      nodeIndex - the index of the node.
      Returns:
      this number.
      See Also:
    • getNumRows

      public int getNumRows(int nodeIndex)
      Description copied from interface: BayesIm
      Returns the number of rows.
      Specified by:
      getNumRows in interface BayesIm
      Parameters:
      nodeIndex - the index of the node.
      Returns:
      this number.
      See Also:
    • getNumParents

      public int getNumParents(int nodeIndex)
      Description copied from interface: BayesIm
      Returns the number of parents for the given node.
      Specified by:
      getNumParents in interface BayesIm
      Parameters:
      nodeIndex - the given node.
      Returns:
      the number of parents for this node.
    • getParent

      public int getParent(int nodeIndex, int parentIndex)
      Description copied from interface: BayesIm
      Returns the ith parent of the givne node.
      Specified by:
      getParent in interface BayesIm
      Parameters:
      nodeIndex - the index of the node.
      parentIndex - the index of the parent.
      Returns:
      the given parent of the given node.
    • getParentDim

      public int getParentDim(int nodeIndex, int parentIndex)
      Description copied from interface: BayesIm
      Returns the dimension of the given parent for the given node.
      Specified by:
      getParentDim in interface BayesIm
      Parameters:
      nodeIndex - the index of the node.
      parentIndex - the index of the parent.
      Returns:
      the dimension of the given parent for the given node.
    • getParentDims

      public int[] getParentDims(int nodeIndex)
      Description copied from interface: BayesIm
      Returns the dimensions of the pararents of the given node.
      Specified by:
      getParentDims in interface BayesIm
      Parameters:
      nodeIndex - the index of the node.
      Returns:
      this array of parent dimensions.
      See Also:
    • getParents

      public int[] getParents(int nodeIndex)
      Description copied from interface: BayesIm
      Returns the parents of the given node.
      Specified by:
      getParents in interface BayesIm
      Parameters:
      nodeIndex - the index of the node.
      Returns:
      (a defensive copy of) the array containing all of the parents of a given node in the order in which they are stored internally.
      See Also:
    • getParentValues

      public int[] getParentValues(int nodeIndex, int rowIndex)
      Description copied from interface: BayesIm
      Returns the parents values of the given node.
      Specified by:
      getParentValues in interface BayesIm
      Parameters:
      nodeIndex - the index of the node.
      rowIndex - the index of the row in question.
      Returns:
      the array representing the combination of parent values for this row.
      See Also:
    • getParentValue

      public int getParentValue(int nodeIndex, int rowIndex, int colIndex)
      Description copied from interface: BayesIm
      Returns the given parent value.
      Specified by:
      getParentValue in interface BayesIm
      Parameters:
      nodeIndex - the index of the node.
      rowIndex - the index of the row in question.
      colIndex - the index of the column in question.
      Returns:
      the value in the probability table for the given node, at the given row and column.
    • getProbability

      public double getProbability(int nodeIndex, int rowIndex, int colIndex)
      Description copied from interface: BayesIm
      Returns the probability for the given cell in the given CPT.
      Specified by:
      getProbability in interface BayesIm
      Parameters:
      nodeIndex - the index of the node in question.
      rowIndex - the row in the table for this for node which represents the combination of parent values in question.
      colIndex - the column in the table for this node which represents the value of the node in question.
      Returns:
      the probability stored for this parameter.
      See Also:
    • getRowIndex

      public int getRowIndex(int nodeIndex, int[] values)
      Description copied from interface: BayesIm
      Returns a row index.
      Specified by:
      getRowIndex in interface BayesIm
      Parameters:
      nodeIndex - the index of the node in question.
      values - the combination of parent values in question.
      Returns:
      the row in the table for the given node and combination of parent values.
      See Also:
    • normalizeAll

      public void normalizeAll()
      Normalizes all rows in the tables associated with each of node in turn.
      Specified by:
      normalizeAll in interface BayesIm
    • normalizeNode

      public void normalizeNode(int nodeIndex)
      Normalizes all rows in the table associated with a given node.
      Specified by:
      normalizeNode in interface BayesIm
      Parameters:
      nodeIndex - the index of the node in question.
    • normalizeRow

      public void normalizeRow(int nodeIndex, int rowIndex)
      Normalizes the given row.
      Specified by:
      normalizeRow in interface BayesIm
      Parameters:
      nodeIndex - the index of the node in question.
      rowIndex - the index of the row in question.
    • setProbability

      public void setProbability(int nodeIndex, double[][] probMatrix)
      Sets the probability for the given node. The matrix row represent row index, the row in the table for this for node which represents the combination of parent values in question. of the CPT. The matrix column represent column index, the column in the table for this node which represents the value of the node in question.
      Specified by:
      setProbability in interface BayesIm
      Parameters:
      nodeIndex - the index of the node in question.
      probMatrix - a matrix containing probabilities of a node along with its parents
    • setProbability

      public void setProbability(int nodeIndex, int rowIndex, int colIndex, double value)
      Sets the probability for the given node at a given row and column in the table for that node. To get the node index, use getNodeIndex(). To get the row index, use getRowIndex(). To get the column index, use getCategoryIndex() from the underlying BayesPm(). The value returned will represent a conditional probability of the form P(N=v0 | P1=v1, P2=v2, ... , Pn=vn), where N is the node referenced by nodeIndex, v0 is the value referenced by colIndex, and the combination of parent values indicated is the combination indicated by rowIndex.
      Specified by:
      setProbability in interface BayesIm
      Parameters:
      nodeIndex - the index of the node in question.
      rowIndex - the row in the table for this for node which represents the combination of parent values in question.
      colIndex - the column in the table for this node which represents the value of the node in question.
      value - the desired probability to be set.
      See Also:
    • getCorrespondingNodeIndex

      public int getCorrespondingNodeIndex(int nodeIndex, BayesIm otherBayesIm)
      Description copied from interface: BayesIm
      Returns the index of the given node in the given BayesIm.
      Specified by:
      getCorrespondingNodeIndex in interface BayesIm
      Parameters:
      nodeIndex - the index of the node in this BayesIm.
      otherBayesIm - the BayesIm in which the node is to be found.
      Returns:
      the index of the node with the given name in the specified BayesIm.
    • clearRow

      public void clearRow(int nodeIndex, int rowIndex)
      Assigns random probability values to the child values of this row that add to 1.
      Specified by:
      clearRow in interface BayesIm
      Parameters:
      nodeIndex - the node for the table that this row belongs to.
      rowIndex - the index of the row.
    • randomizeRow

      public void randomizeRow(int nodeIndex, int rowIndex)
      Assigns random probability values to the child values of this row that add to 1.
      Specified by:
      randomizeRow in interface BayesIm
      Parameters:
      nodeIndex - the node for the table that this row belongs to.
      rowIndex - the index of the row.
    • randomizeIncompleteRows

      public void randomizeIncompleteRows(int nodeIndex)
      Randomizes any row in the table for the given node index that has a Double.NaN value in it.
      Specified by:
      randomizeIncompleteRows in interface BayesIm
      Parameters:
      nodeIndex - the node for the table whose incomplete rows are to be randomized.
    • randomizeTable

      public void randomizeTable(int nodeIndex)
      Randomizes every row in the table for the given node index.
      Specified by:
      randomizeTable in interface BayesIm
      Parameters:
      nodeIndex - the node for the table to be randomized.
    • clearTable

      public void clearTable(int nodeIndex)
      Randomizes every row in the table for the given node index.
      Specified by:
      clearTable in interface BayesIm
      Parameters:
      nodeIndex - the node for the table to be randomized.
    • isIncomplete

      public boolean isIncomplete(int nodeIndex, int rowIndex)
      Description copied from interface: BayesIm
      Returns true iff the given row in the given node has a Double.NaN value in it.
      Specified by:
      isIncomplete in interface BayesIm
      Parameters:
      nodeIndex - the node for the table whose incomplete rows are to be checked.
      rowIndex - the index of the row in question.
      Returns:
      true iff one of the values in the given row is Double.NaN.
    • isIncomplete

      public boolean isIncomplete(int nodeIndex)
      Description copied from interface: BayesIm
      Returns true iff the given node has a Double.NaN value in it.
      Specified by:
      isIncomplete in interface BayesIm
      Parameters:
      nodeIndex - the node for the table whose incomplete rows are to be checked.
      Returns:
      true iff any value in the table for the given node is Double.NaN.
    • simulateData

      public DataSet simulateData(int sampleSize, boolean latentDataSaved)
      Simulates a sample with the given sample size.
      Specified by:
      simulateData in interface BayesIm
      Specified by:
      simulateData in interface Simulator
      Parameters:
      sampleSize - the sample size.
      latentDataSaved - true iff the latent data is to be saved.
      Returns:
      the simulated sample as a DataSet.
    • simulateData

      public DataSet simulateData(DataSet dataSet, boolean latentDataSaved)
      Description copied from interface: BayesIm
      Overwrites the given dataSet with a new simulated dataSet, to avoid allocating memory. The given dataSet must have the necessary number of columns.
      Specified by:
      simulateData in interface BayesIm
      Parameters:
      dataSet - the dataSet to be overwritten.
      latentDataSaved - true iff the latent data is to be saved.
      Returns:
      the simulated sample as a DataSet.
    • equals

      public boolean equals(Object o)
      Description copied from interface: BayesIm
      Returns true iff this Bayes net is equal to the given Bayes net. The sense of equality may vary depending on the type of Bayes net.
      Specified by:
      equals in interface BayesIm
      Overrides:
      equals in class Object
      Parameters:
      o - the Bayes net to be compared to this Bayes net.
      Returns:
      true iff this bayes net is equal to the given Bayes net. The sense of equality may vary depending on the type of Bayes net.
    • toString

      public String toString()
      Prints out the probability table for each variable.
      Specified by:
      toString in interface BayesIm
      Overrides:
      toString in class Object
      Returns:
      a string representation for this Bayes net.
    • getBayesImObs

      public BayesIm getBayesImObs()
    • getJPD

      public StoredCellProbsObs getJPD()
    • getNumRows

      public int getNumRows()
    • getRowValues

      public int[] getRowValues(int rowIndex)
    • getProbability

      public double getProbability(int rowIndex)
    • setProbability

      public void setProbability(int rowIndex, double value)
    • createRandomCellTable

      public void createRandomCellTable()