Class UpdatedBayesIm

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

public final class UpdatedBayesIm extends Object implements BayesIm
Represents a Bayes IM in which all of the conditional probability tables have been updated to take into account evidence. Updated conditional probability values of variables with respect to comabinations of their parent values are calculated on the fly. Values that do not change due to being d-separated from a variable conditional on its parents are not calculated; rather, these are simply looked up in the underlying Bayes IM and returned.
Version:
$Id: $Id
Author:
josephramsey
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs Bayes net in which CPT's updated for the given evidence are calculated on the fly.
    UpdatedBayesIm(BayesIm bayesIm, Evidence evidence)
    Constructs Bayes net in which CPT's updated for the given evidence are calculated on the fly.
  • 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.
    boolean
    Returns true iff this Bayes net is equal to the given Bayes net.
    getBayesPm.
    int
    getCorrespondingNodeIndex(int nodeIndex, BayesIm otherBayesIm)
    Returns the index of the given node in the given BayesIm.
    getDag.
    Getter for the field evidence.
    getMeasuredNodes.
    getNode(int nodeIndex)
    Returns the name of the given node.
    getNode.
    int
    Returns the index of the given node.
    int
    getNumColumns(int nodeIndex)
    Returns the number of columns.
    int
    getNumNodes.
    int
    getNumParents(int nodeIndex)
    Returns the number of parents for the given node.
    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 nodeIndex, int rowIndex, int colIndex)
    Returns the probability for the given cell in the given CPT.
    int
    getRowIndex(int nodeIndex, int[] values)
    getRowIndex.
    getVariableNames.
    getVariables.
    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
    normalizeAll.
    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 nodeIndex, double[][] probMatrix)
    setProbability.
    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 data set with the specified number of rows.
    simulateData(DataSet dataSet, boolean latentDataSaved)
    Simulates data based on the provided data set and saves the latent data if specified.
    Prints out the probability table for each variable.

    Methods inherited from class java.lang.Object

    getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface edu.cmu.tetrad.bayes.BayesIm

    getCptMapType
  • Constructor Details

    • UpdatedBayesIm

      public UpdatedBayesIm(BayesIm bayesIm)
      Constructs Bayes net in which CPT's updated for the given evidence are calculated on the fly.
      Parameters:
      bayesIm - a BayesIm object
    • UpdatedBayesIm

      public UpdatedBayesIm(BayesIm bayesIm, Evidence evidence)
      Constructs Bayes net in which CPT's updated for the given evidence are calculated on the fly.
      Parameters:
      bayesIm - a BayesIm object
      evidence - a Evidence object
  • Method Details

    • serializableInstance

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

      public BayesPm getBayesPm()

      getBayesPm.

      Specified by:
      getBayesPm in interface BayesIm
      Returns:
      a BayesPm object
    • getDag

      public Graph getDag()

      getDag.

      Specified by:
      getDag in interface BayesIm
      Returns:
      a Graph object
    • getNumNodes

      public int getNumNodes()

      getNumNodes.

      Specified by:
      getNumNodes in interface BayesIm
      Returns:
      a int
    • getNode

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

      public Node getNode(String name)

      getNode.

      Specified by:
      getNode in interface BayesIm
      Parameters:
      name - a String object
      Returns:
      a Node object
    • getNodeIndex

      public int getNodeIndex(Node node)
      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.
    • getNumColumns

      public int getNumColumns(int nodeIndex)
      Returns the number of columns.
      Specified by:
      getNumColumns in interface BayesIm
      Parameters:
      nodeIndex - the index of the node.
      Returns:
      the number of columns in the table of the given node N with index 'nodeIndex'--that is, the number of possible values that N can take on. That is, if P(N=v0 | P1=v1, P2=v2, ... Pn=vn) is a conditional probability stored in 'probs', then the maximum number of rows in the table for N is #vals(N).
      See Also:
    • getNumRows

      public int getNumRows(int nodeIndex)
      Returns the number of rows.
      Specified by:
      getNumRows in interface BayesIm
      Parameters:
      nodeIndex - the index of the node.
      Returns:
      the number of rows in the table of the given node, which would be the total number of possible combinations of parent values for a given node. That is, if P(N=v0 | P1=v1, P2=v2, ... Pn=vn) is a conditional probability stored in 'probs', then the maximum number of rows in the table for N is #vals(P1) x #vals(P2) x ... x #vals(Pn).
      See Also:
    • getNumParents

      public int getNumParents(int nodeIndex)
      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 of the given node.
    • getParent

      public int getParent(int nodeIndex, int parentIndex)
      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)
      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)
      Returns the dimensions of the pararents of the given node.
      Specified by:
      getParentDims in interface BayesIm
      Parameters:
      nodeIndex - the index of the node.
      Returns:
      (a defensive copy of) the array representing the dimensionality of each parent of a node, that is, the number of values which that node can take on. The order of entries in this array is the same as the order of entries of nodes returned by getParents() for that node.
      See Also:
    • getParents

      public int[] getParents(int nodeIndex)
      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)
      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:
      an array containing the combination of parent values for a given node and given row in the probability table for that node. To get the combination of parent values from the row number, the row number is represented using a variable-base place value system, where the bases for each place value are the dimensions of the parents in the order in which they are given by getParentDims(). For instance, if the row number (base 10) is 103 and the parent dimension array is [3 5 7], we calculate the first value as 103 / 7 = 14 with a remainder of 5. We then divide 14 / 5 = 2 with a remainder of 4. We then divide 2 / 3 = 0 with a remainder of 2. The variable place value representation is [2 4 5], which is the combination of parent values. This is the inverse function of getRowIndex().
      See Also:
    • getParentValue

      public int getParentValue(int nodeIndex, int rowIndex, int colIndex)
      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)
      Returns the probability for the given cell in the given CPT.

      Calculates the probability for the given node, at the given row and column of the conditional probability table (CPT), updated for the evidence provided in the constuctor.

      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 for the given node at the 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.
      See Also:
    • getRowIndex

      public int getRowIndex(int nodeIndex, int[] values)

      getRowIndex.

      Specified by:
      getRowIndex in interface BayesIm
      Parameters:
      nodeIndex - a int
      values - an array of int objects
      Returns:
      a int
      See Also:
    • normalizeAll

      public void normalizeAll()

      normalizeAll.

      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)

      setProbability.

      Specified by:
      setProbability in interface BayesIm
      Parameters:
      nodeIndex - a int
      probMatrix - an array of double objects
    • 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)
      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)
      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)
      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 data set with the specified number of rows.
      Specified by:
      simulateData in interface BayesIm
      Specified by:
      simulateData in interface Simulator
      Parameters:
      sampleSize - the number of rows to simulate.
      latentDataSaved - if true, latent variables are saved in the data set.
      Returns:
      the simulated data set.
    • simulateData

      public DataSet simulateData(DataSet dataSet, boolean latentDataSaved)
      Simulates data based on the provided data set and saves the latent data if specified.
      Specified by:
      simulateData in interface BayesIm
      Parameters:
      dataSet - the data set to simulate data for
      latentDataSaved - a boolean value indicating whether the latent data should be saved or not
      Returns:
      the simulated data set
    • getVariables

      public List<Node> getVariables()

      getVariables.

      Specified by:
      getVariables in interface BayesIm
      Specified by:
      getVariables in interface VariableSource
      Returns:
      a List object
    • getVariableNames

      public List<String> getVariableNames()

      getVariableNames.

      Specified by:
      getVariableNames in interface BayesIm
      Specified by:
      getVariableNames in interface VariableSource
      Returns:
      a List object
    • getMeasuredNodes

      public List<Node> getMeasuredNodes()

      getMeasuredNodes.

      Specified by:
      getMeasuredNodes in interface BayesIm
      Returns:
      a List object
    • equals

      public boolean equals(Object o)
      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 object
    • getEvidence

      public Evidence getEvidence()

      Getter for the field evidence.

      Returns:
      a Evidence object