com.cra.bnet.engine
Class BayesianNetworks

java.lang.Object
  |
  +--com.cra.bnet.engine.BayesianNetworks

public class BayesianNetworks
extends Object

Utility methods for Bayesian networks.


Method Summary
static double[] copy(double[] array)
          Returns a new array that is a copy of the specified array.
static void generateCases(BayesianNetwork network, Collection nodes, int count, Writer writer)
          Generates data from the specified Bayesian network for the specified nodes and writes it to the specified writer.
static double getEvidence(DiscreteNode node, String state)
          Returns the current evidence for the specified state of the specified node.
static double[] getHardEvidence(DiscreteNode node, String state)
          Returns an array representing hard evidence for the specified state of the specified node.
static int getRandomIndex(double[] distribution)
          Returns an index chosen randomly using the specified distribution.
static String getRandomStateUsingBeliefs(DiscreteNode node)
          Returns a state of the specified node chosen randomly basedon the probabilities of the node's current beliefs.
static String getRandomStateUsingCpt(DiscreteNode node, int[] parentIndices)
          Returns a state of the specified node chosen randomly based on the probabilities in the row in the CPT corresponding to the specified parent states.
static String getState(DiscreteNode node, double[] evidence)
          Returns the state of the specified node that corresponds to the specified hard evidence.
static String getUniqueNodeName(BayesianNetwork network, String baseName)
          Returns a unique node name for the specified Bayesian network based on the specified base name.
static String getUniqueNodeName(BayesianNetwork network, String baseName, int start, boolean alwaysAppend)
          Returns a unique node name for the specified Bayesian network based on the specified base name.
static boolean isAllOnes(double[] evidence)
          Returns true if all evidence values in the specified array are equal to 1.0.
static boolean isAllZeros(double[] evidence)
          Returns true if all evidence values in the specified evidnce array are equal to 0.0.
static boolean isValidBeliefs(double[] beliefs)
          Returns true if the specified beliefs are valid.
static void randomizeCpts(BayesianNetwork network)
          Randomizes the CPT of every node in the specified network.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getUniqueNodeName

public static String getUniqueNodeName(BayesianNetwork network,
                                       String baseName)
Returns a unique node name for the specified Bayesian network based on the specified base name. The

Returned string consists of the specified base name followed by an integer in the range [0, Long.MAX_VALUE]. This method first checks all existing node names in the network to ensure the new name is unique. If all names tried by this method are already contained in the Bayesian network this method returns null. (This would require a Bayesian network with at least 2^63 ~= 9.22*10^18, or 9.22 quintillion nodes.)

This method is equivalent to calling

 BayesianNetworks.getUniqueNodeName(network, baseName, 0, true);
 

Parameters:
network - Bayesian network.
baseName - base name used to construct the unique node name.
Returns:
a unique node name for the Bayesian network based on the specified base name or null if one cannot be computed by this method.

getUniqueNodeName

public static String getUniqueNodeName(BayesianNetwork network,
                                       String baseName,
                                       int start,
                                       boolean alwaysAppend)
Returns a unique node name for the specified Bayesian network based on the specified base name. The

Returned string consists of the specified base name followed by an integer in the range [0, Long.MAX_VALUE]. This method first checks all existing node names in the network to ensure the new name is unique. If all names tried by this method are already contained in the Bayesian network this method returns null. (This would require a Bayesian network with at least 2^63 ~= 9.22*10^18, or 9.22 quintillion nodes.)

Parameters:
network - Bayesian network.
baseName - base name used to construct the unique node name.
start - integer to start with.
alwaysAppend - if true, the start parameter will be appended to baseName, even if no other node uses baseName. If false and no other node uses baseName, baseName will be returned.
Returns:
a unique node name for the Bayesian network based on the specified base name or null if one cannot be computed by this method.
Throws:
NullPointerException - if network is null.

isAllOnes

public static boolean isAllOnes(double[] evidence)
Returns true if all evidence values in the specified array are equal to 1.0.

Parameters:
evidence - array of evidence values.
Returns:
true if all elements of the specified array are equal to 1.0, and false if they are not.
Throws:
NullPointerException - if evidence is null.

isAllZeros

public static boolean isAllZeros(double[] evidence)
Returns true if all evidence values in the specified evidnce array are equal to 0.0.

Parameters:
evidence - array of evidence values.
Returns:
true if all elements of the specified array are equal to 0.0, and false if not.
Throws:
NullPointerException - if evidence is null.

isValidBeliefs

public static boolean isValidBeliefs(double[] beliefs)
Returns true if the specified beliefs are valid.

