Class MlBayesIm
- All Implemented Interfaces:
BayesIm
,Simulator
,VariableSource
,Im
,TetradSerializable
,Serializable
- getWordRatio(int nodeIndex, int rowIndex, int colIndex); and,
- setProbability(int nodeIndex, int rowIndex, int colIndex, int probability).
- getNodeIndex(Node node).
- getRowIndex(int[] parentVals).
- getParents(int nodeIndex)
- getCategoryIndex(Node node)
Thanks to Pucktada Treeratpituk, Frank Wimberly, and Willie Wheeler for advice and earlier versions.
- Version:
- $Id: $Id
- Author:
- josephramsey
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCopy constructor.Constructs a new BayesIm from the given BayesPm, initializing all values as Double.NaN ("?").Constructs a new BayesIm from the given BayesPm, initializing values either as MANUAL or RANDOM.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 TypeMethodDescriptionvoid
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.Getter for the fieldbayesPm
.int
getCorrespondingNodeIndex
(int nodeIndex, BayesIm otherBayesIm) Returns the index of the given node in the given BayesIm.getDag()
getDag.getMeasuredNodes.getNode
(int nodeIndex) Returns the Node at the specified index.getNode.int
getNodeIndex
(Node node) Gets the index of a given node within an array of nodes.int
getNumColumns
(int nodeIndex) Returns the number of columns of a node at the given index.int
getNumNodes.int
getNumParents
(int nodeIndex) Returns the number of parents of the given node.int
getNumRows
(int nodeIndex) Returns the number of rows in the provided node at the given index.getParameterNames.int
getParent
(int nodeIndex, int parentIndex) Returns the parent of a node given its index and parent index.int
getParentDim
(int nodeIndex, int parentIndex) Returns the dimension of the parent at the given node index and parent index.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
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.static MlBayesIm
Generates a simple exemplar of this class to test serialization.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 data set with the given number of rows and option to save latent variables.simulateData
(int sampleSize, boolean latentDataSaved, int[] tiers) Simulates a sample with the given sample size.simulateData
(DataSet dataSet, boolean latentDataSaved) Simulates data based on the provided data set and saves the latent data if specified.simulateData
(DataSet dataSet, boolean latentDataSaved, int[] tiers) simulateData.toString()
Prints out the probability table for each variable.
-
Field Details
-
MANUAL
public static final int MANUALInidicates that new rows in this BayesIm should be initialized as unknowns, forcing them to be specified manually. This is the default.- See Also:
-
RANDOM
public static final int RANDOMIndicates that new rows in this BayesIm should be initialized randomly.- See Also:
-
-
Constructor Details
-
MlBayesIm
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.
-
MlBayesIm
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 distribute 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.
-
MlBayesIm
public MlBayesIm(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.
-
MlBayesIm
Copy constructor.- Parameters:
bayesIm
- aBayesIm
object- Throws:
IllegalArgumentException
- if any.
-
-
Method Details
-
serializableInstance
Generates a simple exemplar of this class to test serialization.- Returns:
- a
MlBayesIm
object
-
getParameterNames
getParameterNames.
- Returns:
- a
List
object
-
getBayesPm
Getter for the field
bayesPm
.- Specified by:
getBayesPm
in interfaceBayesIm
- Returns:
- this PM.
-
getDag
getDag.
-
getNumNodes
public int getNumNodes()getNumNodes.
- Specified by:
getNumNodes
in interfaceBayesIm
- Returns:
- the number of nodes in the model.
-
getNode
Returns the Node at the specified index. -
getNode
getNode.
-
getNodeIndex
Gets the index of a given node within an array of nodes.- Specified by:
getNodeIndex
in interfaceBayesIm
- Parameters:
node
- the given node.- Returns:
- the index of the node in the array of nodes, or -1 if not found.
-
getVariables
getVariables.
- Specified by:
getVariables
in interfaceBayesIm
- Specified by:
getVariables
in interfaceVariableSource
- Returns:
- a
List
object
-
getMeasuredNodes
getMeasuredNodes.
- Specified by:
getMeasuredNodes
in interfaceBayesIm
- Returns:
- the list of measured variableNodes.
-
getVariableNames
getVariableNames.
- Specified by:
getVariableNames
in interfaceBayesIm
- Specified by:
getVariableNames
in interfaceVariableSource
- Returns:
- a
List
object
-
getNumColumns
public int getNumColumns(int nodeIndex) Returns the number of columns of a node at the given index.- Specified by:
getNumColumns
in interfaceBayesIm
- Parameters:
nodeIndex
- the index of the node.- Returns:
- the number of columns of the node at the given index.
- See Also:
-
getNumRows
public int getNumRows(int nodeIndex) Returns the number of rows in the provided node at the given index.- Specified by:
getNumRows
in interfaceBayesIm
- Parameters:
nodeIndex
- the index of the node.- Returns:
- the number of rows in the node.
- See Also:
-
getNumParents
public int getNumParents(int nodeIndex) Returns the number of parents of the given node.- Specified by:
getNumParents
in interfaceBayesIm
- Parameters:
nodeIndex
- the index of the node to get the number of parents for.- Returns:
- the number of parents of the given node.
-
getParent
public int getParent(int nodeIndex, int parentIndex) Returns the parent of a node given its index and parent index. -
getParentDim
public int getParentDim(int nodeIndex, int parentIndex) Returns the dimension of the parent at the given node index and parent index.- Specified by:
getParentDim
in interfaceBayesIm
- Parameters:
nodeIndex
- the index of the node.parentIndex
- the index of the parent.- Returns:
- the dimension of the parent at the given node index and parent index.
-
getParentDims
public int[] getParentDims(int nodeIndex) Returns the dimensions of the pararents of the given node.- Specified by:
getParentDims
in interfaceBayesIm
- 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 interfaceBayesIm
- 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 interfaceBayesIm
- 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 interfaceBayesIm
- 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.- Specified by:
getProbability
in interfaceBayesIm
- 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 interfaceBayesIm
- Parameters:
nodeIndex
- a intvalues
- an array ofinvalid reference
int
- 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 interfaceBayesIm
-
normalizeNode
public void normalizeNode(int nodeIndex) Normalizes all rows in the table associated with a given node.Normalizes all rows in the table associated with a given node.
- Specified by:
normalizeNode
in interfaceBayesIm
- Parameters:
nodeIndex
- the index of the node in question.
-
normalizeRow
public void normalizeRow(int nodeIndex, int rowIndex) Normalizes the given row.Normalizes the given row.
- Specified by:
normalizeRow
in interfaceBayesIm
- 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.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 interfaceBayesIm
- 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.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 interfaceBayesIm
- 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
Returns the index of the given node in the given BayesIm.- Specified by:
getCorrespondingNodeIndex
in interfaceBayesIm
- 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.Assigns random probability values to the child values of this row that add to 1.
-
randomizeRow
public void randomizeRow(int nodeIndex, int rowIndex) Assigns random probability values to the child values of this row that add to 1.Assigns random probability values to the child values of this row that add to 1.
- Specified by:
randomizeRow
in interfaceBayesIm
- 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.Randomizes any row in the table for the given node index that has a Double.NaN value in it.
- Specified by:
randomizeIncompleteRows
in interfaceBayesIm
- 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.Randomizes every row in the table for the given node index.
- Specified by:
randomizeTable
in interfaceBayesIm
- 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.Randomizes every row in the table for the given node index.
- Specified by:
clearTable
in interfaceBayesIm
- 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 interfaceBayesIm
- 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 interfaceBayesIm
- 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
Simulates a sample with the given sample size.- Parameters:
sampleSize
- the sample size.latentDataSaved
- a booleantiers
- an array ofinvalid reference
int
- Returns:
- the simulated sample as a DataSet.
-
simulateData
Simulates a data set with the given number of rows and option to save latent variables.- Specified by:
simulateData
in interfaceBayesIm
- Specified by:
simulateData
in interfaceSimulator
- Parameters:
sampleSize
- the number of rows to simulate.latentDataSaved
- if true, latent variables are saved in the data set.- Returns:
- a simulated DataSet object.
-
simulateData
simulateData.
-
simulateData
Simulates data based on the provided data set and saves the latent data if specified.- Specified by:
simulateData
in interfaceBayesIm
- Parameters:
dataSet
- The data set to simulate.latentDataSaved
- Indicates whether the latent data should be saved.- Returns:
- The simulated data set.
-
equals
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
Prints out the probability table for each variable.
-