Beliefs are valid if no elements are infinite or NaN. NaN indicates that the belief value is not current (that is, that an event has occurred that causes that belief value to be invalid).

Parameters:
beliefs - beliefs to test for validity.
Returns:
true if the specified beliefs are valid, and false if they are invalid or if beliefs is null.

getHardEvidence

public static double[] getHardEvidence(DiscreteNode node,
                                       String state)
Returns an array representing hard evidence for the specified state of the specified node. All elements of the returned array will be zero, except for the element corresponding to the specified state, which will be one. Clients can then post hard evidence to a node using the DiscreteNode.setEvidence method.

Parameters:
node - node to receive hard evidence.
state - known state.
Returns:
an array representing hard evidence for the specified state of the specified node.
Throws:
NullPointerException - if either parameter is null.
IllegalArgumentException - if the node does not contain the specified state.

getEvidence

public static double getEvidence(DiscreteNode node,
                                 String state)
Returns the current evidence for the specified state of the specified node.

Parameters:
node - node.
state - state whose current evidence is to be returned.
Returns:
the current evidence for the specified state of the specified node.
Throws:
NullPointerException - if either parameter is null.
IllegalArgumentException - if the node does not contain the specified state.

copy

public static double[] copy(double[] array)
Returns a new array that is a copy of the specified array.

Parameters:
array - array to be copied.
Returns:
a new array that is a copy of the specified array.
Throws:
NullPointerException - if array is null.

getRandomStateUsingBeliefs

public static String getRandomStateUsingBeliefs(DiscreteNode node)
Returns a state of the specified node chosen randomly basedon the probabilities of the node's current beliefs. For example, for a node with states [high, medium, low] with current beliefs [0.7, 0.1, 0.2] there is a 70% chance that high will be returned, a 10% chance that medium will be returned, and a 20% chance that low will be returned.

Parameters:
node - node whose state is to be chosen randomly.
Returns:
a state of the specified node chosen randomly using the node's current belief values.
Throws:
NullPointerException - if node is null.

getRandomStateUsingCpt

public static String getRandomStateUsingCpt(DiscreteNode node,
                                            int[] parentIndices)
Returns a state of the specified node chosen randomly based on the probabilities in the row in the CPT corresponding to the specified parent states. For example, for a node with states [high, medium, low] and whose CPT has the specified row [0.7, 0.1, 0.2] there is a 70% chance that high will be returned, a 10% chance that medium will be returned, and a 20% chance that low will be returned.

Parameters:
node - node whose state is to be chosen randomly.
parentIndices - parent indices specifying a row in the node's CPT.
Returns:
Returns a state of the specified node chosen randomly using the distribution in the node's CPT corresponding to the specified parent configuration.
Throws:
NullPointerException - if either parameter is null.
IllegalArgumentException - if the number of parentIndices does not match the number of parent the node has.

getRandomIndex

public static int getRandomIndex(double[] distribution)
Returns an index chosen randomly using the specified distribution. The index will be between 0 and distribution.length - 1. For example, for the distribution [0.7, 0.1, 0.2] there is a 70% chance that 0 will be returned, a 10% chance that 1 will be returned, and a 20% chance that 2 will be returned.

Parameters:
distribution - multinomial distribution used to choose the index randomly.
Returns:
an index chosen randomly using the specified distribution.
Throws:
NullPointerException - if distribution is null.
IllegalArgumentException - if the distribution array has less than two elements.

generateCases

public static void generateCases(BayesianNetwork network,
                                 Collection nodes,
                                 int count,
                                 Writer writer)
Generates data from the specified Bayesian network for the specified nodes and writes it to the specified writer. The specified number of cases are generated. All evidence in the network prior to calling this method will be used when generating the cases.

If the specified network contains no evidence, this method uses forward sampling to generate the cases. If the specified network does contain evidence, this method uses belief sampling to generate the cases. In general, forward sampling is faster than belief sampling when there is no evidence since it doesn't require beliefs to be updated, and belief sampling is faster than forward sampling when there is evidence since there are no rejected samples.

Parameters:
network - Bayesian network used to generate the data.
nodes - the nodes of the network to generate data for.
count - the number of cases to generate.
writer - the output stream to write the data to.
Throws:
NullPointerException - if either parameter is null.
IllegalArgumentException - if count is a negative number.

getState

public static String getState(DiscreteNode node,
                              double[] evidence)
Returns the state of the specified node that corresponds to the specified hard evidence.

Parameters:
node - node.
evidence - hard evidence.
Returns:
the state of the specified node that corresponds to the specified hard evidence.

randomizeCpts

public static void randomizeCpts(BayesianNetwork network)
Randomizes the CPT of every node in the specified network.

Parameters:
network - Bayesian network.